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/03 14:53:32 UTC
svn commit: r1333437 - in /karaf/cellar/trunk/config/src:
main/java/org/apache/karaf/cellar/config/
main/java/org/apache/karaf/cellar/config/shell/
main/resources/OSGI-INF/blueprint/ test/java/org/apache/karaf/cellar/config/
Author: jbonofre
Date: Thu May 3 12:53:31 2012
New Revision: 1333437
URL: http://svn.apache.org/viewvc?rev=1333437&view=rev
Log:
[KARAF-1405] Refactore of the config layer
[KARAF-1190] Remove property filtering
Modified:
karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java
karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java
karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java
karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java
karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/shell/PropSetCommand.java
karaf/cellar/trunk/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
karaf/cellar/trunk/config/src/test/java/org/apache/karaf/cellar/config/ConfigurationSupportTest.java
Modified: karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java?rev=1333437&r1=1333436&r2=1333437&view=diff
==============================================================================
--- karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java (original)
+++ karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationEventHandler.java Thu May 3 12:53:31 2012
@@ -15,7 +15,6 @@ package org.apache.karaf.cellar.config;
import org.apache.karaf.cellar.core.Configurations;
import org.apache.karaf.cellar.core.Group;
-import org.apache.karaf.cellar.core.Node;
import org.apache.karaf.cellar.core.control.BasicSwitch;
import org.apache.karaf.cellar.core.control.Switch;
import org.apache.karaf.cellar.core.control.SwitchStatus;
@@ -41,18 +40,8 @@ public class ConfigurationEventHandler e
private final Switch eventSwitch = new BasicSwitch(SWITCH_ID);
- private Node node;
-
- /**
- * Handles
- *
- * @param event
- */
public void handle(RemoteConfigurationEvent event) {
- if (event == null || event.getSourceGroup() == null || node == null || node.equals(event.getSourceNode()))
- return;
-
// check if the handler is ON
if (eventSwitch.getStatus().equals(SwitchStatus.OFF)) {
LOGGER.warn("CELLAR CONFIG: {} switch is OFF, cluster event is not handled", SWITCH_ID);
@@ -72,19 +61,16 @@ public class ConfigurationEventHandler e
if (eventSwitch.getStatus().equals(SwitchStatus.ON)) {
String pid = event.getId();
- //Check if the pid is marked as local.
if (isAllowed(event.getSourceGroup(), Constants.CATEGORY, pid, EventType.INBOUND)) {
Properties dictionary = configurationTable.get(pid);
Configuration conf;
try {
conf = configurationAdmin.getConfiguration(pid);
- //Update the configurationTable.
if (conf != null && dictionary != null) {
- Dictionary existingConfiguration = filterDictionary(conf.getProperties());
- if (!dictionariesEqual(dictionary, existingConfiguration)) {
- conf.update(preparePull(dictionary));
+ Dictionary existingConfiguration = conf.getProperties();
+ if (!equals(dictionary, existingConfiguration)) {
+ conf.update(dictionary);
}
- LOGGER.debug("CELLAR CONFIG: pulling configuration with PID {}", pid);
}
} catch (IOException ex) {
LOGGER.error("CELLAR CONFIG: failed to read remote distributed map", ex);
@@ -97,9 +83,7 @@ public class ConfigurationEventHandler e
* Initialization Method.
*/
public void init() {
- if (clusterManager != null) {
- node = clusterManager.getNode();
- }
+
}
/**
Modified: karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java?rev=1333437&r1=1333436&r2=1333437&view=diff
==============================================================================
--- karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java (original)
+++ karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSupport.java Thu May 3 12:53:31 2012
@@ -24,12 +24,6 @@ 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");
-
- private static String[] FILTERED_PROPERTIES = {"service.pid", "service.factoryPid", "felix.fileinstall.filename"};
-
/**
* Reads a {@code Dictionary} object and creates a property object out of it.
*
@@ -53,131 +47,33 @@ public class ConfigurationSupport extend
}
/**
- * Prepares a dictionary for push
- *
- * @param dictionary
- * @return
- */
- public Dictionary preparePush(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 = convertStrings(value, HOME, RELATIVE_HOME);
- properties.put(key, value);
- }
- }
- return properties;
- }
-
- /**
- * Prepares a dictionary for Pull
- *
- * @param dictionary
- * @return
- */
- public Dictionary preparePull(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 = convertStrings(value, RELATIVE_HOME, HOME);
- properties.put(key, value);
- }
- }
- return properties;
- }
-
- /**
- * Performs a string replacement on value.
- *
- * @param value
- * @return
- */
- public String convertStrings(String value, String absolute, String relative) {
- String result = value;
- if (absolute != null && !absolute.isEmpty() && value.contains(absolute)) {
- result = value.replace(absolute, relative);
- }
- return result;
- }
-
- public Dictionary filterDictionary(Dictionary dictionary) {
- Dictionary result = new Properties();
- if (dictionary != null) {
- Enumeration enumaration = dictionary.keys();
- while (enumaration.hasMoreElements()) {
- String key = (String) enumaration.nextElement();
- if (!isPropertyFiltered(key)) {
- String value = String.valueOf(dictionary.get(key));
- result.put(key, value);
- }
- }
- }
- return result;
- }
-
- /**
* Returns true if dictionaries are equal.
*
* @param dict1
* @param dict2
* @return
*/
- protected boolean dictionariesEqual(Dictionary dict1, Dictionary dict2) {
- return subDictionary(dict1, dict2) && subDictionary(dict2, dict1);
- }
-
- /**
- * Returns true if target contains all source key/value pairs.
- *
- * @param source
- * @param target
- * @return
- */
- public boolean subDictionary(Dictionary source, Dictionary target) {
- if (source == null && target == null) {
+ protected boolean equals(Dictionary dict1, Dictionary dict2) {
+ if (dict1 == null && dict2 == null)
return true;
- } else if (source == null || target == null) {
+
+ if (dict1 == null && dict2 != null)
+ return false;
+
+ if (dict1 != null && dict2 == null)
+ return false;
+
+ if (dict1.size() != dict2.size())
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;
+ for (Enumeration e = dict1.keys(); e.hasMoreElements(); ) {
+ Object key = e.nextElement();
+ if (!dict1.get(key).equals(dict2.get(key))) {
+ return false;
}
- return true;
}
- }
- /**
- * Returns true if property is Filtered.
- *
- * @param propertyName
- * @return
- */
- public boolean isPropertyFiltered(String propertyName) {
- for (int i = 0; i < FILTERED_PROPERTIES.length; i++) {
- if (FILTERED_PROPERTIES[i].equals(propertyName))
- return true;
- }
- return false;
+ return true;
}
}
Modified: karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java?rev=1333437&r1=1333436&r2=1333437&view=diff
==============================================================================
--- karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java (original)
+++ karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/ConfigurationSynchronizer.java Thu May 3 12:53:31 2012
@@ -83,17 +83,12 @@ public class ConfigurationSynchronizer e
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
for (String pid : configurationTable.keySet()) {
- //Check if the pid is marked as local.
if (isAllowed(group, Constants.CATEGORY, pid, EventType.INBOUND)) {
- Properties dictionary = configurationTable.get(pid);
+ Properties remoteDictionary = configurationTable.get(pid);
try {
+ // update the local configuration if needed
Configuration conf = configurationAdmin.getConfiguration(pid);
- //Update the configuration.
- if (conf != null) {
- //Mark the remote configuration event.
- conf.update(preparePull(dictionary));
- LOGGER.debug("CELLAR CONFIG: local configuration updated");
- }
+ conf.update(remoteDictionary);
} catch (IOException ex) {
LOGGER.error("CELLAR CONFIG: failed to read distributed map", ex);
}
@@ -130,31 +125,15 @@ public class ConfigurationSynchronizer e
String pid = conf.getPid();
// check if the pid is marked as local.
if (isAllowed(group, Constants.CATEGORY, pid, EventType.OUTBOUND)) {
- Properties source = dictionaryToProperties(preparePush(filterDictionary(conf.getProperties())));
- Properties target = configurationTable.get(pid);
- if (target != null) {
- boolean requiresUpdate = false;
- if (source != null && source.keys() != null) {
- Enumeration keys = source.keys();
- while (keys.hasMoreElements()) {
- String key = (String) keys.nextElement();
- if (target.get(key) == null || target.get(key).equals(source.get(key))) {
- requiresUpdate = true;
- target.put(key, source.get(key));
- }
- }
- configurationTable.put(pid, target);
- if (requiresUpdate) {
- RemoteConfigurationEvent event = new RemoteConfigurationEvent(conf.getPid());
- eventProducer.produce(event);
- }
- LOGGER.debug("CELLAR CONFIG: publishing configuration with PID {} to the distributed map", pid);
- }
- } else {
+ Properties localDictionary = dictionaryToProperties(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());
- configurationTable.put(pid, source);
+ event.setSourceGroup(group);
eventProducer.produce(event);
- LOGGER.debug("CELLAR CONFIG: publishing configuration with PID {} to the distributed map", pid);
}
}
}
Modified: karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java?rev=1333437&r1=1333436&r2=1333437&view=diff
==============================================================================
--- karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java (original)
+++ karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/LocalConfigurationListener.java Thu May 3 12:53:31 2012
@@ -27,10 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
+import java.util.*;
/**
* Local configuration listener.
@@ -63,40 +60,27 @@ public class LocalConfigurationListener
for (Group group : groups) {
// check if the pid is allowed for outbound.
if (isAllowed(group, Constants.CATEGORY, pid, EventType.OUTBOUND)) {
- // update the distributed map
- push(pid, group);
+ // update the distributed map if needed
+ Map<String, Properties> configurationTable = clusterManager.getMap(Constants.CONFIGURATION_MAP + Configurations.SEPARATOR + group.getName());
+
+ try {
+ Configuration conf = configurationAdmin.getConfiguration(pid);
+ Properties localDictionary = dictionaryToProperties(conf.getProperties());
+ Dictionary remoteDictionary = configurationTable.get(pid);
+ if (!equals(localDictionary, remoteDictionary)) {
+ // update the distributed map
+ configurationTable.put(pid, localDictionary);
+ }
+ } catch (Exception e) {
+ LOGGER.error("CELLAR CONFIG: failed to push configuration with PID {} to the distributed map", pid, e);
+ }
// broadcast the cluster event
- RemoteConfigurationEvent configurationEvent = new RemoteConfigurationEvent(pid);
- configurationEvent.setSourceGroup(group);
- eventProducer.produce(configurationEvent);
- } else LOGGER.debug("CELLAR CONFIG: configuration with PID {} is marked as BLOCKED OUTBOUND", pid);
+ } else LOGGER.warn("CELLAR CONFIG: configuration with PID {} is marked as BLOCKED OUTBOUND", pid);
}
}
}
- /**
- * Push configuration with pid to the table.
- *
- * @param pid
- */
-
- protected void push(String pid, Group group) {
- String groupName = group.getName();
-
- Map<String, Properties> configurationTable = clusterManager.getMap(Constants.CONFIGURATION_MAP + Configurations.SEPARATOR + groupName);
- try {
- Configuration[] configurations = configurationAdmin.listConfigurations("(service.pid=" + pid + ")");
- for (Configuration configuration : configurations) {
- Properties properties = dictionaryToProperties(preparePush(filterDictionary(configuration.getProperties())));
- configurationTable.put(configuration.getPid(), properties);
- }
- } catch (IOException e) {
- LOGGER.error("CELLAR CONFIG: failed to push configuration with PID {}", pid, e);
- } catch (InvalidSyntaxException e) {
- LOGGER.error("CELLAR CONFIG: failed to push configuration with PID {}", pid, e);
- }
- }
/**
* Initialization Method.
Modified: karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/shell/PropSetCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/shell/PropSetCommand.java?rev=1333437&r1=1333436&r2=1333437&view=diff
==============================================================================
--- karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/shell/PropSetCommand.java (original)
+++ karaf/cellar/trunk/config/src/main/java/org/apache/karaf/cellar/config/shell/PropSetCommand.java Thu May 3 12:53:31 2012
@@ -14,8 +14,11 @@
package org.apache.karaf.cellar.config.shell;
import org.apache.karaf.cellar.config.Constants;
+import org.apache.karaf.cellar.config.RemoteConfigurationEvent;
import org.apache.karaf.cellar.core.Configurations;
import org.apache.karaf.cellar.core.Group;
+import org.apache.karaf.cellar.core.control.SwitchStatus;
+import org.apache.karaf.cellar.core.event.EventProducer;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
@@ -40,25 +43,49 @@ public class PropSetCommand extends Conf
@Argument(index = 3, name = "value", description = "The property value.", required = true, multiValued = false)
String value;
+ private EventProducer eventProducer;
+
@Override
protected Object doExecute() throws Exception {
+ // check if the group exists
Group group = groupManager.findGroupByName(groupName);
if (group == null) {
- System.err.println("Cluster group " + groupName + " doesn't exist.");
+ System.err.println("Cluster group " + groupName + " doesn't exist");
return null;
}
- Map<String, Properties> configurationTable = clusterManager.getMap(Constants.CONFIGURATION_MAP + Configurations.SEPARATOR + groupName);
- if (configurationTable != null) {
- Properties properties = configurationTable.get(pid);
+
+ // check if the producer is ON
+ if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
+ System.err.println("Cluster event producer is OFF");
+ return null;
+ }
+
+ Map<String, Properties> configurationMap = clusterManager.getMap(Constants.CONFIGURATION_MAP + Configurations.SEPARATOR + groupName);
+ if (configurationMap != null) {
+ // update the distributed map
+ Properties properties = configurationMap.get(pid);
if (properties == null) {
properties = new Properties();
}
properties.put(key, value);
- configurationTable.put(pid, properties);
+ configurationMap.put(pid, properties);
+
+ // broadcast the cluster event
+ RemoteConfigurationEvent event = new RemoteConfigurationEvent(pid);
+ event.setSourceGroup(group);
+ eventProducer.produce(event);
} else {
- System.out.println("Group " + groupName + " doesn't exist.");
+ System.out.println("Configuration distributed map not found for cluster group " + groupName);
}
return null;
}
+ public EventProducer getEventProducer() {
+ return eventProducer;
+ }
+
+ public void setEventProducer(EventProducer eventProducer) {
+ this.eventProducer = eventProducer;
+ }
+
}
Modified: karaf/cellar/trunk/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml?rev=1333437&r1=1333436&r2=1333437&view=diff
==============================================================================
--- karaf/cellar/trunk/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml (original)
+++ karaf/cellar/trunk/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml Thu May 3 12:53:31 2012
@@ -39,6 +39,7 @@
<action class="org.apache.karaf.cellar.config.shell.PropSetCommand">
<property name="clusterManager" ref="clusterManager"/>
<property name="groupManager" ref="groupManager"/>
+ <property name="eventProducer" ref="eventProducer"/>
</action>
<completers>
<ref component-id="allGroupCompleter"/>
Modified: karaf/cellar/trunk/config/src/test/java/org/apache/karaf/cellar/config/ConfigurationSupportTest.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/config/src/test/java/org/apache/karaf/cellar/config/ConfigurationSupportTest.java?rev=1333437&r1=1333436&r2=1333437&view=diff
==============================================================================
--- karaf/cellar/trunk/config/src/test/java/org/apache/karaf/cellar/config/ConfigurationSupportTest.java (original)
+++ karaf/cellar/trunk/config/src/test/java/org/apache/karaf/cellar/config/ConfigurationSupportTest.java Thu May 3 12:53:31 2012
@@ -27,76 +27,56 @@ public class ConfigurationSupportTest {
ConfigurationSupport support = new ConfigurationSupport();
@Test
- public void testFilterDictionary() {
- Dictionary result = null;
+ public void testEquals() throws Exception {
Dictionary source = new Properties();
- Dictionary expectedResult = new Properties();
+ Dictionary target = new Properties();
source.put("key1", "value1");
- source.put("key2", "value2");
-
- expectedResult.put("key1", "value1");
- expectedResult.put("key2", "value2");
- result = support.filterDictionary(source);
-
- source.put("service.pid", "value3");
- result = support.filterDictionary(source);
-
- Assert.assertEquals(expectedResult, result);
- }
-
- @Test
- public void testConvertStrings() throws Exception {
- String absolutePath = "/somewehre/karaf/etc";
- String home = "/somewehre/karaf";
- String var = "${karaf.home}";
+ source.put("key2", 1);
+ source.put("key3", Boolean.FALSE);
+ source.put("key4", 12L);
- String expectedResult = "${karaf.home}/etc";
+ target.put("key1", "value1");
+ target.put("key2", 1);
+ target.put("key3", Boolean.FALSE);
+ target.put("key4", 12L);
- String result = support.convertStrings(absolutePath, home, var);
- Assert.assertEquals(expectedResult, result);
+ Assert.assertEquals(true, support.equals(source, target));
}
@Test
- public void testPreparePull() throws Exception {
- Dictionary result = null;
+ public void testNotEquals() throws Exception {
Dictionary source = new Properties();
- Dictionary expectedResult = new Properties();
-
+ Dictionary target = new Properties();
+
source.put("key1", "value1");
- source.put("key2", 1);
+ source.put("key2", 2);
source.put("key3", Boolean.FALSE);
source.put("key4", 12L);
- expectedResult.put("key1", "value1");
- expectedResult.put("key2", "1");
- expectedResult.put("key3", "false");
- expectedResult.put("key4", "12");
-
- result = support.preparePull(source);
-
- Assert.assertEquals(expectedResult, result);
+ target.put("key1", "value1");
+ target.put("key2", 1);
+ target.put("key3", Boolean.FALSE);
+ target.put("key4", 12L);
+
+ Assert.assertEquals(false, support.equals(source, target));
}
@Test
- public void testPreparePush() throws Exception {
- Dictionary result = null;
+ public void testNotEqualsDueToSize() throws Exception {
Dictionary source = new Properties();
- Dictionary expectedResult = new Properties();
+ Dictionary target = new Properties();
source.put("key1", "value1");
- source.put("key2", 1);
+ source.put("key2", 2);
source.put("key3", Boolean.FALSE);
- source.put("key4", 12L);
-
- expectedResult.put("key1", "value1");
- expectedResult.put("key2", "1");
- expectedResult.put("key3", "false");
- expectedResult.put("key4", "12");
- result = support.preparePush(source);
+ target.put("key1", "value1");
+ target.put("key2", 1);
+ target.put("key3", Boolean.FALSE);
+ target.put("key4", 12L);
- Assert.assertEquals(expectedResult, result);
+ Assert.assertEquals(false, support.equals(source, target));
}
}