Created
March 16, 2018 07:21
-
-
Save qsLI/25262fd47764745e5b31782f46d66aae to your computer and use it in GitHub Desktop.
泛化调用provider端实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 1999-2011 Alibaba Group. | |
* | |
* 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.dubbo.rpc.filter; | |
import java.io.IOException; | |
import java.lang.reflect.Method; | |
import com.alibaba.dubbo.common.Constants; | |
import com.alibaba.dubbo.common.extension.Activate; | |
import com.alibaba.dubbo.common.extension.ExtensionLoader; | |
import com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream; | |
import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream; | |
import com.alibaba.dubbo.common.serialize.Serialization; | |
import com.alibaba.dubbo.common.utils.PojoUtils; | |
import com.alibaba.dubbo.common.utils.ReflectUtils; | |
import com.alibaba.dubbo.common.utils.StringUtils; | |
import com.alibaba.dubbo.rpc.Filter; | |
import com.alibaba.dubbo.rpc.Invocation; | |
import com.alibaba.dubbo.rpc.Invoker; | |
import com.alibaba.dubbo.rpc.Result; | |
import com.alibaba.dubbo.rpc.RpcException; | |
import com.alibaba.dubbo.rpc.RpcInvocation; | |
import com.alibaba.dubbo.rpc.RpcResult; | |
import com.alibaba.dubbo.rpc.service.GenericException; | |
import com.alibaba.dubbo.rpc.support.ProtocolUtils; | |
/** | |
* GenericInvokerFilter. | |
* | |
* @author william.liangf | |
*/ | |
@Activate(group = Constants.PROVIDER, order = -20000) | |
public class GenericFilter implements Filter { | |
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException { | |
if (inv.getMethodName().equals(Constants.$INVOKE) | |
&& inv.getArguments() != null | |
&& inv.getArguments().length == 3 | |
&& ! invoker.getUrl().getParameter(Constants.GENERIC_KEY, false)) { | |
String name = ((String) inv.getArguments()[0]).trim(); | |
String[] types = (String[]) inv.getArguments()[1]; | |
Object[] args = (Object[]) inv.getArguments()[2]; | |
try { | |
Method method = ReflectUtils.findMethodByMethodSignature(invoker.getInterface(), name, types); | |
Class<?>[] params = method.getParameterTypes(); | |
if (args == null) { | |
args = new Object[params.length]; | |
} | |
String generic = inv.getAttachment(Constants.GENERIC_KEY); | |
if (StringUtils.isEmpty(generic) | |
|| ProtocolUtils.isDefaultGenericSerialization(generic)) { | |
// realize 方法将HashMap形式的参数转换成具体的类 | |
args = PojoUtils.realize(args, params, method.getGenericParameterTypes()); | |
} else if (ProtocolUtils.isJavaGenericSerialization(generic)) { | |
for(int i = 0; i < args.length; i++) { | |
if (byte[].class == args[i].getClass()) { | |
try { | |
UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream((byte[])args[i]); | |
args[i] = ExtensionLoader.getExtensionLoader(Serialization.class) | |
.getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA) | |
.deserialize(null, is).readObject(); | |
} catch (Exception e) { | |
throw new RpcException("Deserialize argument [" + (i + 1) + "] failed.", e); | |
} | |
} else { | |
throw new RpcException( | |
new StringBuilder(32).append("Generic serialization [") | |
.append(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA) | |
.append("] only support message type ") | |
.append(byte[].class) | |
.append(" and your message type is ") | |
.append(args[i].getClass()).toString()); | |
} | |
} | |
} | |
// 实际调用对应实现 | |
Result result = invoker.invoke(new RpcInvocation(method, args, inv.getAttachments())); | |
if (result.hasException() | |
&& ! (result.getException() instanceof GenericException)) { | |
return new RpcResult(new GenericException(result.getException())); | |
} | |
// 将返回结果转成对应的形式 | |
if (ProtocolUtils.isJavaGenericSerialization(generic)) { | |
try { | |
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512); | |
ExtensionLoader.getExtensionLoader(Serialization.class) | |
.getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA) | |
.serialize(null, os).writeObject(result.getValue()); | |
return new RpcResult(os.toByteArray()); | |
} catch (IOException e) { | |
throw new RpcException("Serialize result failed.", e); | |
} | |
} else { | |
// 将返回的具体的对象转成HashMap形式 | |
return new RpcResult(PojoUtils.generalize(result.getValue())); | |
} | |
} catch (NoSuchMethodException e) { | |
throw new RpcException(e.getMessage(), e); | |
} catch (ClassNotFoundException e) { | |
throw new RpcException(e.getMessage(), e); | |
} | |
} | |
return invoker.invoke(inv); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment