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