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