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