You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/03/13 17:19:02 UTC

svn commit: r385586 - in /incubator/servicemix/trunk/servicemix-http/src: main/java/org/apache/servicemix/http/ test/java/org/apache/servicemix/http/ test/resources/xbean/

Author: gnodet
Date: Mon Mar 13 08:19:00 2006
New Revision: 385586

URL: http://svn.apache.org/viewcvs?rev=385586&view=rev
Log:
SM-218: ability generate wsdl for proxied endpoints if not provided
+ ability to specify a spring resource for the wsdl

Added:
    incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/SoapService.wsdl
    incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/provider.wsdl
Modified:
    incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpComponent.java
    incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpEndpoint.java
    incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpExternalEndpoint.java
    incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpConsumerTest.java
    incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpXBeanDeployerTest.java
    incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/xbean.xml

Modified: incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpComponent.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpComponent.java?rev=385586&r1=385585&r2=385586&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpComponent.java (original)
+++ incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpComponent.java Mon Mar 13 08:19:00 2006
@@ -39,7 +39,7 @@
      * @see org.servicemix.common.BaseComponent#createServiceUnitManager()
      */
     public BaseServiceUnitManager createServiceUnitManager() {
-        Deployer[] deployers = new Deployer[] { new HttpWsdl1Deployer(this), new HttpXBeanDeployer(this) };
+        Deployer[] deployers = new Deployer[] { new HttpXBeanDeployer(this), new HttpWsdl1Deployer(this) };
         return new BaseServiceUnitManager(this, deployers);
     }
 

Modified: incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpEndpoint.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpEndpoint.java?rev=385586&r1=385585&r2=385586&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpEndpoint.java (original)
+++ incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpEndpoint.java Mon Mar 13 08:19:00 2006
@@ -15,18 +15,35 @@
  */
 package org.apache.servicemix.http;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 
 import javax.jbi.component.ComponentContext;
 import javax.jbi.messaging.MessageExchange.Role;
 import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.wsdl.Definition;
+import javax.wsdl.Port;
+import javax.wsdl.Service;
 import javax.wsdl.extensions.ExtensibilityElement;
+import javax.wsdl.extensions.http.HTTPAddress;
+import javax.wsdl.extensions.soap.SOAPAddress;
+import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.xml.WSDLReader;
+import javax.xml.parsers.DocumentBuilderFactory;
 
 import org.apache.servicemix.common.ExchangeProcessor;
 import org.apache.servicemix.common.wsdl1.JbiExtension;
+import org.apache.servicemix.common.xbean.XBeanServiceUnit;
 import org.apache.servicemix.http.processors.ConsumerProcessor;
 import org.apache.servicemix.http.processors.ProviderProcessor;
 import org.apache.servicemix.soap.SoapEndpoint;
+import org.springframework.core.io.Resource;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+import com.ibm.wsdl.extensions.http.HTTPAddressImpl;
+import com.ibm.wsdl.extensions.soap.SOAPAddressImpl;
 
 /**
  * 
@@ -46,7 +63,11 @@
     protected String locationURI;
     protected boolean soap;
     protected String soapVersion;
+    protected Resource wsdlResource;
     
+    /* (non-Javadoc)
+     * @see org.apache.servicemix.common.Endpoint#getProcessor()
+     */
     public ExchangeProcessor getProcessor() {
         return this.processor;
     }
@@ -56,6 +77,7 @@
      */
     public void activate() throws Exception {
         ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
+        loadWsdl();
         if (getRole() == Role.PROVIDER) {
             activated = ctx.activateEndpoint(service, endpoint);
             processor = new ProviderProcessor(this);
@@ -146,6 +168,111 @@
 
     public void setSoapVersion(String soapVersion) {
         this.soapVersion = soapVersion;
+    }
+    
+    /**
+     * Load the wsdl for this endpoint.
+     */
+    protected void loadWsdl() {
+        // Load WSDL from the resource
+        if (description == null && wsdlResource != null) {
+            InputStream is = null;
+            ClassLoader cl = Thread.currentThread().getContextClassLoader();
+            try {
+                if (serviceUnit instanceof XBeanServiceUnit) {
+                    XBeanServiceUnit su = (XBeanServiceUnit) serviceUnit;
+                    Thread.currentThread().setContextClassLoader(su.getKernel().getClassLoaderFor(su.getConfiguration()));
+                }
+                is = wsdlResource.getInputStream();
+                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+                dbf.setNamespaceAware(true);
+                Definition def = WSDLFactory.newInstance().newWSDLReader().readWSDL(null, new InputSource(is));
+                overrideDefinition(def);
+            } catch (Exception e) {
+                logger.warn("Could not load description from resource", e);
+            } finally {
+                Thread.currentThread().setContextClassLoader(cl);
+                if (is != null) {
+                    try {
+                        is.close();
+                    } catch (IOException e) {
+                        // Ignore
+                    }
+                }
+            }
+        }
+        // If the endpoint is a consumer, try to find
+        // the proxied endpoint description
+        if (description == null && definition == null && getRole() == Role.CONSUMER) {
+            retrieveProxiedEndpointDefinition();
+        }
+        // If the wsdl definition is provided,
+        // convert it to a DOM document
+        if (description == null && definition != null) {
+            try {
+                description = WSDLFactory.newInstance().newWSDLWriter().getDocument(definition);
+            } catch (Exception e) {
+                logger.warn("Could not create document from wsdl description", e);
+            }
+        }
+    }
+
+    /**
+     * Create a wsdl definition for a consumer endpoint.
+     * Loads the target endpoint definition and add http binding
+     * informations to it.
+     */
+    protected void retrieveProxiedEndpointDefinition() {
+        try {
+            if (service != null && endpoint != null) {
+                ComponentContext ctx = this.serviceUnit.getComponent().getComponentContext();
+                ServiceEndpoint se = ctx.getEndpoint(service, endpoint);
+                if (se != null) {
+                    Document doc = ctx.getEndpointDescriptor(se);
+                    if (doc != null) {
+                        WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
+                        Definition def = reader.readWSDL(null, doc);
+                        if (def != null) {
+                            overrideDefinition(def);
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            logger.debug("Unable to retrieve target endpoint descriptor", e);
+        }
+    }
+    
+    protected void overrideDefinition(Definition def) {
+        Service svc = def.getService(service);
+        if (svc != null) {
+            Port port = svc.getPort(endpoint);
+            if (port != null) {
+                port.getExtensibilityElements().clear();
+                if (isSoap()) {
+                    SOAPAddress address = new SOAPAddressImpl();
+                    address.setLocationURI(getLocationURI());
+                    port.addExtensibilityElement(address);
+                    def.addNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
+                } else {
+                    HTTPAddress address = new HTTPAddressImpl();
+                    address.setLocationURI(getLocationURI());
+                    port.addExtensibilityElement(address);
+                    def.addNamespace("http", "http://schemas.xmlsoap.org/wsdl/http/");
+                }
+                svc.getPorts().clear();
+                svc.addPort(port);
+                definition = def;
+            }
+        }
+    }
+
+    public Resource getWsdlResource() {
+        return wsdlResource;
+    }
+
+    public void setWsdlResource(Resource wsdlResource) {
+        this.wsdlResource = wsdlResource;
     }
 
 }

Modified: incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpExternalEndpoint.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpExternalEndpoint.java?rev=385586&r1=385585&r2=385586&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpExternalEndpoint.java (original)
+++ incubator/servicemix/trunk/servicemix-http/src/main/java/org/apache/servicemix/http/HttpExternalEndpoint.java Mon Mar 13 08:19:00 2006
@@ -53,6 +53,9 @@
     }
 
     public QName[] getInterfaces() {
+        if (endpoint.getInterfaceName() != null) {
+            return new QName[] { endpoint.getInterfaceName() }; 
+        }
         return null;
     }
 

Modified: incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpConsumerTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpConsumerTest.java?rev=385586&r1=385585&r2=385586&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpConsumerTest.java (original)
+++ incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpConsumerTest.java Mon Mar 13 08:19:00 2006
@@ -28,7 +28,6 @@
 
 import junit.framework.TestCase;
 
-import org.apache.commons.httpclient.HttpClient;
 import org.apache.servicemix.client.DefaultServiceMixClient;
 import org.apache.servicemix.components.http.HttpClientMarshaler;
 import org.apache.servicemix.components.http.HttpInvoker;

Modified: incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpXBeanDeployerTest.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpXBeanDeployerTest.java?rev=385586&r1=385585&r2=385586&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpXBeanDeployerTest.java (original)
+++ incubator/servicemix/trunk/servicemix-http/src/test/java/org/apache/servicemix/http/HttpXBeanDeployerTest.java Mon Mar 13 08:19:00 2006
@@ -21,6 +21,13 @@
 
 import javax.jbi.messaging.ExchangeStatus;
 import javax.jbi.messaging.InOut;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.wsdl.Binding;
+import javax.wsdl.Definition;
+import javax.wsdl.Port;
+import javax.wsdl.PortType;
+import javax.wsdl.Service;
+import javax.wsdl.factory.WSDLFactory;
 import javax.xml.namespace.QName;
 
 import org.apache.commons.logging.Log;
@@ -32,6 +39,7 @@
 import org.apache.servicemix.jbi.container.JBIContainer;
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
 import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.w3c.dom.Document;
 
 import junit.framework.TestCase;
 
@@ -61,7 +69,34 @@
         container.activateComponent(component, "HTTPComponent");
         
         // Add a receiver component
-        ActivationSpec asEcho = new ActivationSpec("echo", new EchoComponent());
+        ActivationSpec asEcho = new ActivationSpec("echo", new EchoComponent() {
+            public Document getServiceDescription(ServiceEndpoint endpoint) {
+                try {
+                    Definition def = WSDLFactory.newInstance().newDefinition();
+                    PortType type = def.createPortType();
+                    type.setUndefined(false);
+                    type.setQName(new QName("http://test", "MyConsumerInterface"));
+                    Binding binding = def.createBinding();
+                    binding.setUndefined(false);
+                    binding.setPortType(type);
+                    Service svc = def.createService();
+                    svc.setQName(new QName("http://test", "MyConsumerService"));
+                    Port port = def.createPort();
+                    port.setBinding(binding);
+                    port.setName("myConsumer");
+                    svc.addPort(port);
+                    def.setTargetNamespace("http://test");
+                    def.addNamespace("tns", "http://test");
+                    def.addPortType(type);
+                    def.addBinding(binding);
+                    def.addService(svc);
+                    Document doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(def);
+                    return doc;
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
         asEcho.setEndpoint("myConsumer");
         asEcho.setService(new QName("http://test", "MyConsumerService"));
         container.activateComponent(asEcho);
@@ -75,6 +110,17 @@
         path = path.getParentFile();
         component.getServiceUnitManager().deploy("xbean", path.getAbsolutePath());
         component.getServiceUnitManager().start("xbean");
+        
+        // Test wsdls
+        assertNotNull(container.getRegistry().getEndpointDescriptor(
+                container.getRegistry().getEndpoint(
+                        new QName("http://test", "MyProviderService"), "myProvider")));
+        assertNotNull(container.getRegistry().getEndpointDescriptor(
+                container.getRegistry().getExternalEndpointsForService(
+                        new QName("http://test", "MyConsumerService"))[0]));
+        assertNotNull(container.getRegistry().getEndpointDescriptor(
+                container.getRegistry().getExternalEndpointsForService(
+                        new QName("http://test", "MySoapService"))[0]));
         
         // Test
         DefaultServiceMixClient client = new DefaultServiceMixClient(container);

Added: incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/SoapService.wsdl
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/SoapService.wsdl?rev=385586&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/SoapService.wsdl (added)
+++ incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/SoapService.wsdl Mon Mar 13 08:19:00 2006
@@ -0,0 +1,16 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<definitions name='Consumer' 
+	         targetNamespace='http://test' 
+	         xmlns:tns='http://test' 
+             xmlns='http://schemas.xmlsoap.org/wsdl/'>
+
+   <portType name='Consumer'>
+   </portType>
+
+   <binding name='MyConsumer' type='tns:Consumer'>
+   </binding>
+
+   <service name='MySoapService'>
+      <port name='endpoint' binding='tns:MyConsumer' />
+   </service>
+</definitions>

Added: incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/provider.wsdl
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/provider.wsdl?rev=385586&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/provider.wsdl (added)
+++ incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/provider.wsdl Mon Mar 13 08:19:00 2006
@@ -0,0 +1,16 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<definitions name='Provider' 
+	         targetNamespace='http://test' 
+	         xmlns:tns='http://test' 
+             xmlns='http://schemas.xmlsoap.org/wsdl/'>
+
+   <portType name='Provider'>
+   </portType>
+
+   <binding name='MyProvider' type='tns:Provider'>
+   </binding>
+
+   <service name='MyProviderService'>
+      <port name='myProvider' binding='tns:MyProvider' />
+   </service>
+</definitions>
\ No newline at end of file

Modified: incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/xbean.xml
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/xbean.xml?rev=385586&r1=385585&r2=385586&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/xbean.xml (original)
+++ incubator/servicemix/trunk/servicemix-http/src/test/resources/xbean/xbean.xml Mon Mar 13 08:19:00 2006
@@ -2,6 +2,19 @@
 <beans xmlns:http="http://servicemix.apache.org/http/1.0"
        xmlns:test="http://test">
 
+	<!--  Define an additional classpath location
+	      for wsdl resources -->
+    <classpath>
+        <location>.</location>
+    </classpath>
+
+    <http:endpoint service="test:MySoapService"
+                   endpoint="endpoint"
+                   role="consumer"
+                   locationURI="http://localhost:8192/SoapService/"
+                   soap="true" 
+                   wsdlResource="classpath:SoapService.wsdl"/>
+
     <!-- START SNIPPET: consumer -->
     <http:endpoint service="test:MyConsumerService"
                    endpoint="myConsumer"
@@ -14,7 +27,8 @@
     <http:endpoint service="test:MyProviderService"
                    endpoint="myProvider"
                    role="provider" 
-                   locationURI="http://localhost:8192/Service/" />
+                   locationURI="http://localhost:8192/Service/"
+                   wsdlResource="classpath:provider.wsdl" />
     <!-- END SNIPPET: provider -->
   				   
 </beans>