cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r893510 - in /cxf/dosgi/trunk: discovery/local/ discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/ dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/ samples/greeter/client/src/main/resources/OSGI-INF/remote-service/
Date Wed, 23 Dec 2009 13:47:26 GMT
Author: davidb
Date: Wed Dec 23 13:47:24 2009
New Revision: 893510

URL: http://svn.apache.org/viewvc?rev=893510&view=rev
Log:
First case of local discovery working. Updated the Greeter demo with the new standards-based
file format.
This is WIP - more tests are needed and code needs to be enhanced further.
Also added RenoteServiceAdminCoreTest which was forgotten before.

Added:
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RenoteServiceAdminCoreTest.java
  (with props)
Modified:
    cxf/dosgi/trunk/discovery/local/pom.xml
    cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java
    cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
    cxf/dosgi/trunk/samples/greeter/client/src/main/resources/OSGI-INF/remote-service/remote-services.xml

Modified: cxf/dosgi/trunk/discovery/local/pom.xml
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/pom.xml?rev=893510&r1=893509&r2=893510&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/local/pom.xml (original)
+++ cxf/dosgi/trunk/discovery/local/pom.xml Wed Dec 23 13:47:24 2009
@@ -88,7 +88,8 @@
                         <Import-Package>*</Import-Package>
                         <Private-Package>org.apache.cxf.dosgi.discovery.local</Private-Package>
                         <Export-Package>
-                            org.osgi.service.remoteserviceadmin;version="${remote.service.admin.interfaces.version}"
+                            org.osgi.service.remoteserviceadmin;version="${remote.service.admin.interfaces.version}",
+                            org.osgi.service.discovery;version="1.0";-split-package:=merge-first
                         </Export-Package> 
                     </instructions>
                 </configuration>

Modified: cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java?rev=893510&r1=893509&r2=893510&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java
(original)
+++ cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java
Wed Dec 23 13:47:24 2009
@@ -18,28 +18,109 @@
   */
 package org.apache.cxf.dosgi.discovery.local;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Logger;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleListener;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.remoteserviceadmin.EndpointDescription;
+import org.osgi.service.remoteserviceadmin.EndpointListener;
+import org.osgi.util.tracker.ServiceTracker;
 
 public class LocalDiscovery implements BundleListener {   
     private static final Logger LOG = Logger.getLogger(LocalDiscovery.class.getName());
     
+    // this is effectively a set which allows for multiple service descriptions with the
+    // same interface name but different properties and takes care of itself with respect
to concurrency 
+    ConcurrentHashMap<EndpointDescription, Bundle> endpointDescriptions = 
+        new ConcurrentHashMap<EndpointDescription, Bundle>();
+    Map<EndpointListener, Collection<String>> listenerToFilters = 
+        new HashMap<EndpointListener, Collection<String>>();
+    Map<String, Collection<EndpointListener>> filterToListeners = 
+        new HashMap<String, Collection<EndpointListener>>();
     private final BundleContext bundleContext;
 
+    private ServiceTracker listenerTracker;
+
     public LocalDiscovery(BundleContext bc) {
         bundleContext = bc;
         
+        listenerTracker = new ServiceTracker(bundleContext, EndpointListener.class.getName(),
null) {
+
+            @Override
+            public Object addingService(ServiceReference reference) {
+                System.out.println("@@@@ " + reference + "-" + Arrays.asList(reference.getPropertyKeys()));
+                Object svc = super.addingService(reference);
+                cacheTracker(reference, svc);
+                return svc;
+            }
+
+            @Override
+            public void modifiedService(ServiceReference reference,
+                    Object service) {
+                System.out.println("#### " + reference + "-" + Arrays.asList(reference.getPropertyKeys()));
+
+                cacheTracker(reference, service);
+            }
+
+            @Override
+            public void removedService(ServiceReference reference,
+                    Object service) {
+                // TODO Auto-generated method stub
+                super.removedService(reference, service);
+            }
+            
+        };
+        listenerTracker.open();
+        
         bundleContext.addBundleListener(this);
     }
 
+    protected void cacheTracker(ServiceReference reference, Object svc) {
+        if (svc instanceof EndpointListener) {
+            EndpointListener listener = (EndpointListener) svc;
+            Collection<String> filters = addListener(reference, listener);
+            triggerCallbacks(filters, listener);
+        }
+    }
+
+    private Collection<String> addListener(ServiceReference reference,
+            EndpointListener listener) {
+        List<String> filters = 
+            LocalDiscoveryUtils.getStringPlusProperty(reference, EndpointListener.ENDPOINT_LISTENER_SCOPE);
+        if (filters.size() == 0) {
+            return filters;
+        }
+        
+        listenerToFilters.put(listener, filters);
+        for (String filter : filters) {
+            if (filterToListeners.containsKey(filter)) {
+                filterToListeners.get(filter).add(listener);
+            } else {
+                List<EndpointListener> list = new ArrayList<EndpointListener>();
+                list.add(listener);
+                filterToListeners.put(filter, list);
+            }
+        }
+        
+        return filters;
+    }
+
     public void shutDown() {
         bundleContext.removeBundleListener(this);
+        listenerTracker.close();
     }
 
     // BundleListener method
@@ -54,6 +135,73 @@
     }
 
     private void findDeclaredRemoteServices(Bundle bundle) {
-//        List<EndpointDescription> refs = LocalDiscoveryUtils.getAllRemoteReferences(bundle);
+        List<EndpointDescription> eds = LocalDiscoveryUtils.getAllEndpointDescriptions(bundle);
+        for (EndpointDescription ed : eds) {
+            endpointDescriptions.put(ed, bundle);
+            addedEndpointDescription(ed);
+        }
+    }
+
+    private void addedEndpointDescription(EndpointDescription ed) {
+        triggerCallbacks(ed, true);
+    }
+
+    private void triggerCallbacks(EndpointDescription ed, boolean added) {
+        for (Map.Entry<EndpointListener, Collection<String>> entry : listenerToFilters.entrySet())
{
+            for (String match : entry.getValue()) {
+                triggerCallbacks(entry.getKey(), match, ed, added);
+            }
+        }
     }
+
+    private void triggerCallbacks(EndpointListener listener, String toMatch,
+            EndpointDescription ed, boolean added) {
+        if (!filterMatches(toMatch, ed)) {
+            return;
+        }
+        
+        if (added) {
+            listener.endpointAdded(ed, toMatch);
+        } else {
+            listener.endpointRemoved(ed, toMatch);
+        }
+    }
+    
+    private void triggerCallbacks(Collection<String> filters, EndpointListener listener)
{
+        if (endpointDescriptions.size() > 0) {
+            LOG.info("search for matches to trigger callbacks with delta: " + filters);
+        } else {
+            LOG.info("nothing to search for matches to trigger callbacks with delta: " +
filters);
+        }
+        
+        for (String filter : filters) {
+            for (EndpointDescription ed : endpointDescriptions.keySet()) {
+                triggerCallbacks(listener, filter, ed, true);
+            }
+        }
+    }    
+
+    private boolean filterMatches(String match, EndpointDescription ed) {
+        Filter filter = createFilter(match);
+        
+        Dictionary<String, Object> props = 
+            new Hashtable<String, Object>(ed.getProperties());
+        
+        return filter != null
+            ? filter.match(props)
+            : false;
+    } 
+    
+    private Filter createFilter(String filterValue) {        
+        if (filterValue == null) {
+            return null;
+        }
+        
+        try {
+            return bundleContext.createFilter(filterValue); 
+        } catch (Exception ex) {
+            LOG.severe("Problem creating a Filter from " + filterValue); 
+        }
+        return null;
+    }    
 }

Modified: cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java?rev=893510&r1=893509&r2=893510&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
(original)
+++ cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
Wed Dec 23 13:47:24 2009
@@ -28,6 +28,7 @@
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
@@ -41,6 +42,7 @@
 import org.jdom.output.Format;
 import org.jdom.output.XMLOutputter;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.discovery.ServiceEndpointDescription;
 import org.osgi.service.discovery.ServicePublication;
 import org.osgi.service.remoteserviceadmin.EndpointDescription;
@@ -394,4 +396,40 @@
         
         return names;
     }
+    
+    public static List<String> getStringPlusProperty(ServiceReference sr, String key)
{
+        Object value = sr.getProperty(key);
+        if (value == null) {
+            return Collections.emptyList();
+        }
+
+        if (value instanceof String) {
+            return Collections.singletonList((String) value);
+        }
+
+        if (value instanceof String[]) {
+            String[] values = (String[]) value;
+            List<String> result = new ArrayList<String>(values.length);
+            for (String v : values) {
+                if (v != null) {
+                    result.add(v);
+                }
+            }
+            return Collections.unmodifiableList(result);
+        }
+
+        if (value instanceof Collection<?>) {
+            Collection<?> values = (Collection<?>) value;
+            List<String> result = new ArrayList<String>(values.size());
+            for (Iterator<?> iter = values.iterator(); iter.hasNext();) {
+                Object v = iter.next();
+                if ((v != null) && (v instanceof String)) {
+                    result.add((String) v);
+                }
+            }
+            return Collections.unmodifiableList(result);
+        }
+
+        return Collections.emptyList();
+    }    
 }

Added: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RenoteServiceAdminCoreTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RenoteServiceAdminCoreTest.java?rev=893510&view=auto
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RenoteServiceAdminCoreTest.java
(added)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RenoteServiceAdminCoreTest.java
Wed Dec 23 13:47:24 2009
@@ -0,0 +1,149 @@
+/** 
+  * 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.dosgi.dsw.service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.cxf.dosgi.dsw.handlers.ConfigurationTypeHandler;
+import org.apache.cxf.jaxws.spring.EndpointDefinitionParser;
+import org.easymock.IMocksControl;
+import org.easymock.classextension.EasyMock;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.remoteserviceadmin.EndpointDescription;
+import org.osgi.service.remoteserviceadmin.ImportRegistration;
+import org.osgi.service.remoteserviceadmin.RemoteConstants;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class RenoteServiceAdminCoreTest {
+
+    @Test
+    public void dontExportOwnServiceProxies(){
+        
+        IMocksControl c = EasyMock.createNiceControl();
+        Bundle b = c.createMock(Bundle.class);
+        BundleContext bc = c.createMock(BundleContext.class);
+        
+        EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
+        
+        ServiceReference sref = c.createMock(ServiceReference.class);
+        EasyMock.expect(sref.getBundle()).andReturn(b).anyTimes();
+        
+        RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc);
+        
+        
+        c.replay();
+        
+        
+        // must return null as sref if from the same bundle 
+        assertNull(rsaCore.exportService(sref, null));
+        
+        // must be empty ... 
+        assertEquals(rsaCore.getExportedServices().size(),0);
+        
+        c.verify();
+        
+    }
+    
+    
+    @Test
+    public void testImport(){
+        
+        IMocksControl c = EasyMock.createNiceControl();
+        Bundle b = c.createMock(Bundle.class);
+        BundleContext bc = c.createMock(BundleContext.class);
+        
+        EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
+        EasyMock.expect(b.getSymbolicName()).andReturn("BundleName").anyTimes();
+        
+        RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc){
+            @Override
+            protected void proxifyMatchingInterface(String interfaceName, ImportRegistrationImpl
imReg,
+                                                    ConfigurationTypeHandler handler,
+                                                    BundleContext requestingContext) {
+
+                
+                
+            }
+        };
+        
+        Map p = new HashMap();
+        p.put(RemoteConstants.ENDPOINT_URI, "http://google.de");
+        
+        EndpointDescription endpoint = new EndpointDescription(p);
+        
+        
+        c.replay();
+        
+        // must be null as the endpoint doesn't contain any usable configurations
+        assertNull(rsaCore.importService(endpoint));
+        // must be empty ... 
+        assertEquals(rsaCore.getImportedEndpoints().size(),0);
+        
+        
+        p.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "org.apache.cxf.ws");
+        endpoint = new EndpointDescription(p);
+        
+        // must be null as the endpoint has no interface
+        assertNull(rsaCore.importService(endpoint));
+        // must be empty ... 
+        assertEquals(rsaCore.getImportedEndpoints().size(),0);
+        
+        
+        p.put(Constants.OBJECTCLASS, new String[] {"es.schaaf.my.class"});
+        endpoint = new EndpointDescription(p);
+        
+        
+        ImportRegistration ireg = rsaCore.importService(endpoint);
+        assertNotNull(ireg);
+        
+        assertEquals(rsaCore.getImportedEndpoints().size(),1);
+        
+        
+        // lets import the same endpoint once more -> should get a copy of the ImportRegistration
+        ImportRegistration ireg2 = rsaCore.importService(endpoint);
+        assertNotNull(ireg2);
+        assertEquals(rsaCore.getImportedEndpoints().size(),1);
+        
+        assertEquals(ireg,(rsaCore.getImportedEndpoints().toArray())[0]);
+
+        assertEquals(ireg.getImportReference().getImportedEndpoint(),ireg2.getImportReference().getImportedEndpoint());
+        
+        
+        // remove the registration ....
+        
+        // first call shouldn't remove the import ... 
+        ireg2.close();
+        assertEquals(1,rsaCore.getImportedEndpoints().size());
+        
+        // second call should really close and remove the import ...
+        ireg.close();
+        assertEquals(0,rsaCore.getImportedEndpoints().size());
+        
+        
+        c.verify();
+        
+        
+    }
+}

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RenoteServiceAdminCoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RenoteServiceAdminCoreTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/dosgi/trunk/samples/greeter/client/src/main/resources/OSGI-INF/remote-service/remote-services.xml
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/samples/greeter/client/src/main/resources/OSGI-INF/remote-service/remote-services.xml?rev=893510&r1=893509&r2=893510&view=diff
==============================================================================
--- cxf/dosgi/trunk/samples/greeter/client/src/main/resources/OSGI-INF/remote-service/remote-services.xml
(original)
+++ cxf/dosgi/trunk/samples/greeter/client/src/main/resources/OSGI-INF/remote-service/remote-services.xml
Wed Dec 23 13:47:24 2009
@@ -13,11 +13,16 @@
     License for the specific language governing permissions and
     limitations under the License.
   -->
-<service-descriptions xmlns="http://www.osgi.org/xmlns/sd/v1.0.0">
-  <service-description>
-    <provide interface="org.apache.cxf.dosgi.samples.greeter.GreeterService" />
-    <property name="service.exported.interfaces">*</property>
-    <property name="service.exported.configs">org.apache.cxf.ws</property>
-    <property name="org.apache.cxf.ws.address">http://localhost:9090/greeter</property>
-  </service-description>
-</service-descriptions>
+<rsa:endpoint-descriptions xmlns:rsa="http://www.osgi.org/xmlns/rsa/v1.0.0"
+  xmlns:other="http://www.acme.org/xmlns/other/v1.0.0">
+  <endpoint-description>
+    <property name="objectClass">
+      <array>
+        <value>org.apache.cxf.dosgi.samples.greeter.GreeterService</value>
+      </array>
+    </property>
+    <property name="endpoint.uri">http://localhost:9090/greeter</property>
+    <property name="service.imported.configs">org.apache.cxf.ws</property>
+  </endpoint-description>
+</rsa:endpoint-descriptions>
+



Mime
View raw message