Repository: cxf-dosgi Updated Branches: refs/heads/master 4c86a73f5 -> bf6c53aac
DOSGI-216 Fix ZookeeperServer restarting ZooKeeper unnecessarily Project: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/commit/bf6c53aa Tree: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/tree/bf6c53aa Diff: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/diff/bf6c53aa Branch: refs/heads/master Commit: bf6c53aac82a35b16990027f0448e5499370ea01 Parents: 4c86a73 Author: Amichai Rothman <[email protected]> Authored: Thu Mar 20 15:56:12 2014 +0200 Committer: Amichai Rothman <[email protected]> Committed: Thu Mar 20 15:56:12 2014 +0200 ---------------------------------------------------------------------- .../zookeeper/server/ZookeeperStarter.java | 29 ++++++++++++-------- .../discovery/zookeeper/server/util/Utils.java | 22 +++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/bf6c53aa/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java ---------------------------------------------------------------------- diff --git a/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java b/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java index 69baf15..e32a3eb 100644 --- a/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java +++ b/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java @@ -21,6 +21,7 @@ package org.apache.cxf.dosgi.discovery.zookeeper.server; import java.io.File; import java.io.IOException; import java.util.Dictionary; +import java.util.Map; import org.apache.cxf.dosgi.discovery.zookeeper.server.util.Utils; import org.apache.log4j.Level; @@ -40,6 +41,7 @@ public class ZookeeperStarter implements org.osgi.service.cm.ManagedService { protected ZookeeperServer main; private final BundleContext bundleContext; private Thread zkMainThread; + private Map<String, ?> curConfiguration; public ZookeeperStarter(BundleContext ctx) { bundleContext = ctx; @@ -73,18 +75,23 @@ public class ZookeeperStarter implements org.osgi.service.cm.ManagedService { @SuppressWarnings("unchecked") public synchronized void updated(Dictionary<String, ?> dict) throws ConfigurationException { LOG.debug("Received configuration update for Zookeeper Server: " + dict); - shutdown(); - // config is null if it doesn't exist, is being deleted or has not yet been loaded - // in which case we just stop running - if (dict != null) { - try { - setDefaults((Dictionary<String, String>) dict); - QuorumPeerConfig config = parseConfig(dict); - startFromConfig(config); - LOG.info("Applied configuration update: " + dict); - } catch (Exception th) { - LOG.error("Problem applying configuration update: " + dict, th); + try { + if (dict != null) { + setDefaults((Dictionary<String, String>)dict); + } + Map<String, ?> configMap = Utils.toMap(dict); + if (!configMap.equals(curConfiguration)) { // only if something actually changed + shutdown(); + curConfiguration = configMap; + // config is null if it doesn't exist, is being deleted or has not yet been loaded + // in which case we just stop running + if (dict != null) { + startFromConfig(parseConfig(dict)); + LOG.info("Applied configuration update: " + dict); + } } + } catch (Exception th) { + LOG.error("Problem applying configuration update: " + dict, th); } } http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/bf6c53aa/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/util/Utils.java ---------------------------------------------------------------------- diff --git a/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/util/Utils.java b/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/util/Utils.java index 7359199..f72cbb2 100644 --- a/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/util/Utils.java +++ b/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/util/Utils.java @@ -21,7 +21,9 @@ package org.apache.cxf.dosgi.discovery.zookeeper.server.util; import java.util.ArrayList; import java.util.Dictionary; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; /** @@ -68,6 +70,26 @@ public final class Utils { } /** + * Converts the given Dictionary to a Map. + * + * @param dict a dictionary + * @param <K> the key type + * @param <V> the value type + * @return the converted map, or an empty map if the given dictionary is null + */ + public static <K, V> Map<K, V> toMap(Dictionary<K, V> dict) { + Map<K, V> map = new HashMap<K, V>(); + if (dict != null) { + Enumeration<K> keys = dict.keys(); + while (keys.hasMoreElements()) { + K key = keys.nextElement(); + map.put(key, dict.get(key)); + } + } + return map; + } + + /** * Converts a Dictionary into a Properties instance. * * @param dict a dictionary
