Skip to content

Commit

Permalink
Refactor the serializer of hessian and protobuf. (sofastack#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjboy authored May 13, 2018
1 parent c42e1a0 commit 19cf47e
Show file tree
Hide file tree
Showing 67 changed files with 2,547 additions and 1,586 deletions.
12 changes: 12 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@
<artifactId>sofa-rpc-bootstrap-rest</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-codec-sofa-hessian</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-codec-protobuf</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-fault-tolerance</artifactId>
Expand Down Expand Up @@ -290,6 +300,8 @@
<include>com.alipay.sofa:sofa-rpc-bootstrap-bolt</include>
<include>com.alipay.sofa:sofa-rpc-bootstrap-dubbo</include>
<include>com.alipay.sofa:sofa-rpc-bootstrap-rest</include>
<include>com.alipay.sofa:sofa-rpc-codec-protobuf</include>
<include>com.alipay.sofa:sofa-rpc-codec-sofa-hessian</include>
<include>com.alipay.sofa:sofa-rpc-fault-tolerance</include>
<include>com.alipay.sofa:sofa-rpc-log-common-tools</include>
<include>com.alipay.sofa:sofa-rpc-registry-local</include>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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;

import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.core.exception.RpcErrorType;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;

/**
* @author <a href="mailto:[email protected]">GengZhang</a>
*/
public abstract class AbstractSerializer implements Serializer {

protected SofaRpcException buildSerializeError(String message) {
return new SofaRpcException(getErrorCode(true), message);
}

protected SofaRpcException buildSerializeError(String message, Throwable throwable) {
return new SofaRpcException(getErrorCode(true), message, throwable);
}

protected SofaRpcException buildDeserializeError(String message) {
return new SofaRpcException(getErrorCode(false), message);
}

protected SofaRpcException buildDeserializeError(String message, Throwable throwable) {
return new SofaRpcException(getErrorCode(false), message, throwable);
}

/**
* @param serialize true is serialize, false is deserialize.
*/
private int getErrorCode(boolean serialize) {
if (RpcInternalContext.getContext().isProviderSide()) {
return serialize ? RpcErrorType.SERVER_SERIALIZE : RpcErrorType.SERVER_DESERIALIZE;
} else if (RpcInternalContext.getContext().isConsumerSide()) {
return serialize ? RpcErrorType.CLIENT_SERIALIZE : RpcErrorType.CLIENT_DESERIALIZE;
} else {
return RpcErrorType.UNKNOWN;
}
}
}
30 changes: 20 additions & 10 deletions core/api/src/main/java/com/alipay/sofa/rpc/codec/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package com.alipay.sofa.rpc.codec;

import com.alipay.sofa.rpc.common.annotation.Unstable;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.ext.Extensible;
import com.alipay.sofa.rpc.transport.AbstractByteBuf;

import java.util.Map;

/**
* 序列化器接口
Expand All @@ -31,26 +35,32 @@ public interface Serializer {
/**
* 序列化
*
* @param object 对象
* @return 序列化的字节数组
* @param object 对象
* @param context 上下文
* @return 序列化后的对象
* @throws SofaRpcException 序列化异常
*/
public byte[] encode(Object object);
public AbstractByteBuf encode(Object object, Map<String, String> context) throws SofaRpcException;

/**
* 反序列化
* 反序列化,只有类型,返回对象
*
* @param data 原始字节数组
* @param clazz 期望的类型
* @param data 原始字节数组
* @param clazz 期望的类型
* @param context 上下文
* @return 反序列化后的对象
* @throws SofaRpcException 序列化异常
*/
public Object decode(byte[] data, Class clazz);
public Object decode(AbstractByteBuf data, Class clazz, Map<String, String> context) throws SofaRpcException;

/**
* 反序列化
* 反序列化,已有数据,填充字段
*
* @param data 原始字节数组
* @param template 模板类型
* @param template 模板对象
* @param context 上下文
* @return 反序列化后的对象
* @throws SofaRpcException 序列化异常
*/
public Object decode(byte[] data, Object template);
public Object decode(AbstractByteBuf data, Object template, Map<String, String> context) throws SofaRpcException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.rpc.codec;

import com.alipay.sofa.rpc.common.struct.TwoWayMap;
import com.alipay.sofa.rpc.ext.ExtensionClass;
import com.alipay.sofa.rpc.ext.ExtensionLoader;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
Expand All @@ -37,10 +38,9 @@ public final class SerializerFactory {
private final static ConcurrentHashMap<Byte, Serializer> TYPE_SERIALIZER_MAP = new ConcurrentHashMap<Byte, Serializer>();

/**
* 除了托管给扩展加载器的工厂模式(保留alias:实例)外<br>
* 还需要额外保留编码和实例的映射:{别名:编码}
* 除了托管给扩展加载器的工厂模式(保留alias:实例)外,还需要额外保留编码和实例的映射:{别名:编码}
*/
private final static ConcurrentHashMap<String, Byte> TYPE_CODE_MAP = new ConcurrentHashMap<String, Byte>();
private final static TwoWayMap<String, Byte> TYPE_CODE_MAP = new TwoWayMap<String, Byte>();

/**
* 扩展加载器
Expand Down Expand Up @@ -90,4 +90,14 @@ public static Byte getCodeByAlias(String serializer) {
return TYPE_CODE_MAP.get(serializer);
}

/**
* 通过Code获取别名
*
* @param code 序列化的Code
* @return 序列化别名
*/
public static String getAliasByCode(byte code) {
return TYPE_CODE_MAP.getKey(code);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.rpc.config;

import com.alipay.sofa.rpc.common.utils.ExceptionUtils;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;

import java.util.regex.Pattern;
Expand Down Expand Up @@ -70,8 +71,10 @@ public class ConfigValueHelper {
* @return 是否为空或"false"或"null"
*/
protected static boolean assertFalse(String string) {
return string == null || "".equals(string)
|| "false".equalsIgnoreCase(string) || "null".equals(string);
return string == null
|| StringUtils.EMPTY.equals(string)
|| StringUtils.FALSE.equalsIgnoreCase(string)
|| StringUtils.NULL.equals(string);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void setTargetAppName(String targetAppName) {

//====================== 下面是非传递属性 ===============
/**
* 方法对象(缓存一些,减少反射,服务端使用
* 方法对象(为了减少反射缓存
*/
private transient Method method;

Expand All @@ -139,19 +139,15 @@ public void setTargetAppName(String targetAppName) {
private transient String interfaceName;

/**
* 序列化工厂类型:决定是否泛化调用(客户端使用)
*/
private transient int serializeFactoryType;

/**
* 序列化类型(客户端使用)
* 序列化类型
*/
private transient byte serializeType;

/**
* 调用类型(客户端使用)
*/
private transient String invokeType;

/**
* 用户层服务回调类,调用级别(客户端使用)
*/
Expand Down Expand Up @@ -180,26 +176,6 @@ public void setMethod(Method method) {
this.method = method;
}

/**
* Gets serialize factory type.
*
* @return the serialize factory type
*/
public int getSerializeFactoryType() {
return serializeFactoryType;
}

/**
* Sets serialize factory type.
*
* @param serializeFactoryType the serialize factory type
* @return the serialize factory type
*/
public SofaRequest setSerializeFactoryType(int serializeFactoryType) {
this.serializeFactoryType = serializeFactoryType;
return this;
}

/**
* Gets serialize type.
*
Expand Down Expand Up @@ -306,6 +282,5 @@ public SofaRequest setTimeout(Integer timeout) {
public boolean isAsync() {
return invokeType != null && (RpcConstants.INVOKER_TYPE_CALLBACK.equals(invokeType)
|| RpcConstants.INVOKER_TYPE_FUTURE.equals(invokeType));

}
}
Loading

0 comments on commit 19cf47e

Please sign in to comment.