1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.model.resolvers; |
11 | |
|
12 | |
import org.mule.VoidResult; |
13 | |
import org.mule.api.MuleEventContext; |
14 | |
import org.mule.api.model.EntryPointResolver; |
15 | |
import org.mule.api.model.InvocationResult; |
16 | |
import org.mule.api.transformer.TransformerException; |
17 | |
import org.mule.transport.NullPayload; |
18 | |
import org.mule.util.ClassUtils; |
19 | |
import org.mule.util.StringMessageUtils; |
20 | |
|
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.lang.reflect.Method; |
23 | |
|
24 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
25 | |
|
26 | |
import org.apache.commons.logging.Log; |
27 | |
import org.apache.commons.logging.LogFactory; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 260 | public abstract class AbstractEntryPointResolver implements EntryPointResolver |
36 | |
{ |
37 | |
|
38 | 260 | protected transient final Log logger = LogFactory.getLog(getClass()); |
39 | |
|
40 | 260 | private boolean transformFirst = true; |
41 | |
|
42 | 260 | private boolean acceptVoidMethods = false; |
43 | |
|
44 | |
|
45 | |
|
46 | 260 | protected final ConcurrentHashMap methodCache = new ConcurrentHashMap(4); |
47 | |
|
48 | |
public boolean isTransformFirst() |
49 | |
{ |
50 | 132 | return transformFirst; |
51 | |
} |
52 | |
|
53 | |
public void setTransformFirst(boolean transformFirst) |
54 | |
{ |
55 | 68 | this.transformFirst = transformFirst; |
56 | 68 | } |
57 | |
|
58 | |
public boolean isAcceptVoidMethods() |
59 | |
{ |
60 | 106 | return acceptVoidMethods; |
61 | |
} |
62 | |
|
63 | |
public void setAcceptVoidMethods(boolean acceptVoidMethods) |
64 | |
{ |
65 | 22 | this.acceptVoidMethods = acceptVoidMethods; |
66 | 22 | } |
67 | |
|
68 | |
protected Method getMethodByName(String methodName, MuleEventContext context) |
69 | |
{ |
70 | 34 | StringBuffer key = new StringBuffer(24).append(context.getService().getName()) |
71 | |
.append(".").append(methodName); |
72 | 34 | Method method = (Method) methodCache.get(key.toString()); |
73 | 34 | return method; |
74 | |
} |
75 | |
|
76 | |
protected Method addMethodByName(Method method, MuleEventContext context) |
77 | |
{ |
78 | 18 | StringBuffer key = new StringBuffer(24).append(context.getService().getName()) |
79 | |
.append(".").append(method.getName()); |
80 | 18 | Method previousMethod = (Method) methodCache.putIfAbsent(key.toString(), method); |
81 | 18 | return (previousMethod != null ? previousMethod : method); |
82 | |
} |
83 | |
|
84 | |
protected Method addMethodByArguments(Object component, Method method, Object[] payload) |
85 | |
{ |
86 | 32 | Method previousMethod = (Method) methodCache.putIfAbsent(getCacheKeyForPayload(component, payload), method); |
87 | 32 | return (previousMethod != null ? previousMethod : method); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
protected Method getMethodByArguments(Object component, Object[] payload) |
92 | |
{ |
93 | 48 | Method method = (Method) methodCache.get(getCacheKeyForPayload(component, payload)); |
94 | 48 | return method; |
95 | |
} |
96 | |
|
97 | |
protected String getCacheKeyForPayload(Object component, Object[] payload) |
98 | |
{ |
99 | 80 | StringBuffer key = new StringBuffer(48); |
100 | 170 | for (int i = 0; i < payload.length; i++) |
101 | |
{ |
102 | 90 | Object o = payload[i]; |
103 | 90 | key.append(o.getClass().getName()); |
104 | |
} |
105 | 80 | key.append(".").append(ClassUtils.getClassName(component.getClass())); |
106 | 80 | return key.toString(); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
protected Object[] getPayloadFromMessage(MuleEventContext context) throws TransformerException |
111 | |
{ |
112 | |
Object temp; |
113 | 72 | if (isTransformFirst()) |
114 | |
{ |
115 | 64 | temp = context.transformMessage(); |
116 | |
} |
117 | |
else |
118 | |
{ |
119 | 8 | temp = context.getMessage().getPayload(); |
120 | |
} |
121 | 72 | if (temp instanceof Object[]) |
122 | |
{ |
123 | 20 | return (Object[]) temp; |
124 | |
} |
125 | 52 | else if (temp instanceof NullPayload) |
126 | |
{ |
127 | 6 | return ClassUtils.NO_ARGS; |
128 | |
} |
129 | |
else |
130 | |
{ |
131 | 46 | return new Object[]{temp}; |
132 | |
} |
133 | |
} |
134 | |
|
135 | |
protected InvocationResult invokeMethod(Object component, Method method, Object[] arguments) |
136 | |
throws InvocationTargetException, IllegalAccessException |
137 | |
{ |
138 | 48 | String methodCall = null; |
139 | |
|
140 | 48 | if (logger.isDebugEnabled()) |
141 | |
{ |
142 | 0 | methodCall = component.getClass().getName() + "." + method.getName() + "(" |
143 | |
+ StringMessageUtils.toString(ClassUtils.getClassTypes(arguments)) + ")"; |
144 | 0 | logger.debug("Invoking " + methodCall); |
145 | |
} |
146 | |
|
147 | 48 | Object result = method.invoke(component, arguments); |
148 | 48 | if (method.getReturnType().equals(Void.TYPE)) |
149 | |
{ |
150 | 38 | result = VoidResult.getInstance(); |
151 | |
} |
152 | |
|
153 | 48 | if (logger.isDebugEnabled()) |
154 | |
{ |
155 | 0 | logger.debug("Result of call " + methodCall + " is " + (result == null ? "null" : "not null")); |
156 | |
} |
157 | |
|
158 | 48 | return new InvocationResult(result, method); |
159 | |
} |
160 | |
|
161 | |
|
162 | |
public String toString() |
163 | |
{ |
164 | 0 | final StringBuffer sb = new StringBuffer(); |
165 | 0 | sb.append("AbstractEntryPointResolver"); |
166 | 0 | sb.append("{transformFirst=").append(transformFirst); |
167 | 0 | sb.append(", acceptVoidMethods=").append(acceptVoidMethods); |
168 | 0 | sb.append('}'); |
169 | 0 | return sb.toString(); |
170 | |
} |
171 | |
} |