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