karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject svn commit: r1405326 - in /karaf/cellar/trunk: assembly/src/main/resources/groups.cfg hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java
Date Sat, 03 Nov 2012 12:44:16 GMT
Author: jbonofre
Date: Sat Nov  3 12:44:15 2012
New Revision: 1405326

URL: http://svn.apache.org/viewvc?rev=1405326&view=rev
Log:
[KARAF-1466] Persist group in configuration admin

Modified:
    karaf/cellar/trunk/assembly/src/main/resources/groups.cfg
    karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java

Modified: karaf/cellar/trunk/assembly/src/main/resources/groups.cfg
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/assembly/src/main/resources/groups.cfg?rev=1405326&r1=1405325&r2=1405326&view=diff
==============================================================================
--- karaf/cellar/trunk/assembly/src/main/resources/groups.cfg (original)
+++ karaf/cellar/trunk/assembly/src/main/resources/groups.cfg Sat Nov  3 12:44:15 2012
@@ -1,3 +1,5 @@
+groups = default
+
 default.config.whitelist.inbound=*
 default.config.whitelist.outbound=*
 default.config.blacklist.inbound=org.apache.karaf.cellar.groups, \

Modified: karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java?rev=1405326&r1=1405325&r2=1405326&view=diff
==============================================================================
--- karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java
(original)
+++ karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java
Sat Nov  3 12:44:15 2012
@@ -14,15 +14,8 @@
 package org.apache.karaf.cellar.hazelcast;
 
 import java.io.IOException;
-import java.util.Collection;
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
+import java.util.*;
+
 import com.hazelcast.core.Cluster;
 import com.hazelcast.core.EntryEvent;
 import com.hazelcast.core.EntryListener;
@@ -78,25 +71,44 @@ public class HazelcastGroupManager imple
     public void init() {
         //Create a listener for group configuration.
         IMap groupConfiguration = instance.getMap(GROUPS_CONFIG);
-        groupConfiguration.addEntryListener(this,true);
-        //Add group to configuration
+        groupConfiguration.addEntryListener(this, true);
         try {
+            // create group stored in configuration admin
+            Configuration configuration = configurationAdmin.getConfiguration(Configurations.GROUP);
+            if (configuration != null) {
+                Dictionary<String, String> properties = configuration.getProperties();
+                if (properties == null) {
+                    properties = new Hashtable<String, String>();
+                }
+                String groups = properties.get(Configurations.GROUPS_KEY);
+                Set<String> groupNames = convertStringToSet(groups);
+                if (groupNames != null && !groupNames.isEmpty()) {
+                    for (String groupName : groupNames) {
+                        createGroup(groupName);
+                    }
+                }
+            }
+        } catch (IOException e) {
+            LOGGER.warn("CELLAR HAZELCAST: can't create group from configuration admin",
e);
+        }
+        try {
+            // add group membership from configuration
             Configuration configuration = configurationAdmin.getConfiguration(Configurations.NODE);
             if (configuration != null) {
                 Dictionary<String, String> properties = configuration.getProperties();
-                if (properties != null) {
-                    String groups = properties.get(Configurations.GROUPS_KEY);
-                    Set<String> groupNames = convertStringToSet(groups);
-                    if (groupNames != null && !groupNames.isEmpty()) {
-                        for (String groupName : groupNames) {
-                            registerGroup(groupName);
-                        }
-                        return;
+                if (properties == null) {
+                    properties = new Hashtable<String, String>();
+                }
+                String groups = properties.get(Configurations.GROUPS_KEY);
+                Set<String> groupNames = convertStringToSet(groups);
+                if (groupNames != null && !groupNames.isEmpty()) {
+                    for (String groupName : groupNames) {
+                        registerGroup(groupName);
                     }
                 }
             }
         } catch (IOException e) {
-            LOGGER.error("Error reading group configuration");
+            LOGGER.error("CELLAR HAZELCAST: can't set group membership for the current node",
e);
         }
         registerGroup(DEFAULT_GROUP);
     }
@@ -111,7 +123,7 @@ public class HazelcastGroupManager imple
             listGroups().put(groupName, group);
         }
         // shutdown the group consumer/producers
-        for(Map.Entry<String,EventConsumer> consumerEntry:groupConsumer.entrySet())
{
+        for (Map.Entry<String, EventConsumer> consumerEntry : groupConsumer.entrySet())
{
             EventConsumer consumer = consumerEntry.getValue();
             consumer.stop();
         }
@@ -139,6 +151,12 @@ public class HazelcastGroupManager imple
         if (!listGroups().containsKey(groupName)) {
             copyGroupConfiguration(Configurations.DEFAULT_GROUP_NAME, groupName);
             listGroups().put(groupName, group);
+            try {
+                // store the group list to configuration admin
+                persist(listGroups());
+            } catch (Exception e) {
+                LOGGER.warn("CELLAR HAZELCAST: can't store group list", e);
+            }
         }
         return group;
     }
@@ -150,12 +168,36 @@ public class HazelcastGroupManager imple
             Thread.currentThread().setContextClassLoader(combinedClassLoader);
             if (!groupName.equals(Configurations.DEFAULT_GROUP_NAME)) {
                 listGroups().remove(groupName);
+                try {
+                    // store the group list to configuration admin
+                    persist(listGroups());
+                } catch (Exception e) {
+                    LOGGER.warn("CELLAR HAZELCAST: can't store group list", e);
+                }
             }
         } finally {
             Thread.currentThread().setContextClassLoader(originalClassLoader);
         }
     }
 
+    /**
+     * Store the group names in configuration admin.
+     *
+     * @param groups the list of group to store.
+     * @throws Exception in case of storage failure.
+     */
+    private void persist(Map<String, Group> groups) throws Exception {
+        Configuration configuration = configurationAdmin.getConfiguration(Configurations.GROUP);
+        if (configuration != null) {
+            Dictionary<String, String> properties = configuration.getProperties();
+            if (properties == null) {
+                properties = new Hashtable<String, String>();
+            }
+            properties.put(Configurations.GROUPS_KEY, convertSetToString(groups.keySet()));
+            configuration.update(properties);
+        }
+    }
+
     @Override
     public Set<Group> listLocalGroups() {
         ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
@@ -274,16 +316,16 @@ public class HazelcastGroupManager imple
         String groupName = group.getName();
         createGroup(groupName);
 
-        LOGGER.info("Registering group {}.",groupName);
+        LOGGER.info("Registering group {}.", groupName);
         Properties serviceProperties = new Properties();
         serviceProperties.put("type", "group");
         serviceProperties.put("name", groupName);
 
         if (!producerRegistrations.containsKey(groupName)) {
             EventProducer producer = groupProducers.get(groupName);
-            if(producer == null) {
-                producer = eventTransportFactory.getEventProducer(groupName,Boolean.TRUE);
-                groupProducers.put(groupName,producer);
+            if (producer == null) {
+                producer = eventTransportFactory.getEventProducer(groupName, Boolean.TRUE);
+                groupProducers.put(groupName, producer);
             }
 
             ServiceRegistration producerRegistration = bundleContext.registerService(EventProducer.class.getCanonicalName(),
producer, (Dictionary) serviceProperties);
@@ -292,10 +334,10 @@ public class HazelcastGroupManager imple
 
         if (!consumerRegistrations.containsKey(groupName)) {
             EventConsumer consumer = groupConsumer.get(groupName);
-            if(consumer == null) {
-                consumer = eventTransportFactory.getEventConsumer(groupName,true);
-                groupConsumer.put(groupName,consumer);
-            } else if(!consumer.isConsuming()) {
+            if (consumer == null) {
+                consumer = eventTransportFactory.getEventConsumer(groupName, true);
+                groupConsumer.put(groupName, consumer);
+            } else if (!consumer.isConsuming()) {
                 consumer.start();
             }
             ServiceRegistration consumerRegistration = bundleContext.registerService(EventConsumer.class.getCanonicalName(),
consumer, (Dictionary) serviceProperties);
@@ -316,10 +358,10 @@ public class HazelcastGroupManager imple
                         groups = groupName;
                     } else {
 
-                        Set<String> groupNamesSet = convertStringToSet(groups);
-                        groupNamesSet.add(groupName);
-                        groups = convertSetToString(groupNamesSet);
-                    }
+                Set<String> groupNamesSet = convertStringToSet(groups);
+                groupNamesSet.add(groupName);
+                groups = convertSetToString(groupNamesSet);
+            }
 
                     if (groups == null || groups.isEmpty()) {
                         groups = groupName;
@@ -392,7 +434,7 @@ public class HazelcastGroupManager imple
         //4. Remove Consumers & Producers
         groupProducers.remove(groupName);
         EventConsumer consumer = groupConsumer.remove(groupName);
-        if(consumer != null) {
+        if (consumer != null) {
             consumer.stop();
         }
 
@@ -423,6 +465,7 @@ public class HazelcastGroupManager imple
      * Copies the configuration of a {@link Group}.
      * <b>1.</b> Updates configuration admin from Hazelcast using source config.
      * <b>2.</b> Creates target configuration both on Hazelcast and configuration
admin.
+     *
      * @param sourceGroupName
      * @param targetGroupName
      */
@@ -433,19 +476,17 @@ public class HazelcastGroupManager imple
 
                 //Get configuration from config admin.
                 Dictionary configAdminProperties = conf.getProperties();
-                if(configAdminProperties == null) {
+                if (configAdminProperties == null) {
                     configAdminProperties = new Properties();
                 }
                 //Get configuration from Hazelcast
-                Map<String,String> sourceGropConfig = instance.getMap(GROUPS_CONFIG);
+                Map<String, String> sourceGropConfig = instance.getMap(GROUPS_CONFIG);
 
                 //Update local configuration from cluster.
-                for(Map.Entry<String,String> parentEntry:sourceGropConfig.entrySet())
{
-                    configAdminProperties.put(parentEntry.getKey(),parentEntry.getValue());
+                for (Map.Entry<String, String> parentEntry : sourceGropConfig.entrySet())
{
+                    configAdminProperties.put(parentEntry.getKey(), parentEntry.getValue());
                 }
 
-
-
                 Dictionary updatedProperties = new Properties();
                 Enumeration keyEnumeration = configAdminProperties.keys();
                 while (keyEnumeration.hasMoreElements()) {
@@ -511,8 +552,7 @@ public class HazelcastGroupManager imple
     @Override
     public void configurationEvent(ConfigurationEvent configurationEvent) {
         String pid = configurationEvent.getPid();
-        if(pid.equals(GROUPS)) {
-            LOGGER.info("Local group configuration has been updated, updating distributed
group configuration");
+        if (pid.equals(GROUPS)) {
             Map groupConfiguration = instance.getMap(GROUPS_CONFIG);
 
             try {
@@ -520,11 +560,11 @@ public class HazelcastGroupManager imple
                 Dictionary properties = conf.getProperties();
                 Enumeration keyEnumeration = properties.keys();
                 while (keyEnumeration.hasMoreElements()) {
-                   Object key = keyEnumeration.nextElement();
-                   Object value = properties.get(key);
-                   if(!groupConfiguration.containsKey(key) || groupConfiguration.get(key)
== null || !groupConfiguration.get(key).equals(value)) {
-                      groupConfiguration.put(key,value);
-                   }
+                    Object key = keyEnumeration.nextElement();
+                    Object value = properties.get(key);
+                    if (!groupConfiguration.containsKey(key) || groupConfiguration.get(key)
== null || !groupConfiguration.get(key).equals(value)) {
+                        groupConfiguration.put(key, value);
+                    }
                 }
             } catch (Exception e) {
                 LOGGER.warn("Failed to update group configuration");
@@ -539,7 +579,7 @@ public class HazelcastGroupManager imple
      */
     @Override
     public void entryAdded(EntryEvent entryEvent) {
-         entryUpdated(entryEvent);
+        entryUpdated(entryEvent);
     }
 
     /**
@@ -549,7 +589,7 @@ public class HazelcastGroupManager imple
      */
     @Override
     public void entryRemoved(EntryEvent entryEvent) {
-         entryUpdated(entryEvent);
+        entryUpdated(entryEvent);
     }
 
     /**
@@ -570,7 +610,7 @@ public class HazelcastGroupManager imple
                 conf.update(props);
             }
         } catch (Exception ex) {
-          LOGGER.warn("Error while updating local group configuration", ex);
+            LOGGER.warn("Error while updating local group configuration", ex);
         }
     }
 



Mime
View raw message