You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dv...@apache.org on 2011/10/05 22:42:20 UTC

svn commit: r1179429 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/properties/ test/java/org/apache/camel/component/properties/

Author: dvaleri
Date: Wed Oct  5 20:42:20 2011
New Revision: 1179429

URL: http://svn.apache.org/viewvc?rev=1179429&view=rev
Log:
[CAMEL-4520] [CAMEL-3775] Added support for custom prefix/suffix tokens as well as custom property name prefix and suffix strings.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java?rev=1179429&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java Wed Oct  5 20:42:20 2011
@@ -0,0 +1,51 @@
+/**
+ * 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 java.util.Properties;
+
+/**
+ * Interface for property parses that can attempt parsing property names using a fixed property name prefix and suffix.
+ */
+public interface AugmentedPropertyNameAwarePropertiesParser extends PropertiesParser {
+    
+    /**
+     * Parses the string, applying the optional {@code propertyPrefix} and
+     * {@code propertySuffix} to the parsed property names, and replaces the
+     * property placeholders with values from the given properties.
+     * 
+     * @param text the text to be parsed
+     * @param properties the properties resolved which values should be looked
+     *            up
+     * @param prefixToken the prefix token
+     * @param suffixToken the suffix token
+     * @param propertyPrefix the optional property name prefix to augment parsed
+     *            property names with
+     * @param propertySuffix the optional property name suffix to augment parsed
+     *            property names with
+     * @param fallbackToUnaugmentedProperty flag indicating if the originally
+     *            parsed property name should by used for resolution if there is
+     *            no match to the augmented property name
+     *            
+     * @return the parsed text with replaced placeholders
+     *
+     * @throws IllegalArgumentException if uri syntax is not valid or a property
+     *             is not found
+     */
+    String parseUri(String text, Properties properties, String prefixToken, String suffixToken,
+                    String propertyPrefix, String propertySuffix, boolean fallbackToUnaugmentedProperty) throws IllegalArgumentException;
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/AugmentedPropertyNameAwarePropertiesParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1179429&r1=1179428&r2=1179429&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 Wed Oct  5 20:42:20 2011
@@ -28,10 +28,16 @@ import org.slf4j.LoggerFactory;
  *
  * @version 
  */
-public class DefaultPropertiesParser implements PropertiesParser {
+public class DefaultPropertiesParser implements AugmentedPropertyNameAwarePropertiesParser {
     protected final transient Logger log = LoggerFactory.getLogger(getClass());
     
+    @Override
     public String parseUri(String text, Properties properties, String prefixToken, String suffixToken) throws IllegalArgumentException {
+        return parseUri(text, properties, prefixToken, suffixToken, null, null, false);
+    }
+
+    public String parseUri(String text, Properties properties, String prefixToken, String suffixToken,
+                           String propertyPrefix, String propertySuffix, boolean fallbackToUnaugmentedProperty) throws IllegalArgumentException {
         String answer = text;
         boolean done = false;
 
@@ -40,7 +46,7 @@ public class DefaultPropertiesParser imp
         List<String> visited = new ArrayList<String>();
         while (!done) {
             List<String> replaced = new ArrayList<String>();
-            answer = doParseUri(answer, properties, replaced, prefixToken, suffixToken);
+            answer = doParseUri(answer, properties, replaced, prefixToken, suffixToken, propertyPrefix, propertySuffix, fallbackToUnaugmentedProperty);
 
             // check the replaced with the visited to avoid circular reference
             for (String replace : replaced) {
@@ -61,7 +67,8 @@ public class DefaultPropertiesParser imp
         return value;
     }
 
-    private String doParseUri(String uri, Properties properties, List<String> replaced, String prefixToken, String suffixToken) {
+    private String doParseUri(String uri, Properties properties, List<String> replaced, String prefixToken, String suffixToken,
+                              String propertyPrefix, String propertySuffix, boolean fallbackToUnaugmentedProperty) {
         StringBuilder sb = new StringBuilder();
 
         int pivot = 0;
@@ -81,10 +88,35 @@ public class DefaultPropertiesParser imp
                     throw new IllegalArgumentException("Expecting " + suffixToken + " but found end of string from text: " + uri);
                 }
                 String key = uri.substring(pivot, endIdx);
+                String augmentedKey = key;
+                
+                if (propertyPrefix != null) {
+                    log.debug("Augmenting property key [{}] with prefix: {}", key, propertyPrefix);
+                    augmentedKey = propertyPrefix + augmentedKey;
+                }
+                
+                if (propertySuffix != null) {
+                    log.debug("Augmenting property key [{}] with suffix: {}", key, propertySuffix);
+                    augmentedKey = augmentedKey + propertySuffix;
+                }
 
-                String part = createPlaceholderPart(key, properties, replaced);
+                String part = createPlaceholderPart(augmentedKey, properties, replaced);
+                
+                // 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);
+                }
+                
                 if (part == null) {
-                    throw new IllegalArgumentException("Property with key [" + key + "] not found in properties from text: " + uri);
+                    StringBuilder esb = new StringBuilder();
+                    esb.append("Property with key [").append(augmentedKey).append("] ");
+                    if (fallbackToUnaugmentedProperty && (propertyPrefix != null || propertySuffix != null)) {
+                        esb.append("(and original key [").append(key).append("]) ");
+                    }
+                    esb.append("not found in properties from text: ").append(uri);
+                    
+                    throw new IllegalArgumentException(esb.toString());
                 }
                 sb.append(part);
                 pivot = endIdx + suffixToken.length();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java?rev=1179429&r1=1179428&r2=1179429&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java Wed Oct  5 20:42:20 2011
@@ -38,8 +38,27 @@ import org.slf4j.LoggerFactory;
  */
 public class PropertiesComponent extends DefaultComponent {
 
-    public static final String PREFIX_TOKEN = "{{";
-    public static final String SUFFIX_TOKEN = "}}";
+    /**
+     * The default prefix token.
+     */
+    public static final String DEFAULT_PREFIX_TOKEN = "{{";
+    
+    /**
+     * The default suffix token.
+     */
+    public static final String DEFAULT_SUFFIX_TOKEN = "}}";
+    
+    /**
+     * The default prefix token.
+     * @deprecated Use {@link #DEFAULT_PREFIX_TOKEN} instead.
+     */
+    public static final String PREFIX_TOKEN = DEFAULT_PREFIX_TOKEN;
+    
+    /**
+     * The default suffix token.
+     * @deprecated Use {@link #DEFAULT_SUFFIX_TOKEN} instead.
+     */
+    public static final String SUFFIX_TOKEN = DEFAULT_SUFFIX_TOKEN;
 
     // must be non greedy patterns
     private static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{env:(.*?)\\}", Pattern.DOTALL);
@@ -51,6 +70,11 @@ public class PropertiesComponent extends
     private PropertiesParser propertiesParser = new DefaultPropertiesParser();
     private String[] locations;
     private boolean cache = true;
+    private String propertyPrefix;
+    private String propertySuffix;
+    private boolean fallbackToUnaugmentedProperty = true;
+    private String prefixToken = DEFAULT_PREFIX_TOKEN;
+    private String suffixToken = DEFAULT_SUFFIX_TOKEN;
     
     public PropertiesComponent() {
     }
@@ -100,15 +124,21 @@ public class PropertiesComponent extends
         }
 
         // enclose tokens if missing
-        if (!uri.contains(PREFIX_TOKEN) && !uri.startsWith(PREFIX_TOKEN)) {
-            uri = PREFIX_TOKEN + uri;
+        if (!uri.contains(prefixToken) && !uri.startsWith(prefixToken)) {
+            uri = prefixToken + uri;
         }
-        if (!uri.contains(SUFFIX_TOKEN) && !uri.endsWith(SUFFIX_TOKEN)) {
-            uri = uri + SUFFIX_TOKEN;
+        if (!uri.contains(suffixToken) && !uri.endsWith(suffixToken)) {
+            uri = uri + suffixToken;
         }
 
         LOG.trace("Parsing uri {} with properties: {}", uri, prop);
-        return propertiesParser.parseUri(uri, prop, PREFIX_TOKEN, SUFFIX_TOKEN);
+        
+        if (propertiesParser instanceof AugmentedPropertyNameAwarePropertiesParser) {
+            return ((AugmentedPropertyNameAwarePropertiesParser) propertiesParser).parseUri(uri, prop, prefixToken, suffixToken,
+                                                                                            propertyPrefix, propertySuffix, fallbackToUnaugmentedProperty);
+        } else {
+            return propertiesParser.parseUri(uri, prop, prefixToken, suffixToken);
+        }
     }
 
     public String[] getLocations() {
@@ -146,6 +176,62 @@ public class PropertiesComponent extends
     public void setCache(boolean cache) {
         this.cache = cache;
     }
+    
+    public String getPropertyPrefix() {
+        return propertyPrefix;
+    }
+
+    public void setPropertyPrefix(String propertyPrefix) {
+        this.propertyPrefix = propertyPrefix;
+    }
+
+    public String getPropertySuffix() {
+        return propertySuffix;
+    }
+
+    public void setPropertySuffix(String propertySuffix) {
+        this.propertySuffix = propertySuffix;
+    }
+
+    public boolean isFallbackToUnaugmentedProperty() {
+        return fallbackToUnaugmentedProperty;
+    }
+
+    public void setFallbackToUnaugmentedProperty(boolean fallbackToUnaugmentedProperty) {
+        this.fallbackToUnaugmentedProperty = fallbackToUnaugmentedProperty;
+    }
+    
+    public String getPrefixToken() {
+        return prefixToken;
+    }
+
+    /**
+     * Sets the value of the prefix token used to identify properties to replace.  Setting a value of
+     * {@code null} restores the default token (@link {@link #DEFAULT_PREFIX_TOKEN}).
+     */
+    public void setPrefixToken(String prefixToken) {
+        if (prefixToken == null) {
+            this.prefixToken = DEFAULT_PREFIX_TOKEN;
+        } else {
+            this.prefixToken = prefixToken;
+        }
+    }
+
+    public String getSuffixToken() {
+        return suffixToken;
+    }
+
+    /**
+     * Sets the value of the suffix token used to identify properties to replace.  Setting a value of
+     * {@code null} restores the default token (@link {@link #DEFAULT_SUFFIX_TOKEN}).
+     */
+    public void setSuffixToken(String suffixToken) {
+        if (suffixToken == null) {
+            this.suffixToken = DEFAULT_SUFFIX_TOKEN;
+        } else {
+            this.suffixToken = suffixToken;
+        }
+    }
 
     @Override
     protected void doStart() throws Exception {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java?rev=1179429&r1=1179428&r2=1179429&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java Wed Oct  5 20:42:20 2011
@@ -64,6 +64,35 @@ public class PropertiesComponentTest ext
 
         assertMockEndpointsSatisfied();
     }
+    
+    public void testPropertiesComponentCustomTokens() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setPrefixToken("[[");
+        pc.setSuffixToken("]]");
+        
+        assertEquals("[[", pc.getPrefixToken());
+        assertEquals("]]", pc.getSuffixToken());
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:[[cool.end]]");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+        
+        pc.setPrefixToken(null);
+        pc.setSuffixToken(null);
+        
+        assertEquals(PropertiesComponent.DEFAULT_PREFIX_TOKEN, pc.getPrefixToken());
+        assertEquals(PropertiesComponent.DEFAULT_SUFFIX_TOKEN, pc.getSuffixToken());
+    }
 
     public void testPropertiesComponentTemplate() throws Exception {
         context.addRoutes(new RouteBuilder() {
@@ -239,6 +268,155 @@ public class PropertiesComponentTest ext
 
         assertMockEndpointsSatisfied();
     }
+    
+    public void testPropertiesComponentPropertyPrefix() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setPropertyPrefix("cool.");
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:end");
+                from("direct:foo").to("properties:mock:{{result}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(2);
+
+        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:foo", "Hello Foo");
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    public void testPropertiesComponentPropertyPrefixFallbackDefault() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setPropertyPrefix("cool.");
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:cool.end");
+                from("direct:foo").to("properties:mock:{{result}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(2);
+
+        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:foo", "Hello Foo");
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    public void testPropertiesComponentPropertyPrefixFallbackDefaultNotFound() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setPropertyPrefix("cool.");
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:doesnotexist");
+            }
+        });
+        
+        try {
+            context.start();
+            
+            fail("Should throw exception");
+        } catch (FailedToCreateRouteException e) {
+            ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause());
+            assertEquals("Property with key [cool.doesnotexist] (and original key [doesnotexist]) not found in properties from text: {{doesnotexist}}", iae.getMessage());
+        }
+    }
+    
+    public void testPropertiesComponentPropertyPrefixFallbackFalse() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setPropertyPrefix("cool.");
+        pc.setFallbackToUnaugmentedProperty(false);
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:cool.end");
+                from("direct:foo").to("properties:mock:{{result}}");
+            }
+        });
+        
+        try {
+            context.start();
+            
+            fail("Should throw exception");
+        } catch (FailedToCreateRouteException e) {
+            ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause());
+            assertEquals("Property with key [cool.cool.end] not found in properties from text: {{cool.end}}", iae.getMessage());
+        }
+    }
+    
+    public void testPropertiesComponentPropertySuffix() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setPropertySuffix(".end");
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:cool");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    public void testPropertiesComponentPropertySuffixFallbackDefault() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setPropertySuffix(".end");
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:cool.end");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+    
+    public void testPropertiesComponentPropertySuffixFallbackFalse() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setPropertySuffix(".end");
+        pc.setFallbackToUnaugmentedProperty(false);
+        
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:cool.end");
+            }
+        });
+        
+        try {
+            context.start();
+            
+            fail("Should throw exception");
+        } catch (FailedToCreateRouteException e) {
+            ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+            IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause());
+            assertEquals("Property with key [cool.end.end] not found in properties from text: {{cool.end}}", iae.getMessage());
+        }
+    }
 
     public void testJvmSystemPropertyNotFound() throws Exception {
         try {