View Javadoc

1   /**
2    * 
3    */
4   package org.sourceforge.jemm.comm.shared;
5   
6   import java.lang.reflect.InvocationHandler;
7   import java.lang.reflect.Method;
8   
9   /**
10   * RPCProxyHandler is used to implement the proxy handling behind the supported
11   * remote interfaces.
12   * 
13   * @author Rory Graves
14   */
15  public class RPCProxyHandler implements InvocationHandler {
16      /** The handler that owns this interface. */
17      final RPCHandler handler;
18      
19      /** The interface class being proxied. */
20      final Class<?> ifClass;
21      
22      /**
23       * Creates an RPCProxyHandler for the given handler.
24       * @param handler Handler for a proxy.
25       * @param ifClass
26       */
27      public RPCProxyHandler(RPCHandler handler, Class<?> ifClass) {
28          this.handler = handler;
29          this.ifClass = ifClass;
30      }
31  
32      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
33          if("public java.lang.String java.lang.Object.toString()".equals(method.toString()))
34                  return "" + ifClass.getName() +":Proxy"; 
35       
36          if(method.getReturnType() != void.class || method.getAnnotation(SynchronousCall.class) != null) {
37              RPCCallRespMessage resp = handler.makeSyncCall(ifClass,method.getName(),method.getParameterTypes(),args);
38              if(resp.isCallSuccess())
39                  return resp.getReturnValue();
40              else
41                  throw (Throwable) resp.getReturnValue();
42          } else {
43              // async call
44              handler.makeAsyncCall(ifClass,method.getName(),method.getParameterTypes(),args);
45              return null;
46          }
47      }
48  }