cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r779506 - in /cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src: main/java/org/apache/cxf/dosgi/discovery/zookeeper/ test/java/org/apache/cxf/dosgi/discovery/zookeeper/
Date Thu, 28 May 2009 09:29:39 GMT
Author: davidb
Date: Thu May 28 09:29:39 2009
New Revision: 779506

URL: http://svn.apache.org/viewvc?rev=779506&view=rev
Log:
Support for filters in Discovery

Modified:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java?rev=779506&r1=779505&r2=779506&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizer.java
Thu May 28 09:29:39 2009
@@ -21,8 +21,10 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.zookeeper.WatchedEvent;
@@ -55,8 +57,10 @@
     private void addingService(ServiceReference sr, DiscoveredServiceTracker dst) { 
         removedService(sr, dst);
         
-        Collection<String> interfaces = Util.getMultiValueProperty(
-            sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA));
+        Set<String> interfaces = new HashSet<String>(Util.getMultiValueProperty(
+            sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)));
+        interfaces.addAll(getInterfacesFromFilter(Util.getMultiValueProperty(
+            sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA))));
 
         List<InterfaceMonitor> dmList = new ArrayList<InterfaceMonitor>(interfaces.size());
         for (String intf : interfaces) {
@@ -67,6 +71,35 @@
         watchers.put(dst, Collections.unmodifiableList(dmList));        
     }
     
+    static Collection<String> getInterfacesFromFilter(Collection<String> filters)
{
+        String oc = "objectclass";
+
+        Set<String> interfaces = new HashSet<String>(); 
+        for (String filter : filters) {
+            String lcFilter = filter.toLowerCase();
+            int idx = lcFilter.indexOf(oc);
+            if (idx < 0) {
+                continue;
+            }
+            
+            String s1 = filter.substring(idx + oc.length()).trim();
+            if (!s1.startsWith("=")) {
+                continue;
+            }
+            String s2 = s1.substring(1).trim();
+            
+            int idx2 = 0;
+            while ((s2.charAt(idx2) == '.' || Character.isJavaIdentifierPart(s2.charAt(idx2)))
&& 
+                    idx2 < s2.length()) {
+                idx2++;
+            }
+            
+            interfaces.add(s2.substring(0, idx2));
+        }
+        
+        return interfaces;
+    }
+
     public void modifiedService(ServiceReference sr, Object svcObj) {
         if (svcObj instanceof DiscoveredServiceTracker) {
             addingService(sr, (DiscoveredServiceTracker) svcObj);

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java?rev=779506&r1=779505&r2=779506&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Util.java
Thu May 28 09:29:39 2009
@@ -31,6 +31,8 @@
             return (Collection<String>) property;
         } else if (property instanceof String []) {
             return Arrays.asList((String []) property);
+        } else if (property == null) {
+            return Collections.emptySet();
         } else {
             return Collections.singleton(property.toString());
         }

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java?rev=779506&r1=779505&r2=779506&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/FindInZooKeeperCustomizerTest.java
Thu May 28 09:29:39 2009
@@ -20,7 +20,9 @@
 
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import junit.framework.TestCase;
 
@@ -35,29 +37,27 @@
 import org.osgi.service.discovery.DiscoveredServiceTracker;
 
 public class FindInZooKeeperCustomizerTest extends TestCase {
-    public void testAddingService() {
+    public void testAddingServiceInterface() {
         DiscoveredServiceTracker dst = new DiscoveredServiceTracker() {
-            public void serviceChanged(DiscoveredServiceNotification dsn) {
-                // TODO are we expecting something here?
-                // maybe a separate test?
-            }            
+            public void serviceChanged(DiscoveredServiceNotification dsn) {}
         };
         
         ServiceReference sr = EasyMock.createMock(ServiceReference.class);
         EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)).
             andReturn(Collections.singleton(String.class.getName()));
+        EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)).
+            andReturn(null);
         EasyMock.replay(sr);
         
         DiscoveredServiceTracker dst2 = new DiscoveredServiceTracker() {
-            public void serviceChanged(DiscoveredServiceNotification dsn) {
-                // TODO are we expecting something here?
-                // maybe a separate test?
-            }            
+            public void serviceChanged(DiscoveredServiceNotification dsn) {}
         };
         
         ServiceReference sr2 = EasyMock.createMock(ServiceReference.class);
         EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)).
             andReturn(Arrays.asList(Integer.class.getName(), Comparable.class.getName()));
+        EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)).
+            andReturn(null);
         EasyMock.replay(sr2);
         
         BundleContext bc = EasyMock.createMock(BundleContext.class);
@@ -65,7 +65,7 @@
         EasyMock.expect(bc.getService(sr2)).andReturn(dst2);
         EasyMock.replay(bc);
         
-        ZooKeeper zk = EasyMock.createStrictMock(ZooKeeper.class);
+        ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
         zkExpectExists(zk, String.class.getName());
         zkExpectExists(zk, Integer.class.getName());
         zkExpectExists(zk, Comparable.class.getName());
@@ -100,8 +100,15 @@
         assertEquals(dmList, fc.watchers.get(dst));
         List<InterfaceMonitor> dmList2 = fc.watchers.get(dst2);
         assertEquals(2, dmList2.size());
-        assertEquals(Util.getZooKeeperPath(Integer.class.getName()), dmList2.get(0).znode);
-        assertEquals(Util.getZooKeeperPath(Comparable.class.getName()), dmList2.get(1).znode);
+        
+        Set<String> actual = new HashSet<String>();
+        for (InterfaceMonitor im : dmList2) {
+            actual.add(im.znode);
+        }
+        Set<String> expected = new HashSet<String>(Arrays.asList(
+                Util.getZooKeeperPath(Integer.class.getName()), 
+                Util.getZooKeeperPath(Comparable.class.getName())));
+        assertEquals(expected, actual);
         
         EasyMock.verify(zk);
 
@@ -115,6 +122,129 @@
         EasyMock.reset(sr);
         EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)).
             andReturn(Collections.singleton(List.class.getName()));
+        EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)).
+            andReturn(null);
+        EasyMock.replay(sr);
+        
+        assertEquals("Precondition failed", 2, fc.watchers.size());
+        fc.modifiedService(sr, dst);
+        assertEquals("Precondition failed", 2, fc.watchers.size());
+        
+        assertTrue(fc.watchers.containsKey(dst));
+        assertTrue(fc.watchers.containsKey(dst2));
+        assertEquals(dmList2, fc.watchers.get(dst2));
+        List<InterfaceMonitor> dmList3 = fc.watchers.get(dst);
+        assertEquals(1, dmList3.size());
+        assertEquals(Util.getZooKeeperPath(List.class.getName()), dmList3.iterator().next().znode);
+
+        EasyMock.verify(zk);    
+
+        // ---------------------------------------------------------------
+        // Test the removedService APIs
+        // ---------------------------------------------------------------
+        EasyMock.reset(zk);
+        EasyMock.replay(zk);
+        
+        assertEquals("Precondition failed", 2, fc.watchers.size());
+        fc.removedService(sr2, dst2);
+        assertEquals("Precondition failed", 1, fc.watchers.size());
+        
+        assertEquals(dmList3, fc.watchers.get(dst));
+        assertNull(fc.watchers.get(dst2));
+        
+        EasyMock.verify(zk);       
+    }
+
+    public void testAddingServiceFilter() {
+        DiscoveredServiceTracker dst = new DiscoveredServiceTracker() {
+            public void serviceChanged(DiscoveredServiceNotification dsn) {}
+        };
+        
+        ServiceReference sr = EasyMock.createMock(ServiceReference.class);
+        EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)).
+            andReturn(null);
+        Set<String> stringFilter = Collections.singleton("(objectClass=java.lang.String)");
+        EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)).
+            andReturn(stringFilter);
+        EasyMock.replay(sr);
+        
+        DiscoveredServiceTracker dst2 = new DiscoveredServiceTracker() {
+            public void serviceChanged(DiscoveredServiceNotification dsn) {}
+        };
+        
+        ServiceReference sr2 = EasyMock.createMock(ServiceReference.class);
+        EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)).
+            andReturn(null);
+        List<String> combinedFilter = 
+            Arrays.asList("(objectClass=java.lang.Integer)","(objectClass=java.lang.Comparable)");
+        EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)).
+            andReturn(combinedFilter);
+        EasyMock.replay(sr2);
+        
+        BundleContext bc = EasyMock.createMock(BundleContext.class);
+        EasyMock.expect(bc.getService(sr)).andReturn(dst);
+        EasyMock.expect(bc.getService(sr2)).andReturn(dst2);
+        EasyMock.replay(bc);
+        
+        ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
+        zkExpectExists(zk, String.class.getName());
+        zkExpectExists(zk, Integer.class.getName());
+        zkExpectExists(zk, Comparable.class.getName());
+        EasyMock.expectLastCall();
+        EasyMock.replay(zk);
+        
+        FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(bc, zk);
+
+        // ---------------------------------------------------------------
+        // Test the addingService APIs
+        // ---------------------------------------------------------------
+        
+        assertEquals("Precondition failed", 0, fc.watchers.size());
+        fc.addingService(sr);
+        assertEquals(1, fc.watchers.size());
+        
+        DiscoveredServiceTracker key = fc.watchers.keySet().iterator().next();
+        assertSame(dst, key);
+        List<InterfaceMonitor> dmList = fc.watchers.get(key);
+        assertEquals(1, dmList.size());
+        InterfaceMonitor dm = dmList.iterator().next();
+        assertNotNull(dm.listener);
+        assertSame(zk, dm.zookeeper);
+        assertEquals(Util.getZooKeeperPath(String.class.getName()), dm.znode);        
+
+        assertEquals("Precondition failed", 1, fc.watchers.size());
+        fc.addingService(sr2);
+        assertEquals(2, fc.watchers.size());
+        
+        assertTrue(fc.watchers.containsKey(dst));
+        assertTrue(fc.watchers.containsKey(dst2));
+        assertEquals(dmList, fc.watchers.get(dst));
+        List<InterfaceMonitor> dmList2 = fc.watchers.get(dst2);
+        assertEquals(2, dmList2.size());
+        Set<String> actual = new HashSet<String>();
+        for (InterfaceMonitor im : dmList2) {
+            actual.add(im.znode);
+        }
+        Set<String> expected = new HashSet<String>(Arrays.asList(
+                Util.getZooKeeperPath(Integer.class.getName()), 
+                Util.getZooKeeperPath(Comparable.class.getName())));
+        assertEquals(expected, actual);
+        
+        EasyMock.verify(zk);
+
+        // ---------------------------------------------------------------
+        // Test the modifiedService APIs
+        // ---------------------------------------------------------------
+        EasyMock.reset(zk);
+        zkExpectExists(zk, List.class.getName());
+        EasyMock.replay(zk);
+        
+        EasyMock.reset(sr);
+        Set<String> listFilter = Collections.singleton("(objectClass=java.util.List)");
+        EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)).
+            andReturn(null);
+        EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)).
+            andReturn(listFilter);
         EasyMock.replay(sr);
         
         assertEquals("Precondition failed", 2, fc.watchers.size());
@@ -145,6 +275,27 @@
         
         EasyMock.verify(zk);       
     }
+    
+    public void testGetInterfacesFromFilter() {
+        testGetInterfacesFromFilter("ojectClass=org.apache_2.Some$FunnyClass", 
+                "org.apache_2.Some$FunnyClass");
+        testGetInterfacesFromFilter("(&(a=b)(objectClass = org.acme.Q)", 
+                "org.acme.Q");
+        testGetInterfacesFromFilter("(&(objectClassIdentifier=b)(objectClass = org.acme.Q)",

+                "org.acme.Q");
+        testGetInterfacesFromFilter("(|(OBJECTCLASS=   X  )(objectclass = Y)", 
+                "X", "Y");
+        testGetInterfacesFromFilter(new String [] {"(objectClass=X)", "(objectClass=Y)"},

+                "X", "Y");
+    }
+
+    private void testGetInterfacesFromFilter(String filter, String ... interfaces) {
+        testGetInterfacesFromFilter(new String [] {filter}, interfaces);
+    }
+    
+    private void testGetInterfacesFromFilter(String [] filters, String ... interfaces) {
+        FindInZooKeeperCustomizer.getInterfacesFromFilter(Arrays.asList(filters));
+    }
 
     private void zkExpectExists(ZooKeeper zk, String className) {
         zk.exists(EasyMock.eq(Util.getZooKeeperPath(className)), 



Mime
View raw message