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/02/28 08:59:22 UTC

[camel] branch master updated: CAMEL-13007: properties component should also fallback to env vars as it does for JVM system properties. The default mode is override.

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 d0390c4  CAMEL-13007: properties component should also fallback to env vars as it does for JVM system properties. The default mode is override.
d0390c4 is described below

commit d0390c445d95b2f6ff30f58ce36d62139469bd61
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Feb 28 09:02:28 2019 +0100

    CAMEL-13007: properties component should also fallback to env vars as it does for JVM system properties. The default mode is override.
---
 .../src/main/docs/properties-component.adoc        | 22 ++++-
 .../properties/DefaultPropertiesParser.java        | 22 ++++-
 .../component/properties/PropertiesComponent.java  | 56 ++++++++++++-
 .../properties/PropertiesComponentTest.java        | 94 ++++++++++++++++++++++
 .../camel/component/properties/env.properties      | 18 +++++
 .../PropertiesComponentConfiguration.java          | 19 ++++-
 6 files changed, 219 insertions(+), 12 deletions(-)

diff --git a/components/camel-properties/src/main/docs/properties-component.adoc b/components/camel-properties/src/main/docs/properties-component.adoc
index 497b63e..cee28d9 100644
--- a/components/camel-properties/src/main/docs/properties-component.adoc
+++ b/components/camel-properties/src/main/docs/properties-component.adoc
@@ -15,7 +15,7 @@ Where *key* is the key for the property to lookup
 === Options
 
 // component options: START
-The Properties component supports 17 options, which are listed below.
+The Properties component supports 18 options, which are listed below.
 
 
 
@@ -37,7 +37,8 @@ The Properties component supports 17 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 mode. | 2 | int
+| *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
 | *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
@@ -250,7 +251,7 @@ You can have multiple placeholders in the same location, such as:
 location=file:${env:APP_HOME}/etc/${prop.name}.properties
 ----
 
-#=== Using system and environment variables to configure property prefixes and suffixes
+=== Using system and environment variables to configure property prefixes and suffixes
 
 *Available as of Camel 2.12.5, 2.13.3, 2.14.0*
 
@@ -494,6 +495,21 @@ The example below has property placeholder in the `<jmxAgent>` tag:
 You can also define property placeholders in the various attributes on
 the `<camelContext>` tag such as `trace` as shown here:
 
+=== Using JVM system properties or Environment variables as override or fallback values
+
+The properties components supports using JVM system properties and also OS environment variables
+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.
+
+You can control these modes using the `systemPropertiesMode` and `environmentVariableMode`
+options on the properties component.
+
 === Overriding a property setting using a JVM System Property
 
 *Available as of Camel 2.5* +
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 7cf494b..9204a2c 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
@@ -306,15 +306,23 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper
 
             String value = null;
 
-            // override is the default mode
-            int mode = propertiesComponent != null ? propertiesComponent.getSystemPropertiesMode() : PropertiesComponent.SYSTEM_PROPERTIES_MODE_OVERRIDE;
+            // override is the default mode for SYS
+            int sysMode = propertiesComponent != null ? propertiesComponent.getSystemPropertiesMode() : PropertiesComponent.SYSTEM_PROPERTIES_MODE_OVERRIDE;
+            // fallback is the default mode for ENV
+            int envMode = propertiesComponent != null ? propertiesComponent.getEnvironmentVariableMode() : PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_FALLBACK;
 
-            if (mode == PropertiesComponent.SYSTEM_PROPERTIES_MODE_OVERRIDE) {
+            if (sysMode == PropertiesComponent.SYSTEM_PROPERTIES_MODE_OVERRIDE) {
                 value = System.getProperty(key);
                 if (value != null) {
                     log.debug("Found a JVM system property: {} with value: {} to be used.", key, value);
                 }
             }
+            if (value == null && envMode == PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_OVERRIDE) {
+                value = System.getenv(key);
+                if (value != null) {
+                    log.debug("Found a environment property: {} with value: {} to be used.", key, value);
+                }
+            }
 
             if (value == null && properties != null) {
                 value = properties.getProperty(key);
@@ -323,12 +331,18 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper
                 }
             }
 
-            if (value == null && mode == PropertiesComponent.SYSTEM_PROPERTIES_MODE_FALLBACK) {
+            if (value == null && sysMode == PropertiesComponent.SYSTEM_PROPERTIES_MODE_FALLBACK) {
                 value = System.getProperty(key);
                 if (value != null) {
                     log.debug("Found a JVM system property: {} with value: {} to be used.", key, value);
                 }
             }
+            if (value == null && envMode == PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_FALLBACK) {
+                value = System.getenv(key);
+                if (value != null) {
+                    log.debug("Found a environment property: {} with value: {} to be used.", key, value);
+                }
+            }
 
             return parseProperty(key, value, properties);
         }
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 bfb7756..15e6d95 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
@@ -21,7 +21,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -52,7 +52,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
     public static final int SYSTEM_PROPERTIES_MODE_FALLBACK = 1;
 
     /**
-     * Check system properties first, before trying the specified properties.
+     * Check system properties variables) first, before trying the specified properties.
      * This allows system properties to override any other property source.
      * <p/>
      * This is the default.
@@ -60,6 +60,24 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
     public static final int SYSTEM_PROPERTIES_MODE_OVERRIDE = 2;
 
     /**
+     *  Never check OS environment variables.
+     */
+    public static final int ENVIRONMENT_VARIABLES_MODE_NEVER = 0;
+
+    /**
+     * Check OS environment variables if not resolvable in the specified properties.
+     * <p/>
+     * This is the default.
+     */
+    public static final int ENVIRONMENT_VARIABLES_MODE_FALLBACK = 1;
+
+    /**
+     * Check OS environment variables first, before trying the specified properties.
+     * This allows system properties to override any other property source.
+     */
+    public static final int ENVIRONMENT_VARIABLES_MODE_OVERRIDE = 2;
+
+    /**
      * Key for stores special override properties that containers such as OSGi can store
      * in the OSGi service registry
      */
@@ -67,7 +85,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
 
     @SuppressWarnings("unchecked")
     private final Map<CacheKey, Properties> cacheMap = LRUCacheFactory.newLRUSoftCache(1000);
-    private final Map<String, PropertiesFunction> functions = new HashMap<>();
+    private final Map<String, PropertiesFunction> functions = new LinkedHashMap<>();
     private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver(this);
     private PropertiesParser propertiesParser = new DefaultPropertiesParser(this);
     private List<PropertiesLocation> locations = Collections.emptyList();
@@ -96,6 +114,8 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
     private Properties overrideProperties;
     @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;
 
     public PropertiesComponent() {
         super();
@@ -463,7 +483,11 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
     }
 
     /**
-     * Sets the system property mode.
+     * 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.
      *
      * @see #SYSTEM_PROPERTIES_MODE_NEVER
      * @see #SYSTEM_PROPERTIES_MODE_FALLBACK
@@ -473,6 +497,25 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
         this.systemPropertiesMode = systemPropertiesMode;
     }
 
+    public int getEnvironmentVariableMode() {
+        return environmentVariableMode;
+    }
+
+    /**
+     * 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.
+     *
+     * @see #ENVIRONMENT_VARIABLES_MODE_NEVER
+     * @see #ENVIRONMENT_VARIABLES_MODE_FALLBACK
+     * @see #ENVIRONMENT_VARIABLES_MODE_OVERRIDE
+     */
+    public void setEnvironmentVariableMode(int environmentVariableMode) {
+        this.environmentVariableMode = environmentVariableMode;
+    }
+
     @Override
     public boolean isResolvePropertyPlaceholders() {
         // its chicken and egg, we cannot resolve placeholders on ourselves
@@ -488,6 +531,11 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
                 && systemPropertiesMode != SYSTEM_PROPERTIES_MODE_OVERRIDE) {
             throw new IllegalArgumentException("Option systemPropertiesMode has invalid value: " + systemPropertiesMode);
         }
+        if (environmentVariableMode != ENVIRONMENT_VARIABLES_MODE_NEVER
+                && environmentVariableMode != ENVIRONMENT_VARIABLES_MODE_FALLBACK
+                && environmentVariableMode != ENVIRONMENT_VARIABLES_MODE_OVERRIDE) {
+            throw new IllegalArgumentException("Option environmentVariableMode has invalid value: " + environmentVariableMode);
+        }
 
         // inject the component to the parser
         if (propertiesParser instanceof DefaultPropertiesParser) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
index c86c212..7c65c65 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
@@ -599,6 +599,100 @@ public class PropertiesComponentTest extends ContextTestSupport {
     }
 
     @Test
+    public void testPropertiesComponentEnvOverride() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setEnvironmentVariableMode(PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_OVERRIDE);
+        pc.setLocation("org/apache/camel/component/properties/env.properties");
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:foo").to("mock:{{FOO_SERVICE_HOST}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:hello").expectedMessageCount(0);
+        getMockEndpoint("mock:myserver").expectedMessageCount(1);
+
+        template.sendBody("direct:foo", "Hello Foo");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testPropertiesComponentEnvFallback() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setEnvironmentVariableMode(PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_FALLBACK);
+        pc.setLocation("org/apache/camel/component/properties/env.properties");
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:foo").to("mock:{{FOO_SERVICE_PORT}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:8081").expectedMessageCount(1);
+        getMockEndpoint("mock:hello").expectedMessageCount(0);
+        getMockEndpoint("mock:myserver").expectedMessageCount(0);
+
+        template.sendBody("direct:foo", "Hello Foo");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testPropertiesComponentEnvNever() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setEnvironmentVariableMode(PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_NEVER);
+        pc.setLocation("org/apache/camel/component/properties/env.properties");
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:foo").to("mock:{{UNKNOWN}}");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            assertEquals("Property with key [UNKNOWN] not found in properties from text: mock:{{UNKNOWN}}", e.getCause().getMessage());
+        }
+    }
+
+    @Test
+    public void testPropertiesComponentEnvFallbackJvmOverride() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setEnvironmentVariableMode(PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_FALLBACK);
+        pc.setLocation("org/apache/camel/component/properties/env.properties");
+
+        // lets override the OS environment variable by setting a JVM system property
+        System.setProperty("FOO_SERVICE_PORT", "hello");
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:foo").to("mock:{{FOO_SERVICE_PORT}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:8081").expectedMessageCount(0);
+        getMockEndpoint("mock:hello").expectedMessageCount(1);
+        getMockEndpoint("mock:myserver").expectedMessageCount(0);
+
+        template.sendBody("direct:foo", "Hello Foo");
+
+        assertMockEndpointsSatisfied();
+
+        System.clearProperty("FOO_SERVICE_PORT");
+    }
+
+
+    @Test
     public void testCamelProperties() throws Exception {
         context.getGlobalOptions().put("foo", "Hello {{cool.name}}");
         context.getGlobalOptions().put("bar", "cool.name");
diff --git a/core/camel-core/src/test/resources/org/apache/camel/component/properties/env.properties b/core/camel-core/src/test/resources/org/apache/camel/component/properties/env.properties
new file mode 100644
index 0000000..41a4632
--- /dev/null
+++ b/core/camel-core/src/test/resources/org/apache/camel/component/properties/env.properties
@@ -0,0 +1,18 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+##      http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+
+FOO_SERVICE_HOST = hello
\ No newline at end of file
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 3e530ce..38c8665 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,10 +119,19 @@ public class PropertiesComponentConfiguration
      */
     private String overrideProperties;
     /**
-     * Sets the system property mode.
+     * 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.
      */
     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.
+     */
+    private Integer environmentVariableMode = 1;
+    /**
      * Whether the component should resolve property placeholders on itself when
      * starting. Only properties which are of String type can use property
      * placeholders.
@@ -258,6 +267,14 @@ public class PropertiesComponentConfiguration
         this.systemPropertiesMode = systemPropertiesMode;
     }
 
+    public Integer getEnvironmentVariableMode() {
+        return environmentVariableMode;
+    }
+
+    public void setEnvironmentVariableMode(Integer environmentVariableMode) {
+        this.environmentVariableMode = environmentVariableMode;
+    }
+
     public Boolean getResolvePropertyPlaceholders() {
         return resolvePropertyPlaceholders;
     }