You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by da...@apache.org on 2009/04/24 12:30:19 UTC

svn commit: r768252 - 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/

Author: davidb
Date: Fri Apr 24 10:30:19 2009
New Revision: 768252

URL: http://svn.apache.org/viewvc?rev=768252&view=rev
Log:
[Discovery] Support for DiscoveredServiceNotification.UNAVAILABLE

Removed:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ChildMonitor.java
Modified:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java?rev=768252&r1=768251&r2=768252&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceDataMonitorListenerImpl.java Fri Apr 24 10:30:19 2009
@@ -39,6 +39,9 @@
     final String znode;
     final String interFace;
     final DiscoveredServiceTracker discoveredServiceTracker;
+
+    // This map is *only* accessed in the change() method
+    private Map<String, Map<String, Object>> nodes = new HashMap<String, Map<String,Object>>();
     
     public InterfaceDataMonitorListenerImpl(ZooKeeper zk, String intf, DiscoveredServiceTracker dst) {
         zookeeper = zk;
@@ -47,21 +50,19 @@
         discoveredServiceTracker = dst;
     }
     
-    public void change() {
+    public synchronized void change() {
+        Map<String, Map<String, Object>> newNodes = new HashMap<String, Map<String,Object>>();
+        Map<String, Map<String, Object>> prevNodes = nodes;
         try {
             LOG.info("Zookeeper callback on node: " + znode);
             List<String> children = zookeeper.getChildren(znode, false);
             
             for (String child : children) {
-//                ChildMonitor cl = new ChildMonitor();
-//                zookeeper.exists(znode + '/' + child, cl, cl, null);
-                
-                // move to child watcher?
                 byte[] data = zookeeper.getData(znode + '/' + child, false, null);
                 Properties p = new Properties();
                 p.load(new ByteArrayInputStream(data));
                 
-                Map<String, Object> p2 = new HashMap<String, Object>();
+                Map<String, Object> m = new HashMap<String, Object>();
                 for (Map.Entry<Object, Object> entry : p.entrySet()) {
                     /* TODO this is probably not necessary
                     if (Constants.SERVICE_ID.equals(entry.getKey()) ||
@@ -70,21 +71,34 @@
                         continue;
                     } */
                     
-                    p2.put(entry.getKey().toString(), entry.getValue());
+                    m.put(entry.getKey().toString(), entry.getValue());
+                }
+                
+                newNodes.put(child, m);
+                Map<String, Object> prevVal = prevNodes.remove(child);
+                if (prevVal == null) {
+                    // This guy is new
+                    ServiceEndpointDescriptionImpl sed = new ServiceEndpointDescriptionImpl(Collections.singletonList(interFace), m);
+                    DiscoveredServiceNotification dsn = new DiscoveredServiceNotificationImpl(Collections.emptyList(),
+                        Collections.singleton(interFace), DiscoveredServiceNotification.AVAILABLE, sed);
+                    discoveredServiceTracker.serviceChanged(dsn);                    
+                } else {
+                    // There's been a modification
                 }
                 
-                ServiceEndpointDescriptionImpl sed = new ServiceEndpointDescriptionImpl(Collections.singletonList(interFace), p2);
+            }
+
+            for (Map<String, Object> props : prevNodes.values()) {
+                // whatever's left in prevNodes now has been removed from Discovery
+                ServiceEndpointDescriptionImpl sed = new ServiceEndpointDescriptionImpl(Collections.singletonList(interFace), props);
                 DiscoveredServiceNotification dsn = new DiscoveredServiceNotificationImpl(Collections.emptyList(),
-                    Collections.singleton(interFace), DiscoveredServiceNotification.AVAILABLE, sed);
-                discoveredServiceTracker.serviceChanged(dsn);
+                        Collections.singleton(interFace), DiscoveredServiceNotification.UNAVAILABLE, sed);
+                discoveredServiceTracker.serviceChanged(dsn);                
             }
         } catch (Exception e) {
             LOG.log(Level.SEVERE, "Problem processing Zookeeper callback", e);
-        }                    
-    }
-
-    public void closing(int rc) {
-        System.out.println("*** closing " + rc);
-        // TODO do we need this callback?
+        } finally {
+            nodes = newNodes;
+        }
     }
 }

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java?rev=768252&r1=768251&r2=768252&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/InterfaceMonitorTest.java Fri Apr 24 10:30:19 2009
@@ -111,7 +111,7 @@
         dm.processResult(Code.Ok, null, null, null);
         assertEquals("No changes, so should not get any new notifications", 0, notifications.size());
         
-        // Third time around, with different data
+        // Third time around, removal
         EasyMock.reset(zk);
         zk.exists(Util.getZooKeeperPath(String.class.getName()), false);
         EasyMock.expectLastCall().andReturn(EasyMock.createMock(Stat.class));
@@ -124,12 +124,12 @@
         dm.processResult(Code.Ok, null, null, null);
         DiscoveredServiceNotification dsn = notifications.iterator().next();
         assertEquals(1, notifications.size());
-        assertEquals(DiscoveredServiceNotification.AVAILABLE, dsn.getType());
+        assertEquals(DiscoveredServiceNotification.UNAVAILABLE, dsn.getType());
         assertEquals(Collections.emptyList(), dsn.getFilters());
         assertEquals(Collections.singleton(String.class.getName()), dsn.getInterfaces());
         ServiceEndpointDescription sed = dsn.getServiceEndpointDescription();
         assertEquals(Collections.singleton(String.class.getName()), sed.getProvidedInterfaces());
-        assertEquals(s1Props, sed.getProperties());        
+        assertEquals(s2Props, sed.getProperties());        
     }
     
     public void testInterfaceMonitorNoExist() throws Exception {