1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.impl.model.resolvers; |
12 | |
|
13 | |
import org.mule.config.MuleProperties; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.impl.MuleMessage; |
16 | |
import org.mule.impl.NoSatisfiableMethodsException; |
17 | |
import org.mule.impl.OptimizedRequestContext; |
18 | |
import org.mule.impl.TooManySatisfiableMethodsException; |
19 | |
import org.mule.impl.VoidResult; |
20 | |
import org.mule.providers.NullPayload; |
21 | |
import org.mule.umo.UMOEventContext; |
22 | |
import org.mule.umo.lifecycle.Callable; |
23 | |
import org.mule.umo.model.UMOEntryPoint; |
24 | |
import org.mule.util.ClassUtils; |
25 | |
|
26 | |
import java.lang.reflect.InvocationTargetException; |
27 | |
import java.lang.reflect.Method; |
28 | |
import java.util.Arrays; |
29 | |
import java.util.HashSet; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.List; |
32 | |
import java.util.Set; |
33 | |
|
34 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
35 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap; |
36 | |
import org.apache.commons.lang.BooleanUtils; |
37 | |
import org.apache.commons.logging.Log; |
38 | |
import org.apache.commons.logging.LogFactory; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class DynamicEntryPoint implements UMOEntryPoint |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | 6 | protected static final Log logger = LogFactory.getLog(DynamicEntryPoint.class); |
54 | |
|
55 | |
|
56 | 2 | protected static final Set IgnoredMethodNames = new HashSet(Arrays.asList(new String[]{"equals", |
57 | |
"getInvocationHandler"})); |
58 | |
|
59 | |
|
60 | 108 | private final ConcurrentMap entryPoints = new ConcurrentHashMap(); |
61 | |
|
62 | |
public DynamicEntryPoint() |
63 | |
{ |
64 | 108 | super(); |
65 | 108 | } |
66 | |
|
67 | |
protected Method addMethodByArgumentType(Method method, String payloadClass) |
68 | |
{ |
69 | 2 | Method previousMethod = (Method) entryPoints.putIfAbsent(payloadClass, method); |
70 | 2 | return (previousMethod != null ? previousMethod : method); |
71 | |
} |
72 | |
|
73 | |
protected Method addMethodByName(Method method, String payloadClass) |
74 | |
{ |
75 | 2 | String methodName = method.getName(); |
76 | |
|
77 | 2 | ConcurrentMap argumentTypes = (ConcurrentMap) entryPoints.get(methodName); |
78 | 2 | if (argumentTypes == null) |
79 | |
{ |
80 | 2 | argumentTypes = new ConcurrentHashMap(); |
81 | 2 | ConcurrentMap previousTypes = (ConcurrentMap) entryPoints.putIfAbsent(methodName, argumentTypes); |
82 | 2 | if (previousTypes != null) |
83 | |
{ |
84 | 0 | argumentTypes = previousTypes; |
85 | |
} |
86 | |
} |
87 | |
|
88 | 2 | Method previousMethod = (Method) argumentTypes.putIfAbsent(payloadClass, method); |
89 | 2 | return (previousMethod != null ? previousMethod : method); |
90 | |
} |
91 | |
|
92 | |
protected Method getMethodByArgumentType(String argumentType) |
93 | |
{ |
94 | 16 | return (Method) entryPoints.get(argumentType); |
95 | |
} |
96 | |
|
97 | |
protected Method getMethodByName(String methodName, String argumentType) |
98 | |
{ |
99 | 8 | ConcurrentMap argumentTypes = (ConcurrentMap) entryPoints.get(methodName); |
100 | 8 | return (argumentTypes != null ? (Method) argumentTypes.get(argumentType) : null); |
101 | |
} |
102 | |
|
103 | |
public Object invoke(Object component, UMOEventContext context) throws Exception |
104 | |
{ |
105 | 22 | Method method = null; |
106 | 22 | Object payload = null; |
107 | |
|
108 | |
|
109 | 22 | boolean ignoreMethod = BooleanUtils.toBoolean((Boolean) context.getMessage().removeProperty( |
110 | |
MuleProperties.MULE_IGNORE_METHOD_PROPERTY)); |
111 | |
|
112 | 22 | if (!ignoreMethod) |
113 | |
{ |
114 | |
|
115 | 22 | Object methodOverride = context.getMessage().removeProperty(MuleProperties.MULE_METHOD_PROPERTY); |
116 | |
|
117 | 22 | if (methodOverride instanceof Method) |
118 | |
{ |
119 | |
|
120 | 0 | method = (Method) methodOverride; |
121 | |
} |
122 | 22 | else if (methodOverride != null) |
123 | |
{ |
124 | 8 | payload = context.getTransformedMessage(); |
125 | 8 | String payloadClassName = payload.getClass().getName(); |
126 | |
|
127 | |
|
128 | 8 | String methodOverrideName = methodOverride.toString(); |
129 | 8 | method = this.getMethodByName(methodOverrideName, payloadClassName); |
130 | |
|
131 | |
|
132 | 8 | if (method == null) |
133 | |
{ |
134 | |
|
135 | 8 | List matchingMethods = ClassUtils.getSatisfiableMethods(component.getClass(), ClassUtils |
136 | |
.getClassTypes(payload), true, true, IgnoredMethodNames); |
137 | |
|
138 | |
|
139 | 8 | for (Iterator i = matchingMethods.iterator(); i.hasNext();) |
140 | |
{ |
141 | 4 | Method candidate = (Method) i.next(); |
142 | 4 | if (candidate.getName().equals(methodOverride)) |
143 | |
{ |
144 | 2 | method = candidate; |
145 | 2 | break; |
146 | |
} |
147 | 2 | } |
148 | |
|
149 | |
|
150 | 8 | this.validateMethod(component, method, methodOverrideName); |
151 | |
|
152 | |
|
153 | |
|
154 | 4 | if (method != null) |
155 | |
{ |
156 | 2 | method = this.addMethodByName(method, payloadClassName); |
157 | |
} |
158 | |
} |
159 | |
} |
160 | |
} |
161 | |
|
162 | |
|
163 | 18 | if (method == null) |
164 | |
{ |
165 | |
|
166 | 16 | if (component instanceof Callable) |
167 | |
{ |
168 | 8 | method = Callable.class.getMethods()[0]; |
169 | 8 | payload = context; |
170 | |
} |
171 | |
else |
172 | |
{ |
173 | |
|
174 | |
|
175 | 8 | method = this.getMethodByArgumentType(context.getClass().getName()); |
176 | 8 | if (method == null) |
177 | |
{ |
178 | |
|
179 | 8 | payload = context.getTransformedMessage(); |
180 | 8 | method = this.getMethodByArgumentType(payload.getClass().getName()); |
181 | 8 | if (method != null) |
182 | |
{ |
183 | 0 | OptimizedRequestContext.unsafeRewriteEvent(new MuleMessage(payload, context.getMessage())); |
184 | |
} |
185 | |
} |
186 | |
else |
187 | |
{ |
188 | 0 | payload = context; |
189 | |
} |
190 | |
} |
191 | |
} |
192 | |
|
193 | |
|
194 | 18 | if (method == null) |
195 | |
{ |
196 | |
|
197 | 8 | List methods = ClassUtils.getSatisfiableMethods(component.getClass(), ClassUtils |
198 | |
.getClassTypes(context), true, false, IgnoredMethodNames); |
199 | |
|
200 | 8 | int numMethods = methods.size(); |
201 | 8 | if (numMethods > 1) |
202 | |
{ |
203 | |
|
204 | 2 | TooManySatisfiableMethodsException tmsmex = new TooManySatisfiableMethodsException(component |
205 | |
.getClass(), methods); |
206 | 2 | throw new InvocationTargetException(tmsmex, "There must be only one method accepting " |
207 | |
+ context.getClass().getName() + " in component " |
208 | |
+ component.getClass().getName()); |
209 | |
} |
210 | 6 | else if (numMethods == 1) |
211 | |
{ |
212 | |
|
213 | 0 | payload = context; |
214 | 0 | method = this.addMethodByArgumentType((Method) methods.get(0), payload.getClass().getName()); |
215 | |
} |
216 | |
else |
217 | |
{ |
218 | |
|
219 | 6 | payload = context.getTransformedMessage(); |
220 | 6 | OptimizedRequestContext.unsafeRewriteEvent(new MuleMessage(payload, context.getMessage())); |
221 | |
|
222 | 6 | methods = ClassUtils.getSatisfiableMethods(component.getClass(), ClassUtils |
223 | |
.getClassTypes(payload), true, true, IgnoredMethodNames); |
224 | |
|
225 | 6 | numMethods = methods.size(); |
226 | |
|
227 | 6 | if (numMethods > 1) |
228 | |
{ |
229 | |
|
230 | 2 | throw new TooManySatisfiableMethodsException(component.getClass(), methods); |
231 | |
} |
232 | 4 | else if (numMethods == 1) |
233 | |
{ |
234 | |
|
235 | 2 | method = this.addMethodByArgumentType((Method) methods.get(0), payload.getClass() |
236 | |
.getName()); |
237 | |
} |
238 | |
else |
239 | |
{ |
240 | |
|
241 | 2 | throw new NoSatisfiableMethodsException(component.getClass(), ClassUtils |
242 | |
.getClassTypes(payload)); |
243 | |
} |
244 | |
} |
245 | |
} |
246 | |
|
247 | 12 | if (payload == null) |
248 | |
{ |
249 | 0 | payload = context.getTransformedMessage(); |
250 | 0 | OptimizedRequestContext.unsafeRewriteEvent(new MuleMessage(payload, context.getMessage())); |
251 | |
} |
252 | |
|
253 | 12 | if (logger.isDebugEnabled()) |
254 | |
{ |
255 | 0 | logger.debug("Dynamic Entrypoint using method: " + component.getClass().getName() + "." |
256 | |
+ method.getName() + "(" + payload.getClass().getName() + ")"); |
257 | |
} |
258 | |
|
259 | 12 | return this.invokeMethod(component, method, payload); |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
protected Object invokeMethod(Object component, Method method, Object argument) |
266 | |
throws InvocationTargetException, IllegalAccessException |
267 | |
{ |
268 | 12 | String methodCall = null; |
269 | |
|
270 | 12 | if (logger.isDebugEnabled()) |
271 | |
{ |
272 | 0 | methodCall = component.getClass().getName() + "." + method.getName() + "(" |
273 | |
+ argument.getClass().getName() + ")"; |
274 | 0 | logger.debug("Invoking " + methodCall); |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
Object[] invocationArgs; |
291 | |
|
292 | 12 | if (argument.getClass().isArray()) |
293 | |
{ |
294 | 0 | if (Object[].class.isAssignableFrom(argument.getClass())) |
295 | |
{ |
296 | 0 | invocationArgs = (Object[]) argument; |
297 | |
} |
298 | |
else |
299 | |
{ |
300 | 0 | invocationArgs = new Object[]{argument}; |
301 | |
} |
302 | |
} |
303 | 12 | else if (argument instanceof NullPayload) |
304 | |
{ |
305 | 0 | invocationArgs = null; |
306 | |
} |
307 | |
else |
308 | |
{ |
309 | 12 | invocationArgs = new Object[]{argument}; |
310 | |
} |
311 | |
|
312 | 12 | Object result = method.invoke(component, invocationArgs); |
313 | 12 | if (method.getReturnType().equals(Void.TYPE)) |
314 | |
{ |
315 | 4 | result = VoidResult.getInstance(); |
316 | |
} |
317 | |
|
318 | 12 | if (logger.isDebugEnabled()) |
319 | |
{ |
320 | 0 | logger.debug("Result of call " + methodCall + " is " + (result == null ? "null" : "not null")); |
321 | |
} |
322 | |
|
323 | 12 | return result; |
324 | |
} |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
protected void validateMethod(Object component, Method method, String methodName) |
331 | |
throws NoSuchMethodException |
332 | |
{ |
333 | 8 | boolean fallback = component instanceof Callable; |
334 | |
|
335 | 8 | if (method != null) |
336 | |
{ |
337 | |
|
338 | |
try |
339 | |
{ |
340 | 2 | component.getClass().getMethod(method.getName(), method.getParameterTypes()); |
341 | |
} |
342 | 0 | catch (NoSuchMethodException e) |
343 | |
{ |
344 | 0 | if (!fallback) |
345 | |
{ |
346 | 0 | throw e; |
347 | |
} |
348 | 2 | } |
349 | |
} |
350 | |
else |
351 | |
{ |
352 | 6 | if (!fallback) |
353 | |
{ |
354 | 4 | throw new NoSuchMethodException( |
355 | |
CoreMessages.methodWithParamsNotFoundOnObject(methodName, "unknown", |
356 | |
component.getClass()).toString()); |
357 | |
} |
358 | |
} |
359 | 4 | } |
360 | |
|
361 | |
} |