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.util.ClassUtils; |
15 | |
import org.mule.util.StringMessageUtils; |
16 | |
|
17 | |
import java.lang.reflect.Method; |
18 | |
import java.util.Arrays; |
19 | |
import java.util.HashSet; |
20 | |
import java.util.List; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public abstract class AbstractArgumentEntryPointResolver extends ReflectionEntryPointResolver |
45 | |
{ |
46 | 0 | private Set<String> methods = new HashSet<String>(2); |
47 | |
|
48 | 0 | private boolean enableDiscovery = true; |
49 | |
|
50 | |
public AbstractArgumentEntryPointResolver() |
51 | 0 | { |
52 | |
|
53 | 0 | setAcceptVoidMethods(true); |
54 | |
|
55 | |
|
56 | 0 | setIgnoredMethods(new HashSet<String>(Arrays.asList("toString", |
57 | |
"getClass", "notify", "notifyAll", "wait", "hashCode", "clone", "is*", "get*"))); |
58 | 0 | } |
59 | |
|
60 | |
public Set<String> getMethods() |
61 | |
{ |
62 | 0 | return methods; |
63 | |
} |
64 | |
|
65 | |
public void setMethods(Set<String> methods) |
66 | |
{ |
67 | 0 | this.methods = methods; |
68 | 0 | } |
69 | |
|
70 | |
public void addMethod(String name) |
71 | |
{ |
72 | 0 | this.methods.add(name); |
73 | 0 | } |
74 | |
|
75 | |
public boolean removeMethod(String name) |
76 | |
{ |
77 | 0 | return this.methods.remove(name); |
78 | |
} |
79 | |
|
80 | |
|
81 | |
public boolean isEnableDiscovery() |
82 | |
{ |
83 | 0 | return enableDiscovery; |
84 | |
} |
85 | |
|
86 | |
public void setEnableDiscovery(boolean enableDiscovery) |
87 | |
{ |
88 | 0 | this.enableDiscovery = enableDiscovery; |
89 | 0 | } |
90 | |
|
91 | |
@Override |
92 | |
public InvocationResult invoke(Object component, MuleEventContext context) throws Exception |
93 | |
{ |
94 | 0 | Method method = null; |
95 | 0 | Object[] payload = getPayloadFromMessage(context); |
96 | |
|
97 | 0 | if (payload == null) |
98 | |
{ |
99 | 0 | return new InvocationResult(this, InvocationResult.State.NOT_SUPPORTED); |
100 | |
} |
101 | |
|
102 | 0 | for (String methodName : methods) |
103 | |
{ |
104 | 0 | method = getMethodByName(methodName, context); |
105 | |
|
106 | 0 | if (method == null) |
107 | |
{ |
108 | 0 | method = ClassUtils.getMethod(component.getClass(), methodName, |
109 | |
getMethodArgumentTypes(payload)); |
110 | |
} |
111 | 0 | if (method != null) |
112 | |
{ |
113 | 0 | addMethodByName(method, context); |
114 | 0 | break; |
115 | |
} |
116 | |
} |
117 | |
|
118 | 0 | if (method == null) |
119 | |
{ |
120 | 0 | if (isEnableDiscovery()) |
121 | |
{ |
122 | 0 | Class<?>[] argTypes = getMethodArgumentTypes(payload); |
123 | 0 | List<Method> methods = ClassUtils.getSatisfiableMethods(component.getClass(), argTypes, |
124 | |
isAcceptVoidMethods(), false, getIgnoredMethods(), filter); |
125 | |
|
126 | 0 | if (methods.size() > 1) |
127 | |
{ |
128 | 0 | InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED); |
129 | |
|
130 | 0 | result.setErrorTooManyMatchingMethods(component, argTypes, |
131 | |
StringMessageUtils.toString(methods)); |
132 | 0 | return result; |
133 | |
} |
134 | 0 | else if (methods.size() == 1) |
135 | |
{ |
136 | |
|
137 | 0 | method = this.addMethodByArguments(component, methods.get(0), |
138 | |
getPayloadFromMessage(context)); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 0 | InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED); |
143 | |
|
144 | 0 | result.setErrorNoMatchingMethods(component, ClassUtils.NO_ARGS_TYPE); |
145 | 0 | return result; |
146 | |
} |
147 | 0 | } |
148 | |
else |
149 | |
{ |
150 | 0 | InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED); |
151 | |
|
152 | 0 | result.setErrorNoMatchingMethodsCalled(component, StringMessageUtils.toString(methods)); |
153 | 0 | return result; |
154 | |
} |
155 | |
} |
156 | 0 | return invokeMethod(component, method, getPayloadFromMessage(context)); |
157 | |
} |
158 | |
|
159 | |
protected abstract Class<?>[] getMethodArgumentTypes(Object[] payload); |
160 | |
|
161 | |
@Override |
162 | |
public String toString() |
163 | |
{ |
164 | 0 | final StringBuffer sb = new StringBuffer(); |
165 | 0 | sb.append(ClassUtils.getClassName(getClass())); |
166 | 0 | sb.append("{methods=").append(StringMessageUtils.toString(methods)); |
167 | 0 | sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods()); |
168 | 0 | sb.append('}'); |
169 | 0 | return sb.toString(); |
170 | |
} |
171 | |
} |