1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.cxf; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleException; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.config.MuleProperties; |
13 | |
import org.mule.api.transport.PropertyScope; |
14 | |
import org.mule.component.ComponentException; |
15 | |
import org.mule.transport.NullPayload; |
16 | |
|
17 | |
import java.lang.reflect.Method; |
18 | |
import java.lang.reflect.Proxy; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.apache.cxf.frontend.MethodDispatcher; |
22 | |
import org.apache.cxf.helpers.CastUtils; |
23 | |
import org.apache.cxf.interceptor.Fault; |
24 | |
import org.apache.cxf.message.Exchange; |
25 | |
import org.apache.cxf.message.FaultMode; |
26 | |
import org.apache.cxf.message.Message; |
27 | |
import org.apache.cxf.message.MessageContentsList; |
28 | |
import org.apache.cxf.service.Service; |
29 | |
import org.apache.cxf.service.invoker.Invoker; |
30 | |
import org.apache.cxf.service.model.BindingOperationInfo; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class MuleInvoker implements Invoker |
36 | |
{ |
37 | |
private final CxfInboundMessageProcessor cxfMmessageProcessor; |
38 | |
private Class<?> targetClass; |
39 | |
|
40 | |
public MuleInvoker(CxfInboundMessageProcessor cxfMmessageProcessor, Class<?> targetClass) |
41 | 0 | { |
42 | 0 | this.cxfMmessageProcessor = cxfMmessageProcessor; |
43 | 0 | this.targetClass = targetClass; |
44 | 0 | } |
45 | |
|
46 | |
public Object invoke(Exchange exchange, Object o) |
47 | |
{ |
48 | |
|
49 | 0 | MuleEvent event = (MuleEvent) exchange.get(CxfConstants.MULE_EVENT); |
50 | 0 | MuleEvent responseEvent = null; |
51 | |
try |
52 | |
{ |
53 | 0 | MuleMessage reqMsg = event.getMessage(); |
54 | 0 | reqMsg.setPayload(extractPayload(exchange.getInMessage())); |
55 | |
|
56 | 0 | BindingOperationInfo bop = exchange.get(BindingOperationInfo.class); |
57 | 0 | Service svc = exchange.get(Service.class); |
58 | 0 | if (!cxfMmessageProcessor.isProxy()) |
59 | |
{ |
60 | 0 | MethodDispatcher md = (MethodDispatcher) svc.get(MethodDispatcher.class.getName()); |
61 | 0 | Method m = md.getMethod(bop); |
62 | 0 | if (targetClass != null) |
63 | |
{ |
64 | 0 | m = matchMethod(m, targetClass); |
65 | |
} |
66 | |
|
67 | 0 | reqMsg.setProperty(MuleProperties.MULE_METHOD_PROPERTY, m, PropertyScope.INVOCATION); |
68 | |
} |
69 | |
|
70 | 0 | if (bop != null) |
71 | |
{ |
72 | 0 | reqMsg.setProperty(CxfConstants.INBOUND_OPERATION, bop.getOperationInfo().getName(), PropertyScope.INVOCATION); |
73 | 0 | reqMsg.setProperty(CxfConstants.INBOUND_SERVICE, svc.getName(), PropertyScope.INVOCATION); |
74 | |
} |
75 | |
|
76 | 0 | responseEvent = cxfMmessageProcessor.processNext(event); |
77 | |
} |
78 | 0 | catch (MuleException e) |
79 | |
{ |
80 | 0 | exchange.put(CxfConstants.MULE_EVENT, event); |
81 | |
|
82 | 0 | Throwable cause = e; |
83 | 0 | if (e instanceof ComponentException) { |
84 | 0 | cause = e.getCause(); |
85 | |
} |
86 | 0 | throw new Fault(cause); |
87 | |
} |
88 | 0 | catch (RuntimeException e) |
89 | |
{ |
90 | 0 | exchange.put(CxfConstants.MULE_EVENT, event); |
91 | 0 | throw new Fault(e); |
92 | 0 | } |
93 | |
|
94 | 0 | if (!event.getEndpoint().getExchangePattern().hasResponse()) |
95 | |
{ |
96 | |
|
97 | 0 | responseEvent = null; |
98 | |
} |
99 | |
|
100 | 0 | if (responseEvent != null) |
101 | |
{ |
102 | 0 | exchange.put(CxfConstants.MULE_EVENT, responseEvent); |
103 | 0 | MuleMessage resMessage = responseEvent.getMessage(); |
104 | |
|
105 | 0 | if (resMessage.getExceptionPayload() != null) |
106 | |
{ |
107 | 0 | Throwable cause = resMessage.getExceptionPayload().getException(); |
108 | 0 | if (cause instanceof ComponentException) |
109 | |
{ |
110 | 0 | cause = cause.getCause(); |
111 | |
} |
112 | |
|
113 | 0 | exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT); |
114 | 0 | if (cause instanceof Fault) |
115 | |
{ |
116 | 0 | throw (Fault) cause; |
117 | |
} |
118 | |
|
119 | 0 | throw new Fault(cause); |
120 | |
} |
121 | 0 | else if (resMessage.getPayload() instanceof NullPayload) |
122 | |
{ |
123 | 0 | return new MessageContentsList((Object)null); |
124 | |
} |
125 | 0 | else if (cxfMmessageProcessor.isProxy()) |
126 | |
{ |
127 | 0 | resMessage.getPayload(); |
128 | 0 | return new Object[] { resMessage }; |
129 | |
} |
130 | |
else |
131 | |
{ |
132 | 0 | return new Object[]{resMessage.getPayload()}; |
133 | |
} |
134 | |
} |
135 | |
else |
136 | |
{ |
137 | 0 | exchange.getInMessage().getInterceptorChain().abort(); |
138 | 0 | if (exchange.getOutMessage() != null) |
139 | |
{ |
140 | 0 | exchange.getOutMessage().getInterceptorChain().abort(); |
141 | |
} |
142 | 0 | exchange.put(CxfConstants.MULE_EVENT, null); |
143 | 0 | return null; |
144 | |
} |
145 | |
} |
146 | |
|
147 | |
protected Object extractPayload(Message cxfMessage) |
148 | |
{ |
149 | 0 | List<Object> list = CastUtils.cast(cxfMessage.getContent(List.class)); |
150 | 0 | if (list == null) |
151 | |
{ |
152 | |
|
153 | 0 | Object object = cxfMessage.getContent(Object.class); |
154 | 0 | if (object != null) |
155 | |
{ |
156 | 0 | return object; |
157 | |
} |
158 | |
else |
159 | |
{ |
160 | 0 | return new Object[0]; |
161 | |
} |
162 | |
} |
163 | |
|
164 | 0 | if ((list.size() == 1) && (list.get(0) != null)) |
165 | |
{ |
166 | 0 | return list.get(0); |
167 | |
} |
168 | |
else |
169 | |
{ |
170 | 0 | return list.toArray(); |
171 | |
} |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
private static Method matchMethod(Method methodToMatch, Class<?> targetClass) |
188 | |
{ |
189 | 0 | Class<?>[] interfaces = targetClass.getInterfaces(); |
190 | 0 | for (int i = 0; i < interfaces.length; i++) |
191 | |
{ |
192 | 0 | Method m = getMostSpecificMethod(methodToMatch, interfaces[i]); |
193 | 0 | if (!methodToMatch.equals(m)) |
194 | |
{ |
195 | 0 | return m; |
196 | |
} |
197 | |
} |
198 | 0 | return methodToMatch; |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public static boolean isJdkDynamicProxy(Object object) |
208 | |
{ |
209 | 0 | return object != null && Proxy.isProxyClass(object.getClass()); |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public static Method getMostSpecificMethod(Method method, Class<?> targetClass) { |
226 | 0 | if (method != null && targetClass != null) { |
227 | |
try { |
228 | 0 | method = targetClass.getMethod(method.getName(), method.getParameterTypes()); |
229 | 0 | } catch (NoSuchMethodException ex) { |
230 | |
|
231 | |
|
232 | 0 | } |
233 | |
} |
234 | 0 | return method; |
235 | |
} |
236 | |
} |