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