CAMEL-8974: Properties component support systemPropertiesMode to control 
whether JVM system properties are in use. When in Spring bridge mode it uses 
the spring configured value.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d7b8afc7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d7b8afc7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d7b8afc7

Branch: refs/heads/master
Commit: d7b8afc7411a54c58ea6d9e0fb0ee195912e35df
Parents: 7b1253d
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Jul 16 21:16:35 2015 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Jul 16 21:16:35 2015 +0200

----------------------------------------------------------------------
 .../properties/DefaultPropertiesParser.java     | 29 ++++++--
 .../properties/PropertiesComponent.java         | 40 +++++++++++
 .../properties/PropertiesComponentTest.java     | 74 ++++++++++++++++++++
 .../properties/myproperties.properties          |  1 +
 .../camel/spring/CamelContextFactoryBean.java   |  2 +
 .../BridgePropertyPlaceholderConfigurer.java    |  6 +-
 6 files changed, 145 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d7b8afc7/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
 
b/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
index 5b83983..127b988 100644
--- 
a/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
+++ 
b/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
@@ -20,12 +20,12 @@ import java.util.HashSet;
 import java.util.Properties;
 import java.util.Set;
 
-import static java.lang.String.format;
-
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static java.lang.String.format;
+
 /**
  * A parser to parse a string which contains property placeholders.
  */
@@ -297,12 +297,29 @@ public class DefaultPropertiesParser implements 
AugmentedPropertyNameAwareProper
          * @return Value of the property or {@code null} if not found
          */
         private String doGetPropertyValue(String key) {
-            String value = System.getProperty(key);
-            if (value != null) {
-                log.debug("Found a JVM system property: {} with value: {} to 
be used.", key, value);
-            } else if (properties != null) {
+            String value = null;
+
+            if (propertiesComponent.getSystemPropertiesMode() == 
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 && properties != null) {
                 value = properties.getProperty(key);
+                if (value != null) {
+                    log.debug("Found property: {} with value: {} to be used.", 
key, value);
+                }
+            }
+
+            if (value == null && propertiesComponent.getSystemPropertiesMode() 
== 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);
+                }
             }
+
             return parseProperty(key, value, properties);
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/d7b8afc7/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
 
b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index e9de896..3eee968 100644
--- 
a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++ 
b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -64,6 +64,24 @@ public class PropertiesComponent extends DefaultComponent {
     public static final String SUFFIX_TOKEN = DEFAULT_SUFFIX_TOKEN;
 
     /**
+     *  Never check system properties.
+     */
+    public static final int SYSTEM_PROPERTIES_MODE_NEVER = 0;
+
+    /**
+     * Check system properties if not resolvable in the specified properties.
+     */
+    public static final int SYSTEM_PROPERTIES_MODE_FALLBACK = 1;
+
+    /**
+     * Check system properties first, before trying the specified properties.
+     * This allows system properties to override any other property source.
+     * <p/>
+     * This is the default.
+     */
+    public static final int SYSTEM_PROPERTIES_MODE_OVERRIDE = 2;
+
+    /**
      * Key for stores special override properties that containers such as OSGi 
can store
      * in the OSGi service registry
      */
@@ -87,6 +105,7 @@ public class PropertiesComponent extends DefaultComponent {
     private String suffixToken = DEFAULT_SUFFIX_TOKEN;
     private Properties initialProperties;
     private Properties overrideProperties;
+    private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_OVERRIDE;
 
     public PropertiesComponent() {
         // include out of the box functions
@@ -368,10 +387,31 @@ public class PropertiesComponent extends DefaultComponent 
{
         return functions.containsKey(name);
     }
 
+    public int getSystemPropertiesMode() {
+        return systemPropertiesMode;
+    }
+
+    /**
+     * Sets the system property mode.
+     *
+     * @see #SYSTEM_PROPERTIES_MODE_NEVER
+     * @see #SYSTEM_PROPERTIES_MODE_FALLBACK
+     * @see #SYSTEM_PROPERTIES_MODE_OVERRIDE
+     */
+    public void setSystemPropertiesMode(int systemPropertiesMode) {
+        this.systemPropertiesMode = systemPropertiesMode;
+    }
+
     @Override
     protected void doStart() throws Exception {
         super.doStart();
 
+        if (systemPropertiesMode != SYSTEM_PROPERTIES_MODE_NEVER
+                && systemPropertiesMode != SYSTEM_PROPERTIES_MODE_FALLBACK
+                && systemPropertiesMode != SYSTEM_PROPERTIES_MODE_OVERRIDE) {
+            throw new IllegalArgumentException("Option systemPropertiesMode 
has invalid value: " + systemPropertiesMode);
+        }
+
         // inject the component to the parser
         if (propertiesParser instanceof DefaultPropertiesParser) {
             ((DefaultPropertiesParser) 
propertiesParser).setPropertiesComponent(this);

http://git-wip-us.apache.org/repos/asf/camel/blob/d7b8afc7/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
 
b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
index b8a6a3b..94bf5bb 100644
--- 
a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
@@ -311,6 +311,8 @@ public class PropertiesComponentTest extends 
ContextTestSupport {
         template.sendBody("direct:foo", "Hello Foo");
 
         assertMockEndpointsSatisfied();
+
+        System.clearProperty("myPrefix");
     }
 
     public void testPropertiesComponentPropertyPrefixFallbackDefault() throws 
Exception {
@@ -498,6 +500,78 @@ public class PropertiesComponentTest extends 
ContextTestSupport {
         assertEquals("\"mock", 
context.resolvePropertyPlaceholders("\"{{cool.mock}}"));
     }
 
+    public void testPropertiesComponentOverride() throws Exception {
+        System.setProperty("cool.result", "bar");
+        PropertiesComponent pc = context.getComponent("properties", 
PropertiesComponent.class);
+        
pc.setSystemPropertiesMode(PropertiesComponent.SYSTEM_PROPERTIES_MODE_OVERRIDE);
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:foo").to("mock:{{cool.result}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+
+        template.sendBody("direct:foo", "Hello Foo");
+
+        assertMockEndpointsSatisfied();
+
+        System.clearProperty("cool.result");
+    }
+
+    public void testPropertiesComponentFallback() throws Exception {
+        System.setProperty("cool.result", "bar");
+        System.setProperty("beer", "Carlsberg");
+        PropertiesComponent pc = context.getComponent("properties", 
PropertiesComponent.class);
+        
pc.setSystemPropertiesMode(PropertiesComponent.SYSTEM_PROPERTIES_MODE_FALLBACK);
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:foo").to("mock:{{beer}}").to("mock:{{cool.result}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:bar").expectedMessageCount(0);
+        getMockEndpoint("mock:Carlsberg").expectedMessageCount(1);
+
+        template.sendBody("direct:foo", "Hello Foo");
+
+        assertMockEndpointsSatisfied();
+
+        System.clearProperty("cool.result");
+        System.clearProperty("beer");
+    }
+
+    public void testPropertiesComponentNever() throws Exception {
+        System.setProperty("cool.result", "bar");
+        System.setProperty("beer", "Carlsberg");
+        PropertiesComponent pc = context.getComponent("properties", 
PropertiesComponent.class);
+        
pc.setSystemPropertiesMode(PropertiesComponent.SYSTEM_PROPERTIES_MODE_NEVER);
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:foo").to("mock:{{beer}}").to("mock:{{cool.result}}");
+            }
+        });
+        try {
+            context.start();
+            fail("Should have thrown exception");
+        } catch (FailedToCreateRouteException e) {
+            assertEquals("Property with key [beer] not found in properties 
from text: mock:{{beer}}", e.getCause().getMessage());
+        }
+
+        System.clearProperty("cool.result");
+        System.clearProperty("beer");
+    }
+
     @Override
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();

http://git-wip-us.apache.org/repos/asf/camel/blob/d7b8afc7/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
 
b/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
index a89d393..64e6552 100644
--- 
a/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
+++ 
b/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
@@ -38,3 +38,4 @@ slipDelimiter=##
 
 stop=true
 
+

http://git-wip-us.apache.org/repos/asf/camel/blob/d7b8afc7/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
----------------------------------------------------------------------
diff --git 
a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
 
b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
index 1725227..9703f70 100644
--- 
a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
+++ 
b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
@@ -288,6 +288,8 @@ public class CamelContextFactoryBean extends 
AbstractCamelContextFactoryBean<Spr
                 pc = new PropertiesComponent();
                 getContext().addComponent("properties", pc);
             }
+            // use the spring system properties mode which has a different 
value than Camel may have
+            pc.setSystemPropertiesMode(configurer.getSystemPropertiesMode());
             // replace existing resolver with us
             configurer.setResolver(pc.getPropertiesResolver());
             configurer.setParser(pc.getPropertiesParser());

http://git-wip-us.apache.org/repos/asf/camel/blob/d7b8afc7/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
 
b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
index c30ea65..260cbb2 100644
--- 
a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
+++ 
b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
@@ -51,6 +51,10 @@ public class BridgePropertyPlaceholderConfigurer extends 
PropertyPlaceholderConf
     private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;
     private Boolean ignoreResourceNotFound;
 
+    public int getSystemPropertiesMode() {
+        return systemPropertiesMode;
+    }
+
     @Override
     protected void processProperties(ConfigurableListableBeanFactory 
beanFactoryToProcess, Properties props) throws BeansException {
         super.processProperties(beanFactoryToProcess, props);
@@ -226,7 +230,7 @@ public class BridgePropertyPlaceholderConfigurer extends 
PropertyPlaceholderConf
 
         public String resolvePlaceholder(String placeholderName) {
             String propVal = null;
-            if (systemPropertiesMode  == SYSTEM_PROPERTIES_MODE_OVERRIDE) {
+            if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) {
                 propVal = resolveSystemProperty(placeholderName);
             }
             if (propVal == null) {

Reply via email to