Author: ningjiang
Date: Tue Aug 21 02:59:05 2012
New Revision: 1375376

URL: http://svn.apache.org/viewvc?rev=1375376&view=rev
Log:
CAMEL-5328 supports resolution of nested properties in PropertiesComponent

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentNestPropertiesTest.java
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/adapter.properties
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java?rev=1375376&r1=1375375&r2=1375376&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
 Tue Aug 21 02:59:05 2012
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -101,12 +102,12 @@ public class DefaultPropertiesParser imp
                     augmentedKey = augmentedKey + propertySuffix;
                 }
 
-                String part = createPlaceholderPart(augmentedKey, properties, 
replaced);
+                String part = createPlaceholderPart(augmentedKey, properties, 
replaced, prefixToken, suffixToken);
                 
                 // Note: Only fallback to unaugmented when the original key 
was actually augmented
                 if (part == null && fallbackToUnaugmentedProperty && 
(propertyPrefix != null || propertySuffix != null)) {
                     log.debug("Property wth key [{}] not found, attempting 
with unaugmented key: {}", augmentedKey, key);
-                    part = createPlaceholderPart(key, properties, replaced);
+                    part = createPlaceholderPart(key, properties, replaced, 
prefixToken, suffixToken);
                 }
                 
                 if (part == null) {
@@ -146,12 +147,28 @@ public class DefaultPropertiesParser imp
         }
         return idx;
     }
+    
+    private boolean isNestProperty(String uri, String prefixToken, String 
suffixToken) {
+        if (ObjectHelper.isNotEmpty(uri)) {
+            uri = uri.trim();
+            if (uri.startsWith(prefixToken) && uri.endsWith(suffixToken)) {
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    private String takeOffNestTokes(String uri, String prefixToken, String 
suffixToken) {
+        int start = prefixToken.length(); 
+        int end = uri.length() - suffixToken.length();
+        return uri.substring(start, end); 
+    }
 
     private String createConstantPart(String uri, int start, int end) {
         return uri.substring(start, end);
     }
 
-    private String createPlaceholderPart(String key, Properties properties, 
List<String> replaced) {
+    private String createPlaceholderPart(String key, Properties properties, 
List<String> replaced, String prefixToken, String suffixToken) {
         // keep track of which parts we have replaced
         replaced.add(key);
         
@@ -161,6 +178,25 @@ public class DefaultPropertiesParser imp
         } else if (properties != null) {
             propertyValue = properties.getProperty(key);
         }
+        
+        // we need to check if the propertyValue is nested
+        // we need to check if there is cycle dependency of the nested 
properties
+        List<String> visited = new ArrayList<String>();
+        while (isNestProperty(propertyValue, prefixToken, suffixToken)) {
+            visited.add(key);
+            // need to take off the token first
+            String value = takeOffNestTokes(propertyValue, prefixToken, 
suffixToken);
+            key = parseUri(value, properties, prefixToken, suffixToken);
+            if (visited.contains(key)) {
+                throw new IllegalArgumentException("Circular reference 
detected with key [" + key + "] from text: " + propertyValue);
+            }
+            propertyValue = System.getProperty(key);
+            if (propertyValue != null) {
+                log.debug("Found a JVM system property: {} with value: {} to 
be used.", key, propertyValue);
+            } else if (properties != null) {
+                propertyValue = properties.getProperty(key);
+            }
+        }
 
         return parseProperty(key, propertyValue, properties);
     }

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentNestPropertiesTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentNestPropertiesTest.java?rev=1375376&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentNestPropertiesTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentNestPropertiesTest.java
 Tue Aug 21 02:59:05 2012
@@ -0,0 +1,61 @@
+/**
+ * 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.
+ */
+package org.apache.camel.component.properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class PropertiesComponentNestPropertiesTest extends ContextTestSupport {
+    
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.addComponent("properties", new 
PropertiesComponent("classpath:org/apache/camel/component/properties/adapter.properties"));
+        return context;
+    }
+    
+    @Override
+    protected void setUp() throws Exception {
+        System.setProperty("environment", "junit");
+        super.setUp();
+    }
+    
+    @Override
+    protected void tearDown() throws Exception {
+        System.clearProperty("environment");
+        super.tearDown();
+    }
+    
+    public void testPropertiesComponentDefault() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:start").setBody(simple("${properties:someproperty}${body}"))
+                .to("mock:result");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedBodiesReceived("junitTest");
+
+        template.sendBody("direct:start", "Test");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

Added: 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/adapter.properties
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/adapter.properties?rev=1375376&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/adapter.properties
 (added)
+++ 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/properties/adapter.properties
 Tue Aug 21 02:59:05 2012
@@ -0,0 +1,30 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+someproperty={{{{environment}}.someproperty}}
+
+# LOCAL SERVER
+junit.someproperty=junit
+
+# LOCAL SERVER
+local.someproperty=local
+
+# TEST
+test.someproperty=test
+
+# PROD
+prod.someproperty=prod
\ No newline at end of file


Reply via email to