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.routing.filters.WildcardFilter; |
15 | |
import org.mule.util.ClassUtils; |
16 | |
import org.mule.util.StringMessageUtils; |
17 | |
import org.mule.util.StringUtils; |
18 | |
|
19 | |
import java.lang.reflect.Method; |
20 | |
import java.util.Arrays; |
21 | |
import java.util.Collection; |
22 | |
import java.util.Collections; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.List; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public class ReflectionEntryPointResolver extends AbstractEntryPointResolver |
56 | |
{ |
57 | |
|
58 | |
|
59 | 170 | private Set ignoredMethods = new HashSet(Arrays.asList(new String[]{"equals", |
60 | |
"getInvocationHandler", "set*", "toString", |
61 | |
"getClass", "notify", "notifyAll", "wait", "hashCode", "clone", "is*", "get*"})); |
62 | |
|
63 | |
protected WildcardFilter filter; |
64 | |
|
65 | |
public ReflectionEntryPointResolver() |
66 | 170 | { |
67 | 170 | updateFilter(); |
68 | 170 | } |
69 | |
|
70 | |
private void updateFilter() |
71 | |
{ |
72 | 328 | filter = new WildcardFilter(StringUtils.join(ignoredMethods, ',')); |
73 | 328 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public Collection getIgnoredMethods() |
82 | |
{ |
83 | 14 | return Collections.unmodifiableSet(ignoredMethods); |
84 | |
} |
85 | |
|
86 | |
public void setIgnoredMethods(Collection methods) |
87 | |
{ |
88 | 20 | this.ignoredMethods = new HashSet(methods); |
89 | 20 | updateFilter(); |
90 | 20 | } |
91 | |
|
92 | |
public void addIgnoredMethod(String name) |
93 | |
{ |
94 | 0 | this.ignoredMethods.add(name); |
95 | 0 | updateFilter(); |
96 | 0 | } |
97 | |
|
98 | |
public boolean removeIgnoredMethod(String name) |
99 | |
{ |
100 | 138 | boolean result = this.ignoredMethods.remove(name); |
101 | 138 | updateFilter(); |
102 | 138 | return result; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public InvocationResult invoke(Object component, MuleEventContext context) throws Exception |
117 | |
{ |
118 | 48 | Object[] payload = getPayloadFromMessage(context); |
119 | |
|
120 | |
Method method; |
121 | |
InvocationResult result; |
122 | |
|
123 | 48 | method = this.getMethodByArguments(component, payload); |
124 | |
|
125 | 48 | if (method != null) |
126 | |
{ |
127 | 0 | return invokeMethod(component, method, payload); |
128 | |
} |
129 | |
|
130 | 48 | Class[] types = ClassUtils.getClassTypes(payload); |
131 | |
|
132 | |
|
133 | 48 | List methods = ClassUtils.getSatisfiableMethods(component.getClass(), types, |
134 | |
isAcceptVoidMethods(), false, ignoredMethods, filter); |
135 | |
|
136 | 48 | int numMethods = methods.size(); |
137 | 48 | if (numMethods > 1) |
138 | |
{ |
139 | 0 | result = new InvocationResult(InvocationResult.STATE_INVOKED_FAILED); |
140 | |
|
141 | 0 | result.setErrorTooManyMatchingMethods(component, types, StringMessageUtils.toString(methods), this); |
142 | 0 | return result; |
143 | |
|
144 | |
} |
145 | 48 | else if (numMethods == 1) |
146 | |
{ |
147 | |
|
148 | 12 | method = this.addMethodByArguments(component, (Method) methods.get(0), payload); |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | 36 | methods = ClassUtils.getSatisfiableMethods(component.getClass(), ClassUtils |
153 | |
.getClassTypes(payload), true, true, ignoredMethods); |
154 | |
|
155 | 36 | numMethods = methods.size(); |
156 | |
|
157 | 36 | if (numMethods > 1) |
158 | |
{ |
159 | 8 | result = new InvocationResult(InvocationResult.STATE_INVOKED_FAILED); |
160 | |
|
161 | 8 | result.setErrorTooManyMatchingMethods(component, types, StringMessageUtils.toString(methods), this); |
162 | 8 | return result; |
163 | |
} |
164 | 28 | else if (numMethods == 1) |
165 | |
{ |
166 | |
|
167 | 14 | method = this.addMethodByArguments(component, (Method) methods.get(0), payload); |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 14 | result = new InvocationResult(InvocationResult.STATE_INVOKED_FAILED); |
172 | |
|
173 | 14 | result.setErrorNoMatchingMethods(component, ClassUtils.getClassTypes(payload), this); |
174 | 14 | return result; |
175 | |
} |
176 | |
} |
177 | |
|
178 | 26 | return invokeMethod(component, method, payload); |
179 | |
} |
180 | |
|
181 | |
|
182 | |
public String toString() |
183 | |
{ |
184 | 22 | final StringBuffer sb = new StringBuffer(); |
185 | 22 | sb.append("ReflectionEntryPointResolver"); |
186 | 22 | sb.append("{ignoredMethods=").append(StringMessageUtils.toString(ignoredMethods)); |
187 | 22 | sb.append("{transformFirst=").append(isTransformFirst()); |
188 | 22 | sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods()); |
189 | 22 | sb.append('}'); |
190 | 22 | return sb.toString(); |
191 | |
} |
192 | |
} |