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/06/05 16:07:06 UTC

svn commit: r782023 - in /cxf/dosgi/trunk/discovery/distributed/cxf-discovery: ./ src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/ src/main/resources/ src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/

Author: davidb
Date: Fri Jun  5 14:07:05 2009
New Revision: 782023

URL: http://svn.apache.org/viewvc?rev=782023&view=rev
Log:
Refactored the discovery implementation to not use Spring-DM any more as this was causing issues with the system test harness.

Added:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Activator.java   (with props)
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriver.java   (contents, props changed)
      - copied, changed from r781728, 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/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java   (contents, props changed)
      - copied, changed from r781728, cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
Removed:
    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/resources/
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java
Modified:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/pom.xml

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/pom.xml
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/pom.xml?rev=782023&r1=782022&r2=782023&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/pom.xml (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/pom.xml Fri Jun  5 14:07:05 2009
@@ -97,6 +97,8 @@
                         <Bundle-Name>CXF Zookeeper-based Discovery Service Bundle</Bundle-Name>
                         <Bundle-Description>This bundle contains an implementation of the Distributed OSGi Discovery Service using Zookeeper</Bundle-Description>
                         <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+                        <Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
+                        <Bundle-Activator>org.apache.cxf.dosgi.discovery.zookeeper.Activator</Bundle-Activator>
                         <Import-Package>
                             org.apache.zookeeper.*;version="[3.0.0,4.0.0)",
                             org.apache.cxf.dosgi.discovery.local;version=${pom.version},

Added: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Activator.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Activator.java?rev=782023&view=auto
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Activator.java (added)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Activator.java Fri Jun  5 14:07:05 2009
@@ -0,0 +1,64 @@
+package org.apache.cxf.dosgi.discovery.zookeeper;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+
+public class Activator implements BundleActivator, ManagedService {
+    private static final Logger LOG = Logger.getLogger(Activator.class.getName());
+    
+    private BundleContext bundleContext;
+    private DiscoveryDriver driver;
+    private ServiceRegistration cmReg;
+
+    public void start(BundleContext bc) throws Exception {
+        bundleContext = bc;        
+        cmReg = bc.registerService(ManagedService.class.getName(), this, getCMDefaults());
+    }
+
+    private Dictionary getCMDefaults() {
+        Dictionary props = new Hashtable();
+        props.put("timeout", "3000");
+        props.put("port", "2181");
+        props.put(Constants.SERVICE_PID, "org.apache.cxf.dosgi.discovery.zookeeper");
+        return props;    
+    }
+
+    public synchronized void stop(BundleContext bc) throws Exception {
+        cmReg.unregister();
+        
+        if (driver != null) {
+            driver.destroy();
+        }
+    }
+
+    public void updated(Dictionary configuration) throws ConfigurationException {
+        if (configuration == null) {
+            return;
+        }
+        
+        synchronized (this) {
+            try {
+                if (driver == null) {
+                    driver = new DiscoveryDriver(bundleContext, configuration);
+                } else {
+                    driver.updateConfiguration(configuration);
+                }
+            } catch (IOException e) {
+                LOG.log(Level.WARNING, "Could now create the ZooKeeper client", e);
+            }
+        }
+        cmReg.setProperties(configuration);
+    }
+
+
+}

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Activator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/Activator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriver.java (from r781728, 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/DiscoveryDriver.java?p2=cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriver.java&p1=cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBean.java&r1=781728&r2=782023&rev=782023&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/DiscoveryDriver.java Fri Jun  5 14:07:05 2009
@@ -19,40 +19,70 @@
 package org.apache.cxf.dosgi.discovery.zookeeper;
 
 import java.io.IOException;
+import java.util.Dictionary;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
 import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.discovery.DiscoveredServiceTracker;
 import org.osgi.service.discovery.ServicePublication;
 import org.osgi.util.tracker.ServiceTracker;
-import org.springframework.beans.factory.DisposableBean;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.osgi.context.BundleContextAware;
-
-public class DiscoveryBean implements BundleContextAware, InitializingBean, DisposableBean, Watcher {
-    private BundleContext bundleContext;
-    private DiscoveryServiceImpl discoveryService;
+
+public class DiscoveryDriver implements Watcher {
+    private static final Logger LOG = Logger.getLogger(DiscoveryDriver.class.getName());
+
+    private final BundleContext bundleContext;
 
     FindInZooKeeperCustomizer finderCustomizer;
     ServiceTracker lookupTracker;
     ServiceTracker publicationTracker;
     ZooKeeper zooKeeper;
-        
-    public void setBundleContext(BundleContext bc) {
-        bundleContext = bc;
-    }
 
-    public void setDiscoveryServiceBean(DiscoveryServiceImpl discovery) {
-        discoveryService = discovery;
+    String zkHost;
+    String zkPort;
+    int zkTimeout;
+        
+//    public static DiscoveryDriver getDriver(BundleContext bc, Dictionary props) {
+//        LOG.info("Obtaining ZooKeeper Discovery driver.");
+//        
+//        DiscoveryDriver dd = new DiscoveryDriver(bc);
+//        try {
+//            dd.createZooKeeper(props);
+//            dd.init();
+//            return dd;
+//        } catch (ConfigurationException e) {
+//            LOG.log(Level.INFO, "Insufficient configuration information to create ZooKeeper client.", e);
+//            return null;
+//        } catch (Exception e) {
+//            LOG.log(Level.WARNING, "Problem creating ZooKeeper based discovery driver.", e);
+//            return null;
+//        }
+//    }    
+
+    DiscoveryDriver(BundleContext bc, Dictionary props) throws IOException, ConfigurationException {
+        bundleContext = bc;        
+        createZooKeeper(props);
+        init();
+    }
+    
+    private void createZooKeeper(Dictionary props) throws IOException, ConfigurationException {
+        zkHost = getProp(props, "zookeeper.host");
+        zkPort = getProp(props, "zookeeper.port");
+        zkTimeout = Integer.parseInt(getProp(props, "zookeeper.timeout", "3000"));
+
+        zooKeeper = createZooKeeper();
+    }
+
+    // separated for testing
+    ZooKeeper createZooKeeper() throws IOException {
+        return new ZooKeeper(zkHost + ":" + zkPort, zkTimeout, this);
     }
 
-    public void afterPropertiesSet() throws Exception {
-        String hostPort = discoveryService.getZooKeeperHost() + ":" + 
-                          discoveryService.getZooKeeperPort();
-        zooKeeper = createZooKeeper(hostPort);
-        
+    private void init() {        
         publicationTracker = new ServiceTracker(bundleContext, ServicePublication.class.getName(), 
                 new PublishToZooKeeperCustomizer(bundleContext, zooKeeper));
         publicationTracker.open();
@@ -62,11 +92,7 @@
                 finderCustomizer);
         lookupTracker.open();        
     }
-
-    ZooKeeper createZooKeeper(String hostPort) throws IOException {
-        return new ZooKeeper(hostPort, discoveryService.getZooKeeperTimeout(), this);
-    }
-
+    
     public void destroy() throws Exception {
         lookupTracker.close();
         publicationTracker.close();
@@ -76,4 +102,75 @@
     public void process(WatchedEvent event) {
         finderCustomizer.processGlobalEvent(event);
     }
+    
+    public void updateConfiguration(Dictionary props) {
+        LOG.warning("updateConfiguration not yet implemented");
+        /* TODO not yet finished
+        String host, port;
+        int timeout;
+        try {
+            host = getProp(props, "zookeeper.host");
+            port = getProp(props, "zookeeper.port");
+            timeout = Integer.parseInt(getProp(props, "zookeeper.timeout", "3000"));
+        } catch (Exception e) {
+            LOG.log(Level.INFO, "Insufficient configuration information to update ZooKeeper client.", e);
+            return;
+        }
+        
+        if (hasChanged(zkHost, host) ||
+            hasChanged(zkPort, port) ||
+            hasChanged(zkTimeout, timeout)) {
+            synchronized(this) {
+                try {
+                    zooKeeper.close();
+                } catch (InterruptedException e) {}
+
+                @@@ TODO need to close the trackers/customizers
+                try {
+                    zooKeeper = createZooKeeper(props);
+                    
+                    @@@ TODO need to recreate the trackers/customizers 
+                } catch (Exception e) {
+                    LOG.log(Level.WARNING, "Problem recreating zookeeper client after update.", e);
+                }
+            }
+        } else {
+            LOG.info("No configuration changes for ZooKeeper client.");
+        }
+        */
+    }
+
+    private <T> boolean hasChanged(T orig, T nw) {
+        if (orig == nw) {
+            return false;
+        }
+        
+        if (orig == null) {
+            return true;
+        }
+        
+        return !orig.equals(nw);
+    }
+
+    private static String getProp(Dictionary props, String key) throws ConfigurationException {
+        String val = getProp(props, key, null);
+        if (val != null) {
+            return val;
+        } else {
+            throw new ConfigurationException(key, "The property " + key + " requires a value");
+        }
+    }
+    
+    private static String getProp(Dictionary props, String key, String def) {        
+        Object val = props.get(key);
+        String rv;
+        if (val == null) {
+            rv = def;
+        } else {
+            rv = val.toString();
+        }
+        
+        LOG.fine("Reading Config Admin property: " + key + " value returned: " + rv);
+        return rv;
+    }
 }

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriver.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriver.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Copied: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java (from r781728, 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/DiscoveryDriverTest.java?p2=cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java&p1=cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryBeanTest.java&r1=781728&r2=782023&rev=782023&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/DiscoveryDriverTest.java Fri Jun  5 14:07:05 2009
@@ -20,6 +20,8 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.List;
 
 import junit.framework.TestCase;
@@ -31,46 +33,34 @@
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.discovery.DiscoveredServiceTracker;
 import org.osgi.service.discovery.ServicePublication;
 import org.osgi.util.tracker.ServiceTracker;
 
-public class DiscoveryBeanTest extends TestCase {
-    public void testDiscoveryBean() throws Exception {
+public class DiscoveryDriverTest extends TestCase {
+    public void testDiscoveryDriver() throws Exception {
+        BundleContext bc = getDefaultBundleContext();
+        Dictionary<String, String> props = getDefaultProps();
+        
         final StringBuilder sb = new StringBuilder();
-        DiscoveryBean db = new DiscoveryBean() {
+        DiscoveryDriver dd = new DiscoveryDriver(bc, props) {
             @Override
-            ZooKeeper createZooKeeper(String hostPort) throws IOException {
-                sb.append(hostPort);
+            ZooKeeper createZooKeeper() throws IOException {
+                sb.append(zkHost + ":" + zkPort);
                 ZooKeeper zk = EasyMock.createMock(ZooKeeper.class);
                 EasyMock.replay(zk);
                 return zk;
-            }            
+            }           
         };
-        
-        DiscoveryServiceImpl ds = new DiscoveryServiceImpl();
-        ds.setZooKeeperHost("myhost.mymachine.mytld");
-        ds.setZooKeeperPort(1234);
-        db.setDiscoveryServiceBean(ds);
-        
-        BundleContext bc = EasyMock.createMock(BundleContext.class);
-        expectServiceTrackerCalls(bc, ServicePublication.class.getName());
-        expectServiceTrackerCalls(bc, DiscoveredServiceTracker.class.getName());
-        EasyMock.replay(bc);
-        db.setBundleContext(bc);
-        
-        assertNull("Precondition failed", db.lookupTracker);
-        assertNull("Precondition failed", db.publicationTracker);
-        db.afterPropertiesSet();
-        assertNotNull(db.lookupTracker);
-        assertNotNull(db.publicationTracker);
         EasyMock.verify(bc);
+        assertEquals("somehost:1910", sb.toString());
         
-        EasyMock.verify(db.zooKeeper);
-        EasyMock.reset(db.zooKeeper);
-        db.zooKeeper.close();
+        EasyMock.verify(dd.zooKeeper);
+        EasyMock.reset(dd.zooKeeper);
+        dd.zooKeeper.close();
         EasyMock.expectLastCall();
-        EasyMock.replay(db.zooKeeper);
+        EasyMock.replay(dd.zooKeeper);
         
         ServiceTracker st1 = EasyMock.createMock(ServiceTracker.class);
         st1.close();
@@ -81,10 +71,10 @@
         EasyMock.expectLastCall();
         EasyMock.replay(st2);
         
-        db.lookupTracker = st1;
-        db.publicationTracker = st2;
+        dd.lookupTracker = st1;
+        dd.publicationTracker = st2;
         
-        db.destroy();        
+        dd.destroy();        
     }
 
     private void expectServiceTrackerCalls(BundleContext bc, String objectClass)
@@ -101,8 +91,13 @@
             .andReturn(new ServiceReference [0]).anyTimes();
     }
     
-    public void testProcessEvent() {
-        DiscoveryBean db = new DiscoveryBean();
+    public void testProcessEvent() throws Exception {
+        DiscoveryDriver db = new DiscoveryDriver(getDefaultBundleContext(), getDefaultProps()) {
+            @Override
+            ZooKeeper createZooKeeper() throws IOException {
+                return null;
+            }            
+        };
         
         FindInZooKeeperCustomizer fc = new FindInZooKeeperCustomizer(null, null);
         List<InterfaceMonitor> l1 = new ArrayList<InterfaceMonitor>();
@@ -134,4 +129,21 @@
         EasyMock.verify(dm1b);
         EasyMock.verify(dm2);
     }
+
+    private BundleContext getDefaultBundleContext() throws InvalidSyntaxException {
+        BundleContext bc = EasyMock.createMock(BundleContext.class);
+        expectServiceTrackerCalls(bc, ServicePublication.class.getName());
+        expectServiceTrackerCalls(bc, DiscoveredServiceTracker.class.getName());
+        EasyMock.replay(bc);
+        return bc;
+    }
+
+    private Dictionary<String, String> getDefaultProps() {
+        Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put("zookeeper.host", "somehost");
+        props.put("zookeeper.port", "1910");
+        props.put("zookeeper.timeout", "1500");
+        return props;
+    }
+
 }

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/DiscoveryDriverTest.java
------------------------------------------------------------------------------
    svn:mergeinfo =