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