Repository: karaf Updated Branches: refs/heads/master 0db3724fa -> e97de8675
KARAF-3100 - <config/> in a feature are now store in a cfg file as we do with config:* commands. close #43 Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/e97de867 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/e97de867 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/e97de867 Branch: refs/heads/master Commit: e97de867515bc402e1dea1a2291255c673c43bd9 Parents: 0db3724 Author: Jean-Baptiste Onofré <[email protected]> Authored: Wed Jan 6 15:27:04 2016 +0100 Committer: Jean-Baptiste Onofré <[email protected]> Committed: Wed Jan 6 15:27:49 2016 +0100 ---------------------------------------------------------------------- .../service/FeatureConfigInstaller.java | 79 ++++++++++++++++++-- .../org/apache/karaf/features/AppendTest.java | 11 ++- .../service/FeatureConfigInstallerTest.java | 3 + 3 files changed, 83 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/e97de867/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java ---------------------------------------------------------------------- diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java index 6f71702..38ce81b 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/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; @@ -46,13 +43,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) { @@ -102,17 +103,27 @@ 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(); for (String key : props.stringPropertyNames()) { if (properties.get(key) == null) { - properties.put(key, props.getProperty(key)); + properties.put(pid[0], props.getProperty(key)); update = true; } } if (update) { cfg.update(properties); + try { + updateStorage(pid[0], props); + } catch (Exception e) { + LOGGER.warn("Can't update cfg file", e); + } } } } @@ -226,4 +237,60 @@ public class FeatureConfigInstaller { throw e; } } + + 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<>(); + 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/e97de867/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 f0fb326..49daded 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 @@ -29,7 +29,9 @@ import org.osgi.service.cm.ConfigurationAdmin; 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/service/f08.xml").toURI()); // Check repo @@ -47,22 +49,23 @@ public class AppendTest extends TestCase { String property = properties.getProperty("javax.servlet.context.tempdir"); assertNotNull(property); assertFalse(property.contains("${")); + assertEquals(property, "data/pax-web-jsp"); 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<>(); - original.put("org.apache.karaf.features.configKey", "org.ops4j.pax.web"); - original.put("foo", "bar"); + original.put("javax.servlet.context.tempdir", "data/pax-web-jsp"); EasyMock.expect(config.getProperties()).andReturn(original); + Hashtable<String, Object> expected = new Hashtable<>(); + expected.put("org.ops4j.pax.web", "data/pax-web-jsp"); 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); EasyMock.verify(admin, config); http://git-wip-us.apache.org/repos/asf/karaf/blob/e97de867/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java ---------------------------------------------------------------------- diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java index bbcee8b..c9497fc 100644 --- a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java +++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeatureConfigInstallerTest.java @@ -31,13 +31,16 @@ public class FeatureConfigInstallerTest { @Test public void testSubstFinalName() { final String karafBase = "/tmp/karaf.base"; + final String karafEtc = karafBase + "/etc"; final String foo = "/foo"; System.setProperty("karaf.base", karafBase); + System.setProperty("karaf.etc", karafEtc); System.setProperty("foo", foo); substEqual("etc/test.cfg", karafBase + File.separator + "etc/test.cfg"); substEqual("/etc/test.cfg", karafBase + File.separator + "/etc/test.cfg"); + substEqual("${karaf.etc}/test.cfg", karafEtc + "/test.cfg"); substEqual("${karaf.base}/etc/test.cfg", karafBase + "/etc/test.cfg"); substEqual("etc/${foo}/test.cfg", karafBase + File.separator + "etc/" + foo + "/test.cfg"); substEqual("${foo}/test.cfg", foo + "/test.cfg");
