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.lang.reflect.InvocationHandler;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.Map;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import ch.elca.el4j.core.contextpassing.ImplicitContextPassingRegistry;
29 import ch.elca.el4j.util.interfaceenrichment.MethodDescriptor;
30
31
32
33
34
35
36
37
38 public class ClientContextInvocationHandler implements InvocationHandler {
39
40
41
42 private static Logger s_logger = LoggerFactory
43 .getLogger(ClientContextInvocationHandler.class);
44
45
46
47
48 private final Object m_innerRemoteObject;
49
50
51
52
53
54 private final Class m_serviceInterfaceWithContext;
55
56
57
58
59 private final ContextEnrichmentDecorator m_contextInterfaceDecorator;
60
61
62
63
64 private final ImplicitContextPassingRegistry
65 m_implicitContextPassingRegistry;
66
67
68
69
70
71
72
73
74
75
76
77 public ClientContextInvocationHandler(Object innerRemoteObject,
78 Class serviceInterfaceWithContext,
79 ImplicitContextPassingRegistry implicitContextPassingRegistry) {
80 m_innerRemoteObject = innerRemoteObject;
81 m_serviceInterfaceWithContext = serviceInterfaceWithContext;
82 m_contextInterfaceDecorator = new ContextEnrichmentDecorator();
83 m_implicitContextPassingRegistry = implicitContextPassingRegistry;
84 if (m_implicitContextPassingRegistry == null) {
85 s_logger.warn("No ImplicitContextPassingRegistry defined! "
86 + "Context will not be passed through.");
87 }
88 }
89
90
91
92
93 public Object invoke(Object proxy, Method method, Object[] args)
94 throws Throwable {
95 Method m;
96 Object[] newArgs;
97 String methodName = method.getName();
98 Class[] methodParametersTypes = method.getParameterTypes();
99 Class declaringClass = method.getDeclaringClass();
100
101
102
103
104
105
106
107
108 if (declaringClass.isInterface()) {
109 MethodDescriptor md = new MethodDescriptor();
110 md.setParameterTypes(methodParametersTypes);
111 m_contextInterfaceDecorator.changedMethodSignature(md);
112 Class[] methodParametersTypesWithContext = md.getParameterTypes();
113
114 m = m_serviceInterfaceWithContext.getMethod(methodName,
115 methodParametersTypesWithContext);
116
117 int argsLength = (args == null) ? 0 : args.length;
118 newArgs = new Object[argsLength + 1];
119 for (int i = 0; i < argsLength; i++) {
120 newArgs[i] = args[i];
121 }
122 Map map = null;
123 if (m_implicitContextPassingRegistry != null) {
124 map = m_implicitContextPassingRegistry
125 .getAssembledImplicitContext();
126 }
127 newArgs[newArgs.length - 1] = map;
128 } else {
129 m = declaringClass.getMethod(methodName, methodParametersTypes);
130 newArgs = args;
131 }
132
133 try {
134 return m.invoke(m_innerRemoteObject, newArgs);
135 } catch (InvocationTargetException e) {
136 throw e.getTargetException();
137 }
138 }
139
140
141
142
143
144 protected Object getInnerRemoteObject() {
145 return m_innerRemoteObject;
146 }
147 }