Skip to content

Commit

Permalink
Upgrade sofa-hessian to v3.3.3. (#321)
Browse files Browse the repository at this point in the history
update sofa-hessian to v3.3.3, upgrade blacklist, refactor custom hessian serializer.
  • Loading branch information
ujjboy authored and leizhiyuan committed Sep 27, 2018
1 parent 5b3f148 commit 19b8ab9
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 265 deletions.
2 changes: 1 addition & 1 deletion all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<sofa.common.tools.version>1.0.12</sofa.common.tools.version>
<javassist.version>3.20.0-GA</javassist.version>
<netty.version>4.1.25.Final</netty.version>
<hessian.version>3.3.2</hessian.version>
<hessian.version>3.3.3</hessian.version>
<resteasy.version>3.0.12.Final</resteasy.version>
<bolt.version>1.4.2</bolt.version>
<tracer.version>2.1.1</tracer.version>
Expand Down
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<opentracing.version>0.22.0</opentracing.version>
<dubbo_version>2.4.10</dubbo_version>
<!-- serialization -->
<hessian.version>3.3.2</hessian.version>
<hessian.version>3.3.3</hessian.version>
<thrift.version>0.9.2</thrift.version>
<protobuf.version>3.1.0</protobuf.version>
<jackson.version>2.7.9</jackson.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.rpc.codec.sofahessian;

import com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author <a href=mailto:[email protected]>leizhiyuan</a>
*/
public class CustomHessianSerializerManager {

private final static Map<Class, CustomHessianSerializer> CUSTOM_SERIALIZERS = new ConcurrentHashMap<Class, CustomHessianSerializer>(
2);

public static CustomHessianSerializer getSerializer(Class clazz) {
return CUSTOM_SERIALIZERS.get(clazz);
}

public static void addSerializer(Class clazz, CustomHessianSerializer serializerManager) {
CUSTOM_SERIALIZERS.put(clazz, serializerManager);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import com.alipay.hessian.ClassNameResolver;
import com.alipay.hessian.NameBlackListFilter;
import com.alipay.sofa.rpc.codec.AbstractSerializer;
import com.alipay.sofa.rpc.codec.sofahessian.serialize.RpcSerializeObjector;
import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer;
import com.alipay.sofa.rpc.codec.sofahessian.serialize.SofaRequestHessianSerializer;
import com.alipay.sofa.rpc.codec.sofahessian.serialize.SofaResponseHessianSerializer;
import com.alipay.sofa.rpc.common.RpcConfigs;
import com.alipay.sofa.rpc.common.RpcOptions;
import com.alipay.sofa.rpc.common.SofaConfigs;
import com.alipay.sofa.rpc.common.SofaOptions;
import com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream;
import com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.transport.AbstractByteBuf;
import com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf;
Expand All @@ -41,9 +44,9 @@
/**
* Serializer of SOFAHessian
* <<p>
* Encode: : Support MessageLite, String, SofaRequest and SofaResponse.
* Encode: Object, SofaRequest and SofaResponse.
* <p>
* Decode by class mode : Support MessageLite and String.
* Decode by class mode : Support SofaRequest and SofaResponse.
* <p>
* Decode by object template : Support SofaRequest and SofaResponse.
* <ul>
Expand Down Expand Up @@ -80,7 +83,14 @@ public SofaHessianSerializer() {
resolver.addFilter(new NameBlackListFilter(BlackListFileLoader.SOFA_SERIALIZE_BLACK_LIST, 8192));
serializerFactory.setClassNameResolver(resolver);
genericSerializerFactory.setClassNameResolver(resolver);
} else {
serializerFactory.setClassNameResolver(null);
genericSerializerFactory.setClassNameResolver(null);
}
CustomHessianSerializerManager.addSerializer(SofaRequest.class,
new SofaRequestHessianSerializer(serializerFactory, genericSerializerFactory));
CustomHessianSerializerManager.addSerializer(SofaResponse.class,
new SofaResponseHessianSerializer(serializerFactory, genericSerializerFactory));
}

/**
Expand All @@ -103,7 +113,7 @@ protected SerializerFactory getSerializerFactory(boolean multipleClassLoader, bo
@Override
public AbstractByteBuf encode(Object object, Map<String, String> context) {

final RpcSerializeObjector serializer = RpcSerializeObjectorManager.getSerializer(object.getClass());
CustomHessianSerializer serializer = CustomHessianSerializerManager.getSerializer(object.getClass());
if (serializer != null) {
return serializer.encodeObject(object, context);
} else {
Expand All @@ -126,7 +136,7 @@ public Object decode(AbstractByteBuf data, Class clazz, Map<String, String> cont
if (clazz == null) {
throw buildDeserializeError("class is null!");
} else {
final RpcSerializeObjector serializer = RpcSerializeObjectorManager.getSerializer(clazz);
CustomHessianSerializer serializer = CustomHessianSerializerManager.getSerializer(clazz);
if (serializer != null) {
return serializer.decodeObject(data, context);
} else {
Expand All @@ -149,32 +159,12 @@ public void decode(AbstractByteBuf data, Object template, Map<String, String> co
if (template == null) {
throw buildDeserializeError("template is null!");
} else {
final RpcSerializeObjector serializer = RpcSerializeObjectorManager.getSerializer(template.getClass());
CustomHessianSerializer serializer = CustomHessianSerializerManager.getSerializer(template.getClass());
if (serializer != null) {
serializer.decodeObjectByTemplate(data, context, template);
} else {
throw buildDeserializeError(template.getClass() + " template is not supported");
}
}
}

/**
* Is generic request boolean.
*
* @param serializeType the serialize type
* @return the boolean
*/
protected boolean isGenericRequest(String serializeType) {
return serializeType != null && !serializeType.equals(RemotingConstants.SERIALIZE_FACTORY_NORMAL);
}

/**
* Is generic response boolean.
*
* @param serializeType the serialize type
* @return the boolean
*/
protected boolean isGenericResponse(String serializeType) {
return serializeType != null && serializeType.equals(RemotingConstants.SERIALIZE_FACTORY_GENERIC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,17 @@
*/
package com.alipay.sofa.rpc.codec.sofahessian.serialize;

import com.alipay.hessian.ClassNameResolver;
import com.alipay.hessian.NameBlackListFilter;
import com.alipay.sofa.rpc.codec.sofahessian.BlackListFileLoader;
import com.alipay.sofa.rpc.codec.sofahessian.GenericMultipleClassLoaderSofaSerializerFactory;
import com.alipay.sofa.rpc.codec.sofahessian.GenericSingleClassLoaderSofaSerializerFactory;
import com.alipay.sofa.rpc.codec.sofahessian.MultipleClassLoaderSofaSerializerFactory;
import com.alipay.sofa.rpc.codec.sofahessian.SingleClassLoaderSofaSerializerFactory;
import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.common.RpcConfigs;
import com.alipay.sofa.rpc.common.RpcOptions;
import com.alipay.sofa.rpc.common.SofaConfigs;
import com.alipay.sofa.rpc.common.SofaOptions;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.core.exception.RpcErrorType;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.caucho.hessian.io.SerializerFactory;

/**
* @author bystander
* @version $Id: AbstractRpcSerializeObjector.java, v 0.1 2018年09月08日 7:15 PM bystander Exp $
* @author <a href=mailto:[email protected]>leizhiyuan</a>
* @author <a href=mailto:[email protected]>GengZhang</a>
*/
public class AbstractRpcSerializeObjector {
public abstract class AbstractCustomHessianSerializer<T> implements CustomHessianSerializer<T> {

/**
* Normal Serializer Factory
Expand All @@ -48,35 +37,10 @@ public class AbstractRpcSerializeObjector {
*/
protected SerializerFactory genericSerializerFactory;

public AbstractRpcSerializeObjector() {

boolean enableMultipleClassLoader = RpcConfigs.getBooleanValue(RpcOptions.MULTIPLE_CLASSLOADER_ENABLE);
serializerFactory = getSerializerFactory(enableMultipleClassLoader, false);
genericSerializerFactory = getSerializerFactory(enableMultipleClassLoader, true);
if (RpcConfigs.getBooleanValue(RpcOptions.SERIALIZE_BLACKLIST_ENABLE) &&
SofaConfigs.getBooleanValue(SofaOptions.CONFIG_SERIALIZE_BLACKLIST, true)) {
ClassNameResolver resolver = new ClassNameResolver();
resolver.addFilter(new NameBlackListFilter(BlackListFileLoader.SOFA_SERIALIZE_BLACK_LIST, 8192));
serializerFactory.setClassNameResolver(resolver);
genericSerializerFactory.setClassNameResolver(resolver);
}
}

/**
* Gets serializer factory.
*
* @param multipleClassLoader the multiple class loader
* @param generic the generic
* @return the serializer factory
*/
protected SerializerFactory getSerializerFactory(boolean multipleClassLoader, boolean generic) {
if (generic) {
return multipleClassLoader ? new GenericMultipleClassLoaderSofaSerializerFactory() :
new GenericSingleClassLoaderSofaSerializerFactory();
} else {
return multipleClassLoader ? new MultipleClassLoaderSofaSerializerFactory() :
new SingleClassLoaderSofaSerializerFactory();
}
public AbstractCustomHessianSerializer(SerializerFactory serializerFactory,
SerializerFactory genericSerializerFactory) {
this.serializerFactory = serializerFactory;
this.genericSerializerFactory = genericSerializerFactory;
}

protected SofaRpcException buildDeserializeError(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,38 @@
import java.util.Map;

/**
* @author bystander
* @version $Id: RpcDecoderObjector.java, v 0.1 2018年09月07日 4:27 PM bystander Exp $
* @author <a href=mailto:[email protected]>leizhiyuan</a>
*/
public interface RpcSerializeObjector<T> {
public interface CustomHessianSerializer<T> {

/**
* 反序列化,已有数据,填充字段
*
* @param data 原始字节数组
* @param template 模板对象
* @param context 上下文
* @throws SofaRpcException 序列化异常
*/
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context,
T template) throws SofaRpcException;

public Object decodeObject(AbstractByteBuf data, Map<String, String> context) throws SofaRpcException;

public AbstractByteBuf encodeObject(T sofaRequest, Map<String, String> context);
/**
* 反序列化,只有类型,返回对象
*
* @param data 原始字节数组
* @param context 上下文
* @return 反序列化后的对象
* @throws SofaRpcException 序列化异常
*/
public T decodeObject(AbstractByteBuf data, Map<String, String> context) throws SofaRpcException;

/**
* 序列化
*
* @param object 对象
* @param context 上下文
* @return 序列化后的对象
* @throws SofaRpcException 序列化异常
*/
public AbstractByteBuf encodeObject(T object, Map<String, String> context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@
import com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;

import java.io.IOException;
import java.util.Map;

/**
* @author bystander
* @version $Id: SofaRequestRpcSerializeObjector.java, v 0.1 2018年09月07日 4:34 PM bystander Exp $
* @author <a href=mailto:[email protected]>leizhiyuan</a>
*/
public class SofaRequestRpcSerializeObjector extends AbstractRpcSerializeObjector implements
RpcSerializeObjector<SofaRequest> {
public class SofaRequestHessianSerializer extends AbstractCustomHessianSerializer<SofaRequest> {

/**
* Instantiates a new Sofa hessian serializer.
*/
public SofaRequestRpcSerializeObjector() {
super();
public SofaRequestHessianSerializer(SerializerFactory serializerFactory,
SerializerFactory genericSerializerFactory) {
super(serializerFactory, genericSerializerFactory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@
import com.alipay.sofa.rpc.transport.ByteStreamWrapperByteBuf;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;

import java.io.IOException;
import java.util.Map;

/**
* @author bystander
* @version $Id: SofaResponseRpcSerializeObjector.java, v 0.1 2018年09月07日 4:34 PM bystander Exp $
* @author <a href=mailto:[email protected]>leizhiyuan</a>
*/
public class SofaResponseRpcSerializeObjector extends AbstractRpcSerializeObjector implements
RpcSerializeObjector<SofaResponse> {
public class SofaResponseHessianSerializer extends AbstractCustomHessianSerializer<SofaResponse> {

/**
* Instantiates a new Sofa hessian serializer.
*/
public SofaResponseRpcSerializeObjector() {
super();
public SofaResponseHessianSerializer(SerializerFactory serializerFactory,
SerializerFactory genericSerializerFactory) {
super(serializerFactory, genericSerializerFactory);
}

@Override
Expand Down
Loading

0 comments on commit 19b8ab9

Please sign in to comment.