Author: gnodet
Date: Fri Feb  6 10:55:25 2009
New Revision: 741526

URL: http://svn.apache.org/viewvc?rev=741526&view=rev
Log:
SMX4NMR-65: fix endpoints query

Added:
    
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
    
servicemix/smx4/nmr/trunk/jbi/runtime/src/main/java/org/apache/servicemix/jbi/runtime/impl/ComponentContextImpl.java
    
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.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=741526&r1=741525&r2=741526&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
 Fri Feb  6 10:55:25 2009
@@ -45,6 +45,7 @@
 
 import org.apache.servicemix.nmr.api.Endpoint;
 import org.apache.servicemix.nmr.api.NMR;
+import org.apache.servicemix.nmr.api.internal.InternalEndpoint;
 import org.apache.servicemix.jbi.runtime.impl.utils.DOMUtil;
 import org.apache.servicemix.jbi.runtime.impl.utils.WSAddressingConstants;
 import org.apache.servicemix.jbi.runtime.impl.utils.URIResolver;
@@ -108,23 +109,41 @@
         Map<String, Object> props = null;
         if (interfaceName != null) {
             props = new HashMap<String, Object>();
-            props.put(Endpoint.INTERFACE_NAME, interfaceName);
+            props.put(Endpoint.INTERFACE_NAME, interfaceName.toString());
         }
         return internalQueryEndpoints(props);
     }
 
-    protected ServiceEndpointImpl[] internalQueryEndpoints(Map<String, Object> 
props) {
+    protected ServiceEndpoint[] internalQueryEndpoints(Map<String, Object> 
props) {
         List<Endpoint> endpoints = getNmr().getEndpointRegistry().query(props);
         List<ServiceEndpoint> ses = new ArrayList<ServiceEndpoint>();
         for (Endpoint endpoint : endpoints) {
+            ServiceEndpoint se = getServiceEndpoint(endpoint);
+            if (se != null) {
+                ses.add(se);
+            }
+        }
+        return ses.toArray(new ServiceEndpoint[ses.size()]);
+    }
+
+    protected ServiceEndpoint getServiceEndpoint(Endpoint endpoint) {
+        if (endpoint instanceof InternalEndpoint) {
+            endpoint = ((InternalEndpoint) endpoint).getEndpoint();
+        }
+        if (endpoint instanceof ServiceEndpoint) {
+            ServiceEndpoint se = (ServiceEndpoint) endpoint;
+            if (se.getServiceName() != null && se.getEndpointName() != null) {
+                return se;
+            }
+        } else {
             Map<String, ?> epProps = 
getNmr().getEndpointRegistry().getProperties(endpoint);
             QName serviceName = getServiceQNameFromProperties(epProps);
             String endpointName = epProps.get(Endpoint.ENDPOINT_NAME) != null 
? (String) epProps.get(Endpoint.ENDPOINT_NAME) : null;
             if (serviceName != null && endpointName != null) {
-                ses.add(new ServiceEndpointImpl(epProps));
+                return new ServiceEndpointImpl(epProps);
             }
         }
-        return ses.toArray(new ServiceEndpointImpl[ses.size()]);
+        return null;
     }
 
     protected QName getServiceQNameFromProperties(Map<String, ?> epProps) {

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=741526&r1=741525&r2=741526&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
 Fri Feb  6 10:55:25 2009
@@ -27,6 +27,12 @@
 import javax.jbi.servicedesc.ServiceEndpoint;
 import javax.xml.namespace.QName;
 import javax.xml.transform.TransformerException;
+import javax.wsdl.xml.WSDLReader;
+import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.Definition;
+import javax.wsdl.PortType;
+import javax.wsdl.Service;
+import javax.wsdl.Port;
 
 import org.w3c.dom.Document;
 
@@ -37,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 com.ibm.wsdl.Constants;
 
 /**
  * The ComponentContext implementation
@@ -84,18 +91,30 @@
 
     public synchronized ServiceEndpoint activateEndpoint(QName serviceName, 
String endpointName) throws JBIException {
         try {
+            ServiceEndpoint se = new ServiceEndpointImpl(serviceName, 
endpointName);
             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);
-            EndpointImpl endpoint = new EndpointImpl(props);
-            endpoint.setQueue(queue);
-            Document doc = 
component.getComponent().getServiceDescription(endpoint);
+            Document doc = component.getComponent().getServiceDescription(se);
             if (doc != null) {
+                QName[] interfaceNames = getInterfaces(doc, se);
+                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());
+                }
                 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);
             return endpoint;
         } catch (TransformerException e) {
@@ -133,5 +152,71 @@
         return component;
     }
 
+    public static final String WSDL1_NAMESPACE = 
"http://schemas.xmlsoap.org/wsdl/";;
+
+    protected QName[] getInterfaces(Document document, ServiceEndpoint 
serviceEndpoint) {
+        try {
+            if (document == null || document.getDocumentElement() == null) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Endpoint " + serviceEndpoint + " has no service 
description");
+                }
+                return null;
+            }
+            if 
(!WSDL1_NAMESPACE.equals(document.getDocumentElement().getNamespaceURI())) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Endpoint " + serviceEndpoint + " has a non 
WSDL1 service description");
+                }
+                return null;
+            }
+            WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
+            reader.setFeature(Constants.FEATURE_VERBOSE, false);
+            Definition definition = reader.readWSDL(null, document);
+            // Check if the wsdl is only a port type
+            // In these cases, only the port type is used, as the service name 
and endpoint name
+            // are provided on the jbi endpoint
+            if (definition.getPortTypes().keySet().size() == 1
+                    && definition.getServices().keySet().size() == 0) {
+                PortType portType = (PortType) 
definition.getPortTypes().values().iterator().next();
+                QName interfaceName = portType.getQName();
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Endpoint " + serviceEndpoint + " implements 
interface " + interfaceName);
+                }
+                return new QName[] { interfaceName };
+            } else {
+                Service service = 
definition.getService(serviceEndpoint.getServiceName());
+                if (service == null) {
+                    LOG.info("Endpoint " + serviceEndpoint + " has a service 
description, but no matching service found in "
+                                    + definition.getServices().keySet());
+                    return null;
+                }
+                Port port = service.getPort(serviceEndpoint.getEndpointName());
+                if (port == null) {
+                    LOG.info("Endpoint " + serviceEndpoint + " has a service 
description, but no matching endpoint found in "
+                                    + service.getPorts().keySet());
+                    return null;
+                }
+                if (port.getBinding() == null) {
+                    LOG.info("Endpoint " + serviceEndpoint + " has a service 
description, but no binding found");
+                    return null;
+                }
+                if (port.getBinding().getPortType() == null) {
+                    LOG.info("Endpoint " + serviceEndpoint + " has a service 
description, but no port type found");
+                    return null;
+                }
+                QName interfaceName = 
port.getBinding().getPortType().getQName();
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Endpoint " + serviceEndpoint + " implements 
interface " + interfaceName);
+                }
+                return new QName[] { interfaceName };
+            }
+        } catch (Throwable e) {
+            LOG.warn("Error retrieving interfaces from service description: " 
+ e.getMessage());
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Error retrieving interfaces from service 
description", e);
+            }
+            return null;
+        }
+        
+    }
 
 }

Added: 
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=741526&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/runtime/src/test/java/org/apache/servicemix/jbi/runtime/ComponentContextTest.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/jbi/runtime/src/test/java/org/apache/servicemix/jbi/runtime/ComponentContextTest.java
 Fri Feb  6 10:55:25 2009
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.runtime;
+
+import javax.jbi.component.ComponentContext;
+import javax.jbi.component.Component;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.namespace.QName;
+import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.Definition;
+import javax.wsdl.PortType;
+
+import org.w3c.dom.Document;
+
+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.document.impl.DocumentRepositoryImpl;
+
+public class ComponentContextTest extends TestCase {
+
+    private NMR nmr;
+    private ComponentRegistry componentRegistry;
+
+    @Override
+    protected void setUp() throws Exception {
+        ServiceMix smx = new ServiceMix();
+        smx.init();
+        this.nmr = smx;
+        ComponentRegistryImpl reg = new ComponentRegistryImpl();
+        reg.setNmr(nmr);
+        reg.setDocumentRepository(new DocumentRepositoryImpl());
+        this.componentRegistry = reg;
+
+        DefaultComponent component = new DefaultComponent();
+        component.addEndpoint(new TestEndpoint(component.getServiceUnit(),
+                                               new QName("urn:test", 
"service"),
+                                               "endpoint",
+                                               new QName("urn:test", 
"interface")));
+        this.componentRegistry.register(new SimpleComponentWrapper(component), 
null);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testQueryEndpoint() throws Exception {
+        ComponentContext context = componentRegistry.createComponentContext();
+        ServiceEndpoint ep = context.getEndpoint(new QName("urn:test", 
"service"), "endpoint");
+        assertNotNull(ep);
+    }
+
+    public void testQueryEndpointForService() throws Exception {
+        ComponentContext context = componentRegistry.createComponentContext();
+        ServiceEndpoint[] eps = context.getEndpointsForService(new 
QName("urn:test", "service"));
+        assertNotNull(eps);
+        assertEquals(1, eps.length);
+        assertNotNull(eps[0]);
+    }
+
+    public void testQueryEndpointsForInterface() throws Exception {
+        ComponentContext context = componentRegistry.createComponentContext();
+        ServiceEndpoint[] eps = context.getEndpoints(new QName("urn:test", 
"interface"));
+        assertNotNull(eps);
+        assertEquals(1, eps.length);
+        assertNotNull(eps[0]);
+    }
+
+    public void testQueryEndpoints() throws Exception {
+        ComponentContext context = componentRegistry.createComponentContext();
+        ServiceEndpoint[] eps = context.getEndpoints(null);
+        assertNotNull(eps);
+        assertEquals(1, eps.length);
+        assertNotNull(eps[0]);
+    }
+
+    public static class TestEndpoint extends ProviderEndpoint {
+        public TestEndpoint(ServiceUnit serviceUnit, QName service, String 
endpoint, QName interfaceName) {
+            super(serviceUnit, service, endpoint);
+            setInterfaceName(interfaceName);
+            setDescription(createDescription(interfaceName));
+        }
+
+        private Document createDescription(QName interfaceName) {
+            try {
+                WSDLFactory factory = WSDLFactory.newInstance();
+                Definition def = factory.newDefinition();
+                def.setTargetNamespace(interfaceName.getNamespaceURI());
+                PortType port = def.createPortType();
+                port.setQName(interfaceName);
+                port.setUndefined(false);
+                def.addPortType(port);
+                Document doc = factory.newWSDLWriter().getDocument(def);
+                //System.err.println(DOMUtil.asXML(doc));
+                return doc;
+            } catch (Throwable t) {
+                t.printStackTrace();
+                return null;
+            }
+        }
+
+    }
+}

Modified: 
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java?rev=741526&r1=741525&r2=741526&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java
 Fri Feb  6 10:55:25 2009
@@ -266,9 +266,10 @@
             endpoints.addAll(registry.getServices());
         } else {
             for (InternalEndpoint e : registry.getServices()) {
+                Map<String, ?> epProps = registry.getProperties(e);
                 boolean match = true;
                 for (String name : properties.keySet()) {
-                    if 
(!properties.get(name).equals(registry.getProperties(e).get(name))) {
+                    if (!properties.get(name).equals(epProps.get(name))) {
                         match = false;
                         break;
                     }


Reply via email to