1 package net.sf.crispy.impl.rmi;
2
3 import java.lang.reflect.Method;
4 import java.rmi.RemoteException;
5 import java.rmi.server.UnicastRemoteObject;
6 import java.util.Vector;
7
8 import net.sf.crispy.ExceptionWrapper;
9 import net.sf.crispy.InterceptorHandler;
10 import net.sf.crispy.server.InterceptorHandlerCreator;
11 import net.sf.crispy.server.ServiceEndpoint;
12 import net.sf.crispy.server.ServiceEndpointImpl;
13 import net.sf.crispy.server.SingleServiceContainer;
14 import net.sf.crispy.server.SingleServiceContainerImpl;
15 import net.sf.crispy.util.Converter;
16 import net.sf.crispy.util.Invoker;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 /**
22 * This is the central Invocation Handler for all client calls.
23 * You must register the service. The invoke methode make the call per reflection API.
24 * @author Linke
25 *
26 */
27 public class RmiInvocationHandlerImpl extends UnicastRemoteObject implements RmiInvocationHandler, ServiceEndpoint, SingleServiceContainer {
28
29 protected static final Log log = LogFactory.getLog (RmiInvocationHandlerImpl.class);
30
31 private static final long serialVersionUID = 998729587238L;
32
33 private ServiceEndpoint serviceEndpoint = new ServiceEndpointImpl();
34 private SingleServiceContainer singleServiceImplContainer = new SingleServiceContainerImpl();
35
36 public RmiInvocationHandlerImpl() throws RemoteException {
37 super();
38 }
39
40
41 /**
42 * @see net.sf.crispy.impl.rmi.RmiInvocationHandler#invoke(java.lang.String, java.lang.String, java.util.Vector)
43 */
44 public Object invoke(String pvClassName, String pvMethodName, Vector pvParams) throws RemoteException, Exception {
45 Object lvProxyObject = getService(pvClassName);
46 if (lvProxyObject == null) {
47 throw new RemoteException("On the server is the class: " + pvClassName + " is not registered!");
48 }
49
50 Object lvReturn = null;
51 Converter lvConverter = new Converter();
52 lvConverter.setWithSimpleKeyMapper(true);
53 try {
54 Method lvMethod = Invoker.findMethod(lvProxyObject.getClass(), pvMethodName, pvParams);
55 Object[] lvArgs = Invoker.vector2Array(pvParams, lvMethod, lvConverter);
56 lvReturn = doInvoke(lvProxyObject, lvMethod, lvArgs, createNewInterceptorHandlerInstance());
57 } catch (Exception e) {
58 lvReturn = ExceptionWrapper.isThrowableThanHandleThrowable(e);
59 }
60 lvReturn = lvConverter.makeSimple(lvReturn);
61 return lvReturn;
62 }
63
64 public void addService(String pvServiceInterfaceName, Object pvServiceImpl) {
65 singleServiceImplContainer.addService(pvServiceInterfaceName, pvServiceImpl);
66 }
67
68 public void addService(String pvServiceInterfaceName, String pvServiceImplName) {
69 singleServiceImplContainer.addService(pvServiceInterfaceName, pvServiceImplName);
70 }
71
72 public void removeService(String pvServiceInterfaceName) {
73 singleServiceImplContainer.removeService(pvServiceInterfaceName);
74 }
75
76 public Object getService(String pvServiceInterfaceName) {
77 return singleServiceImplContainer.getService(pvServiceInterfaceName);
78 }
79
80 public int getServiceSize() {
81 return singleServiceImplContainer.getServiceSize();
82 }
83
84
85
86
87 public Object doInvoke(Object pvServiceImpl, Method pvMethod, Object[] pvArgs, InterceptorHandler pvInterceptorHandler) {
88 return serviceEndpoint.doInvoke(pvServiceImpl, pvMethod, pvArgs, pvInterceptorHandler);
89 }
90
91
92 public void setInterceptorHandlerCreator(InterceptorHandlerCreator pvCreator) {
93 serviceEndpoint.setInterceptorHandlerCreator(pvCreator);
94 }
95
96
97 public InterceptorHandlerCreator getInterceptorHandlerCreator() {
98 return serviceEndpoint.getInterceptorHandlerCreator();
99 }
100
101
102 public InterceptorHandler createNewInterceptorHandlerInstance() {
103 return serviceEndpoint.createNewInterceptorHandlerInstance();
104 }
105
106 }