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