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