All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hprose.common.HproseInvocationHandler Maven / Gradle / Ivy

Go to download

Hprose is a High Performance Remote Object Service Engine. It is a modern, lightweight, cross-language, cross-platform, object-oriented, high performance, remote dynamic communication middleware. It is not only easy to use, but powerful. You just need a little time to learn, then you can use it to easily construct cross language cross platform distributed application system. Hprose supports many programming languages, for example: * AAuto Quicker * ActionScript * ASP * C++ * Dart * Delphi/Free Pascal * dotNET(C#, Visual Basic...) * Golang * Java * JavaScript * Node.js * Objective-C * Perl * PHP * Python * Ruby * ... Through Hprose, You can conveniently and efficiently intercommunicate between those programming languages. This project is the implementation of Hprose for Java.

There is a newer version: 2.0.38
Show newest version
/**********************************************************\
|                                                          |
|                          hprose                          |
|                                                          |
| Official WebSite: http://www.hprose.com/                 |
|                   http://www.hprose.org/                 |
|                                                          |
\**********************************************************/
/**********************************************************\
 *                                                        *
 * HproseInvocationHandler.java                           *
 *                                                        *
 * hprose InvocationHandler class for Java.               *
 *                                                        *
 * LastModified: Apr 24, 2016                             *
 * Author: Ma Bingyao                   *
 *                                                        *
\**********************************************************/
package hprose.common;

import hprose.util.ClassUtil;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class HproseInvocationHandler implements InvocationHandler {
    private final HproseInvoker client;
    private final String ns;

    public HproseInvocationHandler(HproseInvoker client, String ns) {
        this.client = client;
        this.ns = (ns == null) ? "" : ns + "_";
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MethodName methodName = method.getAnnotation(MethodName.class);
        ResultMode mode = method.getAnnotation(ResultMode.class);
        ByRef byref = method.getAnnotation(ByRef.class);
        SimpleMode simple = method.getAnnotation(SimpleMode.class);
        Retry retry = method.getAnnotation(Retry.class);
        Idempotent idempotent = method.getAnnotation(Idempotent.class);
        Failswitch failswitch = method.getAnnotation(Failswitch.class);
        Oneway oneway = method.getAnnotation(Oneway.class);

        final String name = ns + ((methodName == null) ? method.getName() : methodName.value());

        final InvokeSettings settings = new InvokeSettings();

        if (mode != null) settings.setMode(mode.value());
        if (simple != null) settings.setSimple(simple.value());
        if (byref != null) settings.setByref(byref.value());
        if (retry != null) settings.setRetry(retry.value());
        if (idempotent != null) settings.setIdempotent(idempotent.value());
        if (failswitch != null) settings.setFailswitch(failswitch.value());
        if (oneway != null) settings.setOneway(oneway.value());

        Type[] paramTypes = method.getGenericParameterTypes();
        Type returnType = method.getGenericReturnType();
        if (void.class.equals(returnType) ||
            Void.class.equals(returnType)) {
            returnType = null;
        }
        int n = paramTypes.length;
        Object result = null;
        if ((n > 0) && ClassUtil.toClass(paramTypes[n - 1]).equals(HproseCallback1.class)) {
            if (paramTypes[n - 1] instanceof ParameterizedType) {
                returnType = ((ParameterizedType)paramTypes[n - 1]).getActualTypeArguments()[0];
            }
            HproseCallback1 callback = (HproseCallback1) args[n - 1];
            Object[] tmpargs = new Object[n - 1];
            System.arraycopy(args, 0, tmpargs, 0, n - 1);
            settings.setReturnType(returnType);
            client.invoke(name, tmpargs, callback, settings);
        }
        else if ((n > 0) && ClassUtil.toClass(paramTypes[n - 1]).equals(HproseCallback.class)) {
            if (paramTypes[n - 1] instanceof ParameterizedType) {
                returnType = ((ParameterizedType)paramTypes[n - 1]).getActualTypeArguments()[0];
            }
            HproseCallback callback = (HproseCallback) args[n - 1];
            Object[] tmpargs = new Object[n - 1];
            System.arraycopy(args, 0, tmpargs, 0, n - 1);
            settings.setReturnType(returnType);
            client.invoke(name, tmpargs, callback, settings);
        }
        else if ((n > 1) && ClassUtil.toClass(paramTypes[n - 2]).equals(HproseCallback1.class)
                         && ClassUtil.toClass(paramTypes[n - 1]).equals(HproseErrorEvent.class)) {
            if (paramTypes[n - 2] instanceof ParameterizedType) {
                returnType = ((ParameterizedType)paramTypes[n - 2]).getActualTypeArguments()[0];
            }
            HproseCallback1 callback = (HproseCallback1) args[n - 2];
            HproseErrorEvent errorEvent = (HproseErrorEvent) args[n - 1];
            Object[] tmpargs = new Object[n - 2];
            System.arraycopy(args, 0, tmpargs, 0, n - 2);
            settings.setReturnType(returnType);
            client.invoke(name, tmpargs, callback, errorEvent, settings);
        }
        else if ((n > 1) && ClassUtil.toClass(paramTypes[n - 2]).equals(HproseCallback.class)
                         && ClassUtil.toClass(paramTypes[n - 1]).equals(HproseErrorEvent.class)) {
            if (paramTypes[n - 2] instanceof ParameterizedType) {
                returnType = ((ParameterizedType)paramTypes[n - 2]).getActualTypeArguments()[0];
            }
            HproseCallback callback = (HproseCallback) args[n - 2];
            HproseErrorEvent errorEvent = (HproseErrorEvent) args[n - 1];
            Object[] tmpargs = new Object[n - 2];
            System.arraycopy(args, 0, tmpargs, 0, n - 2);
            settings.setReturnType(returnType);
            client.invoke(name, tmpargs, callback, errorEvent, settings);
        }
        else {
            settings.setReturnType(returnType);
            result = client.invoke(name, args, settings);
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy