Java Code Examples for com.alibaba.dubbo.rpc.Invocation#getArguments()

The following examples show how to use com.alibaba.dubbo.rpc.Invocation#getArguments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RpcUtils.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
public static Class<?>[] getParameterTypes(Invocation invocation) {
    if (Constants.$INVOKE.equals(invocation.getMethodName())
            && invocation.getArguments() != null
            && invocation.getArguments().length > 1
            && invocation.getArguments()[1] instanceof String[]) {
        String[] types = (String[]) invocation.getArguments()[1];
        if (types == null) {
            return new Class<?>[0];
        }
        Class<?>[] parameterTypes = new Class<?>[types.length];
        for (int i = 0; i < types.length; i++) {
            parameterTypes[i] = ReflectUtils.forName(types[0]);
        }
        return parameterTypes;
    }
    return invocation.getParameterTypes();
}
 
Example 2
Source File: RpcUtils.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public static Class<?>[] getParameterTypes(Invocation invocation){
	if(Constants.$INVOKE.equals(invocation.getMethodName()) 
            && invocation.getArguments() != null 
            && invocation.getArguments().length > 1
            && invocation.getArguments()[1] instanceof String[]){
        String[] types = (String[]) invocation.getArguments()[1];
        if (types == null) {
        	return new Class<?>[0];
        }
        Class<?>[] parameterTypes = new Class<?>[types.length];
        for (int i = 0; i < types.length; i ++) {
        	parameterTypes[i] = ReflectUtils.forName(types[0]);
        }
        return parameterTypes;
    }
	return invocation.getParameterTypes();
}
 
Example 3
Source File: RpcUtils.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public static Class<?>[] getParameterTypes(Invocation invocation){
	if(Constants.$INVOKE.equals(invocation.getMethodName()) 
            && invocation.getArguments() != null 
            && invocation.getArguments().length > 1
            && invocation.getArguments()[1] instanceof String[]){
        String[] types = (String[]) invocation.getArguments()[1];
        if (types == null) {
        	return new Class<?>[0];
        }
        Class<?>[] parameterTypes = new Class<?>[types.length];
        for (int i = 0; i < types.length; i ++) {
        	parameterTypes[i] = ReflectUtils.forName(types[0]);
        }
        return parameterTypes;
    }
	return invocation.getParameterTypes();
}
 
Example 4
Source File: DubboParser.java    From brave with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the method name of the invocation or the first string arg of an "$invoke" method.
 *
 * <p>Like {@link RpcUtils#getMethodName(Invocation)}, except without re-reading fields or
 * returning an unhelpful "$invoke" method name.
 */
static @Nullable String method(Invocation invocation) {
  String methodName = invocation.getMethodName();
  if ("$invoke".equals(methodName)) {
    Object[] arguments = invocation.getArguments();
    if (arguments != null && arguments.length > 0 && arguments[0] instanceof String) {
      methodName = (String) arguments[0];
    } else {
      methodName = null;
    }
  }
  return methodName != null && !methodName.isEmpty() ? methodName : null;
}
 
Example 5
Source File: RpcUtils.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public static String getMethodName(Invocation invocation) {
    if (Constants.$INVOKE.equals(invocation.getMethodName())
            && invocation.getArguments() != null
            && invocation.getArguments().length > 0
            && invocation.getArguments()[0] instanceof String) {
        return (String) invocation.getArguments()[0];
    }
    return invocation.getMethodName();
}
 
Example 6
Source File: RpcUtils.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public static String getMethodName(Invocation invocation){
	if(Constants.$INVOKE.equals(invocation.getMethodName()) 
            && invocation.getArguments() != null 
            && invocation.getArguments().length > 0 
            && invocation.getArguments()[0] instanceof String){
        return (String) invocation.getArguments()[0];
    }
	return invocation.getMethodName();
}
 
Example 7
Source File: DubboEasyTransFilter.java    From EasyTransaction with Apache License 2.0 5 votes vote down vote up
private boolean checkEasyTransRequest(Invocation invocation) {
	if(invocation.getArguments().length == 3){
		Object param3 = invocation.getArguments()[2];
		if(param3 != null && param3.getClass().isArray()){
			Object[] array = (Object[]) param3;
			if(array.length == 2 && array[0] instanceof EasyTransRequest && array[1] instanceof Map){
				return true;
			}
		}
	}
	return false;
}
 
Example 8
Source File: RpcUtils.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
public static Object[] getArguments(Invocation invocation) {
    if (Constants.$INVOKE.equals(invocation.getMethodName())
            && invocation.getArguments() != null
            && invocation.getArguments().length > 2
            && invocation.getArguments()[2] instanceof Object[]) {
        return (Object[]) invocation.getArguments()[2];
    }
    return invocation.getArguments();
}
 
Example 9
Source File: FutureFilter.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
//        从静态上下文中获取抛异常的方法 onthrow.method属性值指定=》
        final Method onthrowMethod = (Method) StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
//        从静态上下文中获取抛异常的对象 onthrow.instance属性值指定
        final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

        //onthrow callback not configured
        if (onthrowMethod == null && onthrowInst == null) {
            return;
        }
        if (onthrowMethod == null || onthrowInst == null) {
            throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a onthrow callback config , but no such " + (onthrowMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
        }
        if (!onthrowMethod.isAccessible()) {
            onthrowMethod.setAccessible(true);
        }
        Class<?>[] rParaTypes = onthrowMethod.getParameterTypes();
        if (rParaTypes[0].isAssignableFrom(exception.getClass())) {
            try {
                Object[] args = invocation.getArguments();
                Object[] params;

                if (rParaTypes.length > 1) {
                    if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)) {
                        params = new Object[2];
                        params[0] = exception;
                        params[1] = args;
                    } else {
                        params = new Object[args.length + 1];
                        params[0] = exception;
                        System.arraycopy(args, 0, params, 1, args.length);
                    }
                } else {
                    params = new Object[]{exception};
                }
//                异常方法执行
                onthrowMethod.invoke(onthrowInst, params);
            } catch (Throwable e) {
                logger.error(invocation.getMethodName() + ".call back method invoke error . callback method :" + onthrowMethod + ", url:" + invoker.getUrl(), e);
            }
        } else {
            logger.error(invocation.getMethodName() + ".call back method invoke error . callback method :" + onthrowMethod + ", url:" + invoker.getUrl(), exception);
        }
    }
 
Example 10
Source File: FutureFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final Method onthrowMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
    final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

    //没有设置onthrow callback.
    if (onthrowMethod == null  &&  onthrowInst == null ){
        return ;
    }
    if (onthrowMethod == null  ||  onthrowInst == null ){
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onthrow callback config , but no such "+(onthrowMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
    }
    if (onthrowMethod != null && ! onthrowMethod.isAccessible()) {
        onthrowMethod.setAccessible(true);
    }
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes() ;
    if (rParaTypes[0].isAssignableFrom(exception.getClass())){
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            
            if (rParaTypes.length >1 ) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args ;
                }else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst,params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), e);
        } 
    } else {
        logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), exception);
    }
}
 
Example 11
Source File: EchoFilter.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    if (inv.getMethodName().equals(Constants.$ECHO) && inv.getArguments() != null && inv.getArguments().length == 1)
        return new RpcResult(inv.getArguments()[0]);
    return invoker.invoke(inv);
}
 
Example 12
Source File: EchoFilter.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
	if(inv.getMethodName().equals(Constants.$ECHO) && inv.getArguments() != null && inv.getArguments().length == 1 )
		return new RpcResult(inv.getArguments()[0]);
	return invoker.invoke(inv);
}
 
Example 13
Source File: FutureFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final Method onthrowMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
    final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

    //没有设置onthrow callback.
    if (onthrowMethod == null  &&  onthrowInst == null ){
        return ;
    }
    if (onthrowMethod == null  ||  onthrowInst == null ){
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onthrow callback config , but no such "+(onthrowMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
    }
    if (onthrowMethod != null && ! onthrowMethod.isAccessible()) {
        onthrowMethod.setAccessible(true);
    }
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes() ;
    if (rParaTypes[0].isAssignableFrom(exception.getClass())){
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            
            if (rParaTypes.length >1 ) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args ;
                }else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst,params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), e);
        } 
    } else {
        logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), exception);
    }
}
 
Example 14
Source File: AccessLogFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    try {
        String accesslog = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY);
        if (ConfigUtils.isNotEmpty(accesslog)) {
            RpcContext context = RpcContext.getContext();
            String serviceName = invoker.getInterface().getName();
            String version = invoker.getUrl().getParameter(Constants.VERSION_KEY);
            String group = invoker.getUrl().getParameter(Constants.GROUP_KEY);
            StringBuilder sn = new StringBuilder();
            sn.append("[").append(new SimpleDateFormat(MESSAGE_DATE_FORMAT).format(new Date())).append("] ").append(context.getRemoteHost()).append(":").append(context.getRemotePort())
            .append(" -> ").append(context.getLocalHost()).append(":").append(context.getLocalPort())
            .append(" - ");
            if (null != group && group.length() > 0) {
                sn.append(group).append("/");
            }
            sn.append(serviceName);
            if (null != version && version.length() > 0) {
                sn.append(":").append(version);
            }
            sn.append(" ");
            sn.append(inv.getMethodName());
            sn.append("(");
            Class<?>[] types = inv.getParameterTypes();
            if (types != null && types.length > 0) {
                boolean first = true;
                for (Class<?> type : types) {
                    if (first) {
                        first = false;
                    } else {
                        sn.append(",");
                    }
                    sn.append(type.getName());
                }
            }
            sn.append(") ");
            Object[] args = inv.getArguments();
            if (args != null && args.length > 0) {
                sn.append(JSON.json(args));
            }
            String msg = sn.toString();
            if (ConfigUtils.isDefault(accesslog)) {
                LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + invoker.getInterface().getName()).info(msg);
            } else {
                log(accesslog, msg);
            }
        }
    } catch (Throwable t) {
        logger.warn("Exception in AcessLogFilter of service(" + invoker + " -> " + inv + ")", t);
    }
    return invoker.invoke(inv);
}
 
Example 15
Source File: EchoFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
	if(inv.getMethodName().equals(Constants.$ECHO) && inv.getArguments() != null && inv.getArguments().length == 1 )
		return new RpcResult(inv.getArguments()[0]);
	return invoker.invoke(inv);
}
 
Example 16
Source File: FutureFilter.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final Method onthrowMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
    final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

    //没有设置onthrow callback.
    if (onthrowMethod == null  &&  onthrowInst == null ){
        return ;
    }
    if (onthrowMethod == null  ||  onthrowInst == null ){
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onthrow callback config , but no such "+(onthrowMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
    }
    if (onthrowMethod != null && ! onthrowMethod.isAccessible()) {
        onthrowMethod.setAccessible(true);
    }
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes() ;
    if (rParaTypes[0].isAssignableFrom(exception.getClass())){
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            
            if (rParaTypes.length >1 ) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args ;
                }else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst,params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), e);
        } 
    } else {
        logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), exception);
    }
}
 
Example 17
Source File: FutureFilter.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final Method onthrowMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
    final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

    //没有设置onthrow callback.
    if (onthrowMethod == null  &&  onthrowInst == null ){
        return ;
    }
    if (onthrowMethod == null  ||  onthrowInst == null ){
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onthrow callback config , but no such "+(onthrowMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
    }
    if (! onthrowMethod.isAccessible()) {
        onthrowMethod.setAccessible(true);
    }
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes() ;
    if (rParaTypes[0].isAssignableFrom(exception.getClass())){
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            
            if (rParaTypes.length >1 ) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args ;
                }else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst,params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), e);
        } 
    } else {
        logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), exception);
    }
}
 
Example 18
Source File: EchoFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
	if(inv.getMethodName().equals(Constants.$ECHO) && inv.getArguments() != null && inv.getArguments().length == 1 )
		return new RpcResult(inv.getArguments()[0]);
	return invoker.invoke(inv);
}
 
Example 19
Source File: AccessLogFilter.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    try {
        String accesslog = invoker.getUrl().getParameter(Constants.ACCESS_LOG_KEY);
        if (ConfigUtils.isNotEmpty(accesslog)) {
            RpcContext context = RpcContext.getContext();
            String serviceName = invoker.getInterface().getName();
            String version = invoker.getUrl().getParameter(Constants.VERSION_KEY);
            String group = invoker.getUrl().getParameter(Constants.GROUP_KEY);
            StringBuilder sn = new StringBuilder();
            sn.append("[").append(new SimpleDateFormat(MESSAGE_DATE_FORMAT).format(new Date())).append("] ").append(context.getRemoteHost()).append(":").append(context.getRemotePort())
            .append(" -> ").append(context.getLocalHost()).append(":").append(context.getLocalPort())
            .append(" - ");
            if (null != group && group.length() > 0) {
                sn.append(group).append("/");
            }
            sn.append(serviceName);
            if (null != version && version.length() > 0) {
                sn.append(":").append(version);
            }
            sn.append(" ");
            sn.append(inv.getMethodName());
            sn.append("(");
            Class<?>[] types = inv.getParameterTypes();
            if (types != null && types.length > 0) {
                boolean first = true;
                for (Class<?> type : types) {
                    if (first) {
                        first = false;
                    } else {
                        sn.append(",");
                    }
                    sn.append(type.getName());
                }
            }
            sn.append(") ");
            Object[] args = inv.getArguments();
            if (args != null && args.length > 0) {
                sn.append(JSON.json(args));
            }
            String msg = sn.toString();
            if (ConfigUtils.isDefault(accesslog)) {
                LoggerFactory.getLogger(ACCESS_LOG_KEY + "." + invoker.getInterface().getName()).info(msg);
            } else {
                log(accesslog, msg);
            }
        }
    } catch (Throwable t) {
        logger.warn("Exception in AcessLogFilter of service(" + invoker + " -> " + inv + ")", t);
    }
    return invoker.invoke(inv);
}
 
Example 20
Source File: FutureFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
private void fireThrowCallback(final Invoker<?> invoker, final Invocation invocation, final Throwable exception) {
    final Method onthrowMethod = (Method)StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_METHOD_KEY));
    final Object onthrowInst = StaticContext.getSystemContext().get(StaticContext.getKey(invoker.getUrl(), invocation.getMethodName(), Constants.ON_THROW_INSTANCE_KEY));

    //没有设置onthrow callback.
    if (onthrowMethod == null  &&  onthrowInst == null ){
        return ;
    }
    if (onthrowMethod == null  ||  onthrowInst == null ){
        throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() +" has a onthrow callback config , but no such "+(onthrowMethod == null ? "method" : "instance")+" found. url:"+invoker.getUrl());
    }
    if (onthrowMethod != null && ! onthrowMethod.isAccessible()) {
        onthrowMethod.setAccessible(true);
    }
    Class<?>[] rParaTypes = onthrowMethod.getParameterTypes() ;
    if (rParaTypes[0].isAssignableFrom(exception.getClass())){
        try {
            Object[] args = invocation.getArguments();
            Object[] params;
            
            if (rParaTypes.length >1 ) {
                if (rParaTypes.length == 2 && rParaTypes[1].isAssignableFrom(Object[].class)){
                    params = new Object[2];
                    params[0] = exception;
                    params[1] = args ;
                }else {
                    params = new Object[args.length + 1];
                    params[0] = exception;
                    System.arraycopy(args, 0, params, 1, args.length);
                }
            } else {
                params = new Object[] { exception };
            }
            onthrowMethod.invoke(onthrowInst,params);
        } catch (Throwable e) {
            logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), e);
        } 
    } else {
        logger.error(invocation.getMethodName() +".call back method invoke error . callback method :" + onthrowMethod + ", url:"+ invoker.getUrl(), exception);
    }
}