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.util.ClassUtils;
15 import org.mule.util.StringMessageUtils;
16
17 import java.lang.reflect.Method;
18 import java.util.Arrays;
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 public abstract class AbstractArgumentEntryPointResolver extends ReflectionEntryPointResolver
45 {
46 private Set<String> methods = new HashSet<String>(2);
47
48 private boolean enableDiscovery = true;
49
50 public AbstractArgumentEntryPointResolver()
51 {
52
53 setAcceptVoidMethods(true);
54
55
56 setIgnoredMethods(new HashSet<String>(Arrays.asList("toString",
57 "getClass", "notify", "notifyAll", "wait", "hashCode", "clone", "is*", "get*")));
58 }
59
60 public Set<String> getMethods()
61 {
62 return methods;
63 }
64
65 public void setMethods(Set<String> methods)
66 {
67 this.methods = methods;
68 }
69
70 public void addMethod(String name)
71 {
72 this.methods.add(name);
73 }
74
75 public boolean removeMethod(String name)
76 {
77 return this.methods.remove(name);
78 }
79
80
81 public boolean isEnableDiscovery()
82 {
83 return enableDiscovery;
84 }
85
86 public void setEnableDiscovery(boolean enableDiscovery)
87 {
88 this.enableDiscovery = enableDiscovery;
89 }
90
91 @Override
92 public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
93 {
94 Method method = null;
95 Object[] payload = getPayloadFromMessage(context);
96
97 if (payload == null)
98 {
99 return new InvocationResult(this, InvocationResult.State.NOT_SUPPORTED);
100 }
101
102 for (String methodName : methods)
103 {
104 method = getMethodByName(methodName, context);
105
106 if (method == null)
107 {
108 method = ClassUtils.getMethod(component.getClass(), methodName,
109 getMethodArgumentTypes(payload));
110 }
111 if (method != null)
112 {
113 addMethodByName(method, context);
114 break;
115 }
116 }
117
118 if (method == null)
119 {
120 if (isEnableDiscovery())
121 {
122 Class<?>[] argTypes = getMethodArgumentTypes(payload);
123 List<Method> methods = ClassUtils.getSatisfiableMethods(component.getClass(), argTypes,
124 isAcceptVoidMethods(), false, getIgnoredMethods(), filter);
125
126 if (methods.size() > 1)
127 {
128 InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
129
130 result.setErrorTooManyMatchingMethods(component, argTypes,
131 StringMessageUtils.toString(methods));
132 return result;
133 }
134 else if (methods.size() == 1)
135 {
136
137 method = this.addMethodByArguments(component, methods.get(0),
138 getPayloadFromMessage(context));
139 }
140 else
141 {
142 InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
143
144 result.setErrorNoMatchingMethods(component, ClassUtils.NO_ARGS_TYPE);
145 return result;
146 }
147 }
148 else
149 {
150 InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
151
152 result.setErrorNoMatchingMethodsCalled(component, StringMessageUtils.toString(methods));
153 return result;
154 }
155 }
156 return invokeMethod(component, method, getPayloadFromMessage(context));
157 }
158
159 protected abstract Class<?>[] getMethodArgumentTypes(Object[] payload);
160
161 @Override
162 public String toString()
163 {
164 final StringBuffer sb = new StringBuffer();
165 sb.append(ClassUtils.getClassName(getClass()));
166 sb.append("{methods=").append(StringMessageUtils.toString(methods));
167 sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
168 sb.append('}');
169 return sb.toString();
170 }
171 }