This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 68a93674a82e01b1d0c0312ff5998c05de94b8ae Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Jun 25 15:58:46 2019 +0200 CAMEL-13681: camel-main - Allow ENV variables to configure any option --- .../properties/DefaultPropertiesParser.java | 18 +----- .../java/org/apache/camel/main/MainSupport.java | 71 ++++++++++++++++++++-- .../main/java/org/apache/camel/util/IOHelper.java | 16 +++++ 3 files changed, 83 insertions(+), 22 deletions(-) diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java index bc78cb5..f035912 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java @@ -25,6 +25,8 @@ import org.apache.camel.util.StringHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.camel.util.IOHelper.lookupEnvironmentVariable; + /** * A parser to parse a string which contains property placeholders. */ @@ -350,22 +352,6 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper } /** - * Lookup the OS environment variable in a safe manner by - * using upper case keys and underscore instead of dash. - */ - private static String lookupEnvironmentVariable(String key) { - // lookup OS env with upper case key - String upperKey = key.toUpperCase(); - String value = System.getenv(upperKey); - // some OS do not support dashes in keys, so replace with underscore - if (value == null) { - String noDashKey = upperKey.replace('-', '_'); - value = System.getenv(noDashKey); - } - return value; - } - - /** * This inner class is the definition of a property used in a string */ private static final class Property { diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java index 9e8ebf1..3f80690 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java @@ -26,6 +26,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.concurrent.CountDownLatch; @@ -62,6 +63,7 @@ import org.slf4j.LoggerFactory; import static org.apache.camel.support.ObjectHelper.invokeMethod; import static org.apache.camel.util.ReflectionHelper.findMethod; +import static org.apache.camel.util.StringHelper.dashToCamelCase; import static org.apache.camel.util.StringHelper.matches; /** @@ -855,9 +857,44 @@ public abstract class MainSupport extends ServiceSupport { } } + protected Properties loadEnvironmentVariablesAsProperties(String[] prefixes) { + Properties answer = new OrderedProperties(); + if (prefixes == null || prefixes.length == 0) { + return answer; + } + + for (String prefix : prefixes) { + final String pk = prefix.toUpperCase(Locale.US).replaceAll("[^\\w]", "-"); + final String pk2 = pk.replace('-', '_'); + System.getenv().forEach((k, v) -> { + k = k.toUpperCase(Locale.US); + if (k.startsWith(pk) || k.startsWith(pk2)) { + String key = k.toLowerCase(Locale.US).replace('_', '.'); + answer.put(key, v); + } + }); + } + + return answer; + } + protected void autoConfigurationPropertiesComponent(CamelContext camelContext) throws Exception { // load properties Properties prop = camelContext.getPropertiesComponent().loadProperties(); + LOG.debug("Properties from Camel properties component:"); + for (String key : prop.stringPropertyNames()) { + LOG.debug(" {}={}", key, prop.getProperty(key)); + } + + // load properties from ENV (override existing) + Properties propENV = loadEnvironmentVariablesAsProperties(new String[]{"camel.component.properties."}); + if (!propENV.isEmpty()) { + prop.putAll(propENV); + LOG.debug("Properties from OS environment variables:"); + for (String key : propENV.stringPropertyNames()) { + LOG.debug(" {}={}", key, propENV.getProperty(key)); + } + } Map<Object, Map<String, Object>> properties = new LinkedHashMap<>(); @@ -890,6 +927,20 @@ public abstract class MainSupport extends ServiceSupport { protected void autoConfigurationMainConfiguration(CamelContext camelContext, MainConfigurationProperties config) throws Exception { // load properties Properties prop = camelContext.getPropertiesComponent().loadProperties(); + LOG.debug("Properties from Camel properties component:"); + for (String key : prop.stringPropertyNames()) { + LOG.debug(" {}={}", key, prop.getProperty(key)); + } + + // load properties from ENV (override existing) + Properties propENV = loadEnvironmentVariablesAsProperties(new String[]{"camel.main."}); + if (!propENV.isEmpty()) { + prop.putAll(propENV); + LOG.debug("Properties from OS environment variables:"); + for (String key : propENV.stringPropertyNames()) { + LOG.debug(" {}={}", key, propENV.getProperty(key)); + } + } Map<String, Object> properties = new LinkedHashMap<>(); @@ -932,13 +983,21 @@ public abstract class MainSupport extends ServiceSupport { // ignore as this file is optional } - // load properties from properties component + // load properties from properties component (override existing) Properties propPC = camelContext.getPropertiesComponent().loadProperties(); - if (propPC != null) { - prop.putAll(propPC); - LOG.debug("Properties from Camel properties component:"); - for (String key : propPC.stringPropertyNames()) { - LOG.debug(" {}={}", key, propPC.getProperty(key)); + prop.putAll(propPC); + LOG.debug("Properties from Camel properties component:"); + for (String key : propPC.stringPropertyNames()) { + LOG.debug(" {}={}", key, propPC.getProperty(key)); + } + + // load properties from ENV (override existing) + Properties propENV = loadEnvironmentVariablesAsProperties(new String[]{"camel.component.", "camel.dataformat.", "camel.language."}); + if (!propENV.isEmpty()) { + prop.putAll(propENV); + LOG.debug("Properties from OS environment variables:"); + for (String key : propENV.stringPropertyNames()) { + LOG.debug(" {}={}", key, propENV.getProperty(key)); } } diff --git a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java index ee52b06..565b54c 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java @@ -519,6 +519,22 @@ public final class IOHelper { } /** + * Lookup the OS environment variable in a safe manner by + * using upper case keys and underscore instead of dash. + */ + public static String lookupEnvironmentVariable(String key) { + // lookup OS env with upper case key + String upperKey = key.toUpperCase(); + String value = System.getenv(upperKey); + // some OS do not support dashes in keys, so replace with underscore + if (value == null) { + String noDashKey = upperKey.replace('-', '_'); + value = System.getenv(noDashKey); + } + return value; + } + + /** * Encoding-aware input stream. */ public static class EncodingInputStream extends InputStream {