1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.cxf; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.config.MuleProperties; |
17 | |
import org.mule.api.endpoint.InboundEndpoint; |
18 | |
import org.mule.api.service.ServiceException; |
19 | |
import org.mule.transport.NullPayload; |
20 | |
|
21 | |
import java.lang.reflect.Method; |
22 | |
import java.lang.reflect.Proxy; |
23 | |
|
24 | |
import org.apache.cxf.frontend.MethodDispatcher; |
25 | |
import org.apache.cxf.interceptor.Fault; |
26 | |
import org.apache.cxf.message.Exchange; |
27 | |
import org.apache.cxf.message.FaultMode; |
28 | |
import org.apache.cxf.message.MessageContentsList; |
29 | |
import org.apache.cxf.service.Service; |
30 | |
import org.apache.cxf.service.invoker.Invoker; |
31 | |
import org.apache.cxf.service.model.BindingOperationInfo; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class MuleInvoker implements Invoker |
37 | |
{ |
38 | |
private final CxfMessageReceiver receiver; |
39 | |
private final boolean synchronous; |
40 | |
private Class<?> targetClass; |
41 | |
|
42 | |
public MuleInvoker(CxfMessageReceiver receiver, Class<?> targetClass, boolean synchronous) |
43 | 290 | { |
44 | 290 | this.receiver = receiver; |
45 | 290 | this.targetClass = targetClass; |
46 | 290 | this.synchronous = synchronous; |
47 | 290 | } |
48 | |
|
49 | |
public Object invoke(Exchange exchange, Object o) |
50 | |
{ |
51 | |
|
52 | 106 | MuleMessage message = null; |
53 | |
try |
54 | |
{ |
55 | 106 | MuleMessage reqMsg = (MuleMessage) exchange.getInMessage().get(CxfConstants.MULE_MESSAGE); |
56 | 106 | CxfMessageAdapter messageAdapter = (CxfMessageAdapter) receiver.getConnector().getMessageAdapter( |
57 | |
reqMsg); |
58 | 106 | messageAdapter.setPayload(exchange.getInMessage()); |
59 | |
|
60 | 106 | if (!receiver.isProxy()) |
61 | |
{ |
62 | 92 | BindingOperationInfo bop = exchange.get(BindingOperationInfo.class); |
63 | 92 | Service svc = exchange.get(Service.class); |
64 | 92 | MethodDispatcher md = (MethodDispatcher) svc.get(MethodDispatcher.class.getName()); |
65 | 92 | Method m = md.getMethod(bop); |
66 | 92 | if (targetClass != null) |
67 | |
{ |
68 | 92 | m = matchMethod(m, targetClass); |
69 | |
} |
70 | |
|
71 | 92 | messageAdapter.setProperty(MuleProperties.MULE_METHOD_PROPERTY, m); |
72 | |
} |
73 | |
|
74 | 106 | DefaultMuleMessage muleReq = new DefaultMuleMessage(messageAdapter); |
75 | 106 | String replyTo = (String) exchange.getInMessage().get(MuleProperties.MULE_REPLY_TO_PROPERTY); |
76 | 106 | if (replyTo != null) |
77 | |
{ |
78 | 0 | muleReq.setReplyTo(replyTo); |
79 | |
} |
80 | |
|
81 | 106 | String corId = (String) exchange.getInMessage().get(MuleProperties.MULE_CORRELATION_ID_PROPERTY); |
82 | 106 | if (corId != null) |
83 | |
{ |
84 | 12 | muleReq.setCorrelationId(corId); |
85 | |
} |
86 | |
|
87 | 106 | String corGroupSize = (String) exchange.getInMessage().get(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY); |
88 | 106 | if (corGroupSize != null) |
89 | |
{ |
90 | 12 | muleReq.setCorrelationGroupSize(Integer.valueOf(corGroupSize)); |
91 | |
} |
92 | |
|
93 | 106 | String corSeq = (String) exchange.getInMessage().get(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY); |
94 | 106 | if (corSeq != null) |
95 | |
{ |
96 | 12 | muleReq.setCorrelationSequence(Integer.valueOf(corSeq)); |
97 | |
} |
98 | |
|
99 | 106 | message = receiver.routeMessage(muleReq, synchronous); |
100 | |
} |
101 | 0 | catch (MuleException e) |
102 | |
{ |
103 | 0 | throw new Fault(e); |
104 | 106 | } |
105 | |
|
106 | 106 | if (message != null) |
107 | |
{ |
108 | 106 | if (message.getExceptionPayload() != null) |
109 | |
{ |
110 | 8 | Throwable cause = message.getExceptionPayload().getException(); |
111 | 8 | if (cause instanceof ServiceException) |
112 | |
{ |
113 | 8 | cause = cause.getCause(); |
114 | |
} |
115 | |
|
116 | 8 | exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT); |
117 | 8 | if (cause instanceof Fault) |
118 | |
{ |
119 | 0 | throw (Fault) cause; |
120 | |
} |
121 | |
|
122 | 8 | throw new Fault(cause); |
123 | |
} |
124 | 98 | else if (message.getPayload() instanceof NullPayload) |
125 | |
{ |
126 | 0 | return new MessageContentsList((Object)null); |
127 | |
} |
128 | |
else |
129 | |
{ |
130 | 98 | return new Object[]{message.getPayload()}; |
131 | |
} |
132 | |
} |
133 | |
else |
134 | |
{ |
135 | 0 | return new MessageContentsList((Object)null); |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
public InboundEndpoint getEndpoint() |
140 | |
{ |
141 | 0 | return receiver.getEndpoint(); |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private static Method matchMethod(Method methodToMatch, Class<?> targetClass) { |
158 | 92 | Class<?>[] interfaces = targetClass.getInterfaces(); |
159 | 176 | for (int i = 0; i < interfaces.length; i++) { |
160 | 162 | Method m = getMostSpecificMethod(methodToMatch, interfaces[i]); |
161 | 162 | if (!methodToMatch.equals(m)) { |
162 | 78 | return m; |
163 | |
} |
164 | |
} |
165 | 14 | return methodToMatch; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public static boolean isJdkDynamicProxy(Object object) { |
175 | 0 | return object != null && Proxy.isProxyClass(object.getClass()); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public static Method getMostSpecificMethod(Method method, Class<?> targetClass) { |
192 | 162 | if (method != null && targetClass != null) { |
193 | |
try { |
194 | 162 | method = targetClass.getMethod(method.getName(), method.getParameterTypes()); |
195 | 84 | } catch (NoSuchMethodException ex) { |
196 | |
|
197 | |
|
198 | 78 | } |
199 | |
} |
200 | 162 | return method; |
201 | |
} |
202 | |
} |