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.config.MuleProperties; |
14 | |
import org.mule.api.lifecycle.Callable; |
15 | |
import org.mule.api.model.InvocationResult; |
16 | |
import org.mule.api.transport.PropertyScope; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.util.ClassUtils; |
19 | |
|
20 | |
import java.lang.reflect.Method; |
21 | |
|
22 | |
import org.apache.commons.lang.BooleanUtils; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class MethodHeaderPropertyEntryPointResolver extends AbstractEntryPointResolver |
30 | |
{ |
31 | |
|
32 | 0 | private String methodProperty = MuleProperties.MULE_METHOD_PROPERTY; |
33 | |
|
34 | |
public String getMethodProperty() |
35 | |
{ |
36 | 0 | return methodProperty; |
37 | |
} |
38 | |
|
39 | |
public void setMethodProperty(String methodProperty) |
40 | |
{ |
41 | 0 | this.methodProperty = methodProperty; |
42 | 0 | } |
43 | |
|
44 | |
public InvocationResult invoke(Object component, MuleEventContext context) throws Exception |
45 | |
{ |
46 | |
|
47 | 0 | boolean ignoreMethod = BooleanUtils.toBoolean(context.getMessage().<Boolean>getInboundProperty(MuleProperties.MULE_IGNORE_METHOD_PROPERTY)); |
48 | |
|
49 | 0 | if (ignoreMethod) |
50 | |
{ |
51 | |
|
52 | 0 | InvocationResult result = new InvocationResult(this, InvocationResult.State.NOT_SUPPORTED); |
53 | 0 | result.setErrorMessage("Property: " + MuleProperties.MULE_IGNORE_METHOD_PROPERTY + " was set so skipping this resolver"); |
54 | 0 | return result; |
55 | |
} |
56 | |
|
57 | |
|
58 | 0 | Object[] payload = getPayloadFromMessage(context); |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | Object methodProp = context.getMessage().removeProperty(getMethodProperty(), PropertyScope.INVOCATION); |
64 | 0 | if (methodProp == null) |
65 | |
{ |
66 | 0 | methodProp = context.getMessage().getInboundProperty(getMethodProperty()); |
67 | |
} |
68 | 0 | if (methodProp == null) |
69 | |
{ |
70 | 0 | InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED); |
71 | |
|
72 | 0 | result.setErrorMessage(CoreMessages.propertyIsNotSetOnEvent(getMethodProperty()).toString()); |
73 | 0 | return result; |
74 | |
} |
75 | |
|
76 | |
Method method; |
77 | |
String methodName; |
78 | 0 | if (methodProp instanceof Method) |
79 | |
{ |
80 | 0 | method = (Method) methodProp; |
81 | 0 | methodName = method.getName(); |
82 | |
} |
83 | |
else |
84 | |
{ |
85 | 0 | methodName = methodProp.toString(); |
86 | 0 | method = getMethodByName(component, methodName, context); |
87 | |
} |
88 | |
|
89 | 0 | if (method != null && method.getParameterTypes().length == 0) |
90 | |
{ |
91 | 0 | return invokeMethod(component, method, ClassUtils.NO_ARGS_TYPE); |
92 | |
} |
93 | |
|
94 | 0 | if (method == null) |
95 | |
{ |
96 | 0 | Class<?>[] classTypes = ClassUtils.getClassTypes(payload); |
97 | |
|
98 | 0 | method = ClassUtils.getMethod(component.getClass(), methodName, classTypes); |
99 | |
|
100 | 0 | if (method == null) |
101 | |
{ |
102 | 0 | InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED); |
103 | 0 | result.setErrorNoMatchingMethods(component, classTypes); |
104 | 0 | return result; |
105 | |
} |
106 | |
|
107 | |
} |
108 | |
|
109 | 0 | validateMethod(component, method); |
110 | 0 | addMethodByName(component, method, context); |
111 | |
|
112 | 0 | return invokeMethod(component, method, payload); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
protected void validateMethod(Object component, Method method) throws NoSuchMethodException |
124 | |
{ |
125 | 0 | boolean fallback = component instanceof Callable; |
126 | |
|
127 | 0 | if (method != null) |
128 | |
{ |
129 | |
|
130 | |
try |
131 | |
{ |
132 | 0 | component.getClass().getMethod(method.getName(), method.getParameterTypes()); |
133 | |
} |
134 | 0 | catch (NoSuchMethodException e) |
135 | |
{ |
136 | 0 | if (!fallback) |
137 | |
{ |
138 | 0 | throw e; |
139 | |
} |
140 | 0 | } |
141 | |
} |
142 | |
else |
143 | |
{ |
144 | 0 | if (!fallback) |
145 | |
{ |
146 | 0 | throw new NoSuchMethodException( |
147 | |
CoreMessages.methodWithParamsNotFoundOnObject("null", "unknown", |
148 | |
component.getClass()).toString()); |
149 | |
} |
150 | |
} |
151 | 0 | } |
152 | |
|
153 | |
@Override |
154 | |
public String toString() |
155 | |
{ |
156 | 0 | final StringBuffer sb = new StringBuffer(); |
157 | 0 | sb.append("MethodHeaderPropertyEntryPointResolver"); |
158 | 0 | sb.append("{methodHeader=").append(methodProperty); |
159 | 0 | sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods()); |
160 | 0 | sb.append('}'); |
161 | 0 | return sb.toString(); |
162 | |
} |
163 | |
|
164 | |
} |