1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.services.remoting;
19
20 import java.io.Serializable;
21 import java.lang.reflect.InvocationHandler;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.util.Map;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import ch.elca.el4j.core.contextpassing.ImplicitContextPassingRegistry;
30
31
32
33
34
35
36
37
38 public class ServerContextInvocationHandler
39 implements InvocationHandler, Serializable {
40
41
42
43
44 private static Logger s_logger = LoggerFactory
45 .getLogger(ServerContextInvocationHandler.class);
46
47
48
49
50 private final Object m_service;
51
52
53
54
55 private final Class m_serviceInterface;
56
57
58
59
60 private final ImplicitContextPassingRegistry
61 m_implicitContextPassingRegistry;
62
63
64
65
66
67
68
69
70
71
72
73 public ServerContextInvocationHandler(Object service,
74 Class serviceInterface,
75 ImplicitContextPassingRegistry implicitContextPassingRegistry) {
76 m_service = service;
77 m_serviceInterface = serviceInterface;
78 m_implicitContextPassingRegistry = implicitContextPassingRegistry;
79 if (m_implicitContextPassingRegistry == null) {
80 s_logger.warn("No ImplicitContextPassingRegistry defined! "
81 + "Context will not be passed through.");
82 }
83 }
84
85
86
87
88 public Object invoke(Object proxy, Method method, Object[] args)
89 throws Throwable {
90 Method m;
91 Object[] newArgs;
92 String methodName = method.getName();
93 Class[] methodParametersTypes = method.getParameterTypes();
94 Class declaringClass = method.getDeclaringClass();
95
96
97
98
99
100
101
102
103 if (declaringClass.isInterface()) {
104 Class[] methodParametersTypesWithoutContext
105 = new Class[methodParametersTypes.length - 1];
106 for (int i = 0;
107 i < methodParametersTypesWithoutContext.length; i++) {
108 methodParametersTypesWithoutContext[i]
109 = methodParametersTypes[i];
110 }
111
112 m = m_serviceInterface.getMethod(methodName,
113 methodParametersTypesWithoutContext);
114
115 newArgs = new Object[args.length - 1];
116 for (int i = 0; i < newArgs.length; i++) {
117 newArgs[i] = args[i];
118 }
119 Map map = (Map) args[args.length - 1];
120 if (m_implicitContextPassingRegistry != null) {
121 m_implicitContextPassingRegistry.pushAssembledImplicitContext(
122 map);
123 }
124 } else {
125 m = declaringClass.getMethod(methodName, methodParametersTypes);
126 newArgs = args;
127 }
128
129 try {
130 return m.invoke(m_service, newArgs);
131 } catch (InvocationTargetException e) {
132 throw e.getTargetException();
133 }
134 }
135 }