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 2014/12/03 09:23:40 UTC

[2/5] camel git commit: CAMEL-8109: Allow to plugin custom functions to property placeholder

CAMEL-8109: Allow to plugin custom functions to property placeholder


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

Branch: refs/heads/camel-2.14.x
Commit: 7be7d574d5e68d9d28edad0c9eea7c39d26dc216
Parents: 3beb8e2
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Dec 2 20:56:03 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Dec 3 08:54:04 2014 +0100

----------------------------------------------------------------------
 .../properties/DefaultPropertiesParser.java     | 17 +++++-
 .../properties/PropertiesComponent.java         | 18 +++++-
 .../properties/PropertiesFunction.java          | 37 +++++++++++
 .../PropertiesComponentFunctionTest.java        | 64 ++++++++++++++++++++
 4 files changed, 133 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7be7d574/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 137c6c1..ecc3ac6 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.
  */
@@ -209,6 +209,19 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper
          * @return Value of the property with the given key
          */
         private String getPropertyValue(String key, String input) {
+
+            // the key may be a function, so lets check this first
+            if (propertiesComponent != null) {
+                for (PropertiesFunction function : propertiesComponent.getFunctions().values()) {
+                    String token = function.getName() + ":";
+                    if (key.startsWith(token)) {
+                        String remainder = key.substring(token.length());
+                        log.debug("Property with key [{}] is applied by function [{}]", key, function.getName());
+                        return function.apply(remainder);
+                    }
+                }
+            }
+
             // they key may have a get or else expression
             String defaultValue = null;
             if (key.contains(GET_OR_ELSE_TOKEN)) {

http://git-wip-us.apache.org/repos/asf/camel/blob/7be7d574/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 fec14a7..30692b0 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
@@ -19,6 +19,7 @@ package org.apache.camel.component.properties;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -70,6 +71,7 @@ public class PropertiesComponent extends DefaultComponent {
 
     private static final Logger LOG = LoggerFactory.getLogger(PropertiesComponent.class);
     private final Map<CacheKey, Properties> cacheMap = new LRUSoftCache<CacheKey, Properties>(1000);
+    private final Map<String, PropertiesFunction> functions = new HashMap<String, PropertiesFunction>();
     private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver();
     private PropertiesParser propertiesParser = new DefaultPropertiesParser(this);
     private String[] locations;
@@ -84,7 +86,7 @@ public class PropertiesComponent extends DefaultComponent {
     private String suffixToken = DEFAULT_SUFFIX_TOKEN;
     private Properties initialProperties;
     private Properties overrideProperties;
-    
+
     public PropertiesComponent() {
     }
     
@@ -315,6 +317,20 @@ public class PropertiesComponent extends DefaultComponent {
         this.overrideProperties = overrideProperties;
     }
 
+    /**
+     * Gets the functions registered in this properties component.
+     */
+    public Map<String, PropertiesFunction> getFunctions() {
+        return functions;
+    }
+
+    /**
+     * Add the function
+     */
+    public void addFunction(PropertiesFunction function) {
+        this.functions.put(function.getName(), function);
+    }
+
     @Override
     protected void doStop() throws Exception {
         cacheMap.clear();

http://git-wip-us.apache.org/repos/asf/camel/blob/7be7d574/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesFunction.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesFunction.java b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesFunction.java
new file mode 100644
index 0000000..25bb724
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesFunction.java
@@ -0,0 +1,37 @@
+/**
+ * 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;
+
+/**
+ * A function that is applied instead of looking up a property placeholder.
+ */
+public interface PropertiesFunction {
+
+    /**
+     * Name of the function which is used as <tt>name:</tt> to let the properties component know its a function.
+     */
+    String getName();
+
+    /**
+     * Applies the function
+     *
+     * @param remainder    the remainder value
+     * @return a value as the result of the function
+     */
+    String apply(String remainder);
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/7be7d574/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java
new file mode 100644
index 0000000..4213cd0
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentFunctionTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class PropertiesComponentFunctionTest extends ContextTestSupport {
+
+    private class MyFunction implements PropertiesFunction {
+
+        @Override
+        public String getName() {
+            return "beer";
+        }
+
+        @Override
+        public String apply(String remainder) {
+            return "mock:" + remainder.toLowerCase();
+        }
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    public void testFunction() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.addFunction(new MyFunction());
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("{{beer:FOO}}")
+                        .to("{{beer:BAR}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}