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