View Javadoc

1   /**
2    *
3    */
4   package org.sourceforge.jemmrpc.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 remote interfaces.
11   *
12   * @author Rory Graves
13   */
14  public class RPCProxyHandler implements InvocationHandler
15  {
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       *
25       * @param fHandler Handler for a proxy.
26       * @param fIfClass The target interface class.
27       */
28      public RPCProxyHandler(RPCHandler fHandler, Class<?> fIfClass)
29      {
30          this.handler = fHandler;
31          this.ifClass = fIfClass;
32      }
33  
34      /*
35       * (non-Javadoc)
36       *
37       * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method,
38       *      java.lang.Object[])
39       */
40      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
41      {
42          if ("public java.lang.String java.lang.Object.toString()".equals(method
43                  .toString()))
44              return "" + ifClass.getName() + ":Proxy";
45  
46          if (method.getReturnType() != void.class
47                  || method.getAnnotation(SynchronousCall.class) != null)
48          {
49              final RPCCallRespMessage resp = handler.makeSyncCall(ifClass, method
50                      .getName(), method.getParameterTypes(), args);
51              if (resp.callSuccess)
52                  return resp.returnValue;
53              else
54                  throw (Throwable) resp.returnValue;
55          } 
56          else
57          {
58              // asynchronous call
59              handler.makeAsyncCall(ifClass, method.getName(), method.getParameterTypes(),
60                      args);
61              return null;
62          }
63      }
64  }