karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject karaf git commit: KARAF-3100 - <config/> in a feature are now store in a cfg file as we do with config:* commands
Date Wed, 06 Jan 2016 15:37:42 GMT
Repository: karaf
Updated Branches:
  refs/heads/karaf-3.0.x 48c791585 -> e9b92ac92


KARAF-3100 - <config/> in a feature are now store in a cfg file as we do with config:*
commands


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/e9b92ac9
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/e9b92ac9
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/e9b92ac9

Branch: refs/heads/karaf-3.0.x
Commit: e9b92ac92baee96a11abb710dc9f3254ed13bf0c
Parents: 48c7915
Author: Jean-Baptiste Onofré <jbonofre@apache.org>
Authored: Wed Jan 6 16:36:43 2016 +0100
Committer: Jean-Baptiste Onofré <jbonofre@apache.org>
Committed: Wed Jan 6 16:36:43 2016 +0100

----------------------------------------------------------------------
 features/core/pom.xml                           |  1 +
 .../internal/FeatureConfigInstaller.java        | 76 +++++++++++++++++--
 .../org/apache/karaf/features/AppendTest.java   | 78 ++++++++++----------
 3 files changed, 112 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/e9b92ac9/features/core/pom.xml
----------------------------------------------------------------------
diff --git a/features/core/pom.xml b/features/core/pom.xml
index 57044b7..762f1ea 100644
--- a/features/core/pom.xml
+++ b/features/core/pom.xml
@@ -120,6 +120,7 @@
                             org.apache.karaf.features.management.internal,
                             org.apache.felix.utils.version,
                             org.apache.felix.utils.manifest,
+                            org.apache.felix.utils.properties,
                             org.apache.karaf.util.collections
                         </Private-Package>
                     </instructions>

http://git-wip-us.apache.org/repos/asf/karaf/blob/e9b92ac9/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java
b/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java
index 155a756..ca6a7dc 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/FeatureConfigInstaller.java
@@ -23,12 +23,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
 import java.net.MalformedURLException;
+import java.net.URI;
 import java.net.URL;
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.List;
-import java.util.Properties;
+import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -43,13 +40,17 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class FeatureConfigInstaller {
+
 	private static final Logger LOGGER = LoggerFactory.getLogger(FeaturesServiceImpl.class);
     private static final String CONFIG_KEY = "org.apache.karaf.features.configKey";
+    private static final String FILEINSTALL_FILE_NAME = "felix.fileinstall.filename";
 
     private final ConfigurationAdmin configAdmin;
+    private File storage;
     
     public FeatureConfigInstaller(ConfigurationAdmin configAdmin) {
 		this.configAdmin = configAdmin;
+        this.storage = new File(System.getProperty("karaf.etc"));
 	}
 
     private String[] parsePid(String pid) {
@@ -103,6 +104,11 @@ public class FeatureConfigInstaller {
 				String key = createConfigurationKey(pid[0], pid[1]);
 				cfgProps.put(CONFIG_KEY, key);
 				cfg.update(cfgProps);
+                try {
+                    updateStorage(pid[0], props);
+                } catch (Exception e) {
+                    LOGGER.warn("Can't update cfg file", e);
+                }
 			} else if (config.isAppend()) {
                 boolean update = false;
 				Dictionary<String,Object> properties = cfg.getProperties();
@@ -114,6 +120,11 @@ public class FeatureConfigInstaller {
                 }
                 if (update) {
                     cfg.update(properties);
+                    try {
+                        updateStorage(pid[0], props);
+                    } catch (Exception e) {
+                        LOGGER.warn("Can't update cfg file", e);
+                    }
                 }
 			}
 		}
@@ -195,5 +206,60 @@ public class FeatureConfigInstaller {
 		}
             
     }
+
+    protected void updateStorage(String pid, Dictionary props) throws IOException {
+        if (storage != null) {
+            // get the cfg file
+            File cfgFile = new File(storage, pid + ".cfg");
+            Configuration cfg = configAdmin.getConfiguration(pid, null);
+            // update the cfg file depending of the configuration
+            if (cfg != null && cfg.getProperties() != null) {
+                Object val = cfg.getProperties().get(FILEINSTALL_FILE_NAME);
+                try {
+                    if (val instanceof URL) {
+                        cfgFile = new File(((URL) val).toURI());
+                    }
+                    if (val instanceof URI) {
+                        cfgFile = new File((URI) val);
+                    }
+                    if (val instanceof String) {
+                        cfgFile = new File(new URL((String) val).toURI());
+                    }
+                } catch (Exception e) {
+                    throw (IOException) new IOException(e.getMessage()).initCause(e);
+                }
+            }
+            LOGGER.trace("Update {}", cfgFile.getName());
+            // update the cfg file
+            org.apache.felix.utils.properties.Properties properties = new org.apache.felix.utils.properties.Properties(cfgFile);
+            for (Enumeration<String> keys = props.keys(); keys.hasMoreElements(); )
{
+                String key = keys.nextElement();
+                if (!Constants.SERVICE_PID.equals(key)
+                        && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key)
+                        && !FILEINSTALL_FILE_NAME.equals(key)) {
+                    if (props.get(key) != null) {
+                        properties.put(key, props.get(key).toString());
+                    }
+                }
+            }
+            // remove "removed" properties from the cfg file
+            ArrayList<String> propertiesToRemove = new ArrayList<String>();
+            for (String key : properties.keySet()) {
+                if (props.get(key) == null
+                        && !Constants.SERVICE_PID.equals(key)
+                        && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key)
+                        && !FILEINSTALL_FILE_NAME.equals(key)) {
+                    propertiesToRemove.add(key);
+                }
+            }
+            for (String key : propertiesToRemove) {
+                properties.remove(key);
+            }
+
+            // save the cfg file
+            storage.mkdirs();
+            properties.save();
+        }
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/e9b92ac9/features/core/src/test/java/org/apache/karaf/features/AppendTest.java
----------------------------------------------------------------------
diff --git a/features/core/src/test/java/org/apache/karaf/features/AppendTest.java b/features/core/src/test/java/org/apache/karaf/features/AppendTest.java
index c4e4fb1..0257d53 100644
--- a/features/core/src/test/java/org/apache/karaf/features/AppendTest.java
+++ b/features/core/src/test/java/org/apache/karaf/features/AppendTest.java
@@ -32,6 +32,7 @@ public class AppendTest extends TestCase {
 
     public void testLoad() throws Exception {
 		System.setProperty("karaf.data", "data");
+        System.setProperty("karaf.etc", "etc");
 
         RepositoryImpl r = new RepositoryImpl(getClass().getResource("internal/f07.xml").toURI());
         // Check repo
@@ -40,46 +41,47 @@ public class AppendTest extends TestCase {
         assertEquals(1, features.length);
         Feature feature = features[0];
 
-		ConfigInfo configInfo = feature.getConfigurations().get(0);
-		assertNotNull(configInfo);
-		assertTrue(configInfo.isAppend());
-		
-		Properties properties = configInfo.getProperties();
-		assertNotNull(properties);
-		String property = properties.getProperty("javax.servlet.context.tempdir");
-		assertNotNull(property);
-		assertFalse(property.contains("${"));
+        ConfigInfo configInfo = feature.getConfigurations().get(0);
+        assertNotNull(configInfo);
+        assertTrue(configInfo.isAppend());
 
-		ConfigurationAdmin admin = EasyMock.createMock(ConfigurationAdmin.class);
-		Configuration config = EasyMock.createMock(Configuration.class);
-		EasyMock.expect(admin.listConfigurations(EasyMock.eq("(service.pid=org.ops4j.pax.web)")))
-				.andReturn(new Configuration[] { config });
-		Hashtable<String, Object> original = new Hashtable<String, Object>();
-		original.put("org.apache.karaf.features.configKey", "org.ops4j.pax.web");
-		original.put("foo", "bar");
-		EasyMock.expect(config.getProperties()).andReturn(original);
-		Hashtable<String, Object> expected = new Hashtable<String, Object>();
-		expected.put("org.apache.karaf.features.configKey", "org.ops4j.pax.web");
-		expected.put("javax.servlet.context.tempdir", "data/pax-web-jsp");
-		expected.put("foo", "bar");
-		config.update(EasyMock.eq(expected));
-		EasyMock.expectLastCall();
-		EasyMock.replay(admin, config);
-		FeatureConfigInstaller installer = new FeatureConfigInstaller(admin);
-		installer.installFeatureConfigs(feature, false);
-		EasyMock.verify(admin, config);
+        Properties properties = configInfo.getProperties();
+        assertNotNull(properties);
+        String property = properties.getProperty("javax.servlet.context.tempdir");
+        assertNotNull(property);
+        assertFalse(property.contains("${"));
+        assertEquals(property, "data/pax-web-jsp");
 
-		EasyMock.reset(admin, config);
-		EasyMock.expect(admin.listConfigurations(EasyMock.eq("(service.pid=org.ops4j.pax.web)")))
-				.andReturn(new Configuration[]{config});
-		original = new Hashtable<String, Object>();
-		original.put("org.apache.karaf.features.configKey", "org.ops4j.pax.web");
-		original.put("javax.servlet.context.tempdir", "value");
-		original.put("foo", "bar");
-		EasyMock.expect(config.getProperties()).andReturn(original);
-		EasyMock.replay(admin, config);
-		installer.installFeatureConfigs(feature, false);
-		EasyMock.verify(admin, config);
+        ConfigurationAdmin admin = EasyMock.createMock(ConfigurationAdmin.class);
+        Configuration config = EasyMock.createMock(Configuration.class);
+        EasyMock.expect(admin.listConfigurations(EasyMock.eq("(service.pid=org.ops4j.pax.web)")))
+                .andReturn(new Configuration[] { config });
+        Hashtable<String, Object> original = new Hashtable<String, Object>();
+        original.put("javax.servlet.context.tempdir", "data/pax-web-jsp");
+        EasyMock.expect(config.getProperties()).andReturn(original);
+
+        Hashtable<String, Object> expected = new Hashtable<String, Object>();
+        expected.put("org.ops4j.pax.web", "data/pax-web-jsp");
+        expected.put("org.apache.karaf.features.configKey", "org.ops4j.pax.web");
+        expected.put("foo", "bar");
+        EasyMock.expectLastCall();
+        EasyMock.replay(admin, config);
+
+        FeatureConfigInstaller installer = new FeatureConfigInstaller(admin);
+        installer.installFeatureConfigs(feature, false);
+        EasyMock.verify(admin, config);
+
+        EasyMock.reset(admin, config);
+        EasyMock.expect(admin.listConfigurations(EasyMock.eq("(service.pid=org.ops4j.pax.web)")))
+                .andReturn(new Configuration[]{config});
+        original = new Hashtable<String, Object>();
+        original.put("org.apache.karaf.features.configKey", "org.ops4j.pax.web");
+        original.put("javax.servlet.context.tempdir", "value");
+        original.put("foo", "bar");
+        EasyMock.expect(config.getProperties()).andReturn(original);
+        EasyMock.replay(admin, config);
+        installer.installFeatureConfigs(feature, false);
+        EasyMock.verify(admin, config);
 	}
 
 }


Mime
View raw message