1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package com.intel.bluetooth.rmi;
27
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import java.rmi.RemoteException;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 class RemoteServiceImpl implements RemoteService {
35
36 private static final long serialVersionUID = 1L;
37
38 private Map<String, Class<?>> cash = new HashMap<String, Class<?>>();
39
40 public RemoteServiceImpl() throws RemoteException {
41 }
42
43 private Class<?> getClassByInterfaceName(String interfaceName) throws ClassNotFoundException {
44 Class<?> c;
45 synchronized (cash) {
46 c = cash.get(interfaceName);
47 if (c == null) {
48 c = Class.forName(interfaceName + "Impl");
49 cash.put(interfaceName, c);
50 }
51 }
52 return c;
53 }
54
55 public boolean verify(String interfaceName) throws RemoteException {
56 try {
57 getClassByInterfaceName(interfaceName);
58 } catch (Throwable e) {
59 throw new RemoteException("Service for " + interfaceName + " not ready", e);
60 }
61 return true;
62 }
63
64 public ServiceResponse execute(ServiceRequest request) {
65 try {
66 Class<?> c = getClassByInterfaceName(request.getClassName());
67 Method m = c.getDeclaredMethod(request.getMethodName(), request.getParameterTypes());
68 ServiceResponse response = new ServiceResponse();
69 try {
70 response.setReturnValue(m.invoke(c.newInstance(), request.getParameters()));
71 } catch (InvocationTargetException e) {
72 response.setException(e.getTargetException());
73 }
74 return response;
75 } catch (Throwable e) {
76 return new ServiceResponse(e);
77 }
78 }
79
80 }