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.config.i18n.CoreMessages;
12 import org.mule.util.ClassUtils;
13 import org.mule.util.StringMessageUtils;
14
15 import java.lang.reflect.Method;
16 import java.util.Iterator;
17 import java.util.LinkedHashSet;
18 import java.util.Set;
19
20
21
22
23
24
25
26 public class ExplicitMethodEntryPointResolver extends AbstractEntryPointResolver
27 {
28
29 private Set<String> methods = new LinkedHashSet<String>(2);
30
31 public void setMethods(Set<String> methods)
32 {
33 this.methods.addAll(methods);
34 }
35
36 public void addMethod(String name)
37 {
38 this.methods.add(name);
39 }
40
41 public boolean removeMethod(String name)
42 {
43 return this.methods.remove(name);
44 }
45
46 public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
47 {
48 if (methods == null || methods.size() == 0)
49 {
50 throw new IllegalStateException(CoreMessages.objectIsNull("methods").toString());
51 }
52
53 Object[] payload = getPayloadFromMessage(context);
54 Class<?>[] classTypes = ClassUtils.getClassTypes(payload);
55 Method method = null;
56 for (Iterator<String> iterator = methods.iterator(); iterator.hasNext();)
57 {
58 String methodName = iterator.next();
59 method = getMethodByName(component, methodName, context);
60
61 if (method == null)
62 {
63 method = ClassUtils.getMethod(component.getClass(), methodName, classTypes, true);
64 }
65 if (method != null)
66 {
67 addMethodByName(component, method, context);
68
69
70 Class<?>[] parameterTypes = method.getParameterTypes();
71 if (ClassUtils.compare(parameterTypes, classTypes, false, true))
72 {
73
74 break;
75 }
76 else
77 {
78
79 method = null;
80 }
81 }
82 }
83
84 if (method == null)
85 {
86 InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
87 result.setErrorNoMatchingMethods(component, classTypes);
88 return result;
89 }
90 return invokeMethod(component, method, payload);
91 }
92
93 @Override
94 public String toString()
95 {
96 final StringBuffer sb = new StringBuffer();
97 sb.append("ExplicitMethodEntryPointResolver");
98 sb.append("{methods=").append(StringMessageUtils.toString(methods));
99 sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
100 sb.append('}');
101 return sb.toString();
102 }
103
104 }