1 17 package org.apache.servicemix.soap; 18 19 import java.net.URI ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.jbi.component.ComponentContext; 27 import javax.jbi.messaging.MessageExchange.Role; 28 import javax.jbi.servicedesc.ServiceEndpoint; 29 import javax.wsdl.Definition; 30 import javax.wsdl.Import; 31 import javax.wsdl.Types; 32 import javax.wsdl.WSDLException; 33 import javax.wsdl.extensions.ExtensibilityElement; 34 import javax.wsdl.extensions.ExtensionRegistry; 35 import javax.wsdl.extensions.schema.Schema; 36 import javax.wsdl.extensions.schema.SchemaImport; 37 import javax.wsdl.factory.WSDLFactory; 38 import javax.wsdl.xml.WSDLReader; 39 import javax.wsdl.xml.WSDLWriter; 40 import javax.xml.namespace.QName ; 41 42 import org.apache.servicemix.common.Endpoint; 43 import org.apache.servicemix.common.ExchangeProcessor; 44 import org.apache.servicemix.common.wsdl1.JbiExtension; 45 import org.apache.servicemix.common.xbean.XBeanServiceUnit; 46 import org.apache.servicemix.jbi.security.auth.AuthenticationService; 47 import org.apache.servicemix.jbi.security.keystore.KeystoreManager; 48 import org.apache.servicemix.soap.handlers.addressing.AddressingHandler; 49 import org.springframework.core.io.Resource; 50 import org.w3c.dom.Document ; 51 52 import com.ibm.wsdl.Constants; 53 54 public abstract class SoapEndpoint extends Endpoint { 55 56 protected ServiceEndpoint activated; 57 protected ExchangeProcessor processor; 58 protected Role role; 59 protected URI defaultMep; 60 protected boolean soap; 61 protected String soapVersion; 62 protected Resource wsdlResource; 63 protected QName defaultOperation; 64 protected QName targetInterfaceName; 65 protected QName targetService; 66 protected String targetEndpoint; 67 protected List policies; 68 protected Map wsdls = new HashMap (); 69 70 public SoapEndpoint() { 71 policies = Collections.singletonList(new AddressingHandler()); 72 } 73 74 public AuthenticationService getAuthenticationService() { 75 return null; 76 } 77 78 public KeystoreManager getKeystoreManager() { 79 return null; 80 } 81 82 85 public List getPolicies() { 86 return policies; 87 } 88 91 public void setPolicies(List policies) { 92 this.policies = policies; 93 } 94 97 public URI getDefaultMep() { 98 return defaultMep; 99 } 100 103 public void setDefaultMep(URI defaultMep) { 104 this.defaultMep = defaultMep; 105 } 106 109 public QName getDefaultOperation() { 110 return defaultOperation; 111 } 112 115 public void setDefaultOperation(QName defaultOperation) { 116 this.defaultOperation = defaultOperation; 117 } 118 121 public Role getRole() { 122 return role; 123 } 124 127 public void setRole(Role role) { 128 this.role = role; 129 } 130 133 public boolean isSoap() { 134 return soap; 135 } 136 139 public void setSoap(boolean soap) { 140 this.soap = soap; 141 } 142 145 public String getSoapVersion() { 146 return soapVersion; 147 } 148 151 public void setSoapVersion(String soapVersion) { 152 this.soapVersion = soapVersion; 153 } 154 157 public String getTargetEndpoint() { 158 return targetEndpoint; 159 } 160 163 public void setTargetEndpoint(String targetEndpoint) { 164 this.targetEndpoint = targetEndpoint; 165 } 166 169 public QName getTargetInterfaceName() { 170 return targetInterfaceName; 171 } 172 175 public void setTargetInterfaceName(QName targetInterfaceName) { 176 this.targetInterfaceName = targetInterfaceName; 177 } 178 181 public QName getTargetService() { 182 return targetService; 183 } 184 187 public void setTargetService(QName targetServiceName) { 188 this.targetService = targetServiceName; 189 } 190 193 public Resource getWsdlResource() { 194 return wsdlResource; 195 } 196 199 public void setWsdlResource(Resource wsdlResource) { 200 this.wsdlResource = wsdlResource; 201 } 202 206 public void setRoleAsString(String role) { 207 if (role == null) { 208 throw new IllegalArgumentException ("Role must be specified"); 209 } else if (JbiExtension.ROLE_CONSUMER.equals(role)) { 210 setRole(Role.CONSUMER); 211 } else if (JbiExtension.ROLE_PROVIDER.equals(role)) { 212 setRole(Role.PROVIDER); 213 } else { 214 throw new IllegalArgumentException ("Unrecognized role: " + role); 215 } 216 } 217 218 221 protected void loadWsdl() { 222 if (description == null && wsdlResource != null) { 224 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 225 try { 226 if (serviceUnit instanceof XBeanServiceUnit) { 227 XBeanServiceUnit su = (XBeanServiceUnit) serviceUnit; 228 Thread.currentThread().setContextClassLoader(su.getKernel().getClassLoaderFor(su.getConfiguration())); 229 } 230 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); 231 reader.setFeature(Constants.FEATURE_VERBOSE, false); 232 Definition def = reader.readWSDL(wsdlResource.getURL().toString()); 233 overrideDefinition(def); 234 } catch (Exception e) { 235 logger.warn("Could not load description from resource", e); 236 } finally { 237 Thread.currentThread().setContextClassLoader(cl); 238 } 239 } 240 if (description == null && definition == null && getRole() == Role.CONSUMER) { 243 retrieveProxiedEndpointDefinition(); 244 } 245 if (description == null && definition != null) { 248 try { 249 description = WSDLFactory.newInstance().newWSDLWriter().getDocument(definition); 250 } catch (Exception e) { 251 logger.warn("Could not create document from wsdl description", e); 252 } 253 } 254 if (definition == null && description != null) { 257 try { 258 definition = WSDLFactory.newInstance().newWSDLReader().readWSDL(null, description); 259 } catch (Exception e) { 260 logger.warn("Could not create wsdl definition from dom document", e); 261 } 262 } 263 if (definition != null) { 264 try { 265 mapDefinition(definition); 266 } catch (Exception e) { 267 logger.warn("Could not map wsdl definition to documents", e); 268 } 269 } 270 } 271 272 277 protected void retrieveProxiedEndpointDefinition() { 278 if (logger.isDebugEnabled()) { 279 logger.debug("Retrieving proxied endpoint definition"); 280 } 281 try { 282 ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext(); 283 ServiceEndpoint ep = null; 284 if (targetService != null && targetEndpoint != null) { 285 ep = ctx.getEndpoint(targetService, targetEndpoint); 286 if (ep == null && logger.isDebugEnabled()) { 287 logger.debug("Could not retrieve endpoint targetService/targetEndpoint"); 288 } 289 } 290 if (ep == null && targetService != null) { 291 ServiceEndpoint[] eps = ctx.getEndpointsForService(targetService); 292 if (eps != null && eps.length > 0) { 293 ep = eps[0]; 294 } 295 if (ep == null && logger.isDebugEnabled()) { 296 logger.debug("Could not retrieve endpoint for targetService"); 297 } 298 } 299 if (ep == null && targetInterfaceName != null) { 300 ServiceEndpoint[] eps = ctx.getEndpoints(targetInterfaceName); 301 if (eps != null && eps.length > 0) { 302 ep = eps[0]; 303 } 304 if (ep == null && logger.isDebugEnabled()) { 305 logger.debug("Could not retrieve endpoint for targetInterfaceName"); 306 } 307 } 308 if (ep == null && service != null && endpoint != null) { 309 ep = ctx.getEndpoint(service, endpoint); 310 if (ep == null && logger.isDebugEnabled()) { 311 logger.debug("Could not retrieve endpoint for service/endpoint"); 312 } 313 } 314 if (ep != null) { 315 Document doc = ctx.getEndpointDescriptor(ep); 316 if (doc != null) { 317 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); 318 reader.setFeature(Constants.FEATURE_VERBOSE, false); 319 Definition def = reader.readWSDL(null, doc); 320 if (def != null) { 321 overrideDefinition(def); 322 } 323 } 324 } 325 } catch (Exception e) { 326 logger.debug("Unable to retrieve target endpoint descriptor", e); 327 } 328 } 329 330 333 public ExchangeProcessor getProcessor() { 334 return this.processor; 335 } 336 337 340 public void activate() throws Exception { 341 ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext(); 342 loadWsdl(); 343 if (getRole() == Role.PROVIDER) { 344 activated = ctx.activateEndpoint(service, endpoint); 345 processor = createProviderProcessor(); 346 } else { 347 activated = createExternalEndpoint(); 348 ctx.registerExternalEndpoint(activated); 349 processor = createConsumerProcessor(); 350 } 351 processor.start(); 352 } 353 354 public void activateDynamic() throws Exception { 355 if (getRole() == Role.PROVIDER) { 356 processor = createProviderProcessor(); 357 } else { 358 processor = createConsumerProcessor(); 359 } 360 processor.start(); 361 } 362 363 366 public void deactivate() throws Exception { 367 if (activated != null) { 368 ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext(); 369 if (getRole() == Role.PROVIDER) { 370 ServiceEndpoint ep = activated; 371 activated = null; 372 ctx.deactivateEndpoint(ep); 373 } else { 374 ServiceEndpoint ep = activated; 375 activated = null; 376 ctx.deregisterExternalEndpoint(ep); 377 } 378 } 379 processor.stop(); 380 } 381 382 protected abstract void overrideDefinition(Definition def) throws Exception ; 383 384 protected abstract ExchangeProcessor createProviderProcessor(); 385 386 protected abstract ExchangeProcessor createConsumerProcessor(); 387 388 protected abstract ServiceEndpoint createExternalEndpoint(); 389 390 protected WSDLReader createWsdlReader() throws WSDLException { 391 WSDLFactory factory = WSDLFactory.newInstance(); 392 ExtensionRegistry registry = factory.newPopulatedExtensionRegistry(); 393 registerExtensions(registry); 394 WSDLReader reader = factory.newWSDLReader(); 395 reader.setFeature(Constants.FEATURE_VERBOSE, false); 396 reader.setExtensionRegistry(registry); 397 return reader; 398 } 399 400 protected WSDLWriter createWsdlWriter() throws WSDLException { 401 WSDLFactory factory = WSDLFactory.newInstance(); 402 ExtensionRegistry registry = factory.newPopulatedExtensionRegistry(); 403 registerExtensions(registry); 404 WSDLWriter writer = factory.newWSDLWriter(); 405 return writer; 407 } 408 409 protected void registerExtensions(ExtensionRegistry registry) { 410 JbiExtension.register(registry); 411 } 412 413 414 protected void mapDefinition(Definition def) throws WSDLException { 415 wsdls.put("main.wsdl", createWsdlWriter().getDocument(def)); 416 mapImports(def); 417 } 418 419 protected void mapImports(Definition def) throws WSDLException { 420 Map imports = def.getImports(); 422 for (Iterator iter = imports.values().iterator(); iter.hasNext();) { 423 List imps = (List ) iter.next(); 424 for (Iterator iterator = imps.iterator(); iterator.hasNext();) { 425 Import imp = (Import) iterator.next(); 426 Definition impDef = imp.getDefinition(); 427 String impLoc = imp.getLocationURI(); 428 if (impDef != null && impLoc != null && !URI.create(impLoc).isAbsolute()) { 429 wsdls.put(impLoc, createWsdlWriter().getDocument(impDef)); 430 mapImports(impDef); 431 } 432 } 433 } 434 Types types = def.getTypes(); 436 if (types != null) { 437 for (Iterator it = types.getExtensibilityElements().iterator(); it.hasNext();) { 438 ExtensibilityElement ee = (ExtensibilityElement) it.next(); 439 if (ee instanceof Schema) { 440 Schema schema = (Schema) ee; 441 Map schemaImports = schema.getImports(); 442 for (Iterator iter = schemaImports.values().iterator(); iter.hasNext();) { 443 List imps = (List ) iter.next(); 444 for (Iterator iterator = imps.iterator(); iterator.hasNext();) { 445 SchemaImport schemaImport = (SchemaImport) iterator.next(); 446 Schema schemaImp = schemaImport.getReferencedSchema(); 447 String schemaLoc = schemaImport.getSchemaLocationURI(); 448 if (schemaLoc != null && schemaImp != null && schemaImp.getElement() != null && !URI.create(schemaLoc).isAbsolute()) { 449 wsdls.put(schemaLoc, schemaImp.getElement()); 450 } 451 } 452 } 453 } 454 } 455 } 456 } 457 458 461 public Map getWsdls() { 462 return wsdls; 463 } 464 465 } 466 | Popular Tags |