| /*
|
| * 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 com.alibaba.dubbo.common.Constants; |
| import com.alibaba.dubbo.common.Extension; |
| import com.alibaba.dubbo.common.utils.PojoUtils; |
| import com.alibaba.dubbo.common.utils.ReflectUtils; |
| import com.alibaba.dubbo.rpc.Filter; |
| import com.alibaba.dubbo.rpc.Invoker; |
| import com.alibaba.dubbo.rpc.RpcException; |
| import com.alibaba.dubbo.rpc.Invocation; |
| import com.alibaba.dubbo.rpc.Result; |
| import com.alibaba.dubbo.rpc.RpcInvocation;
|
| import com.alibaba.dubbo.rpc.RpcResult;
|
| |
| /** |
| * GenericImplInvokerFilter |
| * |
| * @author william.liangf |
| */ |
| @Extension("genericimpl") |
| public class GenericImplFilter implements Filter { |
| |
| private static final Class<?>[] GENERIC_PARAMETER_TYPES = new Class<?>[] {String.class, String[].class, Object[].class}; |
| |
| public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { |
| if (invoker.getUrl().getParameter(Constants.GENERIC_KEY, false)
|
| && ! Constants.$INVOKE.equals(invocation.getMethodName()) |
| && invocation instanceof RpcInvocation) { |
| RpcInvocation invocation2 = (RpcInvocation) invocation; |
| String methodName = invocation2.getMethodName(); |
| Class<?>[] parameterTypes = invocation2.getParameterTypes(); |
| Object[] arguments = invocation2.getArguments(); |
| |
| String[] types = new String[parameterTypes.length]; |
| for (int i = 0; i < parameterTypes.length; i ++) { |
| types[i] = ReflectUtils.getName(parameterTypes[i]); |
| } |
| Object[] args = PojoUtils.generalize(arguments); |
| |
| invocation2.setMethodName(Constants.$INVOKE); |
| invocation2.setParameterTypes(GENERIC_PARAMETER_TYPES); |
| invocation2.setArguments(new Object[] {methodName, types, args}); |
| Result result = invoker.invoke(invocation2); |
| |
| if (! result.hasException()) { |
| Object value = result.getResult(); |
| try { |
| return new RpcResult(PojoUtils.realize(value, invoker.getInterface().getMethod(methodName, parameterTypes).getReturnType())); |
| } catch (NoSuchMethodException e) { |
| throw new RpcException(e.getMessage(), e); |
| } |
| } |
| return result; |
| } |
| return invoker.invoke(invocation); |
| } |
| |
| } |