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 2019/05/12 09:11:35 UTC

[camel] branch master updated: CAMEL-13502: Properties component - Let OS environment variable take precedence by having it in override mode by default

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


The following commit(s) were added to refs/heads/master by this push:
     new 71f1f92  CAMEL-13502: Properties component - Let OS environment variable take precedence by having it in override mode by default
71f1f92 is described below

commit 71f1f923a53c2a66dc1dd0d85dc2b458a2be01c6
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun May 12 11:11:15 2019 +0200

    CAMEL-13502: Properties component - Let OS environment variable take precedence by having it in override mode by default
---
 .../src/main/docs/properties-component.adoc        |  9 +++---
 .../component/properties/PropertiesComponent.java  | 20 ++++++------
 .../java/org/apache/camel/main/MainSupport.java    | 37 +++++++++++++++++++++-
 .../src/main/resources/application.properties      |  3 ++
 .../PropertiesComponentConfiguration.java          | 12 +++----
 5 files changed, 58 insertions(+), 23 deletions(-)

diff --git a/components/camel-properties/src/main/docs/properties-component.adoc b/components/camel-properties/src/main/docs/properties-component.adoc
index b1b55a6..d5cf8a8 100644
--- a/components/camel-properties/src/main/docs/properties-component.adoc
+++ b/components/camel-properties/src/main/docs/properties-component.adoc
@@ -37,8 +37,8 @@ The Properties component supports 18 options, which are listed below.
 | *suffixToken* (advanced) | Sets the value of the suffix token used to identify properties to replace. Setting a value of null restores the default token (link DEFAULT_SUFFIX_TOKEN). | }} | String
 | *initialProperties* (advanced) | Sets initial properties which will be used before any locations are resolved. |  | Properties
 | *overrideProperties* (advanced) | Sets a special list of override properties that take precedence and will use first, if a property exist. |  | Properties
-| *systemPropertiesMode* (common) | Sets the system property (and environment variable) mode. The default mode (override) is to check system properties (and environment variables) first, before trying the specified properties. This allows system properties/environment variables to override any other property source. | 2 | int
-| *environmentVariableMode* (common) | Sets the OS environment variables mode. The default mode (fallback) is to check OS environment variables, if the property cannot be resolved from its sources first. This allows environment variables as fallback values. | 1 | int
+| *systemPropertiesMode* (common) | Sets the system property mode. The default mode (override) is to use system properties if present, and override any existing properties. | 2 | int
+| *environmentVariableMode* (common) | Sets the OS environment variables mode. The default mode (override) is to use OS environment variables if present, and override any existing properties. | 1 | int
 | *resolveProperty Placeholders* (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean
 |===
 // component options: END
@@ -508,9 +508,8 @@ as values which can either be used as override or fallback values.
 The default mode is that JVM system properties are in override mode, which means they
 are checked first.
 
-And as of Camel 3.0 then OS environment variables are in fallback mode as default mode,
-which means that if a property was not found then as a fallback the OS environment variables
-is checked as last.
+And as of Camel 3.0 then OS environment variables are in override mode as default mode,
+which means that if a OS environment variable exists, then it will be used.
 
 You can control these modes using the `systemPropertiesMode` and `environmentVariableMode`
 options on the properties component.
diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index fd09e8e..6d77953 100644
--- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -57,7 +57,8 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
 
     /**
      * Check system properties variables) first, before trying the specified properties.
-     * This allows system properties to override any other property source.
+     * This allows system properties to override any other property source
+     * (environment variable and then system properties takes precedence).
      * <p/>
      * This is the default.
      */
@@ -77,7 +78,8 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
 
     /**
      * Check OS environment variables first, before trying the specified properties.
-     * This allows system properties to override any other property source.
+     * This allows environment variables to override any other property source
+     * (environment variable and then system properties takes precedence).
      */
     public static final int ENVIRONMENT_VARIABLES_MODE_OVERRIDE = 2;
 
@@ -122,7 +124,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
     @Metadata(defaultValue = "" + SYSTEM_PROPERTIES_MODE_OVERRIDE, enums = "0,1,2")
     private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_OVERRIDE;
     @Metadata(defaultValue = "" + SYSTEM_PROPERTIES_MODE_FALLBACK, enums = "0,1,2")
-    private int environmentVariableMode = ENVIRONMENT_VARIABLES_MODE_FALLBACK;
+    private int environmentVariableMode = ENVIRONMENT_VARIABLES_MODE_OVERRIDE;
 
     public PropertiesComponent() {
         super();
@@ -528,11 +530,10 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
     }
 
     /**
-     * Sets the system property (and environment variable) mode.
+     * Sets the system property mode.
      *
-     * The default mode (override) is to check system properties (and environment variables) first,
-     * before trying the specified properties.
-     * This allows system properties/environment variables to override any other property source.
+     * The default mode (override) is to use system properties if present,
+     * and override any existing properties.
      *
      * @see #SYSTEM_PROPERTIES_MODE_NEVER
      * @see #SYSTEM_PROPERTIES_MODE_FALLBACK
@@ -550,9 +551,8 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
     /**
      * Sets the OS environment variables mode.
      *
-     * The default mode (fallback) is to check OS environment variables,
-     * if the property cannot be resolved from its sources first.
-     * This allows environment variables as fallback values.
+     * The default mode (override) is to use OS environment variables if present,
+     * and override any existing properties.
      *
      * @see #ENVIRONMENT_VARIABLES_MODE_NEVER
      * @see #ENVIRONMENT_VARIABLES_MODE_FALLBACK
diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
index c024fe4..207e7fe 100644
--- a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
@@ -794,7 +794,12 @@ public abstract class MainSupport extends ServiceSupport {
             camelContext.getManagementStrategy().addEventNotifier(notifier);
         }
 
-        // try to load configurations
+        // need to eager allow to auto configure properties component
+        if (autoConfigurationEnabled) {
+            autoConfigurationPropertiesComponent(camelContext);
+        }
+
+        // try to load configuration classes
         loadConfigurations(camelContext);
 
         // conventional configuration via properties to allow configuring options on
@@ -817,6 +822,36 @@ public abstract class MainSupport extends ServiceSupport {
         }
     }
 
+    protected void autoConfigurationPropertiesComponent(CamelContext camelContext) throws Exception {
+        // load properties
+        Properties prop = camelContext.getPropertiesComponent().loadProperties();
+
+        Map<Object, Map<String, Object>> properties = new LinkedHashMap<>();
+
+        for (String key : prop.stringPropertyNames()) {
+            int dot = key.indexOf(".", 26);
+            if (key.startsWith("camel.component.properties.") && dot > 0) {
+                Component component = camelContext.getPropertiesComponent();
+                // grab the value
+                String value = prop.getProperty(key);
+                String option = key.substring(dot + 1);
+                Map<String, Object> values = properties.getOrDefault(component, new LinkedHashMap<>());
+                values.put(option, value);
+                properties.put(component, values);
+            }
+        }
+
+        if (!properties.isEmpty()) {
+            long total = properties.values().stream().mapToLong(Map::size).sum();
+            LOG.info("Auto configuring {} properties component from loaded properties: {}", properties.size(), total);
+        }
+
+        for (Object obj : properties.keySet()) {
+            Map<String, Object> values = properties.get(obj);
+            setCamelProperties(camelContext, obj, values, true);
+        }
+    }
+
     protected void autoConfigurationFromProperties(CamelContext camelContext) throws Exception {
         // load properties
         Properties prop = camelContext.getPropertiesComponent().loadProperties();
diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main/src/main/resources/application.properties
index c6f0390..1c47fff 100644
--- a/examples/camel-example-main/src/main/resources/application.properties
+++ b/examples/camel-example-main/src/main/resources/application.properties
@@ -19,6 +19,9 @@
 # here we can configure the options on the component level (and we can use dash-naming-style)
 camel.component.quartz2.start-delayed-seconds = 3
 
+# you can configure whether OS environment should override (=2 which is default) or as fallback (=1)
+### camel.component.properties.environment-variable-mode=1
+
 # properties used in the route
 myCron = 0/2 * * * * ?
 
diff --git a/platforms/spring-boot/components-starter/camel-properties-starter/src/main/java/org/apache/camel/component/properties/springboot/PropertiesComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-properties-starter/src/main/java/org/apache/camel/component/properties/springboot/PropertiesComponentConfiguration.java
index 7ba9e50..945965e 100644
--- a/platforms/spring-boot/components-starter/camel-properties-starter/src/main/java/org/apache/camel/component/properties/springboot/PropertiesComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-properties-starter/src/main/java/org/apache/camel/component/properties/springboot/PropertiesComponentConfiguration.java
@@ -119,16 +119,14 @@ public class PropertiesComponentConfiguration
      */
     private String overrideProperties;
     /**
-     * Sets the system property (and environment variable) mode. The default
-     * mode (override) is to check system properties (and environment variables)
-     * first, before trying the specified properties. This allows system
-     * properties/environment variables to override any other property source.
+     * Sets the system property mode. The default mode (override) is to use
+     * system properties if present, and override any existing properties.
      */
     private Integer systemPropertiesMode = 2;
     /**
-     * Sets the OS environment variables mode. The default mode (fallback) is to
-     * check OS environment variables, if the property cannot be resolved from
-     * its sources first. This allows environment variables as fallback values.
+     * Sets the OS environment variables mode. The default mode (override) is to
+     * use OS environment variables if present, and override any existing
+     * properties.
      */
     private Integer environmentVariableMode = 1;
     /**