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.util.Map;
21
22 import ch.elca.el4j.util.interfaceenrichment.EnrichmentDecorator;
23 import ch.elca.el4j.util.interfaceenrichment.InterfaceEnricher;
24 import ch.elca.el4j.util.interfaceenrichment.MethodDescriptor;
25
26
27
28
29
30
31
32
33
34
35 public class ContextEnrichmentDecorator implements EnrichmentDecorator {
36
37
38
39 private static final Class CONTEXT_CLASS = Map.class;
40
41
42
43
44 private static final String CONTEXT_PARAMETER_NAME = "contextMap";
45
46
47
48
49 public String changedInterfaceName(String originalInterfaceName) {
50 return originalInterfaceName + "WithContext";
51 }
52
53
54
55
56 public Class[] changedExtendedInterface(Class[] extendedInterfaces) {
57 InterfaceEnricher interfaceIndirector = new InterfaceEnricher();
58 ClassLoader cl = Thread.currentThread().getContextClassLoader();
59
60 Class[] changedInterfaces = new Class[extendedInterfaces.length];
61 for (int i = 0; i < extendedInterfaces.length; i++) {
62 changedInterfaces[i] = interfaceIndirector.createShadowInterfaceAndLoadItDirectly(
63 extendedInterfaces[i], this, cl);
64 }
65
66 return changedInterfaces;
67 }
68
69
70
71
72 public MethodDescriptor changedMethodSignature(MethodDescriptor method) {
73 Class[] methodParameterTypes = method.getParameterTypes();
74 Class[] methodParameterTypesWithContext = null;
75 if (methodParameterTypes == null || methodParameterTypes.length == 0) {
76 methodParameterTypesWithContext = new Class[] {CONTEXT_CLASS};
77
78 } else {
79 methodParameterTypesWithContext
80 = new Class[methodParameterTypes.length + 1];
81 for (int i = 0; i < methodParameterTypes.length; i++) {
82 methodParameterTypesWithContext[i] = methodParameterTypes[i];
83 }
84 methodParameterTypesWithContext[
85 methodParameterTypesWithContext.length - 1] = CONTEXT_CLASS;
86 }
87 method.setParameterTypes(methodParameterTypesWithContext);
88
89 String[] methodParameterNames = method.getParameterNames();
90
91
92
93
94
95 if ((methodParameterNames != null
96 && methodParameterNames.length == methodParameterTypes.length)
97 || methodParameterTypesWithContext.length == 1) {
98 String[] methodParameterNamesWithContext = null;
99 if (methodParameterTypesWithContext.length == 1) {
100 methodParameterNamesWithContext
101 = new String[] {CONTEXT_PARAMETER_NAME};
102 } else {
103 methodParameterNamesWithContext
104 = new String[methodParameterNames.length + 1];
105 for (int i = 0; i < methodParameterNames.length; i++) {
106 methodParameterNamesWithContext[i]
107 = methodParameterNames[i];
108 }
109 methodParameterNamesWithContext[
110 methodParameterNamesWithContext.length - 1]
111 = CONTEXT_PARAMETER_NAME;
112 }
113 method.setParameterNames(methodParameterNamesWithContext);
114 }
115
116 return method;
117 }
118 }