You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2013/06/25 07:23:32 UTC

svn commit: r1496354 - /karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java

Author: jbonofre
Date: Tue Jun 25 05:23:32 2013
New Revision: 1496354

URL: http://svn.apache.org/r1496354
Log:
[KARAF-2347] Small improvements on the Cellar configuration

Modified:
    karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java

Modified: karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java?rev=1496354&r1=1496353&r2=1496354&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java Tue Jun 25 05:23:32 2013
@@ -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 extend
      */
     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 extend
         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 extend
                 return false;
         }
 
-        if (source.size() != target.size())
-            return false;
-
         return true;
     }
 
@@ -106,7 +107,7 @@ public class ConfigurationSupport extend
             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 extend
                         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();