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/16 09:30:15 UTC

svn commit: r1339030 - in /karaf/cellar/branches/cellar-2.2.x: ./ config/ config/src/main/java/org/apache/karaf/cellar/config/ config/src/main/resources/OSGI-INF/blueprint/ features/src/main/java/org/apache/karaf/cellar/features/

Author: jbonofre
Date: Wed May 16 07:30:15 2012
New Revision: 1339030

URL: http://svn.apache.org/viewvc?rev=1339030&view=rev
Log:
[KARAF-1480] Add creation of the cfg file

Modified:
    karaf/cellar/branches/cellar-2.2.x/config/pom.xml
    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
    karaf/cellar/branches/cellar-2.2.x/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml
    karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java
    karaf/cellar/branches/cellar-2.2.x/pom.xml

Modified: karaf/cellar/branches/cellar-2.2.x/config/pom.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/config/pom.xml?rev=1339030&r1=1339029&r2=1339030&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/config/pom.xml (original)
+++ karaf/cellar/branches/cellar-2.2.x/config/pom.xml Wed May 16 07:30:15 2012
@@ -49,6 +49,10 @@
         <osgi.export>
             org.apache.karaf.cellar.config*;version="${project.version}"
         </osgi.export>
+        <osgi.private>
+            org.apache.karaf.util;-split-package:=merge-first,
+            !*
+        </osgi.private>
     </properties>
 
     <dependencies>
@@ -57,6 +61,10 @@
             <groupId>org.apache.karaf.cellar</groupId>
             <artifactId>org.apache.karaf.cellar.core</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf</groupId>
+            <artifactId>org.apache.karaf.util</artifactId>
+        </dependency>
 
         <!-- Configuration Admin -->
         <dependency>

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=1339030&r1=1339029&r2=1339030&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 Wed May 16 07:30:15 2012
@@ -71,7 +71,10 @@ public class ConfigurationEventHandler e
                 conf = configurationAdmin.getConfiguration(pid);
                 if (conf != null) {
                     if (event.getType() == ConfigurationEvent.CM_DELETED) {
-                        conf.delete();
+                        if (conf.getProperties() != null) {
+                            conf.delete();
+                            deleteStorage(pid);
+                        }
                     } else {
                         if (remoteDictionary != null) {
                             remoteDictionary.put(Constants.SYNC_PROPERTY, new Long(System.currentTimeMillis()).toString());
@@ -80,6 +83,7 @@ public class ConfigurationEventHandler e
                                 localDictionary = new Properties();
                             filter(remoteDictionary, localDictionary);
                             conf.update(localDictionary);
+                            persistConfiguration(configurationAdmin, pid, localDictionary);
                         }
                     }
                 }

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=1339030&r1=1339029&r2=1339030&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 Wed May 16 07:30:15 2012
@@ -14,17 +14,25 @@
 package org.apache.karaf.cellar.config;
 
 import org.apache.karaf.cellar.core.CellarSupport;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
 
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.Properties;
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URL;
+import java.util.*;
 
 /**
  * Configuration support.
  */
 public class ConfigurationSupport extends CellarSupport {
 
-    private static String[] EXCLUDED_PROPERTIES = { "felix.fileinstall.filename", "felix.fileinstall.dir", "felix.fileinstall.tmpdir", "org.ops4j.pax.url.mvn.defaultRepositories" };
+    private static String[] EXCLUDED_PROPERTIES = {"felix.fileinstall.filename", "felix.fileinstall.dir", "felix.fileinstall.tmpdir", "org.ops4j.pax.url.mvn.defaultRepositories"};
+
+    private static final String FELIX_FILEINSTALL_FILENAME = "felix.fileinstall.filename";
+
+    protected File storage;
 
     /**
      * Filter a dictionary, and populate a target dictionary.
@@ -59,4 +67,55 @@ public class ConfigurationSupport extend
         return false;
     }
 
+    /**
+     * Persist a configuration to a storage.
+     *
+     * @param pid
+     * @throws Exception
+     */
+    protected void persistConfiguration(ConfigurationAdmin admin, String pid, Dictionary props) {
+        try {
+            if (pid.startsWith("org.apache.felix.fileinstall")) {
+                return;
+            }
+            File storageFile = new File(storage, pid + ".cfg");
+            Configuration cfg = admin.getConfiguration(pid, null);
+            if (cfg != null && cfg.getProperties() != null) {
+                Object val = cfg.getProperties().get(FELIX_FILEINSTALL_FILENAME);
+                if (val instanceof String) {
+                    if (((String) val).startsWith("file:")) {
+                        val = ((String) val).substring("file:".length());
+                    }
+                    storageFile = new File((String) val);
+                }
+            }
+            org.apache.karaf.util.Properties p = new org.apache.karaf.util.Properties(storageFile);
+            for (Enumeration keys = props.keys(); keys.hasMoreElements(); ) {
+                Object key = keys.nextElement();
+                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));
+                }
+            }
+            storage.mkdirs();
+            p.save();
+        } catch (Exception e) {
+            // nothing to do
+        }
+    }
+
+    protected void deleteStorage(String pid) {
+        File cfgFile = new File(storage, pid + ".cfg");
+        cfgFile.delete();
+    }
+
+    public File getStorage() {
+        return storage;
+    }
+
+    public void setStorage(File storage) {
+        this.storage = storage;
+    }
+
 }

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=1339030&r1=1339029&r2=1339030&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 Wed May 16 07:30:15 2012
@@ -94,6 +94,7 @@ public class ConfigurationSynchronizer e
                                 localDictionary = new Properties();
                             filter(remoteDictionary, localDictionary);
                             conf.update(localDictionary);
+                            persistConfiguration(configurationAdmin, pid, localDictionary);
                         } catch (IOException ex) {
                             LOGGER.error("CELLAR CONFIG: failed to read from the distributed map", ex);
                         }

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=1339030&r1=1339029&r2=1339030&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 Wed May 16 07:30:15 2012
@@ -91,7 +91,13 @@ public class LocalConfigurationListener 
 
                     try {
                         if (event.getType() == ConfigurationEvent.CM_DELETED) {
+                            // update the distributed map
                             configurationTable.remove(pid);
+                            // broadcast the cluster event
+                            RemoteConfigurationEvent remoteConfigurationEvent = new RemoteConfigurationEvent(pid);
+                            remoteConfigurationEvent.setType(ConfigurationEvent.CM_DELETED);
+                            remoteConfigurationEvent.setSourceGroup(group);
+                            eventProducer.produce(remoteConfigurationEvent);
                         } else {
                             Properties localProperties = new Properties();
                             filter(localDictionary, localProperties);

Modified: karaf/cellar/branches/cellar-2.2.x/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml?rev=1339030&r1=1339029&r2=1339030&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml (original)
+++ karaf/cellar/branches/cellar-2.2.x/config/src/main/resources/OSGI-INF/blueprint/blueprint.xml Wed May 16 07:30:15 2012
@@ -13,7 +13,9 @@
    See the License for the specific language governing permissions and
    limitations under the License.
   -->
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
+           xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0">
 
     <!-- Local Configuration Listener -->
     <bean id="configurationListener" class="org.apache.karaf.cellar.config.LocalConfigurationListener" init-method="init"
@@ -32,6 +34,7 @@
         <property name="groupManager" ref="groupManager"/>
         <property name="clusterManager" ref="clusterManager"/>
         <property name="eventProducer" ref="eventProducer"/>
+        <property name="storage" value="${storage}"/>
     </bean>
     <service ref="synchronizer" interface="org.apache.karaf.cellar.core.Synchronizer"/>
 
@@ -41,6 +44,7 @@
         <property name="clusterManager" ref="clusterManager"/>
         <property name="groupManager" ref="groupManager"/>
         <property name="configurationAdmin" ref="configurationAdmin"/>
+        <property name="storage" value="${storage}"/>
     </bean>
     <service ref="eventHandler" interface="org.apache.karaf.cellar.core.event.EventHandler">
         <service-properties>
@@ -54,4 +58,12 @@
     <reference id="configurationAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"/>
     <reference id="eventProducer" interface="org.apache.karaf.cellar.core.event.EventProducer"/>
 
+    <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
+
+    <cm:property-placeholder persistent-id="org.apache.karaf.shell.config">
+        <cm:default-properties>
+            <cm:property name="storage" value="$[karaf.base]/etc/"/>
+        </cm:default-properties>
+    </cm:property-placeholder>
+
 </blueprint>

Modified: karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java?rev=1339030&r1=1339029&r2=1339030&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java Wed May 16 07:30:15 2012
@@ -104,56 +104,62 @@ public class LocalFeaturesListener exten
             return;
         }
 
-        if (event != null && event.getRepository() != null) {
-            Set<Group> groups = groupManager.listLocalGroups();
-
-            if (groups != null && !groups.isEmpty()) {
-                for (Group group : groups) {
-                    RemoteRepositoryEvent repositoryEvent = new RemoteRepositoryEvent(event.getRepository().getURI().toString(), event.getType());
-                    repositoryEvent.setSourceGroup(group);
-                    RepositoryEvent.EventType type = event.getType();
-
-                    // update the distributed map
-                    if (RepositoryEvent.EventType.RepositoryAdded.equals(type)){
-                        pushRepository(event.getRepository(), group);
-                        // update the feature map
-                        Map<FeatureInfo, Boolean> distributedFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + group.getName());
-                        try {
-                            for (Feature feature : event.getRepository().getFeatures()) {
-                                // check the feature in the distributed map
-                                FeatureInfo featureInfo = null;
-                                for (FeatureInfo distributedFeature : distributedFeatures.keySet()) {
-                                    if (distributedFeature.getName().equals(feature.getName()) && distributedFeature.getVersion().equals(feature.getVersion())) {
-                                        featureInfo = distributedFeature;
-                                        break;
+        ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+        try {
+            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+            if (event != null && event.getRepository() != null) {
+                Set<Group> groups = groupManager.listLocalGroups();
+
+                if (groups != null && !groups.isEmpty()) {
+                    for (Group group : groups) {
+                        RemoteRepositoryEvent repositoryEvent = new RemoteRepositoryEvent(event.getRepository().getURI().toString(), event.getType());
+                        repositoryEvent.setSourceGroup(group);
+                        RepositoryEvent.EventType type = event.getType();
+
+                        // update the distributed map
+                        if (RepositoryEvent.EventType.RepositoryAdded.equals(type)) {
+                            pushRepository(event.getRepository(), group);
+                            // update the feature map
+                            Map<FeatureInfo, Boolean> distributedFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + group.getName());
+                            try {
+                                for (Feature feature : event.getRepository().getFeatures()) {
+                                    // check the feature in the distributed map
+                                    FeatureInfo featureInfo = null;
+                                    for (FeatureInfo distributedFeature : distributedFeatures.keySet()) {
+                                        if (distributedFeature.getName().equals(feature.getName()) && distributedFeature.getVersion().equals(feature.getVersion())) {
+                                            featureInfo = distributedFeature;
+                                            break;
+                                        }
+                                    }
+                                    if (featureInfo == null) {
+                                        featureInfo = new FeatureInfo(feature.getName(), feature.getVersion());
+                                        distributedFeatures.put(featureInfo, false);
                                     }
                                 }
-                                if (featureInfo == null) {
-                                    featureInfo = new FeatureInfo(feature.getName(), feature.getVersion());
-                                    distributedFeatures.put(featureInfo, false);
-                                }
+                            } catch (Exception e) {
+                                LOGGER.warn("CELLAR FEATURES: can't update the distributed features map", e);
                             }
-                        } catch (Exception e) {
-                            LOGGER.warn("CELLAR FEATURES: can't update the distributed features map", e);
-                        }
-                    } else {
-                        removeRepository(event.getRepository(), group);
-                        // update the feature map
-                        Map<FeatureInfo, Boolean> distributedFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + group.getName());
-                        try {
-                            for (Feature feature : event.getRepository().getFeatures()) {
-                                FeatureInfo info = new FeatureInfo(feature.getName(), feature.getVersion());
-                                distributedFeatures.remove(info);
+                        } else {
+                            removeRepository(event.getRepository(), group);
+                            // update the feature map
+                            Map<FeatureInfo, Boolean> distributedFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + group.getName());
+                            try {
+                                for (Feature feature : event.getRepository().getFeatures()) {
+                                    FeatureInfo info = new FeatureInfo(feature.getName(), feature.getVersion());
+                                    distributedFeatures.remove(info);
+                                }
+                            } catch (Exception e) {
+                                LOGGER.warn("CELLAR FEATURES: can't update the distributed features map", e);
                             }
-                        } catch (Exception e) {
-                            LOGGER.warn("CELLAR FEATURES: can't update the distributed features map", e);
                         }
-                    }
 
-                    // broadcast the cluster event
-                    eventProducer.produce(repositoryEvent);
+                        // broadcast the cluster event
+                        eventProducer.produce(repositoryEvent);
+                    }
                 }
             }
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
         }
     }
 

Modified: karaf/cellar/branches/cellar-2.2.x/pom.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/pom.xml?rev=1339030&r1=1339029&r2=1339030&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/pom.xml (original)
+++ karaf/cellar/branches/cellar-2.2.x/pom.xml Wed May 16 07:30:15 2012
@@ -218,6 +218,11 @@
                 <artifactId>org.apache.karaf.shell.console</artifactId>
                 <version>${karaf.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.apache.karaf</groupId>
+                <artifactId>org.apache.karaf.util</artifactId>
+                <version>${karaf.version}</version>
+            </dependency>
 
             <dependency>
                 <groupId>org.apache.karaf.tooling</groupId>