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 2012/05/11 09:31:59 UTC

svn commit: r1337044 - in /karaf/cellar/branches/cellar-2.2.x/config/src: main/java/org/apache/karaf/cellar/config/ test/java/org/apache/karaf/cellar/config/

Author: jbonofre
Date: Fri May 11 07:31:59 2012
New Revision: 1337044

URL: http://svn.apache.org/viewvc?rev=1337044&view=rev
Log:
[KARAF-1463] Reintroduce config filtering, specially for the karaf.home property

Removed:
    karaf/cellar/branches/cellar-2.2.x/config/src/test/java/org/apache/karaf/cellar/config/ConfigurationSupportTest.java
Modified:
    karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java
    karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java
    karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java
    karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java

Modified: karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java?rev=1337044&r1=1337043&r2=1337044&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java Fri May 11 07:31:59 2012
@@ -75,9 +75,7 @@ public class ConfigurationEventHandler e
                     } else {
                         if (remoteDictionary != null) {
                             Dictionary localDictionary = conf.getProperties();
-                            if (!this.equals(localDictionary, remoteDictionary)) {
-                                conf.update(remoteDictionary);
-                            }
+                            conf.update(filter(remoteDictionary, true));
                         }
                     }
                 }

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=1337044&r1=1337043&r2=1337044&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 Fri May 11 07:31:59 2012
@@ -24,6 +24,10 @@ import java.util.Properties;
  */
 public class ConfigurationSupport extends CellarSupport {
 
+    private static String HOME_PLACEHOLDER = "karaf.home";
+    private static String RELATIVE_HOME = "${" + HOME_PLACEHOLDER + "}";
+    private static String HOME = System.getProperty("karaf.home");
+
     /**
      * Reads a {@code Dictionary} object and creates a property object out of it.
      *
@@ -46,67 +50,49 @@ public class ConfigurationSupport extend
     }
 
     /**
-     * Check if two dictionaries are egal.
-     *
-     * @param dict1 the source dictionary.
-     * @param dict2 the target dictionary.
-     * @return true if the two dictionaries are egal, false else
+     * Filter a dictionary, specially replace the karaf.home property with a relative value.
+     * @param dictionary
+     * @return
      */
-    protected boolean equals(Dictionary dict1, Dictionary dict2) {
-        if (dict1 == null && dict2 == null)
-            return true;
-
-        if (dict1 == null && dict2 != null)
-            return false;
-
-        if (dict1 != null && dict2 == null)
-            return false;
-
-        if (dict1.size() != dict2.size())
-            return false;
-
-        for (Enumeration e = dict1.keys(); e.hasMoreElements(); ) {
-            Object key = e.nextElement();
-            if (!dict1.get(key).equals(dict2.get(key))) {
-                return false;
+    public Dictionary filter(Dictionary dictionary) {
+        Dictionary properties = new Properties();
+        Enumeration keys = dictionary.keys();
+        while (keys.hasMoreElements()) {
+            String key = (String) keys.nextElement();
+            if (key != null && dictionary.get(key) != null) {
+                String value = String.valueOf(dictionary.get(key));
+                value = this.convert(value, HOME, RELATIVE_HOME);
+                properties.put(key, value);
             }
         }
-
-        return true;
+        return properties;
     }
 
     /**
-     * Returns true if target contains all source key/value pairs.
-     *
-     * @param source
-     * @param target
+     * Filter a dictionary, specially replace the karaf.home property with a relative value.
+     * @param dictionary
      * @return
      */
-    private boolean subDictionary(Dictionary source, Dictionary target) {
-        if (source == null && target == null) {
-            return true;
-        } else if (source == null || target == null) {
-            return false;
-        } else if (source.isEmpty() && target.isEmpty()) {
-            return true;
-        } else {
-            Enumeration keys = source.keys();
-            while (keys.hasMoreElements()) {
-                String key = (String) keys.nextElement();
-                String value1 = (String) source.get(key);
-                String value2 = (String) target.get(key);
-
-                if (value1 == null && value2 == null)
-                    continue;
-                else if (value1 == null)
-                    return false;
-                else if (value2 == null)
-                    return false;
-                else if (value1.equals(value2))
-                    continue;
+    public Dictionary filter(Dictionary dictionary, boolean reverse) {
+        Dictionary properties = new Properties();
+        Enumeration keys = dictionary.keys();
+        while (keys.hasMoreElements()) {
+            String key = (String) keys.nextElement();
+            if (key != null && dictionary.get(key) != null) {
+                String value = String.valueOf(dictionary.get(key));
+                value = this.convert(value, RELATIVE_HOME, HOME);
+                properties.put(key, value);
             }
-            return true;
         }
+        return properties;
+    }
+
+    public String convert(String value, String absolute, String relative) {
+        String result = value;
+        if (absolute != null && (absolute.trim().length() > 0) && value.contains(absolute)) {
+            result = value.replace(absolute, relative);
+        }
+        return result;
     }
 
 }

Modified: karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java?rev=1337044&r1=1337043&r2=1337044&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java Fri May 11 07:31:59 2012
@@ -88,7 +88,7 @@ public class ConfigurationSynchronizer e
                         try {
                             // update the local configuration if needed
                             Configuration conf = configurationAdmin.getConfiguration(pid);
-                            conf.update(remoteDictionary);
+                            conf.update(filter(remoteDictionary, true));
                         } catch (IOException ex) {
                             LOGGER.error("CELLAR CONFIG: failed to read from the distributed map", ex);
                         }
@@ -125,16 +125,14 @@ public class ConfigurationSynchronizer e
                     for (Configuration conf : configs) {
                         String pid = conf.getPid();
                         if (isAllowed(group, Constants.CATEGORY, pid, EventType.OUTBOUND)) {
-                            Properties localDictionary = dictionaryToProperties(conf.getProperties());
+                            Properties localDictionary = dictionaryToProperties(filter(conf.getProperties()));
                             Properties remoteDictionary = configurationTable.get(pid);
-                            if (!this.equals(localDictionary, remoteDictionary)) {
-                                // update the distributed map
-                                configurationTable.put(pid, localDictionary);
-                                // broadcast the cluster event
-                                RemoteConfigurationEvent event = new RemoteConfigurationEvent(conf.getPid());
-                                event.setSourceGroup(group);
-                                eventProducer.produce(event);
-                            }
+                            // update the distributed map
+                            configurationTable.put(pid, localDictionary);
+                            // broadcast the cluster event
+                            RemoteConfigurationEvent event = new RemoteConfigurationEvent(conf.getPid());
+                            event.setSourceGroup(group);
+                            eventProducer.produce(event);
                         } else
                             LOGGER.warn("CELLAR CONFIG: configuration with PID {} is marked as BLOCKED OUTBOUND", pid);
                     }

Modified: karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java?rev=1337044&r1=1337043&r2=1337044&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java Fri May 11 07:31:59 2012
@@ -72,13 +72,11 @@ public class LocalConfigurationListener 
                             configurationTable.remove(pid);
                         } else {
                             Configuration conf = configurationAdmin.getConfiguration(pid);
-                            Properties localDictionary = dictionaryToProperties(conf.getProperties());
+                            Properties localDictionary = dictionaryToProperties(filter(conf.getProperties()));
                             Dictionary remoteDictionary = configurationTable.get(pid);
-                            if (!equals(localDictionary, remoteDictionary)) {
                                 // update the distributed map
                                 configurationTable.put(pid, localDictionary);
                                 // TODO broadcast a cluster event but it creates a loop
-                            }
                         }
                     } catch (Exception e) {
                         LOGGER.error("CELLAR CONFIG: failed to push configuration with PID {} to the distributed map", pid, e);