1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.model.resolvers; |
11 | |
|
12 | |
import org.mule.api.MuleEventContext; |
13 | |
import org.mule.api.model.InvocationResult; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.util.ClassUtils; |
16 | |
import org.mule.util.StringMessageUtils; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
import java.util.Collection; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.LinkedHashSet; |
22 | |
import java.util.Set; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 8 | public class ExplicitMethodEntryPointResolver extends AbstractEntryPointResolver |
31 | |
{ |
32 | |
|
33 | 8 | private Set methods = new LinkedHashSet(2); |
34 | |
|
35 | |
public void setMethods(Collection methods) |
36 | |
{ |
37 | 0 | this.methods = new LinkedHashSet(methods); |
38 | 0 | } |
39 | |
|
40 | |
public void addMethod(String name) |
41 | |
{ |
42 | 10 | this.methods.add(name); |
43 | 10 | } |
44 | |
|
45 | |
public boolean removeMethod(String name) |
46 | |
{ |
47 | 0 | return this.methods.remove(name); |
48 | |
} |
49 | |
|
50 | |
public InvocationResult invoke(Object component, MuleEventContext context) throws Exception |
51 | |
{ |
52 | 8 | if (methods == null || methods.size() == 0) |
53 | |
{ |
54 | 2 | throw new IllegalStateException(CoreMessages.objectIsNull("methods").toString()); |
55 | |
} |
56 | |
|
57 | 6 | Object[] payload = getPayloadFromMessage(context); |
58 | 6 | Class[] classTypes = ClassUtils.getClassTypes(payload); |
59 | 6 | Method method = null; |
60 | 6 | for (Iterator iterator = methods.iterator(); iterator.hasNext();) |
61 | |
{ |
62 | 8 | String methodName = (String) iterator.next(); |
63 | 8 | method = getMethodByName(methodName, context); |
64 | |
|
65 | 8 | if (method == null) |
66 | |
{ |
67 | 8 | method = ClassUtils.getMethod(component.getClass(), methodName, classTypes); |
68 | |
} |
69 | 8 | if (method != null) |
70 | |
{ |
71 | 4 | addMethodByName(method, context); |
72 | |
|
73 | |
|
74 | 4 | Class[] parameterTypes = method.getParameterTypes(); |
75 | 4 | if (ClassUtils.compare(parameterTypes, classTypes, false)) |
76 | |
{ |
77 | |
|
78 | 4 | break; |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | |
|
83 | 0 | method = null; |
84 | |
} |
85 | |
} |
86 | 4 | } |
87 | |
|
88 | 6 | if (method == null) |
89 | |
{ |
90 | 2 | InvocationResult result = new InvocationResult(InvocationResult.STATE_INVOKED_FAILED); |
91 | 2 | result.setErrorNoMatchingMethods(component, classTypes, this); |
92 | 2 | return result; |
93 | |
} |
94 | 4 | return invokeMethod(component, method, payload); |
95 | |
} |
96 | |
|
97 | |
public String toString() |
98 | |
{ |
99 | 2 | final StringBuffer sb = new StringBuffer(); |
100 | 2 | sb.append("ExplicitMethodEntryPointResolver"); |
101 | 2 | sb.append("{methods=").append(StringMessageUtils.toString(methods)); |
102 | 2 | sb.append("{transformFirst=").append(isTransformFirst()); |
103 | 2 | sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods()); |
104 | 2 | sb.append('}'); |
105 | 2 | return sb.toString(); |
106 | |
} |
107 | |
|
108 | |
} |