You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/02/06 16:47:42 UTC

[camel] 01/04: CAMEL-16160: camel-main - JVM system properties should override application.properties

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 115e95a02c57f899d75d82a6518566a64bd54281
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Feb 6 17:01:02 2021 +0100

    CAMEL-16160: camel-main - JVM system properties should override application.properties
---
 .../MainConfigurationPropertiesConfigurer.java     |  6 +++
 .../camel-main-configuration-metadata.json         |  1 +
 core/camel-main/src/main/docs/main.adoc            |  1 +
 .../org/apache/camel/main/BaseMainSupport.java     | 59 +++++++++++++++++++---
 .../camel/main/MainConfigurationProperties.java    | 33 ++++++++++++
 .../java/org/apache/camel/main/MainHelper.java     | 21 ++++++++
 6 files changed, 115 insertions(+), 6 deletions(-)

diff --git a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
index 98f902c..797ed98 100644
--- a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
+++ b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
@@ -31,6 +31,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
         case "AutoConfigurationFailFast": target.setAutoConfigurationFailFast(property(camelContext, boolean.class, value)); return true;
         case "autoconfigurationlogsummary":
         case "AutoConfigurationLogSummary": target.setAutoConfigurationLogSummary(property(camelContext, boolean.class, value)); return true;
+        case "autoconfigurationsystempropertiesenabled":
+        case "AutoConfigurationSystemPropertiesEnabled": target.setAutoConfigurationSystemPropertiesEnabled(property(camelContext, boolean.class, value)); return true;
         case "autostartup":
         case "AutoStartup": target.setAutoStartup(property(camelContext, boolean.class, value)); return true;
         case "autowiredenabled":
@@ -210,6 +212,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
         case "AutoConfigurationFailFast": return boolean.class;
         case "autoconfigurationlogsummary":
         case "AutoConfigurationLogSummary": return boolean.class;
+        case "autoconfigurationsystempropertiesenabled":
+        case "AutoConfigurationSystemPropertiesEnabled": return boolean.class;
         case "autostartup":
         case "AutoStartup": return boolean.class;
         case "autowiredenabled":
@@ -390,6 +394,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
         case "AutoConfigurationFailFast": return target.isAutoConfigurationFailFast();
         case "autoconfigurationlogsummary":
         case "AutoConfigurationLogSummary": return target.isAutoConfigurationLogSummary();
+        case "autoconfigurationsystempropertiesenabled":
+        case "AutoConfigurationSystemPropertiesEnabled": return target.isAutoConfigurationSystemPropertiesEnabled();
         case "autostartup":
         case "AutoStartup": return target.isAutoStartup();
         case "autowiredenabled":
diff --git a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index 9e75fce..bab09f1 100644
--- a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -15,6 +15,7 @@
     { "name": "camel.main.autoConfigurationEnvironmentVariablesEnabled", "description": "Whether auto configuration should include OS environment variables as well. When enabled this allows to overrule any configuration using an OS environment variable. For example to set a shutdown timeout of 5 seconds: CAMEL_MAIN_SHUTDOWNTIMEOUT=5. This option is default enabled.", "sourceType": "org.apache.camel.main.MainConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue [...]
     { "name": "camel.main.autoConfigurationFailFast", "description": "Whether auto configuration should fail fast when configuring one ore more properties fails for whatever reason such as a invalid property name, etc. This option is default enabled.", "sourceType": "org.apache.camel.main.MainConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": true },
     { "name": "camel.main.autoConfigurationLogSummary", "description": "Whether auto configuration should log a summary with the configured properties. This option is default enabled.", "sourceType": "org.apache.camel.main.MainConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": true },
+    { "name": "camel.main.autoConfigurationSystemPropertiesEnabled", "description": "Whether auto configuration should include JVM system properties as well. When enabled this allows to overrule any configuration using a JVM system property. For example to set a shutdown timeout of 5 seconds: -D camel.main.shutdown-timeout=5. Note that JVM system properties take precedence over OS environment variables. This option is default enabled.", "sourceType": "org.apache.camel.main.MainConfigurat [...]
     { "name": "camel.main.autoStartup", "description": "Sets whether the object should automatically start when Camel starts. Important: Currently only routes can be disabled, as CamelContext's are always started. Note: When setting auto startup false on CamelContext then that takes precedence and no routes are started. You would need to start CamelContext explicit using the org.apache.camel.CamelContext.start() method, to start the context, and then you would need to start the routes ma [...]
     { "name": "camel.main.autowiredEnabled", "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. Default is true.", "sourceType": "org.apache.camel.main.DefaultConfiguration [...]
     { "name": "camel.main.backlogTracing", "description": "Sets whether backlog tracing is enabled or not. Default is false.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean" },
diff --git a/core/camel-main/src/main/docs/main.adoc b/core/camel-main/src/main/docs/main.adoc
index bff9c2c..176b620 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -27,6 +27,7 @@ The following table lists all the options:
 | *camel.main.autoConfiguration{zwsp}EnvironmentVariablesEnabled* | Whether auto configuration should include OS environment variables as well. When enabled this allows to overrule any configuration using an OS environment variable. For example to set a shutdown timeout of 5 seconds: CAMEL_MAIN_SHUTDOWNTIMEOUT=5. This option is default enabled. | true | boolean
 | *camel.main.autoConfiguration{zwsp}FailFast* | Whether auto configuration should fail fast when configuring one ore more properties fails for whatever reason such as a invalid property name, etc. This option is default enabled. | true | boolean
 | *camel.main.autoConfiguration{zwsp}LogSummary* | Whether auto configuration should log a summary with the configured properties. This option is default enabled. | true | boolean
+| *camel.main.autoConfiguration{zwsp}SystemPropertiesEnabled* | Whether auto configuration should include JVM system properties as well. When enabled this allows to overrule any configuration using a JVM system property. For example to set a shutdown timeout of 5 seconds: -D camel.main.shutdown-timeout=5. Note that JVM system properties take precedence over OS environment variables. This option is default enabled. | true | boolean
 | *camel.main.autoStartup* | Sets whether the object should automatically start when Camel starts. Important: Currently only routes can be disabled, as CamelContext's are always started. Note: When setting auto startup false on CamelContext then that takes precedence and no routes are started. You would need to start CamelContext explicit using the org.apache.camel.CamelContext.start() method, to start the context, and then you would need to start the routes manually using CamelContext.g [...]
 | *camel.main.autowiredEnabled* | Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. Default is true. | true | boolean
 | *camel.main.backlogTracing* | Sets whether backlog tracing is enabled or not. Default is false. |  | boolean
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index 32faf50..1c16fa9 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -67,12 +67,7 @@ import org.apache.camel.util.StringHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.camel.main.MainHelper.computeProperties;
-import static org.apache.camel.main.MainHelper.loadEnvironmentVariablesAsProperties;
-import static org.apache.camel.main.MainHelper.lookupPropertyFromSysOrEnv;
-import static org.apache.camel.main.MainHelper.optionKey;
-import static org.apache.camel.main.MainHelper.setPropertiesOnTarget;
-import static org.apache.camel.main.MainHelper.validateOptionAndValue;
+import static org.apache.camel.main.MainHelper.*;
 import static org.apache.camel.support.ObjectHelper.invokeMethod;
 import static org.apache.camel.util.ReflectionHelper.findMethod;
 import static org.apache.camel.util.StringHelper.matches;
@@ -568,9 +563,27 @@ public abstract class BaseMainSupport extends BaseService {
                 }
             }
         }
+        // load properties from JVM (override existing)
+        Properties propJVM = null;
+        if (mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
+            propJVM = loadJvmSystemPropertiesAsProperties(new String[] { "camel.main." });
+            if (!propJVM.isEmpty()) {
+                prop.putAll(propJVM);
+                LOG.debug("Properties from JVM system properties:");
+                for (String key : propJVM.stringPropertyNames()) {
+                    LOG.debug("    {}={}", key, propJVM.getProperty(key));
+                }
+            }
+        }
 
         // special for fail-fast as we need to know this early before we set all the other options
         Object failFast = propENV != null ? propENV.remove("camel.main.autoconfigurationfailfast") : null;
+        if (propJVM != null) {
+            Object val = propJVM.remove("camel.main.autoconfigurationfailfast");
+            if (val != null) {
+                failFast = val;
+            }
+        }
         if (failFast != null) {
             mainConfigurationProperties
                     .setAutoConfigurationFailFast(CamelContextHelper.parseBoolean(camelContext, failFast.toString()));
@@ -642,6 +655,17 @@ public abstract class BaseMainSupport extends BaseService {
                 }
             }
         }
+        // load properties from JVM (override existing)
+        if (mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
+            Properties propJVM = loadJvmSystemPropertiesAsProperties(new String[] { "camel.component.properties." });
+            if (!propJVM.isEmpty()) {
+                prop.putAll(propJVM);
+                LOG.debug("Properties from JVM system properties:");
+                for (String key : propJVM.stringPropertyNames()) {
+                    LOG.debug("    {}={}", key, propJVM.getProperty(key));
+                }
+            }
+        }
 
         Map<String, Object> contextProperties = new LinkedHashMap<>();
         Map<String, Object> hystrixProperties = new LinkedHashMap<>();
@@ -1007,6 +1031,13 @@ public abstract class BaseMainSupport extends BaseService {
                 prop.putAll(propENV);
             }
         }
+        // load properties from JVM (override existing)
+        if (mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
+            Properties propJVM = loadJvmSystemPropertiesAsProperties(new String[] { "camel.component.properties." });
+            if (!propJVM.isEmpty()) {
+                prop.putAll(propJVM);
+            }
+        }
 
         Map<String, Object> properties = new LinkedHashMap<>();
 
@@ -1045,6 +1076,7 @@ public abstract class BaseMainSupport extends BaseService {
         // load properties from ENV (override existing)
         if (mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled()) {
             Properties propENV = loadEnvironmentVariablesAsProperties(new String[] { "camel.main." });
+            // ENV variables cannot use dash so replace with dot
             propENV.remove(INITIAL_PROPERTIES_LOCATION.replace('-', '.'));
             propENV.remove(OVERRIDE_PROPERTIES_LOCATION.replace('-', '.'));
             propENV.remove(PROPERTY_PLACEHOLDER_LOCATION.replace('-', '.'));
@@ -1052,6 +1084,13 @@ public abstract class BaseMainSupport extends BaseService {
                 prop.putAll(propENV);
             }
         }
+        // load properties from JVM (override existing)
+        if (mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
+            Properties propJVM = loadJvmSystemPropertiesAsProperties(new String[] { "camel.main." });
+            if (!propJVM.isEmpty()) {
+                prop.putAll(propJVM);
+            }
+        }
 
         Map<String, Object> properties = new LinkedHashMap<>();
 
@@ -1117,6 +1156,14 @@ public abstract class BaseMainSupport extends BaseService {
                 prop.putAll(propENV);
             }
         }
+        // load properties from JVM (override existing)
+        if (mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
+            Properties propJVM = loadJvmSystemPropertiesAsProperties(
+                    new String[] { "camel.component.", "camel.dataformat.", "camel.language." });
+            if (!propJVM.isEmpty()) {
+                prop.putAll(propJVM);
+            }
+        }
 
         Map<PropertyOptionKey, Map<String, Object>> properties = new LinkedHashMap<>();
 
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
index b883d62..74b00d6 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
@@ -34,6 +34,7 @@ public class MainConfigurationProperties extends DefaultConfigurationProperties<
 
     private boolean autoConfigurationEnabled = true;
     private boolean autoConfigurationEnvironmentVariablesEnabled = true;
+    private boolean autoConfigurationSystemPropertiesEnabled = true;
     private boolean autoConfigurationFailFast = true;
     private boolean autoConfigurationLogSummary = true;
     private int durationHitExitCode;
@@ -206,6 +207,23 @@ public class MainConfigurationProperties extends DefaultConfigurationProperties<
         this.autoConfigurationEnvironmentVariablesEnabled = autoConfigurationEnvironmentVariablesEnabled;
     }
 
+    public boolean isAutoConfigurationSystemPropertiesEnabled() {
+        return autoConfigurationSystemPropertiesEnabled;
+    }
+
+    /**
+     * Whether auto configuration should include JVM system properties as well. When enabled this allows to overrule any
+     * configuration using a JVM system property. For example to set a shutdown timeout of 5 seconds: -D
+     * camel.main.shutdown-timeout=5.
+     * <p/>
+     * Note that JVM system properties take precedence over OS environment variables.
+     * <p/>
+     * This option is default enabled.
+     */
+    public void setAutoConfigurationSystemPropertiesEnabled(boolean autoConfigurationSystemPropertiesEnabled) {
+        this.autoConfigurationSystemPropertiesEnabled = autoConfigurationSystemPropertiesEnabled;
+    }
+
     public boolean isAutoConfigurationFailFast() {
         return autoConfigurationFailFast;
     }
@@ -410,6 +428,21 @@ public class MainConfigurationProperties extends DefaultConfigurationProperties<
     }
 
     /**
+     * Whether auto configuration should include JVM system properties as well. When enabled this allows to overrule any
+     * configuration using a JVM system property. For example to set a shutdown timeout of 5 seconds: -D
+     * camel.main.shutdown-timeout=5.
+     * <p/>
+     * Note that JVM system properties take precedence over OS environment variables.
+     * <p/>
+     * This option is default enabled.
+     */
+    public MainConfigurationProperties withAutoConfigurationSystemPropertiesEnabled(
+            boolean autoConfigurationSystemPropertiesEnabled) {
+        this.autoConfigurationSystemPropertiesEnabled = autoConfigurationSystemPropertiesEnabled;
+        return this;
+    }
+
+    /**
      * Whether auto configuration should fail fast when configuring one ore more properties fails for whatever reason
      * such as a invalid property name, etc.
      * <p/>
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
index 7049fe9..c48cad0 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
@@ -77,6 +77,27 @@ public final class MainHelper {
         return answer;
     }
 
+    public static Properties loadJvmSystemPropertiesAsProperties(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.getProperties().forEach((k, v) -> {
+                String key = k.toString().toUpperCase(Locale.US);
+                if (key.startsWith(pk) || key.startsWith(pk2)) {
+                    key = key.toLowerCase(Locale.ENGLISH).replace('_', '.');
+                    answer.put(key, v);
+                }
+            });
+        }
+
+        return answer;
+    }
+
     public static String optionKey(String key) {
         // as we ignore case for property names we should use keys in same case and without dashes
         key = StringHelper.dashToCamelCase(key);