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