Author: gnodet
Date: Wed Feb 11 16:57:26 2009
New Revision: 743397

URL: http://svn.apache.org/viewvc?rev=743397&view=rev
Log:
SMX4NMR-56: handle external endpoints

Modified:
    
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/AbstractComponentContext.java
    
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/ComponentContextImpl.java
    
servicemix/smx4/nmr/trunk/jbi/runtime/src/test/java/org/apache/servicemix/jbi/runtime/ComponentContextTest.java

Modified: 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/AbstractComponentContext.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/AbstractComponentContext.java?rev=743397&r1=743396&r2=743397&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/AbstractComponentContext.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/AbstractComponentContext.java
 Wed Feb 11 16:57:26 2009
@@ -55,6 +55,9 @@
 
 public abstract class AbstractComponentContext implements ComponentContext, 
MBeanNames {
 
+    public static final String INTERNAL_ENDPOINT = "jbi.internal";
+    public static final String EXTERNAL_ENDPOINT = "jbi.external";
+
     private static final Log LOG = 
LogFactory.getLog(AbstractComponentContext.class);
 
     protected DeliveryChannel dc;
@@ -111,10 +114,22 @@
             props = new HashMap<String, Object>();
             props.put(Endpoint.INTERFACE_NAME, interfaceName.toString());
         }
-        return internalQueryEndpoints(props);
+        return queryInternalEndpoints(props);
+    }
+
+    protected ServiceEndpoint[] queryInternalEndpoints(Map<String, Object> 
props) {
+        return doQueryEndpoints(props, false);
+    }
+
+    protected ServiceEndpoint[] queryExternalEndpoints(Map<String, Object> 
props) {
+        return doQueryEndpoints(props, true);
     }
 
-    protected ServiceEndpoint[] internalQueryEndpoints(Map<String, Object> 
props) {
+    protected ServiceEndpoint[] doQueryEndpoints(Map<String, Object> props, 
boolean external) {
+        if (props == null) {
+            props = new HashMap<String, Object>();
+        }
+        props.put(external ? EXTERNAL_ENDPOINT : INTERNAL_ENDPOINT, 
Boolean.TRUE.toString());
         List<Endpoint> endpoints = getNmr().getEndpointRegistry().query(props);
         List<ServiceEndpoint> ses = new ArrayList<ServiceEndpoint>();
         for (Endpoint endpoint : endpoints) {
@@ -148,7 +163,6 @@
 
     protected QName getServiceQNameFromProperties(Map<String, ?> epProps) {
         QName svcName = null;
-
         if (epProps != null && epProps.containsKey(Endpoint.SERVICE_NAME)) {
             Object prop = epProps.get(Endpoint.SERVICE_NAME);
             if (prop instanceof QName) {
@@ -168,15 +182,26 @@
         }
         Map<String, Object> props = new HashMap<String, Object>();
         props.put(Endpoint.SERVICE_NAME, serviceName.toString());
-        return internalQueryEndpoints(props);
+        return queryInternalEndpoints(props);
     }
 
     public ServiceEndpoint[] getExternalEndpoints(QName interfaceName) {
-        return new ServiceEndpoint[0];  // TODO
+        Map<String, Object> props = null;
+        if (interfaceName != null) {
+            props = new HashMap<String, Object>();
+            props.put(Endpoint.INTERFACE_NAME, interfaceName.toString());
+        }
+        return queryExternalEndpoints(props);
     }
 
     public ServiceEndpoint[] getExternalEndpointsForService(QName serviceName) 
{
-        return new ServiceEndpoint[0];  // TODO
+        if (serviceName == null) {
+            // invalid
+            throw new IllegalArgumentException("This method needs a non-null 
serviceName parameter!");
+        }
+        Map<String, Object> props = new HashMap<String, Object>();
+        props.put(Endpoint.SERVICE_NAME, serviceName.toString());
+        return queryExternalEndpoints(props);
     }
 
     public DeliveryChannel getDeliveryChannel() throws MessagingException {

Modified: 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/ComponentContextImpl.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/ComponentContextImpl.java?rev=743397&r1=743396&r2=743397&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/ComponentContextImpl.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/ComponentContextImpl.java
 Wed Feb 11 16:57:26 2009
@@ -43,6 +43,7 @@
 import org.apache.servicemix.jbi.runtime.impl.utils.DOMUtil;
 import org.apache.servicemix.nmr.api.Endpoint;
 import org.apache.servicemix.nmr.api.Exchange;
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
 import com.ibm.wsdl.Constants;
 
 /**
@@ -96,6 +97,7 @@
             props.put(Endpoint.NAME, serviceName.toString() + ":" + 
endpointName);
             props.put(Endpoint.SERVICE_NAME, serviceName.toString());
             props.put(Endpoint.ENDPOINT_NAME, endpointName);
+            props.put(INTERNAL_ENDPOINT, Boolean.TRUE.toString());
             Document doc = component.getComponent().getServiceDescription(se);
             if (doc != null) {
                 QName[] interfaceNames = getInterfaces(doc, se);
@@ -129,11 +131,51 @@
     }
 
     public void registerExternalEndpoint(ServiceEndpoint externalEndpoint) 
throws JBIException {
-        // TODO
+        try {
+            QName serviceName = externalEndpoint.getServiceName();
+            String endpointName = externalEndpoint.getEndpointName();
+            Map<String, Object> props = new HashMap<String, Object>();
+            props.put(Endpoint.NAME, serviceName.toString() + ":" + 
endpointName);
+            props.put(Endpoint.SERVICE_NAME, serviceName.toString());
+            props.put(Endpoint.ENDPOINT_NAME, endpointName);
+            props.put(EXTERNAL_ENDPOINT, Boolean.TRUE.toString());
+            props.put(ServiceEndpoint.class.getName(), externalEndpoint);
+            QName[] interfaceNames = externalEndpoint.getInterfaces();
+            if (interfaceNames != null) {
+                StringBuilder sb = new StringBuilder();
+                for (QName itf : interfaceNames) {
+                    if (sb.length() > 0) {
+                        sb.append(",");
+                    }
+                    sb.append(itf.toString());
+                }
+                props.put(Endpoint.INTERFACE_NAME, sb.toString());
+            }
+            Document doc = 
component.getComponent().getServiceDescription(externalEndpoint);
+            if (doc != null) {
+                String data = DOMUtil.asXML(doc);
+                String url = 
componentRegistry.getDocumentRepository().register(data.getBytes());
+                props.put(Endpoint.WSDL_URL, url);
+            }
+            EndpointImpl endpoint = new EndpointImpl(props);
+            endpoint.setQueue(queue);
+            
componentRegistry.getNmr().getEndpointRegistry().register(endpoint,  props);
+        } catch (TransformerException e) {
+            throw new JBIException(e);
+        }
     }
 
     public void deregisterExternalEndpoint(ServiceEndpoint externalEndpoint) 
throws JBIException {
-        // TODO
+        ServiceEndpoint[] ses = 
doQueryEndpoints(ServiceHelper.createMap(Endpoint.SERVICE_NAME,
+                                                                         
externalEndpoint.getServiceName().toString(),
+                                                                         
Endpoint.ENDPOINT_NAME,
+                                                                         
externalEndpoint.getEndpointName()),
+                                                 true);
+        if (ses != null && ses.length == 1) {
+            // TODO: retrieve the correct endpoint if needed
+            EndpointImpl ep = (EndpointImpl) ses[0];
+            componentRegistry.getNmr().getEndpointRegistry().unregister(ep, 
null);
+        }
     }
 
     public String getComponentName() {

Modified: 
servicemix/smx4/nmr/trunk/jbi/runtime/src/test/java/org/apache/servicemix/jbi/runtime/ComponentContextTest.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/runtime/src/test/java/org/apache/servicemix/jbi/runtime/ComponentContextTest.java?rev=743397&r1=743396&r2=743397&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/runtime/src/test/java/org/apache/servicemix/jbi/runtime/ComponentContextTest.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/runtime/src/test/java/org/apache/servicemix/jbi/runtime/ComponentContextTest.java
 Wed Feb 11 16:57:26 2009
@@ -17,8 +17,8 @@
 package org.apache.servicemix.jbi.runtime;
 
 import javax.jbi.component.ComponentContext;
-import javax.jbi.component.Component;
 import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.jbi.messaging.MessageExchange;
 import javax.xml.namespace.QName;
 import javax.wsdl.factory.WSDLFactory;
 import javax.wsdl.Definition;
@@ -29,18 +29,18 @@
 import junit.framework.TestCase;
 import org.apache.servicemix.nmr.api.NMR;
 import org.apache.servicemix.nmr.core.ServiceMix;
-import org.apache.servicemix.jbi.runtime.impl.ClientComponentContext;
 import org.apache.servicemix.jbi.runtime.impl.ComponentRegistryImpl;
-import org.apache.servicemix.jbi.util.DOMUtil;
 import org.apache.servicemix.common.DefaultComponent;
 import org.apache.servicemix.common.ServiceUnit;
 import org.apache.servicemix.common.endpoints.ProviderEndpoint;
+import org.apache.servicemix.common.endpoints.ConsumerEndpoint;
 import org.apache.servicemix.document.impl.DocumentRepositoryImpl;
 
 public class ComponentContextTest extends TestCase {
 
     private NMR nmr;
     private ComponentRegistry componentRegistry;
+    private SimpleComponentWrapper componentWrapper;
 
     @Override
     protected void setUp() throws Exception {
@@ -53,21 +53,29 @@
         this.componentRegistry = reg;
 
         DefaultComponent component = new DefaultComponent();
-        component.addEndpoint(new TestEndpoint(component.getServiceUnit(),
+        component.addEndpoint(new 
TestProviderEndpoint(component.getServiceUnit(),
                                                new QName("urn:test", 
"service"),
-                                               "endpoint",
+                                               "provider",
                                                new QName("urn:test", 
"interface")));
-        this.componentRegistry.register(new SimpleComponentWrapper(component), 
null);
+        component.addEndpoint(new 
TestConsumerEndpoint(component.getServiceUnit(),
+                                               new QName("urn:test", 
"service"),
+                                               "consumer",
+                                               new QName("urn:test", 
"interface")));
+        componentWrapper = new SimpleComponentWrapper(component);
+        this.componentRegistry.register(componentWrapper, null);
     }
 
     @Override
     protected void tearDown() throws Exception {
+        componentWrapper.getComponent().getLifeCycle().stop();
+        componentWrapper.getComponent().getLifeCycle().shutDown();
+        this.componentRegistry.unregister(componentWrapper, null);
         super.tearDown();
     }
 
     public void testQueryEndpoint() throws Exception {
         ComponentContext context = componentRegistry.createComponentContext();
-        ServiceEndpoint ep = context.getEndpoint(new QName("urn:test", 
"service"), "endpoint");
+        ServiceEndpoint ep = context.getEndpoint(new QName("urn:test", 
"service"), "provider");
         assertNotNull(ep);
     }
 
@@ -95,8 +103,32 @@
         assertNotNull(eps[0]);
     }
 
-    public static class TestEndpoint extends ProviderEndpoint {
-        public TestEndpoint(ServiceUnit serviceUnit, QName service, String 
endpoint, QName interfaceName) {
+    public void testQueryExternalEndpointForService() throws Exception {
+        ComponentContext context = componentRegistry.createComponentContext();
+        ServiceEndpoint[] eps = context.getExternalEndpointsForService(new 
QName("urn:test", "service"));
+        assertNotNull(eps);
+        assertEquals(1, eps.length);
+        assertNotNull(eps[0]);
+    }
+
+    public void testQueryExternalEndpointsForInterface() throws Exception {
+        ComponentContext context = componentRegistry.createComponentContext();
+        ServiceEndpoint[] eps = context.getExternalEndpoints(new 
QName("urn:test", "interface"));
+        assertNotNull(eps);
+        assertEquals(1, eps.length);
+        assertNotNull(eps[0]);
+    }
+
+    public void testQueryExternalEndpoints() throws Exception {
+        ComponentContext context = componentRegistry.createComponentContext();
+        ServiceEndpoint[] eps = context.getExternalEndpoints(null);
+        assertNotNull(eps);
+        assertEquals(1, eps.length);
+        assertNotNull(eps[0]);
+    }
+
+    public static class TestProviderEndpoint extends ProviderEndpoint {
+        public TestProviderEndpoint(ServiceUnit serviceUnit, QName service, 
String endpoint, QName interfaceName) {
             super(serviceUnit, service, endpoint);
             setInterfaceName(interfaceName);
             setDescription(createDescription(interfaceName));
@@ -119,6 +151,16 @@
                 return null;
             }
         }
+    }
+
+    public static class TestConsumerEndpoint extends ConsumerEndpoint {
+        public TestConsumerEndpoint(ServiceUnit serviceUnit, QName service, 
String endpoint, QName interfaceName) {
+            super(serviceUnit, service,  endpoint);
+            setTargetService(service);
+            setInterfaceName(interfaceName);
+        }
 
+        public void process(MessageExchange exchange) throws Exception {
+        }
     }
 }


Reply via email to