This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/master by this push:
     new 92564f8  [KARAF-4609] Allow var substitution in env variables or sys 
properties
     new aa8e2b5  Merge pull request #1206 from jbonofre/KARAF-4609
92564f8 is described below

commit 92564f87e4faae5c6c02a905c24f8580811415d2
Author: jbonofre <jbono...@apache.org>
AuthorDate: Wed Sep 30 10:36:29 2020 +0200

    [KARAF-4609] Allow var substitution in env variables or sys properties
---
 .../config/core/impl/KarafConfigurationPlugin.java | 28 ++++++++++++++--------
 .../core/impl/KarafConfigurationPluginTest.java    | 14 +++++++++++
 .../main/asciidoc/user-guide/configuration.adoc    | 11 +++++++++
 3 files changed, 43 insertions(+), 10 deletions(-)

diff --git 
a/config/src/main/java/org/apache/karaf/config/core/impl/KarafConfigurationPlugin.java
 
b/config/src/main/java/org/apache/karaf/config/core/impl/KarafConfigurationPlugin.java
index 913bcab..816e5af 100644
--- 
a/config/src/main/java/org/apache/karaf/config/core/impl/KarafConfigurationPlugin.java
+++ 
b/config/src/main/java/org/apache/karaf/config/core/impl/KarafConfigurationPlugin.java
@@ -16,19 +16,15 @@
  */
 package org.apache.karaf.config.core.impl;
 
+import org.apache.felix.utils.properties.InterpolationHelper;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.ConfigurationPlugin;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
-import java.util.Dictionary;
-import java.util.Enumeration;
+import java.util.*;
 
 public class KarafConfigurationPlugin implements ConfigurationPlugin {
 
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(KarafConfigurationPlugin.class);
-
     public static final String PLUGIN_ID = "org.apache.karaf.config.plugin";
     public static final int PLUGIN_RANKING = 500;
 
@@ -41,19 +37,31 @@ public class KarafConfigurationPlugin implements 
ConfigurationPlugin {
             String env = (pid + "." + key).toUpperCase().replaceAll("\\.", 
"_");
             String sys = pid + "." + key;
             if (System.getenv(env) != null) {
+                String value = 
InterpolationHelper.substVars(System.getenv(env), null,null, 
convertDictionaryToMap(properties));
                 if (properties.get(key) != null && (properties.get(key) 
instanceof Number)) {
-                    properties.put(key, Integer.parseInt(System.getenv(env)));
+                    properties.put(key, Integer.parseInt(value));
                 } else {
-                    properties.put(key, System.getenv(env));
+                    properties.put(key, value);
                 }
             } else if (System.getProperty(sys) != null) {
+                String value = 
InterpolationHelper.substVars(System.getProperty(sys), null, null, 
convertDictionaryToMap(properties));
                 if (properties.get(key) != null && (properties.get(key) 
instanceof Number)) {
-                    properties.put(key, 
Integer.parseInt(System.getProperty(sys)));
+                    properties.put(key, Integer.parseInt(value));
                 } else {
-                    properties.put(key, System.getProperty(sys));
+                    properties.put(key, value);
                 }
             }
         }
     }
 
+    private static Map<String, String> 
convertDictionaryToMap(Dictionary<String, Object> dictionary) {
+        Map<String, String> converted = new HashMap<>();
+        Enumeration<String> keys = dictionary.keys();
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement();
+            converted.put(key, dictionary.get(key).toString());
+        }
+        return converted;
+    }
+
 }
diff --git 
a/config/src/test/java/org/apache/karaf/config/core/impl/KarafConfigurationPluginTest.java
 
b/config/src/test/java/org/apache/karaf/config/core/impl/KarafConfigurationPluginTest.java
index 266afea..cd2a138 100644
--- 
a/config/src/test/java/org/apache/karaf/config/core/impl/KarafConfigurationPluginTest.java
+++ 
b/config/src/test/java/org/apache/karaf/config/core/impl/KarafConfigurationPluginTest.java
@@ -39,4 +39,18 @@ public class KarafConfigurationPluginTest {
         Assert.assertEquals("bar", properties.get("foo"));
     }
 
+    @Test
+    public void testAppending() throws Exception {
+        System.setProperty("org.apache.karaf.features.repositories", 
"${repositories},third");
+        KarafConfigurationPlugin plugin = new KarafConfigurationPlugin();
+        Dictionary<String, Object> properties = new Hashtable<>();
+        properties.put(Constants.SERVICE_PID, "org.apache.karaf.features");
+        properties.put("repositories", "first,second");
+        properties.put("foo", "bar");
+        plugin.modifyConfiguration(null, properties);
+
+        Assert.assertEquals("first,second,third", 
properties.get("repositories"));
+        Assert.assertEquals("bar", properties.get("foo"));
+    }
+
 }
diff --git a/manual/src/main/asciidoc/user-guide/configuration.adoc 
b/manual/src/main/asciidoc/user-guide/configuration.adoc
index fe50808..0747fc7 100644
--- a/manual/src/main/asciidoc/user-guide/configuration.adoc
+++ b/manual/src/main/asciidoc/user-guide/configuration.adoc
@@ -38,6 +38,17 @@ export ORG_APACHE_KARAF_SHELL_SSHPORT=8102
 
 You can override any configuration property using this mechanism.
 
+It's possible to "append" a new value based on the content of a configuration 
property.
+For instance, you want to add a new features repository to the default value 
(define the `etc/org.apache.karaf.features.cfg` config file 
`featuresRepositories` property.
+You can use the following env variable:
+
+----
+export 
ORG_APACHE_KARAF_FEATURES_FEATURESREPOSITORIES='${featuresRepositories},mvn:org.apache.karaf.decanter/apache-karaf-decanter/2.5.0/xml/features'
+----
+
+It means that Karaf will take value of `featuresRepositories` property 
contained in the `etc/org.apache.karaf.features.cfg` configuration file and add
+`mvn:org.apache.karaf.decanter/apache-karaf-decanter/2.5.0/xml/features`.
+
 ==== Files
 
 Apache Karaf stores and loads all configuration in files located in the `etc` 
folder.

Reply via email to