From 864155fe559cbc3601a8a6ca101c5398684697a0 Mon Sep 17 00:00:00 2001 From: agapple Date: Wed, 28 Aug 2019 14:14:12 +0800 Subject: [PATCH] fixed issue #812, support inteAddress keep origin --- .../manager/web/common/NumberFormatUtil.java | 2 +- .../manager/web/common/model/SeniorCanal.java | 2 +- .../otter/shared/common/utils/JsonUtils.java | 149 +++++++++++++++++- .../otter/shared/common/InteAddressTest.java | 66 ++++++++ .../otter/shared/common/JsonUtilsTest.java | 2 +- 5 files changed, 213 insertions(+), 8 deletions(-) create mode 100644 shared/common/src/test/java/com/alibaba/otter/shared/common/InteAddressTest.java diff --git a/manager/web/src/main/java/com/alibaba/otter/manager/web/common/NumberFormatUtil.java b/manager/web/src/main/java/com/alibaba/otter/manager/web/common/NumberFormatUtil.java index f86b73cc..82c97530 100644 --- a/manager/web/src/main/java/com/alibaba/otter/manager/web/common/NumberFormatUtil.java +++ b/manager/web/src/main/java/com/alibaba/otter/manager/web/common/NumberFormatUtil.java @@ -190,7 +190,7 @@ public String formatGroupDbAddress(SourcingType defaultType, List address = new ArrayList(); for (DataSourcing dbAddress : groupDbAddress) { StringBuilder dbAddressBuilder = new StringBuilder(); - dbAddressBuilder.append(dbAddress.getDbAddress().getAddress().getHostName()); + dbAddressBuilder.append(dbAddress.getDbAddress().getHostString()); dbAddressBuilder.append(":"); dbAddressBuilder.append(String.valueOf(dbAddress.getDbAddress().getPort())); if (!defaultType.equals(dbAddress.getType())) { diff --git a/manager/web/src/main/java/com/alibaba/otter/manager/web/common/model/SeniorCanal.java b/manager/web/src/main/java/com/alibaba/otter/manager/web/common/model/SeniorCanal.java index 592b46d9..749bc77c 100644 --- a/manager/web/src/main/java/com/alibaba/otter/manager/web/common/model/SeniorCanal.java +++ b/manager/web/src/main/java/com/alibaba/otter/manager/web/common/model/SeniorCanal.java @@ -58,7 +58,7 @@ public String getUrl() { int i = 0; for (DataSourcing dbAddress : groupAddress) { ++i; - address.append(dbAddress.getDbAddress().getAddress().getHostName()) + address.append(dbAddress.getDbAddress().getHostString()) .append(":") .append(dbAddress.getDbAddress().getPort()); diff --git a/shared/common/src/main/java/com/alibaba/otter/shared/common/utils/JsonUtils.java b/shared/common/src/main/java/com/alibaba/otter/shared/common/utils/JsonUtils.java index 29273176..587174cc 100644 --- a/shared/common/src/main/java/com/alibaba/otter/shared/common/utils/JsonUtils.java +++ b/shared/common/src/main/java/com/alibaba/otter/shared/common/utils/JsonUtils.java @@ -22,13 +22,20 @@ import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.commons.lang.StringUtils; + import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; +import com.alibaba.fastjson.parser.DefaultJSONParser; +import com.alibaba.fastjson.parser.JSONLexer; +import com.alibaba.fastjson.parser.JSONToken; import com.alibaba.fastjson.parser.ParserConfig; +import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer; import com.alibaba.fastjson.serializer.JSONSerializer; import com.alibaba.fastjson.serializer.ObjectSerializer; import com.alibaba.fastjson.serializer.PropertyFilter; @@ -36,6 +43,8 @@ import com.alibaba.fastjson.serializer.SerializeWriter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.otter.shared.common.model.config.Transient; +import com.alibaba.otter.shared.common.utils.JsonUtils.InetSocketAddressSerializer.InetAddressDeserializer; +import com.alibaba.otter.shared.common.utils.JsonUtils.InetSocketAddressSerializer.InetSocketAddressDeserializer; /** * 字节处理相关工具类 @@ -48,9 +57,17 @@ public class JsonUtils { SerializeConfig.getGlobalInstance().put(InetAddress.class, InetAddressSerializer.instance); SerializeConfig.getGlobalInstance().put(Inet4Address.class, InetAddressSerializer.instance); SerializeConfig.getGlobalInstance().put(Inet6Address.class, InetAddressSerializer.instance); + SerializeConfig.getGlobalInstance().put(InetSocketAddress.class, InetSocketAddressSerializer.instance); - // ParserConfig.getGlobalInstance().setAutoTypeSupport(true); + ParserConfig.getGlobalInstance().getDeserializers().put(InetAddress.class, InetAddressDeserializer.instance); + ParserConfig.getGlobalInstance().getDeserializers().put(Inet4Address.class, InetAddressDeserializer.instance); + ParserConfig.getGlobalInstance().getDeserializers().put(Inet6Address.class, InetAddressDeserializer.instance); + ParserConfig.getGlobalInstance() + .getDeserializers() + .put(InetSocketAddress.class, InetSocketAddressDeserializer.instance); + SerializeConfig.getGlobalInstance().put(Inet6Address.class, InetAddressSerializer.instance); + // ParserConfig.getGlobalInstance().setAutoTypeSupport(true); ParserConfig.getGlobalInstance().addAccept("com.alibaba.otter."); ParserConfig.getGlobalInstance().addAccept("com.taobao.tddl.dbsync."); } @@ -140,8 +157,8 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty } InetAddress address = (InetAddress) object; - // 优先使用name - serializer.write(address.getHostName()); + String[] data = StringUtils.split(address.toString(), '/'); + serializer.write(data[0]); } public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) @@ -152,8 +169,130 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty } InetAddress address = (InetAddress) object; - // 优先使用name - serializer.write(address.getHostName()); + String[] data = StringUtils.split(address.toString(), '/'); + serializer.write(data[0]); + } + } + + public static class InetSocketAddressSerializer implements ObjectSerializer { + + public static InetSocketAddressSerializer instance = new InetSocketAddressSerializer(); + + public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) + throws IOException { + if (object == null) { + serializer.writeNull(); + return; + } + + SerializeWriter out = serializer.out; + InetSocketAddress address = (InetSocketAddress) object; + InetAddress inetAddress = address.getAddress(); + serializer.write('{'); + out.writeFieldName("address"); + if (inetAddress != null) { + serializer.write(inetAddress); + } else { + out.writeString(address.getHostString()); + } + out.write(','); + out.writeFieldName("port"); + out.writeInt(address.getPort()); + serializer.write('}'); + } + + public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) + throws IOException { + if (object == null) { + serializer.writeNull(); + return; + } + + SerializeWriter out = serializer.out; + InetSocketAddress address = (InetSocketAddress) object; + address.getHostString(); + InetAddress inetAddress = address.getAddress(); + out.write('{'); + out.writeFieldName("address"); + if (inetAddress != null) { + serializer.write(inetAddress); + } else { + out.writeString(address.getHostString()); + } + out.write(','); + out.writeFieldName("port"); + out.writeInt(address.getPort()); + out.write('}'); + } + + public static class InetAddressDeserializer implements ObjectDeserializer { + + public static InetAddressDeserializer instance = new InetAddressDeserializer(); + + @Override + public String deserialze(DefaultJSONParser parser, Type type, Object fieldName) { + return (String) parser.parse(); + } + + @Override + public int getFastMatchToken() { + return 0; + } + + } + + public static class InetSocketAddressDeserializer implements ObjectDeserializer { + + public static InetSocketAddressDeserializer instance = new InetSocketAddressDeserializer(); + + @Override + public InetSocketAddress deserialze(DefaultJSONParser parser, Type type, Object fieldName) { + JSONLexer lexer = parser.lexer; + if (lexer.token() == JSONToken.NULL) { + lexer.nextToken(); + return null; + } + + parser.accept(JSONToken.LBRACE); + + Object address = null; + int port = 0; + for (;;) { + String key = lexer.stringVal(); + lexer.nextToken(JSONToken.COLON); + + if (key.equals("address")) { + parser.accept(JSONToken.COLON); + address = parser.parseObject(InetAddress.class); + } else if (key.equals("port")) { + parser.accept(JSONToken.COLON); + if (lexer.token() != JSONToken.LITERAL_INT) { + throw new RuntimeException("port is not int"); + } + port = lexer.intValue(); + lexer.nextToken(); + } else { + parser.accept(JSONToken.COLON); + parser.parse(); + } + + if (lexer.token() == JSONToken.COMMA) { + lexer.nextToken(); + continue; + } + + break; + } + + parser.accept(JSONToken.RBRACE); + return new InetSocketAddress(address.toString(), port); + } + + @Override + public int getFastMatchToken() { + return 0; + } + } } } diff --git a/shared/common/src/test/java/com/alibaba/otter/shared/common/InteAddressTest.java b/shared/common/src/test/java/com/alibaba/otter/shared/common/InteAddressTest.java new file mode 100644 index 00000000..22d85c33 --- /dev/null +++ b/shared/common/src/test/java/com/alibaba/otter/shared/common/InteAddressTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2010-2101 Alibaba Group Holding Limited. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.otter.shared.common; + +import java.net.InetSocketAddress; + +import com.alibaba.otter.shared.common.utils.JsonUtils; + +public class InteAddressTest { + + public static void main(String args[]) { + { + InetSocketAddress address = new InetSocketAddress("127.0.0.1", 3306); + System.out.println(address.getHostString()); + String str = JsonUtils.marshalToString(address); + System.out.println(str); + + address = JsonUtils.unmarshalFromString(str, InetSocketAddress.class); + System.out.println(address); + } + + { + InetSocketAddress address = new InetSocketAddress("localhost", 3306); + System.out.println(address.getHostString()); + String str = JsonUtils.marshalToString(address); + System.out.println(str); + + address = JsonUtils.unmarshalFromString(str, InetSocketAddress.class); + System.out.println(address); + } + + { + InetSocketAddress address = new InetSocketAddress("127.0.0.2", 3306); + System.out.println(address.getHostString()); + String str = JsonUtils.marshalToString(address); + System.out.println(str); + + address = JsonUtils.unmarshalFromString(str, InetSocketAddress.class); + System.out.println(address); + } + + { + InetSocketAddress address = new InetSocketAddress("notx", 3306); + System.out.println(address.getHostString()); + String str = JsonUtils.marshalToString(address); + System.out.println(str); + + address = JsonUtils.unmarshalFromString(str, InetSocketAddress.class); + System.out.println(address); + } + } +} diff --git a/shared/common/src/test/java/com/alibaba/otter/shared/common/JsonUtilsTest.java b/shared/common/src/test/java/com/alibaba/otter/shared/common/JsonUtilsTest.java index e8b6cd6d..202a0adf 100644 --- a/shared/common/src/test/java/com/alibaba/otter/shared/common/JsonUtilsTest.java +++ b/shared/common/src/test/java/com/alibaba/otter/shared/common/JsonUtilsTest.java @@ -84,7 +84,7 @@ public void test_bytes() { data.put("two", two); StringWriter jsonStr = new StringWriter(); - JSONWriter writer = new JSONWriter(jsonStr);//超大文本写入 + JSONWriter writer = new JSONWriter(jsonStr);// 超大文本写入 writer.startArray(); writer.writeValue(one); writer.writeValue(two);