Merge pull request #28 from rmannibucau/rmannibucau/KARAF-6906
[KARAF-6906] ensure config admin also uses env variables OOTB
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 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 @@
}
}
- // 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 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 @@
};
@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<>();