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 81798b0 CAMEL-13832: Properties component - Check ENV before JVM System property 81798b0 is described below commit 81798b06c4013e28f57b35c5e8cb8d3f1a124070 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Aug 7 20:37:31 2019 +0200 CAMEL-13832: Properties component - Check ENV before JVM System property --- .../src/main/docs/properties-component.adoc | 41 ++++++---------------- .../properties/DefaultPropertiesParser.java | 29 ++++++++------- .../component/properties/PropertiesComponent.java | 8 +++-- .../PropertiesComponentConfiguration.java | 13 ++++--- 4 files changed, 38 insertions(+), 53 deletions(-) diff --git a/components/camel-properties/src/main/docs/properties-component.adoc b/components/camel-properties/src/main/docs/properties-component.adoc index 7cce039..6dc94e0 100644 --- a/components/camel-properties/src/main/docs/properties-component.adoc +++ b/components/camel-properties/src/main/docs/properties-component.adoc @@ -30,8 +30,8 @@ The Properties component supports 12 options, which are listed below. | *ignoreMissingLocation* (common) | Whether to silently ignore if a location cannot be located, such as a properties file not found. | false | boolean | *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. 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. | 2 | int +| *systemPropertiesMode* (common) | Sets the JVM system property mode (0 = never, 1 = fallback, 2 = override). The default mode (override) is to use system properties if present, and override any existing properties. OS environment variable mode is checked before JVM system property mode | 2 | int +| *environmentVariableMode* (common) | Sets the OS environment variables mode (0 = never, 1 = fallback, 2 = override). The default mode (override) is to use OS environment variables if present, and override any existing properties. OS environment variable mode is checked before JVM system property mode | 2 | 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 | *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean |=== @@ -118,8 +118,6 @@ You can use the method `resolvePropertyPlaceholders` on the == Using PropertyPlaceholder -*Available as of Camel 2.3* - Camel now provides a new `PropertiesComponent` in *camel-core* which allows you to use property placeholders when defining Camel Endpoint URIs. @@ -178,8 +176,6 @@ with comma such as: pc.setLocation("com/mycompany/myprop.properties,com/mycompany/other.properties"); ---- -*Available as of Camel 2.19.0* - You can set which location can be discarded if missing by by setting the ``optional`` attribute, which is false by default, i.e: [source,java] @@ -191,8 +187,6 @@ pc.setLocations( == Using system and environment variables in locations -*Available as of Camel 2.7* - The location now supports using placeholders for JVM system properties and OS environments variables. @@ -291,7 +285,6 @@ inside the Spring as well as the Blueprint XML. == Using a Properties from the Registry -*Available as of Camel 2.4* + For example in OSGi you may want to expose a service which returns the properties as a `java.util.Properties` object. @@ -418,19 +411,21 @@ the `<camelContext>` tag such as `trace` as shown here: 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. +The default mode is that both of them are in override mode, and they are check in the following order: + +1. OS environment variable (override mode) +2. JVM system property (override mode) +3. Property files and other locations +4. OS environment variable (fallback mode) +5. JVM system property (fallback mode) -OS environment variables are in override mode as default mode, -which means that if a OS environment variable exists, then it will be used. +The check stops at first found property value for the key. You can control these modes using the `systemPropertiesMode` and `environmentVariableMode` options on the properties component. == Using property placeholders for any kind of attribute in the XML DSL -*Available as of Camel 2.7* - In the example below we use the `prop` prefix for the namespace `\http://camel.apache.org/schema/placeholder` by which we can use the `prop` prefix in the attributes in the XML DSLs. Notice how we use that @@ -447,8 +442,6 @@ stop=true == Using Blueprint property placeholder with Camel routes -*Available as of Camel 2.7* - Camel supports Blueprint which also offers a property placeholder service. Camel supports convention over configuration, so all you have to do is to define the @@ -552,8 +545,6 @@ Each location is separated by comma. == Overriding Blueprint property placeholders outside CamelContext -*Available as of Camel 2.10.4* - When using Blueprint property placeholder in the Blueprint XML file, you can declare the properties directly in the XML file as shown below: @@ -572,8 +563,6 @@ file. == Using .cfg or .properties file for Blueprint property placeholders -*Available as of Camel 2.10.4* - When using Blueprint property placeholder in the Blueprint XML file, you can declare the properties in a `.properties` or `.cfg` file. If you use Apache ServieMix / Karaf then this container has a convention that it @@ -620,8 +609,6 @@ destination=mock:result == Bridging Spring and Camel property placeholders -*Available as of Camel 2.10* - The Spring Framework does not allow 3rd party frameworks such as Apache Camel to seamless hook into the Spring property placeholder mechanism. However you can easily bridge Spring and Camel by declaring a Spring @@ -676,8 +663,6 @@ An alternative is to configure the `PropertyPlaceholderConfigurer` with == Overriding properties from Camel test kit -*Available as of Camel 2.10* - When Testing with Camel and using the xref:properties-component.adoc[Properties] component, you may want to be able to provide the properties to be used from directly within the unit test @@ -704,8 +689,6 @@ access to the location of the properties. == Using @PropertyInject -*Available as of Camel 2.12* - Camel allows to inject property placeholders in POJOs using the `@PropertyInject` annotation which can be set on fields and setter methods. @@ -755,8 +738,6 @@ private int timeout; == Using out of the box functions -*Available as of Camel 2.14.1* - The xref:properties-component.adoc[Properties] component includes the following functions out of the box @@ -852,8 +833,6 @@ example to call a service on localhost, maybe for unit testing etc == Using custom functions -*Available as of Camel 2.14.1* - The xref:properties-component.adoc[Properties] component allow to plugin 3rd party functions which can be used during parsing of the property placeholders. These functions are then able to do custom logic to resolve the 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 47377fa..972012a 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 @@ -262,21 +262,21 @@ public class DefaultPropertiesParser implements PropertiesParser { String value = null; + // override is the default mode for ENV + int envMode = propertiesComponent != null ? propertiesComponent.getEnvironmentVariableMode() : PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_FALLBACK; // 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 (sysMode == PropertiesComponent.SYSTEM_PROPERTIES_MODE_OVERRIDE) { - value = System.getProperty(key); + if (envMode == PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_OVERRIDE) { + value = lookupEnvironmentVariable(key); if (value != null) { - log.debug("Found a JVM system property: {} with value: {} to be used.", key, value); + log.debug("Found an OS environment property: {} with value: {} to be used.", key, value); } } - if (value == null && envMode == PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_OVERRIDE) { - value = lookupEnvironmentVariable(key); + if (value == null && sysMode == PropertiesComponent.SYSTEM_PROPERTIES_MODE_OVERRIDE) { + value = System.getProperty(key); if (value != null) { - log.debug("Found a environment property: {} with value: {} to be used.", key, value); + log.debug("Found a JVM system property: {} with value: {} to be used.", key, value); } } @@ -287,17 +287,16 @@ public class DefaultPropertiesParser implements PropertiesParser { } } - if (value == null && sysMode == PropertiesComponent.SYSTEM_PROPERTIES_MODE_FALLBACK) { - value = System.getProperty(key); + if (value == null && envMode == PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_FALLBACK) { + value = lookupEnvironmentVariable(key); if (value != null) { - log.debug("Found a JVM system property: {} with value: {} to be used.", key, value); + log.debug("Found an OS environment property: {} with value: {} to be used.", key, value); } } - if (value == null && envMode == PropertiesComponent.ENVIRONMENT_VARIABLES_MODE_FALLBACK) { - // lookup OS env with upper case key - value = lookupEnvironmentVariable(key); + if (value == null && sysMode == PropertiesComponent.SYSTEM_PROPERTIES_MODE_FALLBACK) { + value = System.getProperty(key); if (value != null) { - log.debug("Found a environment property: {} with value: {} to be used.", key, value); + log.debug("Found a JVM system property: {} with value: {} to be used.", key, value); } } 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 5094e47..1492d62 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 @@ -403,11 +403,13 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. } /** - * Sets the system property mode. + * Sets the JVM system property mode (0 = never, 1 = fallback, 2 = override). * * The default mode (override) is to use system properties if present, * and override any existing properties. * + * OS environment variable mode is checked before JVM system property mode + * * @see #SYSTEM_PROPERTIES_MODE_NEVER * @see #SYSTEM_PROPERTIES_MODE_FALLBACK * @see #SYSTEM_PROPERTIES_MODE_OVERRIDE @@ -422,11 +424,13 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. } /** - * Sets the OS environment variables mode. + * Sets the OS environment variables mode (0 = never, 1 = fallback, 2 = override). * * The default mode (override) is to use OS environment variables if present, * and override any existing properties. * + * OS environment variable mode is checked before JVM system property mode + * * @see #ENVIRONMENT_VARIABLES_MODE_NEVER * @see #ENVIRONMENT_VARIABLES_MODE_FALLBACK * @see #ENVIRONMENT_VARIABLES_MODE_OVERRIDE 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 606df5c..2749c5c 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 @@ -84,14 +84,17 @@ public class PropertiesComponentConfiguration */ private String overrideProperties; /** - * Sets the system property mode. The default mode (override) is to use - * system properties if present, and override any existing properties. + * Sets the JVM system property mode (0 = never, 1 = fallback, 2 = + * override). The default mode (override) is to use system properties if + * present, and override any existing properties. OS environment variable + * mode is checked before JVM system property mode */ private Integer systemPropertiesMode = 2; /** - * Sets the OS environment variables mode. The default mode (override) is to - * use OS environment variables if present, and override any existing - * properties. + * Sets the OS environment variables mode (0 = never, 1 = fallback, 2 = + * override). The default mode (override) is to use OS environment variables + * if present, and override any existing properties. OS environment variable + * mode is checked before JVM system property mode */ private Integer environmentVariableMode = 2; /**