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