Author: davidb
Date: Mon Nov 30 11:48:42 2009
New Revision: 885385
URL: http://svn.apache.org/viewvc?rev=885385&view=rev
Log:
Made the ZooKeeper Server Running Bundle reconfigurable.
The ZooKeeper server is configured through the OSGi Configuration Admin service.
PID = org.apache.cxf.dosgi.discovery.zookeeper.server
Required configuration setting:
clientPort
(an example value would be clientPort=2181)
All other configuration settings (such as tickTime, initLimit, syncLimit &
dataDir) are defaulted. The configuration properties supported are exactly the
same as the ones supported by ZooKeeper in the config/zoo.cfg file.
Reconfiguration support:
* The ZooKeeper server is started when a configuration object with the relevant
PID appears.
* Removing the configuration object with the relevant PID will stop the server.
* Changing the configuration information will cause the ZooKeeper server to be
restarted within the OSGi Framework with the changed configuration.
Modified:
cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java
cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java
Modified:
cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java?rev=885385&r1=885384&r2=885385&view=diff
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java
(original)
+++
cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java
Mon Nov 30 11:48:42 2009
@@ -48,6 +48,11 @@
if (main != null) {
LOG.info("Shutting down ZooKeeper server");
main.shutdown();
+ try {
+ zkMainThread.join();
+ } catch (InterruptedException e) {
+ // ignore
+ }
main = null;
zkMainThread = null;
}
@@ -72,13 +77,14 @@
@SuppressWarnings("unchecked")
public synchronized void updated(Dictionary dict) throws
ConfigurationException {
if (dict == null) {
- // stop server... TODO
+ shutdown();
return;
}
if (main != null) {
- // reconfiguration not yet supported
- return;
+ // stop the current instance
+ shutdown();
+ // then reconfigure and start again.
}
if (dict.get("clientPort") == null) {
Modified:
cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java
URL:
http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java?rev=885385&r1=885384&r2=885385&view=diff
==============================================================================
---
cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java
(original)
+++
cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java
Mon Nov 30 11:48:42 2009
@@ -18,16 +18,20 @@
package org.apache.cxf.dosgi.discovery.zookeeper.server;
import java.io.File;
+import java.util.Dictionary;
import java.util.Hashtable;
import junit.framework.TestCase;
+import
org.apache.cxf.dosgi.discovery.zookeeper.server.ManagedService.MyZooKeeperServerMain;
+import org.apache.zookeeper.ZooKeeperMain;
import org.apache.zookeeper.server.ServerConfig;
import org.easymock.IAnswer;
import org.easymock.classextension.EasyMock;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
public class ManagedServiceTest extends TestCase {
public void testManagedService() throws Exception {
@@ -101,4 +105,85 @@
assertNull(ms.main);
assertNull(ms.zkMainThread);
}
+
+ public void testRemoveConfiguration() throws Exception {
+ BundleContext bc = EasyMock.createMock(BundleContext.class);
+
+ final StringBuilder shutDownTracker = new StringBuilder();
+ ManagedService ms = new ManagedService(bc) {
+ @Override
+ public synchronized void shutdown() {
+ shutDownTracker.append("called");
+ }
+
+ @Override
+ void startThread() {}
+ };
+
+ assertEquals("Precondition failed", 0, shutDownTracker.length());
+ ms.updated(null);
+ assertEquals("called", shutDownTracker.toString());
+ // check that it didn't get reinitialized TODO
+ }
+
+ public void testNewConfiguration() throws Exception {
+ BundleContext bc = EasyMock.createMock(BundleContext.class);
+
+ final StringBuilder shutDownTracker = new StringBuilder();
+ ManagedService ms = new ManagedService(bc) {
+ @Override
+ public synchronized void shutdown() {
+ shutDownTracker.append("called");
+ }
+
+ @Override
+ void startThread() {}
+ };
+
+ assertEquals("Precondition failed", 0, shutDownTracker.length());
+ assertNull("Precondition failed", ms.main);
+ assertNull("Precondition failed", ms.zkMainThread);
+
+ Hashtable<String, Object> props = new Hashtable<String, Object>();
+ props.put("clientPort", "9911");
+ ms.updated(props);
+ assertEquals("Shutdown should not have been called", 0,
shutDownTracker.length());
+ assertNotNull(ms.main);
+ assertNotNull(ms.zkMainThread);
+ }
+
+ public void testChangeConfiguration() throws Exception {
+ BundleContext bc = EasyMock.createMock(BundleContext.class);
+
+ final StringBuilder shutDownTracker = new StringBuilder();
+ ManagedService ms = new ManagedService(bc) {
+ @Override
+ public synchronized void shutdown() {
+ shutDownTracker.append("called");
+ }
+
+ @Override
+ void startThread() {}
+ };
+
+ MyZooKeeperServerMain initialMsMain =
+ EasyMock.createMock(ManagedService.MyZooKeeperServerMain.class);
+ ms.main = initialMsMain;
+ Thread initialZkThread = new Thread();
+ ms.zkMainThread = initialZkThread;
+
+ assertEquals("Precondition failed", 0, shutDownTracker.length());
+ assertNotNull("Precondition failed", ms.main);
+ assertNotNull("Precondition failed", ms.zkMainThread);
+
+ Hashtable<String, Object> props = new Hashtable<String, Object>();
+ props.put("clientPort", "9911");
+ ms.updated(props);
+ assertEquals("We are reconfiguring, so shutdown should be called",
+ "called", shutDownTracker.toString());
+ assertNotNull(ms.main);
+ assertNotNull(ms.zkMainThread);
+ assertNotSame(ms.main, initialMsMain);
+ assertNotSame(ms.zkMainThread, initialZkThread);
+ }
}