You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2012/08/20 20:05:04 UTC

svn commit: r1375151 - in /cxf/trunk/services/ws-discovery: ./ ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/ ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/listeners/ ws-discovery-service/ ws-discovery-service/src/ ws-discover...

Author: dkulp
Date: Mon Aug 20 18:05:04 2012
New Revision: 1375151

URL: http://svn.apache.org/viewvc?rev=1375151&view=rev
Log:
Start wiring the discovery service into the bus.
Auto-publish new services, handle basic probe responses.

Added:
    cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/listeners/
    cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/listeners/WSDiscoveryServerListener.java
    cxf/trunk/services/ws-discovery/ws-discovery-service/
    cxf/trunk/services/ws-discovery/ws-discovery-service/pom.xml
    cxf/trunk/services/ws-discovery/ws-discovery-service/src/
    cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/
    cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/resources/
    cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/resources/META-INF/
    cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/resources/META-INF/cxf/
    cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/resources/META-INF/cxf/bus-extensions.txt
Modified:
    cxf/trunk/services/ws-discovery/pom.xml
    cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryClient.java
    cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryService.java

Modified: cxf/trunk/services/ws-discovery/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/services/ws-discovery/pom.xml?rev=1375151&r1=1375150&r2=1375151&view=diff
==============================================================================
--- cxf/trunk/services/ws-discovery/pom.xml (original)
+++ cxf/trunk/services/ws-discovery/pom.xml Mon Aug 20 18:05:04 2012
@@ -34,6 +34,7 @@
 
     <modules>
         <module>ws-discovery-api</module>
+        <module>ws-discovery-service</module>
     </modules>
 
 </project>

Modified: cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryClient.java?rev=1375151&r1=1375150&r2=1375151&view=diff
==============================================================================
--- cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryClient.java (original)
+++ cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryClient.java Mon Aug 20 18:05:04 2012
@@ -57,6 +57,7 @@ import org.apache.cxf.ws.discovery.wsdl.
 import org.apache.cxf.ws.discovery.wsdl.ByeType;
 import org.apache.cxf.ws.discovery.wsdl.HelloType;
 import org.apache.cxf.ws.discovery.wsdl.ObjectFactory;
+import org.apache.cxf.ws.discovery.wsdl.ProbeMatchType;
 import org.apache.cxf.ws.discovery.wsdl.ProbeMatchesType;
 import org.apache.cxf.ws.discovery.wsdl.ProbeType;
 import org.apache.cxf.ws.discovery.wsdl.ScopesType;
@@ -166,6 +167,9 @@ public class WSDiscoveryClient implement
      * @return the hello
      */
     public HelloType register(HelloType hello) {
+        if (hello.getEndpointReference() == null) {
+            hello.setEndpointReference(generateW3CEndpointReference());
+        }
         getDispatchInternal(true).invokeOneWay(factory.createHello(hello));
         return hello;
     }
@@ -199,6 +203,25 @@ public class WSDiscoveryClient implement
         unregister(bt);
     }
     
+    public List<EndpointReference> probe(QName type) {
+        ProbeType p = new ProbeType();
+        p.getTypes().add(type);
+        ProbeMatchesType pmt = probe(p, 1000);
+        List<EndpointReference> er = new ArrayList<EndpointReference>();
+        for (ProbeMatchType pm : pmt.getProbeMatch()) {
+            for (String add : pm.getXAddrs()) {
+                W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
+                builder.address(add);
+                //builder.serviceName(type);
+                //builder.endpointName(type);
+                er.add(builder.build());
+            }
+        }
+        return er;
+    }    
+    
+    
+    
     
     public ProbeMatchesType probe(ProbeType params) {
         return probe(params, 1000);

Modified: cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryService.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryService.java?rev=1375151&r1=1375150&r2=1375151&view=diff
==============================================================================
--- cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryService.java (original)
+++ cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/WSDiscoveryService.java Mon Aug 20 18:05:04 2012
@@ -20,8 +20,10 @@
 package org.apache.cxf.ws.discovery;
 
 import java.io.IOException;
-import java.util.ArrayList;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.ListIterator;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
@@ -29,6 +31,7 @@ import javax.xml.bind.JAXBException;
 import javax.xml.bind.annotation.XmlSeeAlso;
 import javax.xml.bind.util.JAXBSource;
 import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.Source;
 import javax.xml.ws.Endpoint;
 import javax.xml.ws.EndpointReference;
@@ -36,21 +39,27 @@ import javax.xml.ws.Provider;
 import javax.xml.ws.WebServiceProvider;
 import javax.xml.ws.soap.Addressing;
 
+import org.w3c.dom.Document;
+
 import org.apache.cxf.Bus;
 import org.apache.cxf.common.jaxb.JAXBContextCache;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.staxutils.StaxUtils;
 import org.apache.cxf.ws.discovery.wsdl.ByeType;
 import org.apache.cxf.ws.discovery.wsdl.HelloType;
 import org.apache.cxf.ws.discovery.wsdl.ObjectFactory;
 import org.apache.cxf.ws.discovery.wsdl.ProbeMatchType;
 import org.apache.cxf.ws.discovery.wsdl.ProbeMatchesType;
 import org.apache.cxf.ws.discovery.wsdl.ProbeType;
+import org.apache.cxf.ws.discovery.wsdl.ScopesType;
 
 public class WSDiscoveryService {
     Bus bus;
     Endpoint udpEndpoint;
     WSDiscoveryClient client;
-    List<HelloType> registered = new ArrayList<HelloType>();
+    List<HelloType> registered = new CopyOnWriteArrayList<HelloType>();
     ObjectFactory factory = new ObjectFactory();
+    boolean started;
     
     public WSDiscoveryService(Bus b) {
         bus = b;
@@ -58,10 +67,53 @@ public class WSDiscoveryService {
     }
     
     public HelloType register(EndpointReference ref) {
+        startup();
         HelloType ht = client.register(ref); 
         registered.add(ht);
         return ht;
     }
+    
+    public void serverStarted(Server server) {
+        startup();
+        HelloType ht = new HelloType();
+        ht.setScopes(new ScopesType());
+        ht.setMetadataVersion(1);
+        ht.getTypes().add(server.getEndpoint().getEndpointInfo().getInterface().getName());
+        Object o = server.getEndpoint().get("ws-discover.scopes");
+        if (o != null) {
+            setScopes(ht, o);
+        }
+        setXAddrs(ht, server);
+        ht = client.register(ht);
+        registered.add(ht);
+        server.getEndpoint().put(HelloType.class.getName(), ht);
+    }
+
+    private void setXAddrs(HelloType ht, Server server) {
+        String s = server.getEndpoint().getEndpointInfo().getAddress();
+        //FIXME - servlet mode, need all the servlet information to create a full address
+        ht.getXAddrs().add(s);
+    }
+
+    private void setScopes(HelloType ht, Object o) {
+        if (o instanceof List) {
+            List<?> l = (List)o;
+            for (Object o2 : l) {
+                ht.getScopes().getValue().add(o2.toString());
+            }
+        } else {
+            ht.getScopes().getValue().add(o.toString());
+        }
+    }
+
+    public void serverStopped(Server server) {
+        HelloType ht = (HelloType)server.getEndpoint().get(HelloType.class.getName());
+        if (ht != null) {
+            unregister(ht);
+        }
+    }    
+    
+    
     public void unregister(HelloType ht) {
         registered.remove(ht);
         client.unregister(ht); 
@@ -69,10 +121,85 @@ public class WSDiscoveryService {
     
     
     
-    public void startup() {
-        udpEndpoint = Endpoint.create(new WSDiscoveryProvider());
-        udpEndpoint.publish("soap.udp://239.255.255.250:3702");
+    public synchronized void startup() {
+        if (!started) {
+            udpEndpoint = Endpoint.create(new WSDiscoveryProvider());
+            udpEndpoint.publish("soap.udp://239.255.255.250:3702");
+            started = true;
+        }
     }
+    
+    
+    public ProbeMatchesType handleProbe(ProbeType pt) {
+        List<HelloType> consider = new LinkedList<HelloType>(registered);
+        //step one, consider the "types"
+        //ALL types in the probe must be in the registered type
+        if (pt.getTypes() != null && !pt.getTypes().isEmpty()) {
+            ListIterator<HelloType> cit = consider.listIterator();
+            while (cit.hasNext()) {
+                HelloType ht = cit.next();
+                boolean matches = true;
+                for (QName qn : pt.getTypes()) {
+                    if (!ht.getTypes().contains(qn)) {
+                        matches = false;
+                    }
+                }
+                if (!matches) {
+                    cit.remove();
+                }
+            }
+        }
+        //next, consider the scopes
+        matchScopes(pt, consider);
+
+        if (consider.isEmpty()) {
+            return null;
+        }
+        ProbeMatchesType pmt = new ProbeMatchesType();
+        for (HelloType ht : consider) {
+            ProbeMatchType m = new ProbeMatchType();
+            m.setEndpointReference(ht.getEndpointReference());
+            m.setScopes(ht.getScopes());
+            m.setMetadataVersion(ht.getMetadataVersion());
+            m.getTypes().addAll(ht.getTypes());
+            m.getXAddrs().addAll(ht.getXAddrs());
+            pmt.getProbeMatch().add(m);
+        }
+        return pmt;
+    }
+    
+    private void matchScopes(ProbeType pt, List<HelloType> consider) {
+        if (pt.getScopes() == null || pt.getScopes().getValue().isEmpty()) {
+            return;
+        }
+        String mb = pt.getScopes().getMatchBy();
+        if (mb == null) {
+            mb = "http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/rfc3986";
+        }
+        
+        ListIterator<HelloType> cit = consider.listIterator();
+        while (cit.hasNext()) {
+            HelloType ht = cit.next();
+            boolean matches = false;
+            
+            if ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/rfc3986".equals(mb)) {
+                //FIXME
+            } else if ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/uuid".equals(mb)) {
+                //FIXME
+            } else if ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/ldap".equals(mb)) {
+                //FIXME
+            } else if ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/strcmp0".equals(mb)) {
+                //FIXME
+            } else if ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/none".equals(mb)
+                && (ht.getScopes() == null || ht.getScopes().getValue().isEmpty())) {
+                matches = true;
+            }
+            if (!matches) {
+                cit.remove();
+            }
+        }
+    }
+    
 
     @WebServiceProvider(wsdlLocation = "classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl",
         targetNamespace = "http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01",
@@ -91,22 +218,23 @@ public class WSDiscoveryService {
             }
         }
         
+
         public Source invoke(Source request) {
             try {
-                Object obj = context.createUnmarshaller().unmarshal(request);
+                //Bug in JAXB - if you pass the StaxSource or SaxSource into unmarshall,
+                //the namespaces for the QNames for the Types elements are lost.
+                //Since WS-Discovery messages are small (UDP datagram size), parsing to DOM
+                //is not a HUGE deal
+                Document doc = StaxUtils.read(request);
+                
+                Object obj = context.createUnmarshaller().unmarshal(doc.getDocumentElement());
                 if (obj instanceof JAXBElement) {
                     obj = ((JAXBElement)obj).getValue();
                 }
                 if (obj instanceof ProbeType) {
-                    ProbeMatchesType pmt = new ProbeMatchesType();
-                    for (HelloType ht : registered) {
-                        ProbeMatchType m = new ProbeMatchType();
-                        m.setEndpointReference(ht.getEndpointReference());
-                        m.setScopes(ht.getScopes());
-                        m.setMetadataVersion(ht.getMetadataVersion());
-                        m.getTypes().addAll(ht.getTypes());
-                        m.getXAddrs().addAll(ht.getXAddrs());
-                        pmt.getProbeMatch().add(m);
+                    ProbeMatchesType pmt = handleProbe((ProbeType)obj);
+                    if (pmt == null) {
+                        return null;
                     }
                     return new JAXBSource(context, factory.createProbeMatches(pmt));
                 } else if (obj instanceof HelloType) {
@@ -140,6 +268,9 @@ public class WSDiscoveryService {
             } catch (JAXBException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
+            } catch (XMLStreamException e1) {
+                // TODO Auto-generated catch block
+                e1.printStackTrace();
             }
             return null;
         }

Added: cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/listeners/WSDiscoveryServerListener.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/listeners/WSDiscoveryServerListener.java?rev=1375151&view=auto
==============================================================================
--- cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/listeners/WSDiscoveryServerListener.java (added)
+++ cxf/trunk/services/ws-discovery/ws-discovery-api/src/main/java/org/apache/cxf/ws/discovery/listeners/WSDiscoveryServerListener.java Mon Aug 20 18:05:04 2012
@@ -0,0 +1,59 @@
+/**
+ * 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.cxf.ws.discovery.listeners;
+
+import javax.xml.namespace.QName;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.endpoint.ServerLifeCycleListener;
+import org.apache.cxf.ws.discovery.WSDiscoveryService;
+
+/**
+ * 
+ */
+public class WSDiscoveryServerListener implements ServerLifeCycleListener {
+    WSDiscoveryService service;
+    
+    public WSDiscoveryServerListener(Bus bus) {
+        service = bus.getExtension(WSDiscoveryService.class);
+        if (service == null) {
+            service = new WSDiscoveryService(bus);
+            bus.setExtension(service, WSDiscoveryService.class);
+        }
+    }
+
+    public void startServer(Server server) {
+        QName sn = server.getEndpoint().getEndpointInfo().getInterface().getName();
+        System.out.println(sn);
+        if ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01".equals(sn.getNamespaceURI())) {
+            return;
+        }
+        service.serverStarted(server);
+    }
+
+    public void stopServer(Server server) {
+        QName sn = server.getEndpoint().getEndpointInfo().getInterface().getName();
+        if ("http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01".equals(sn.getNamespaceURI())) {
+            return;
+        }
+        service.serverStopped(server);
+    }
+}

Added: cxf/trunk/services/ws-discovery/ws-discovery-service/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/services/ws-discovery/ws-discovery-service/pom.xml?rev=1375151&view=auto
==============================================================================
--- cxf/trunk/services/ws-discovery/ws-discovery-service/pom.xml (added)
+++ cxf/trunk/services/ws-discovery/ws-discovery-service/pom.xml Mon Aug 20 18:05:04 2012
@@ -0,0 +1,67 @@
+<!--
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>org.apache.cxf.services.ws-discovery</groupId>
+    <artifactId>cxf-services-ws-discovery-service</artifactId>
+    <packaging>jar</packaging>
+    <version>2.7.0-SNAPSHOT</version>
+    <name>Apache CXF WS-Discovery Service</name>
+    <description>Apache CXF WS-Discovery Service</description>
+    <url>http://cxf.apache.org</url>
+
+    <parent>
+        <groupId>org.apache.cxf</groupId>
+        <artifactId>cxf-parent</artifactId>
+        <version>2.7.0-SNAPSHOT</version>
+        <relativePath>../../../parent/pom.xml</relativePath>
+    </parent>
+
+	<dependencies>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-frontend-jaxws</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-transports-udp</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.cxf.services.ws-discovery</groupId>
+            <artifactId>cxf-services-ws-discovery-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+    
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-transports-http-jetty</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>

Added: cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/resources/META-INF/cxf/bus-extensions.txt
URL: http://svn.apache.org/viewvc/cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/resources/META-INF/cxf/bus-extensions.txt?rev=1375151&view=auto
==============================================================================
--- cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/resources/META-INF/cxf/bus-extensions.txt (added)
+++ cxf/trunk/services/ws-discovery/ws-discovery-service/src/main/resources/META-INF/cxf/bus-extensions.txt Mon Aug 20 18:05:04 2012
@@ -0,0 +1,2 @@
+org.apache.cxf.ws.discovery.listeners.WSDiscoveryServerListener::true
+org.apache.cxf.ws.discovery.WSDiscoveryService::true