1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.soap.axis; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.RequestContext; |
15 | |
import org.mule.api.ExceptionPayload; |
16 | |
import org.mule.api.MuleException; |
17 | |
import org.mule.api.MuleMessage; |
18 | |
import org.mule.api.component.JavaComponent; |
19 | |
import org.mule.api.config.MuleProperties; |
20 | |
import org.mule.api.lifecycle.Callable; |
21 | |
import org.mule.api.lifecycle.Disposable; |
22 | |
import org.mule.api.lifecycle.Initialisable; |
23 | |
import org.mule.api.service.Service; |
24 | |
import org.mule.api.transport.MessageAdapter; |
25 | |
import org.mule.config.ExceptionHelper; |
26 | |
import org.mule.transport.AbstractMessageReceiver; |
27 | |
import org.mule.transport.soap.SoapConstants; |
28 | |
import org.mule.transport.soap.axis.extras.AxisCleanAndAddProperties; |
29 | |
import org.mule.util.ClassUtils; |
30 | |
|
31 | |
import java.lang.reflect.InvocationHandler; |
32 | |
import java.lang.reflect.Method; |
33 | |
import java.lang.reflect.Proxy; |
34 | |
import java.util.ArrayList; |
35 | |
import java.util.Arrays; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class AxisServiceProxy |
46 | |
{ |
47 | |
|
48 | 2 | private static ThreadLocal properties = new ThreadLocal(); |
49 | |
|
50 | |
public static Object createProxy(AbstractMessageReceiver receiver, boolean synchronous, Class[] classes) |
51 | |
{ |
52 | 160 | final ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
53 | 160 | return Proxy.newProxyInstance(cl, classes, createServiceHandler(receiver, synchronous)); |
54 | |
} |
55 | |
|
56 | |
public static InvocationHandler createServiceHandler(AbstractMessageReceiver receiver, boolean synchronous) |
57 | |
{ |
58 | 160 | return new AxisServiceHandler(receiver, synchronous); |
59 | |
} |
60 | |
|
61 | 0 | private static class AxisServiceHandler implements InvocationHandler |
62 | |
{ |
63 | |
private AbstractMessageReceiver receiver; |
64 | 160 | private boolean synchronous = true; |
65 | |
|
66 | |
public AxisServiceHandler(AbstractMessageReceiver receiver, boolean synchronous) |
67 | 160 | { |
68 | 160 | this.receiver = receiver; |
69 | 160 | this.synchronous = synchronous; |
70 | 160 | } |
71 | |
|
72 | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
73 | |
{ |
74 | 160 | MessageAdapter messageAdapter = receiver.getConnector().getMessageAdapter(args); |
75 | 160 | messageAdapter.setProperty(MuleProperties.MULE_METHOD_PROPERTY, method); |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | 160 | messageAdapter.addProperties(AxisCleanAndAddProperties.cleanAndAdd(RequestContext.getEventContext())); |
83 | |
|
84 | 160 | MuleMessage message = receiver.routeMessage(new DefaultMuleMessage(messageAdapter), synchronous); |
85 | |
|
86 | 160 | if (message != null) |
87 | |
{ |
88 | 160 | ExceptionPayload wsException = message.getExceptionPayload(); |
89 | |
|
90 | 160 | if (wsException != null) |
91 | |
{ |
92 | 8 | MuleException umoException = ExceptionHelper.getRootMuleException(wsException.getException()); |
93 | |
|
94 | 8 | if (umoException.getCause() != null) |
95 | |
{ |
96 | 8 | throw umoException.getCause(); |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | throw umoException; |
101 | |
} |
102 | |
} |
103 | |
|
104 | 152 | return message.getPayload(); |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | return null; |
109 | |
} |
110 | |
} |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public static void setProperties(Map properties) |
131 | |
{ |
132 | 540 | AxisServiceProxy.properties.set(properties); |
133 | 540 | } |
134 | |
|
135 | |
public static Class[] getInterfacesForComponent(Service service) |
136 | |
throws MuleException, ClassNotFoundException |
137 | |
{ |
138 | |
Class[] interfaces; |
139 | 674 | List ifaces = null; |
140 | |
|
141 | 674 | Map localProperties = (Map) properties.get(); |
142 | 674 | if (null != localProperties) |
143 | |
{ |
144 | 666 | ifaces = (List) localProperties.get(SoapConstants.SERVICE_INTERFACES); |
145 | |
} |
146 | 674 | if (ifaces == null || ifaces.size() == 0) |
147 | |
{ |
148 | |
final Class implementationClass; |
149 | |
|
150 | 624 | if (service.getComponent() instanceof JavaComponent) |
151 | |
{ |
152 | |
try |
153 | |
{ |
154 | 624 | implementationClass = ((JavaComponent) service.getComponent()).getObjectType(); |
155 | |
} |
156 | 0 | catch (Exception e) |
157 | |
{ |
158 | 0 | throw new ClassNotFoundException("Unable to retrieve class from service factory", e); |
159 | 624 | } |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | 0 | throw new ClassNotFoundException("Unable to retrieve class from service factory"); |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | 624 | final List intfList = ClassUtils.getAllInterfaces(implementationClass); |
169 | 624 | interfaces = (Class[])intfList.toArray(new Class[intfList.size()]); |
170 | 624 | } |
171 | |
else |
172 | |
{ |
173 | 50 | interfaces = new Class[ifaces.size()]; |
174 | 150 | for (int i = 0; i < ifaces.size(); i++) |
175 | |
{ |
176 | 100 | String iface = (String)ifaces.get(i); |
177 | 100 | interfaces[i] = ClassUtils.loadClass(iface, AxisServiceProxy.class); |
178 | |
} |
179 | |
} |
180 | |
|
181 | 674 | interfaces = removeInterface(interfaces, Callable.class); |
182 | 674 | interfaces = removeInterface(interfaces, Disposable.class); |
183 | 674 | interfaces = removeInterface(interfaces, Initialisable.class); |
184 | 674 | return interfaces; |
185 | |
} |
186 | |
|
187 | |
public static Class[] removeInterface(Class[] interfaces, Class iface) |
188 | |
{ |
189 | 2022 | if (interfaces == null) |
190 | |
{ |
191 | 0 | return null; |
192 | |
} |
193 | 2022 | List results = new ArrayList(); |
194 | 10222 | for (int i = 0; i < interfaces.length; i++) |
195 | |
{ |
196 | 8200 | Class anInterface = interfaces[i]; |
197 | 8200 | if (!anInterface.equals(iface)) |
198 | |
{ |
199 | 7002 | results.add(anInterface); |
200 | |
} |
201 | |
} |
202 | 2022 | Class[] arResults = new Class[results.size()]; |
203 | 2022 | if (arResults.length == 0) |
204 | |
{ |
205 | 6 | return arResults; |
206 | |
} |
207 | |
else |
208 | |
{ |
209 | 2016 | results.toArray(arResults); |
210 | 2016 | return arResults; |
211 | |
} |
212 | |
} |
213 | |
|
214 | |
public static Method[] getMethods(Class[] interfaces) |
215 | |
{ |
216 | 370 | List methodNames = new ArrayList(); |
217 | 1530 | for (int i = 0; i < interfaces.length; i++) |
218 | |
{ |
219 | 1160 | methodNames.addAll(Arrays.asList(interfaces[i].getMethods())); |
220 | |
} |
221 | 370 | Method[] results = new Method[methodNames.size()]; |
222 | 370 | return (Method[])methodNames.toArray(results); |
223 | |
|
224 | |
} |
225 | |
|
226 | |
public static String[] getMethodNames(Class[] interfaces) |
227 | |
{ |
228 | 370 | Method[] methods = getMethods(interfaces); |
229 | |
|
230 | 370 | String[] results = new String[methods.length]; |
231 | 2084 | for (int i = 0; i < results.length; i++) |
232 | |
{ |
233 | 1714 | results[i] = methods[i].getName(); |
234 | |
} |
235 | 370 | return results; |
236 | |
} |
237 | |
|
238 | |
} |