[KARAF-2347] Small improvements on the Cellar configuration git-svn-id: https://svn.apache.org/repos/asf/karaf/cellar/branches/cellar-2.2.x@1496354 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/d49b6d85 Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/d49b6d85 Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/d49b6d85 Branch: refs/heads/cellar-2.2.x Commit: d49b6d85b49ea03a81c8f69c527270d6fc4f1b95 Parents: 33637b8 Author: jbonofre <jbonofre@13f79535-47bb-0310-9956-ffa450edef68> Authored: Tue Jun 25 05:23:32 2013 +0000 Committer: jbonofre <jbonofre@13f79535-47bb-0310-9956-ffa450edef68> Committed: Tue Jun 25 05:23:32 2013 +0000 ---------------------------------------------------------------------- .../cellar/config/ConfigurationSupport.java | 51 +++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/d49b6d85/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java ---------------------------------------------------------------------- diff --git a/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java b/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java index 8ac5edb..25b48c9 100644 --- a/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java +++ b/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java @@ -14,6 +14,8 @@ package org.apache.karaf.cellar.config; import org.apache.karaf.cellar.core.CellarSupport; +import org.osgi.framework.*; +import org.osgi.framework.Constants; import org.osgi.service.cm.Configuration; import org.osgi.service.cm.ConfigurationAdmin; @@ -42,13 +44,12 @@ public class ConfigurationSupport extends CellarSupport { */ public Properties dictionaryToProperties(Dictionary dictionary) { Properties properties = new Properties(); - if (dictionary != null && dictionary.keys() != null) { + if (dictionary != null) { Enumeration keys = dictionary.keys(); while (keys.hasMoreElements()) { - String key = (String) keys.nextElement(); + Object key = keys.nextElement(); if (key != null && dictionary.get(key) != null) { - String value = (String) dictionary.get(key); properties.put(key, dictionary.get(key)); } } @@ -73,11 +74,14 @@ public class ConfigurationSupport extends CellarSupport { if (source.isEmpty() && target.isEmpty()) return true; + if (source.size() != target.size()) + return false; + Enumeration sourceKeys = source.keys(); while (sourceKeys.hasMoreElements()) { - String key = (String) sourceKeys.nextElement(); - String sourceValue = String.valueOf(source.get(key)); - String targetValue = String.valueOf(target.get(key)); + Object key = sourceKeys.nextElement(); + Object sourceValue = source.get(key); + Object targetValue = target.get(key); if (sourceValue != null && targetValue == null) return false; if (sourceValue == null && targetValue != null) @@ -86,9 +90,6 @@ public class ConfigurationSupport extends CellarSupport { return false; } - if (source.size() != target.size()) - return false; - return true; } @@ -106,7 +107,7 @@ public class ConfigurationSupport extends CellarSupport { while (sourceKeys.hasMoreElements()) { String key = (String) sourceKeys.nextElement(); if (!isExcludedProperty(key)) { - String value = String.valueOf(dictionary.get(key)); + Object value = dictionary.get(key); result.put(key, value); } } @@ -155,31 +156,35 @@ public class ConfigurationSupport extends CellarSupport { storageFile = new File(new URL((String) val).toURI()); } } catch (Exception e) { - throw (IOException) new IOException(e.getMessage()).initCause(e); + throw new IOException(e.getMessage(), e); } } + org.apache.karaf.util.Properties p = new org.apache.karaf.util.Properties(storageFile); - for (Enumeration keys = props.keys(); keys.hasMoreElements(); ) { - Object key = keys.nextElement(); + List<String> propertiesToRemove = new ArrayList<String>(); + Set<String> set = p.keySet(); + + for (String key : set) { if (!org.osgi.framework.Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FELIX_FILEINSTALL_FILENAME.equals(key)) { - p.put((String) key, (String) props.get(key)); + propertiesToRemove.add(key); } } - // remove "removed" properties from the file - ArrayList<String> propertiesToRemove = new ArrayList<String>(); - for (Object key : p.keySet()) { - if (props.get(key) == null - && !org.osgi.framework.Constants.SERVICE_PID.equals(key) + + for (String key : propertiesToRemove) { + p.remove(key); + } + + for (Enumeration<String> keys = props.keys(); keys.hasMoreElements(); ) { + String key = keys.nextElement(); + if (!Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FELIX_FILEINSTALL_FILENAME.equals(key)) { - propertiesToRemove.add(key.toString()); + p.put(key, (String) props.get(key)); } } - for (String key : propertiesToRemove) { - p.remove(key); - } + // save the cfg file storage.mkdirs(); p.save();
