1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.soap.axis; |
12 | |
|
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.component.Component; |
15 | |
import org.mule.api.component.JavaComponent; |
16 | |
import org.mule.api.endpoint.EndpointURI; |
17 | |
import org.mule.api.endpoint.InboundEndpoint; |
18 | |
import org.mule.api.lifecycle.CreateException; |
19 | |
import org.mule.api.lifecycle.InitialisationException; |
20 | |
import org.mule.api.service.Service; |
21 | |
import org.mule.api.transport.Connector; |
22 | |
import org.mule.component.AbstractJavaComponent; |
23 | |
import org.mule.config.i18n.CoreMessages; |
24 | |
import org.mule.transport.AbstractMessageReceiver; |
25 | |
import org.mule.transport.soap.NamedParameter; |
26 | |
import org.mule.transport.soap.SoapMethod; |
27 | |
import org.mule.transport.soap.axis.extensions.MuleMsgProvider; |
28 | |
import org.mule.transport.soap.axis.extensions.MuleRPCProvider; |
29 | |
import org.mule.transport.soap.axis.i18n.AxisMessages; |
30 | |
import org.mule.util.StringUtils; |
31 | |
|
32 | |
import java.util.HashMap; |
33 | |
import java.util.Iterator; |
34 | |
import java.util.List; |
35 | |
import java.util.Map; |
36 | |
|
37 | |
import javax.xml.rpc.ParameterMode; |
38 | |
|
39 | |
import org.apache.axis.AxisProperties; |
40 | |
import org.apache.axis.constants.Style; |
41 | |
import org.apache.axis.constants.Use; |
42 | |
import org.apache.axis.description.OperationDesc; |
43 | |
import org.apache.axis.description.ParameterDesc; |
44 | |
import org.apache.axis.handlers.soap.SOAPService; |
45 | |
import org.apache.axis.providers.java.JavaProvider; |
46 | |
import org.apache.axis.wsdl.fromJava.Namespaces; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class AxisMessageReceiver extends AbstractMessageReceiver |
54 | |
{ |
55 | |
|
56 | |
public static final String AXIS_OPTIONS = "axisOptions"; |
57 | |
public static final String BEAN_TYPES = "beanTypes"; |
58 | |
public static final String SERVICE_NAMESPACE = "serviceNamespace"; |
59 | |
|
60 | |
protected AxisConnector connector; |
61 | |
protected SOAPService soapService; |
62 | |
|
63 | |
public AxisMessageReceiver(Connector connector, Service service, InboundEndpoint endpoint) |
64 | |
throws CreateException |
65 | |
{ |
66 | |
|
67 | 378 | super(connector, service, endpoint); |
68 | |
|
69 | 378 | this.connector = (AxisConnector) connector; |
70 | |
try |
71 | |
{ |
72 | 378 | AxisServiceProxy.setProperties(endpoint.getProperties()); |
73 | 378 | create(); |
74 | |
} |
75 | 2 | catch (Exception e) |
76 | |
{ |
77 | 2 | throw new CreateException(e, this); |
78 | 376 | } |
79 | 376 | } |
80 | |
|
81 | |
protected void create() throws Exception |
82 | |
{ |
83 | 378 | AxisProperties.setProperty("axis.doAutoTypes", String.valueOf(connector.isDoAutoTypes())); |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | 378 | String style = (String) endpoint.getProperties().get(AxisConnector.STYLE); |
89 | 378 | String use = (String) endpoint.getProperties().get(AxisConnector.USE); |
90 | 378 | String doc = (String) endpoint.getProperties().get("documentation"); |
91 | |
|
92 | 378 | EndpointURI uri = endpoint.getEndpointURI(); |
93 | 378 | String serviceName = service.getName(); |
94 | |
|
95 | 378 | SOAPService existing = this.connector.getAxis().getService(serviceName); |
96 | 378 | if (existing != null) |
97 | |
{ |
98 | 0 | soapService = existing; |
99 | 0 | logger.debug("Using existing service for " + serviceName); |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | |
|
104 | |
|
105 | 378 | if (style != null && style.equalsIgnoreCase("message")) |
106 | |
{ |
107 | 52 | logger.debug("Creating Message Provider"); |
108 | 52 | soapService = new SOAPService(new MuleMsgProvider(connector)); |
109 | |
|
110 | |
|
111 | |
|
112 | |
} |
113 | |
else |
114 | |
{ |
115 | 326 | logger.debug("Creating RPC Provider"); |
116 | 326 | soapService = new SOAPService(new MuleRPCProvider(connector)); |
117 | |
} |
118 | |
|
119 | 378 | soapService.setEngine(connector.getAxis()); |
120 | |
} |
121 | |
|
122 | 378 | String servicePath = uri.getPath(); |
123 | 378 | soapService.setOption(serviceName, this); |
124 | 378 | soapService.setOption(AxisConnector.SERVICE_PROPERTY_SERVCE_PATH, servicePath); |
125 | 378 | soapService.setOption(AxisConnector.SERVICE_PROPERTY_COMPONENT_NAME, serviceName); |
126 | |
|
127 | 378 | soapService.setName(serviceName); |
128 | |
|
129 | |
|
130 | 378 | Map options = (Map) endpoint.getProperties().get(AXIS_OPTIONS); |
131 | |
|
132 | |
|
133 | 378 | if (options == null) |
134 | |
{ |
135 | 320 | options = new HashMap(2); |
136 | |
} |
137 | 378 | if (options.get("wsdlServiceElement") == null) |
138 | |
{ |
139 | 320 | options.put("wsdlServiceElement", serviceName); |
140 | |
} |
141 | |
|
142 | |
Map.Entry entry; |
143 | 378 | for (Iterator iterator = options.entrySet().iterator(); iterator.hasNext();) |
144 | |
{ |
145 | 390 | entry = (Map.Entry) iterator.next(); |
146 | 390 | soapService.setOption(entry.getKey().toString(), entry.getValue()); |
147 | 390 | logger.debug("Adding Axis option: " + entry); |
148 | |
} |
149 | |
|
150 | |
|
151 | 378 | Class[] interfaces = AxisServiceProxy.getInterfacesForComponent(service); |
152 | 378 | if (interfaces.length == 0) |
153 | |
{ |
154 | 2 | throw new InitialisationException( |
155 | |
AxisMessages.objectMustImplementAnInterface(serviceName), service); |
156 | |
} |
157 | |
|
158 | |
|
159 | 376 | String methodNames = "*"; |
160 | |
|
161 | 376 | Map methods = (Map) endpoint.getProperties().get(AxisConnector.SOAP_METHODS); |
162 | 376 | if (methods != null) |
163 | |
{ |
164 | 6 | Iterator i = methods.keySet().iterator(); |
165 | 6 | StringBuffer buf = new StringBuffer(64); |
166 | 12 | while (i.hasNext()) |
167 | |
{ |
168 | 6 | String name = (String) i.next(); |
169 | 6 | Object m = methods.get(name); |
170 | 6 | SoapMethod method = null; |
171 | 6 | if (m instanceof List) |
172 | |
{ |
173 | 6 | method = new SoapMethod(name, (List) m); |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | 0 | method = new SoapMethod(name, (String) m); |
178 | |
} |
179 | |
|
180 | 6 | List namedParameters = method.getNamedParameters(); |
181 | 6 | ParameterDesc[] parameters = new ParameterDesc[namedParameters.size()]; |
182 | 18 | for (int j = 0; j < namedParameters.size(); j++) |
183 | |
{ |
184 | 12 | NamedParameter parameter = (NamedParameter) namedParameters.get(j); |
185 | 12 | byte mode = ParameterDesc.INOUT; |
186 | 12 | if (parameter.getMode().equals(ParameterMode.IN)) |
187 | |
{ |
188 | 12 | mode = ParameterDesc.IN; |
189 | |
} |
190 | 0 | else if (parameter.getMode().equals(ParameterMode.OUT)) |
191 | |
{ |
192 | 0 | mode = ParameterDesc.OUT; |
193 | |
} |
194 | |
|
195 | 12 | parameters[j] = new ParameterDesc(parameter.getName(), mode, parameter.getType()); |
196 | |
} |
197 | |
|
198 | 6 | soapService.getServiceDescription().addOperationDesc( |
199 | |
new OperationDesc(method.getName().getLocalPart(), parameters, method.getReturnType())); |
200 | 6 | buf.append(method.getName().getLocalPart() + ","); |
201 | 6 | } |
202 | 6 | methodNames = buf.toString(); |
203 | 6 | methodNames = methodNames.substring(0, methodNames.length() - 1); |
204 | 6 | } |
205 | |
else |
206 | |
{ |
207 | 370 | String[] methodNamesArray = AxisServiceProxy.getMethodNames(interfaces); |
208 | 370 | StringBuffer buf = new StringBuffer(64); |
209 | 2084 | for (int i = 0; i < methodNamesArray.length; i++) |
210 | |
{ |
211 | 1714 | buf.append(methodNamesArray[i]).append(","); |
212 | |
} |
213 | 370 | methodNames = buf.toString(); |
214 | 370 | methodNames = methodNames.substring(0, methodNames.length() - 1); |
215 | |
} |
216 | |
|
217 | 376 | String className = interfaces[0].getName(); |
218 | |
|
219 | |
|
220 | 376 | String namespace = (String) endpoint.getProperties().get(SERVICE_NAMESPACE); |
221 | 376 | if (namespace == null) |
222 | |
{ |
223 | 318 | namespace = Namespaces.makeNamespace(className); |
224 | |
} |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | 376 | String wsdlFile = (String) endpoint.getProperties().get("wsdlFile"); |
230 | 376 | if (wsdlFile != null) |
231 | |
{ |
232 | 6 | soapService.getServiceDescription().setWSDLFile(wsdlFile); |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | 376 | setOptionIfNotset(soapService, JavaProvider.OPTION_WSDL_SERVICEPORT, serviceName); |
248 | 376 | setOptionIfNotset(soapService, JavaProvider.OPTION_CLASSNAME, className); |
249 | 376 | setOptionIfNotset(soapService, JavaProvider.OPTION_SCOPE, "Request"); |
250 | 376 | if (StringUtils.isNotBlank(namespace)) |
251 | |
{ |
252 | 376 | setOptionIfNotset(soapService, JavaProvider.OPTION_WSDL_TARGETNAMESPACE, namespace); |
253 | |
} |
254 | |
|
255 | |
|
256 | 376 | if (methodNames == null) |
257 | |
{ |
258 | 0 | setOptionIfNotset(soapService, JavaProvider.OPTION_ALLOWEDMETHODS, "*"); |
259 | |
} |
260 | |
else |
261 | |
{ |
262 | 376 | setOptionIfNotset(soapService, JavaProvider.OPTION_ALLOWEDMETHODS, methodNames); |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | 376 | if (style != null) |
270 | |
{ |
271 | 64 | Style s = Style.getStyle(style); |
272 | 64 | if (s == null) |
273 | |
{ |
274 | 0 | throw new CreateException( |
275 | |
CoreMessages.valueIsInvalidFor(style, AxisConnector.STYLE), this); |
276 | |
} |
277 | |
else |
278 | |
{ |
279 | 64 | soapService.setStyle(s); |
280 | |
} |
281 | |
} |
282 | |
|
283 | 376 | if (use != null) |
284 | |
{ |
285 | 12 | Use u = Use.getUse(use); |
286 | 12 | if (u == null) |
287 | |
{ |
288 | 0 | throw new CreateException(CoreMessages.valueIsInvalidFor(use, AxisConnector.USE), |
289 | |
this); |
290 | |
} |
291 | |
else |
292 | |
{ |
293 | 12 | soapService.setUse(u); |
294 | |
} |
295 | |
} |
296 | |
|
297 | 376 | soapService.getServiceDescription().setDocumentation(doc); |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | 376 | soapService.setName(serviceName); |
316 | |
|
317 | |
|
318 | 376 | Component component = service.getComponent(); |
319 | 376 | if (component instanceof JavaComponent) |
320 | |
{ |
321 | 376 | ((AbstractJavaComponent) component).getObjectFactory().addObjectInitialisationCallback( |
322 | |
new AxisInitialisationCallback(soapService)); |
323 | |
} |
324 | |
|
325 | 376 | if (uri.getScheme().equalsIgnoreCase("servlet")) |
326 | |
{ |
327 | 2 | connector.addServletService(soapService); |
328 | 2 | String endpointUrl = uri.getAddress() + "/" + serviceName; |
329 | 2 | endpointUrl = endpointUrl.replaceFirst("servlet:", "http:"); |
330 | 2 | soapService.getServiceDescription().setEndpointURL(endpointUrl); |
331 | 2 | } |
332 | |
else |
333 | |
{ |
334 | 374 | soapService.getServiceDescription().setEndpointURL(uri.getAddress() + "/" + serviceName); |
335 | |
} |
336 | 376 | if (StringUtils.isNotBlank(namespace)) |
337 | |
{ |
338 | 376 | soapService.getServiceDescription().setDefaultNamespace(namespace); |
339 | |
} |
340 | 376 | soapService.init(); |
341 | 376 | soapService.stop(); |
342 | 376 | } |
343 | |
|
344 | |
protected void doConnect() throws Exception |
345 | |
{ |
346 | |
|
347 | 374 | connector.getServerProvider().deployService(soapService.getName(), soapService); |
348 | 374 | connector.registerReceiverWithMuleService(this, endpoint.getEndpointURI()); |
349 | 374 | } |
350 | |
|
351 | |
protected void doDisconnect() throws Exception |
352 | |
{ |
353 | |
try |
354 | |
{ |
355 | 374 | doStop(); |
356 | |
} |
357 | 0 | catch (MuleException e) |
358 | |
{ |
359 | 0 | logger.error(e.getMessage(), e); |
360 | 374 | } |
361 | |
|
362 | |
|
363 | |
|
364 | 374 | connector.unregisterReceiverWithMuleService(this, endpoint.getEndpointURI()); |
365 | 374 | } |
366 | |
|
367 | |
protected void doStart() throws MuleException |
368 | |
{ |
369 | 374 | if (soapService != null) |
370 | |
{ |
371 | 374 | soapService.start(); |
372 | |
} |
373 | 374 | } |
374 | |
|
375 | |
protected void doStop() throws MuleException |
376 | |
{ |
377 | 748 | if (soapService != null) |
378 | |
{ |
379 | 748 | soapService.stop(); |
380 | |
} |
381 | 748 | } |
382 | |
|
383 | |
protected void doDispose() |
384 | |
{ |
385 | |
|
386 | 752 | } |
387 | |
|
388 | |
protected void setOptionIfNotset(SOAPService service, String option, Object value) |
389 | |
{ |
390 | 1880 | Object val = service.getOption(option); |
391 | 1880 | if (val == null) |
392 | |
{ |
393 | 1874 | service.setOption(option, value); |
394 | |
} |
395 | 1880 | } |
396 | |
|
397 | |
public SOAPService getSoapService() |
398 | |
{ |
399 | 374 | return soapService; |
400 | |
} |
401 | |
} |