camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/5] camel git commit: CAMEL-8109: Allow to plugin custom functions to property placeholder
Date Wed, 03 Dec 2014 07:50:02 GMT
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/73abacce
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/73abacce
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/73abacce

Branch: refs/heads/master
Commit: 73abacce6e7e983760e2dbc3ed8ee7aa8b695c44
Parents: 61570e6
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Tue Dec 2 20:56:03 2014 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Dec 3 07:36:38 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/73abacce/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 eb490c2..3c33bbf 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.
  */
@@ -208,6 +208,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/73abacce/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/73abacce/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/73abacce/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();
+    }
+
+}


Mime
View raw message