cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r767409 - 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 Wed, 22 Apr 2009 08:36:30 GMT
Author: davidb
Date: Wed Apr 22 08:36:30 2009
New Revision: 767409

URL: http://svn.apache.org/viewvc?rev=767409&view=rev
Log:
[Discovery] Added support for modifications in the Discovered Service Tracker (modifiedService
and removedService callbacks).
New unit tests included.

Modified:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
    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/PublishToZooKeeperCustomizer.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.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/DataMonitor.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java?rev=767409&r1=767408&r2=767409&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitor.java
Wed Apr 22 08:36:30 2009
@@ -40,7 +40,10 @@
         listener = new DataMonitorListenerImpl(zk, intf, dst);
         zookeeper = zk;
         znode = Util.getZooKeeperPath(intf);
-        
+    }
+    
+    public void process() {
+        /* */ System.out.println("*** Kicking off a zookeeper.exists()");
         zookeeper.exists(znode, true, this, null);
     }
 
@@ -61,7 +64,7 @@
             LOG.info("ZooKeeper reports: NoAuth on node: " + znode);
             return;
         default:
-            zookeeper.exists(znode, true, this, null);
+            process();
             return;
         }
         

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java?rev=767409&r1=767408&r2=767409&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java
Wed Apr 22 08:36:30 2009
@@ -35,6 +35,7 @@
     private BundleContext bundleContext;
     private DiscoveryServiceImpl discoveryService;
 
+    FindInZooKeeperCustomizer finderCustomizer;
     ServiceTracker lookupTracker;
     ServiceTracker publicationTracker;
     ZooKeeper zooKeeper;
@@ -56,8 +57,9 @@
                 new PublishToZooKeeperCustomizer(bundleContext, zooKeeper));
         publicationTracker.open();
         
+        finderCustomizer = new FindInZooKeeperCustomizer(bundleContext, zooKeeper);
         lookupTracker = new ServiceTracker(bundleContext, DiscoveredServiceTracker.class.getName(),
-                new FindInZooKeeperCustomizer(bundleContext, zooKeeper));
+                finderCustomizer);
         lookupTracker.open();        
     }
 
@@ -72,8 +74,6 @@
     }
 
     public void process(WatchedEvent event) {
-        System.out.println("*** [Spring] process (dropped): " + event);
-        // TODO do we need this? The zookeeper examples do this, but I'm unsure why...
-        // finderCustomizer.process(event);
+        finderCustomizer.processGlobalEvent(event);
     }
 }

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=767409&r1=767408&r2=767409&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
Wed Apr 22 08:36:30 2009
@@ -18,10 +18,14 @@
   */
 package org.apache.cxf.dosgi.discovery.zookeeper;
 
+import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
+import java.util.Collections;
+import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.ZooKeeper;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -31,7 +35,8 @@
 public class FindInZooKeeperCustomizer implements ServiceTrackerCustomizer {
     private final BundleContext bundleContext;
     private final ZooKeeper zookeeper;
-    final Map<String, DataMonitor> watchers = new HashMap<String, DataMonitor>();
+    final Map<DiscoveredServiceTracker, List<DataMonitor>> watchers = 
+        new ConcurrentHashMap<DiscoveredServiceTracker, List<DataMonitor>>();
     
     public FindInZooKeeperCustomizer(BundleContext bc, ZooKeeper zk) {
         bundleContext = bc;
@@ -42,26 +47,44 @@
         Object svcObj = bundleContext.getService(sr);
 
         if (svcObj instanceof DiscoveredServiceTracker) {
-            DiscoveredServiceTracker dst = (DiscoveredServiceTracker) svcObj;
-            Collection<String> interfaces = Util.getMultiValueProperty(
-                sr.getProperty(DiscoveredServiceTracker.PROP_KEY_MATCH_CRITERIA_INTERFACES));
-
-            for (String intf : interfaces) {
-                DataMonitor dm = new DataMonitor(zookeeper, intf, dst);
-                watchers.put(intf, dm);
-            }
+            addingService(sr, (DiscoveredServiceTracker) svcObj);
         }
         return svcObj;
     }
 
-    public void modifiedService(ServiceReference arg0, Object arg1) {
-        // TODO Auto-generated method stub
-
+    private void addingService(ServiceReference sr, DiscoveredServiceTracker dst) { 
+        removedService(sr, dst);
+        
+        Collection<String> interfaces = Util.getMultiValueProperty(
+            sr.getProperty(DiscoveredServiceTracker.PROP_KEY_MATCH_CRITERIA_INTERFACES));
+
+        List<DataMonitor> dmList = new ArrayList<DataMonitor>(interfaces.size());
+        for (String intf : interfaces) {
+            DataMonitor dm = new DataMonitor(zookeeper, intf, dst);
+            dmList.add(dm);
+            dm.process();
+        }
+        watchers.put(dst, Collections.unmodifiableList(dmList));        
+    }
+    
+    public void modifiedService(ServiceReference sr, Object svcObj) {
+        if (svcObj instanceof DiscoveredServiceTracker) {
+            addingService(sr, (DiscoveredServiceTracker) svcObj);
+        }
     }
 
-    public void removedService(ServiceReference arg0, Object arg1) {
-        // TODO Auto-generated method stub
-
+    public void removedService(ServiceReference sr, Object svcObj) {
+        List<DataMonitor> oldVal = watchers.remove(svcObj);
+        if (oldVal != null) {
+            // TODO unregister any listeners directly registered with ZooKeeper
+        }
     }
 
+    public void processGlobalEvent(WatchedEvent event) {
+        for (List<DataMonitor> dmList : watchers.values()) {
+            for (DataMonitor dm : dmList) {
+                dm.process();
+            }
+        }
+    }
 }

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java?rev=767409&r1=767408&r2=767409&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
Wed Apr 22 08:36:30 2009
@@ -62,7 +62,7 @@
                 LOG.info("Creating ZooKeeper node: " + fullPath);
 
                 ensurePath(path);
-                Object x = zookeeper.create(fullPath, getData(sr),
+                zookeeper.create(fullPath, getData(sr),
                         Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
             }
             return obj;
@@ -78,7 +78,14 @@
     }
 
     public void removedService(ServiceReference sr, Object obj) {
-        // TODO Auto-generated method stub
+//        Collection<String> interfaces = Util.getMultiValueProperty(sr.getProperty("service.interface"));
+//        String endpoint = sr.getProperty("osgi.remote.endpoint.location").toString();
+//        String endpointKey = getKey(endpoint);
+//        
+//        for (String name : interfaces) {
+//            
+//        }
+//        zookeeper.delete(fullPath, -1);
 
     }
 

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java?rev=767409&r1=767408&r2=767409&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DataMonitorTest.java
Wed Apr 22 08:36:30 2009
@@ -154,12 +154,6 @@
     
     public void testDataMonitorNoExist() throws Exception {
         ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
-        zk.exists(
-            EasyMock.eq(Util.getZooKeeperPath(String.class.getName())),
-            EasyMock.eq(true),
-            (StatCallback) EasyMock.anyObject(), 
-            EasyMock.isNull());
-        EasyMock.expectLastCall();
         EasyMock.replay(zk);
 
         DataMonitor dm = new DataMonitor(zk, String.class.getName(), null);        
@@ -168,7 +162,7 @@
         EasyMock.verify(zk);
     }
     
-    public void testDataMonitorDefault() {
+    public void testProcess() {
         ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
         zk.exists(
             EasyMock.eq(Util.getZooKeeperPath(String.class.getName())),
@@ -178,6 +172,16 @@
         EasyMock.expectLastCall();
         EasyMock.replay(zk);
 
+        DataMonitor dm = new DataMonitor(zk, String.class.getName(), null);        
+        dm.process();
+
+        EasyMock.verify(zk);        
+    }
+
+    public void testDataMonitorDefault() {
+        ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
+        EasyMock.replay(zk);
+
         DataMonitor dm = new DataMonitor(zk, String.class.getName(), null);
         EasyMock.verify(zk);        
         

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java?rev=767409&r1=767408&r2=767409&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
(original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
Wed Apr 22 08:36:30 2009
@@ -19,6 +19,9 @@
 package org.apache.cxf.dosgi.discovery.zookeeper;
 
 import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.List;
 
 import junit.framework.TestCase;
 
@@ -98,4 +101,38 @@
         EasyMock.expect(bc.getServiceReferences(objectClass, null))
             .andReturn(new ServiceReference [0]).anyTimes();
     }
+    
+    public void testProcessEvent() {
+        DiscoveryBean db = new DiscoveryBean();
+        
+        FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(null, null);
+        List<DataMonitor> l1 = new ArrayList<DataMonitor>();
+        DataMonitor dm1a = EasyMock.createMock(DataMonitor.class);
+        dm1a.process();
+        EasyMock.expectLastCall();
+        EasyMock.replay(dm1a);
+        DataMonitor dm1b = EasyMock.createMock(DataMonitor.class);
+        dm1b.process();
+        EasyMock.expectLastCall();
+        EasyMock.replay(dm1b);
+        l1.add(dm1a);
+        l1.add(dm1b);
+        
+        List<DataMonitor> l2 = new ArrayList<DataMonitor>();
+        DataMonitor dm2 = EasyMock.createMock(DataMonitor.class);
+        dm2.process();
+        EasyMock.expectLastCall();
+        EasyMock.replay(dm2);
+        l2.add(dm2);
+
+        fc.watchers.put(EasyMock.createMock(DiscoveredServiceTracker.class), l1);
+        fc.watchers.put(EasyMock.createMock(DiscoveredServiceTracker.class), l2);
+        
+        db.finderCustomizer = fc;
+        db.process(null);
+        
+        EasyMock.verify(dm1a);
+        EasyMock.verify(dm1b);
+        EasyMock.verify(dm2);
+    }
 }

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=767409&r1=767408&r2=767409&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
Wed Apr 22 08:36:30 2009
@@ -18,11 +18,14 @@
  */
 package org.apache.cxf.dosgi.discovery.zookeeper;
 
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.List;
 
 import junit.framework.TestCase;
 
 import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.AsyncCallback.StatCallback;
 import org.easymock.classextension.EasyMock;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -33,6 +36,8 @@
     public void testAddingService() {
         DiscoveredServiceTracker dst = new DiscoveredServiceTracker() {
             public void serviceChanged(DiscoveredServiceNotification dsn) {
+                // TODO are we expecting something here?
+                // maybe a separate test?
             }            
         };
         
@@ -41,25 +46,106 @@
             andReturn(Collections.singleton(String.class.getName()));
         EasyMock.replay(sr);
         
+        DiscoveredServiceTracker dst2 = new DiscoveredServiceTracker() {
+            public void serviceChanged(DiscoveredServiceNotification dsn) {
+                // TODO are we expecting something here?
+                // maybe a separate test?
+            }            
+        };
+        
+        ServiceReference sr2 = EasyMock.createMock(ServiceReference.class);
+        EasyMock.expect(sr2.getProperty(DiscoveredServiceTracker.PROP_KEY_MATCH_CRITERIA_INTERFACES)).
+            andReturn(Arrays.asList(Integer.class.getName(), Comparable.class.getName()));
+        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.createNiceMock(ZooKeeper.class);
+        ZooKeeper zk = EasyMock.createStrictMock(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());
         
-        String key = fc.watchers.keySet().iterator().next();
-        assertEquals(String.class.getName(), key);
-        DataMonitor dm = fc.watchers.get(key);
+        DiscoveredServiceTracker key = fc.watchers.keySet().iterator().next();
+        assertSame(dst, key);
+        List<DataMonitor> dmList = fc.watchers.get(key);
+        assertEquals(1, dmList.size());
+        DataMonitor dm = dmList.iterator().next();
         assertNotNull(dm.listener);
         assertSame(zk, dm.zookeeper);
-        assertEquals(Util.getZooKeeperPath(String.class.getName()), dm.znode);
+        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<DataMonitor> 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);
         
+        EasyMock.verify(zk);
+
+        // ---------------------------------------------------------------
+        // Test the modifiedService APIs
+        // ---------------------------------------------------------------
+        EasyMock.reset(zk);
+        zkExpectExists(zk, List.class.getName());
+        EasyMock.replay(zk);
+        
+        EasyMock.reset(sr);
+        EasyMock.expect(sr.getProperty(DiscoveredServiceTracker.PROP_KEY_MATCH_CRITERIA_INTERFACES)).
+            andReturn(Collections.singleton(List.class.getName()));
+        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<DataMonitor> 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);       
+    }
+
+    private void zkExpectExists(ZooKeeper zk, String className) {
+        zk.exists(EasyMock.eq(Util.getZooKeeperPath(className)), EasyMock.eq(true), 
+                (StatCallback) EasyMock.anyObject(), EasyMock.isNull());
     }
 }



Mime
View raw message