Skip to content

Commit

Permalink
fixed issue #812, support inteAddress keep origin
Browse files Browse the repository at this point in the history
  • Loading branch information
agapple committed Aug 28, 2019
1 parent 3c488da commit 864155f
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public String formatGroupDbAddress(SourcingType defaultType, List<List<DataSourc
List<String> address = new ArrayList<String>();
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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@
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;
import com.alibaba.fastjson.serializer.SerializeConfig;
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;

/**
* 字节处理相关工具类
Expand All @@ -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.");
}
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 864155f

Please sign in to comment.