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-winegrower.git
The following commit(s) were added to refs/heads/master by this push: new 8f45051 [KARAF-6906] ensure config admin also uses env variables OOTB new 4b6315b Merge pull request #28 from rmannibucau/rmannibucau/KARAF-6906 8f45051 is described below commit 8f4505166dcaff00b4062f00dd918e8cb3cd229c Author: Romain Manni-Bucau <rmannibu...@gmail.com> AuthorDate: Mon Nov 9 14:11:42 2020 +0100 [KARAF-6906] ensure config admin also uses env variables OOTB --- pom.xml | 5 ++++ .../service/DefaultConfigurationAdmin.java | 27 ++++++++++++++++++++-- .../service/DefaultConfigurationAdminTest.java | 16 +++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 381518a..ee865c3 100644 --- a/pom.xml +++ b/pom.xml @@ -162,6 +162,11 @@ <version>3.0.0-M5</version> <configuration> <trimStackTrace>false</trimStackTrace> + <environmentVariables> + <WINEGROWER_SERVICE_A_B_C_FOOBAR_NAME>fooBar</WINEGROWER_SERVICE_A_B_C_FOOBAR_NAME> + <WINEGROWER_SERVICE_A_B_C_FOOBAR>dummy</WINEGROWER_SERVICE_A_B_C_FOOBAR> + <WINEGROWER_SERVICE_A_B_C_SIMPLE>set</WINEGROWER_SERVICE_A_B_C_SIMPLE> + </environmentVariables> <systemPropertyVariables> <org.slf4j.simpleLogger.defaultLogLevel>${surefire.log.level}</org.slf4j.simpleLogger.defaultLogLevel> <org.slf4j.simpleLogger.logFile>System.out</org.slf4j.simpleLogger.logFile> diff --git a/winegrower-core/src/main/java/org/apache/winegrower/service/DefaultConfigurationAdmin.java b/winegrower-core/src/main/java/org/apache/winegrower/service/DefaultConfigurationAdmin.java index e04d387..18639ce 100644 --- a/winegrower-core/src/main/java/org/apache/winegrower/service/DefaultConfigurationAdmin.java +++ b/winegrower-core/src/main/java/org/apache/winegrower/service/DefaultConfigurationAdmin.java @@ -44,6 +44,7 @@ import java.util.concurrent.atomic.AtomicLong; import static java.util.Arrays.asList; import static java.util.Collections.list; +import static java.util.Locale.ROOT; import static java.util.Optional.ofNullable; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toMap; @@ -225,10 +226,32 @@ public abstract class DefaultConfigurationAdmin implements ConfigurationAdmin { } } - // and finally from system properties - System.getProperties().stringPropertyNames().stream().filter(it -> it.startsWith(prefix)) + // and finally from system properties and env variables + // (env is for the machine so less precise than system props so set first) + final String envPrefix = prefix.toUpperCase(ROOT).replace('.', '_'); + System.getenv().keySet().stream() + .filter(it -> it.length() > prefix.length() && envPrefix.equalsIgnoreCase(it.substring(0, envPrefix.length()))) + .forEach(key -> { + final String k = key.substring(envPrefix.length()); + // env keys loose the case so in case it is important, enable to force the key name + // ex: to set configuration{pid=a.b.c, key=fooBar, value=dummy} you would set: + // A_B_C_FOOBAR_NAME=fooBar + // A_B_C_FOOBAR=dummy + // note that the FOOBAR in the key is not important, previous config is the same than: + // A_B_C_1_NAME=fooBar + // A_B_C_1=dummy + // but when there key is not ambiguous (all lowercase) it is simpler to set (key=foobar): + // A_B_C_FOOBAR=dummy + properties.put( + ofNullable(System.getenv(key + "_NAME")).orElseGet(() -> k.toLowerCase(ROOT)), + System.getenv(key)); + }); + + System.getProperties().stringPropertyNames().stream() + .filter(it -> it.startsWith(prefix)) .forEach(key -> properties.put(key.substring(prefix.length()), System.getProperty(key))); + // ensure the factoryPid/pid is there if exists ofNullable(pid).ifPresent(v -> properties.putIfAbsent("service.pid", v)); ofNullable(factoryPid).ifPresent(v -> properties.putIfAbsent("service.factoryPid", v)); diff --git a/winegrower-core/src/test/java/org/apache/winegrower/service/DefaultConfigurationAdminTest.java b/winegrower-core/src/test/java/org/apache/winegrower/service/DefaultConfigurationAdminTest.java index 47d2118..681a6a6 100644 --- a/winegrower-core/src/test/java/org/apache/winegrower/service/DefaultConfigurationAdminTest.java +++ b/winegrower-core/src/test/java/org/apache/winegrower/service/DefaultConfigurationAdminTest.java @@ -32,6 +32,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; class DefaultConfigurationAdminTest { @@ -44,6 +45,21 @@ class DefaultConfigurationAdminTest { }; @Test + @DisplayName("ConfigurationAdmin should be able to use implicitly the environment") + void envVarOverride(final TestInfo info) { + final List<ConfigurationListener> listeners = new ArrayList<>(); + final DefaultConfigurationAdmin configurationAdmin = new DefaultConfigurationAdmin(emptyMap(), listeners) { + @Override + protected ServiceReference<ConfigurationAdmin> getSelfReference() { // not needed for this tests + return new ServiceReferenceImpl<>(new Hashtable<>(), null, null); + } + }; + final Configuration java = configurationAdmin.getConfiguration("a.b.c"); + assertEquals("dummy", java.getProperties().get("fooBar")); + assertEquals("set", java.getProperties().get("simple")); + } + + @Test @DisplayName("Configuration creation can be forced - ConfigurationListener case") void preload(final TestInfo info) { final List<ConfigurationListener> listeners = new ArrayList<>();