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 2016/08/30 16:47:56 UTC

camel git commit: CAMEL-10275: Allow @ProperyInject on bean method arguments

Repository: camel
Updated Branches:
  refs/heads/master a2f5daa5d -> c8ca9f5b2


CAMEL-10275: Allow @ProperyInject on bean method arguments


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

Branch: refs/heads/master
Commit: c8ca9f5b2eeed70bc177b483b152d3ae9836edb5
Parents: a2f5daa
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Aug 30 15:38:58 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Aug 30 18:47:43 2016 +0200

----------------------------------------------------------------------
 .../java/org/apache/camel/PropertyInject.java   |   2 +-
 .../apache/camel/builder/ExpressionBuilder.java |   6 +-
 .../apache/camel/component/bean/BeanInfo.java   |   5 +
 .../simple/ast/SimpleFunctionExpression.java    |   4 +-
 .../PropertyInjectAnnotationParameterTest.java  | 118 +++++++++++++++++++
 5 files changed, 131 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c8ca9f5b/camel-core/src/main/java/org/apache/camel/PropertyInject.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/PropertyInject.java b/camel-core/src/main/java/org/apache/camel/PropertyInject.java
index b6d6852..2b2d4ac 100644
--- a/camel-core/src/main/java/org/apache/camel/PropertyInject.java
+++ b/camel-core/src/main/java/org/apache/camel/PropertyInject.java
@@ -28,7 +28,7 @@ import java.lang.annotation.Target;
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
-@Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR})
+@Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
 public @interface PropertyInject {
     String value();
     String defaultValue() default "";

http://git-wip-us.apache.org/repos/asf/camel/blob/c8ca9f5b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
index b6069ea..d80306a 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
@@ -2063,7 +2063,7 @@ public final class ExpressionBuilder {
         return constantExpression(str);
     }
 
-    public static Expression propertiesComponentExpression(final String key, final String locations) {
+    public static Expression propertiesComponentExpression(final String key, final String locations, final String defaultValue) {
         return new ExpressionAdapter() {
             public Object evaluate(Exchange exchange) {
                 String text = simpleExpression(key).evaluate(exchange, String.class);
@@ -2091,6 +2091,10 @@ public final class ExpressionBuilder {
                         return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken());
                     }
                 } catch (Exception e) {
+                    // property with key not found, use default value if provided
+                    if (defaultValue != null) {
+                        return defaultValue;
+                    }
                     throw ObjectHelper.wrapRuntimeCamelException(e);
                 }
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/c8ca9f5b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
index 752db1e..87be7ee 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
@@ -47,6 +47,7 @@ import org.apache.camel.Message;
 import org.apache.camel.OutHeaders;
 import org.apache.camel.Properties;
 import org.apache.camel.Property;
+import org.apache.camel.PropertyInject;
 import org.apache.camel.builder.ExpressionBuilder;
 import org.apache.camel.language.LanguageAnnotation;
 import org.apache.camel.spi.Registry;
@@ -973,6 +974,10 @@ public class BeanInfo {
             return ExpressionBuilder.outHeadersExpression();
         } else if (annotation instanceof ExchangeException) {
             return ExpressionBuilder.exchangeExceptionExpression(CastUtils.cast(parameterType, Exception.class));
+        } else if (annotation instanceof PropertyInject) {
+            PropertyInject propertyAnnotation = (PropertyInject) annotation;
+            Expression inject = ExpressionBuilder.propertiesComponentExpression(propertyAnnotation.value(), null, propertyAnnotation.defaultValue());
+            return ExpressionBuilder.convertToExpression(inject, parameterType);
         } else {
             LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class);
             if (languageAnnotation != null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/c8ca9f5b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index efa92d2..89c7ea8 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -177,7 +177,7 @@ public class SimpleFunctionExpression extends LiteralExpression {
             if (parts.length > 2) {
                 throw new SimpleParserException("Valid syntax: ${properties:key[:default]} was: " + function, token.getIndex());
             }
-            return ExpressionBuilder.propertiesComponentExpression(remainder, null);
+            return ExpressionBuilder.propertiesComponentExpression(remainder, null, null);
         }
 
         // properties-location: prefix
@@ -194,7 +194,7 @@ public class SimpleFunctionExpression extends LiteralExpression {
                 locations = ObjectHelper.before(remainder, ":");
                 key = ObjectHelper.after(remainder, ":");
             }
-            return ExpressionBuilder.propertiesComponentExpression(key, locations);
+            return ExpressionBuilder.propertiesComponentExpression(key, locations, null);
         }
 
         // ref: prefix

http://git-wip-us.apache.org/repos/asf/camel/blob/c8ca9f5b/camel-core/src/test/java/org/apache/camel/language/PropertyInjectAnnotationParameterTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/PropertyInjectAnnotationParameterTest.java b/camel-core/src/test/java/org/apache/camel/language/PropertyInjectAnnotationParameterTest.java
new file mode 100644
index 0000000..4399666
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/language/PropertyInjectAnnotationParameterTest.java
@@ -0,0 +1,118 @@
+/**
+ * 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.language;
+
+import java.util.Properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.PropertyInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.properties.PropertiesComponent;
+
+/**
+ *
+ */
+public class PropertyInjectAnnotationParameterTest extends ContextTestSupport {
+
+    public void testPropertyInjectAnnotationOne() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:one", "World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testPropertyInjectAnnotationTwo() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("WorldWorldWorld");
+
+        template.sendBody("direct:two", "World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testPropertyInjectAnnotationThree() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Goodbye World");
+
+        template.sendBody("direct:three", "World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        PropertiesComponent pc = new PropertiesComponent();
+        Properties props = new Properties();
+        props.put("greeting", "Hello");
+        props.put("times", "3");
+        pc.setInitialProperties(props);
+        context.addComponent("properties", pc);
+
+        return context;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:one")
+                    .bean(MyBean.class)
+                    .to("mock:result");
+
+                from("direct:two")
+                    .bean(MyCounterBean.class)
+                    .to("mock:result");
+
+                from("direct:three")
+                    .bean(MyOtherBean.class)
+                    .to("mock:result");
+            }
+        };
+    }
+
+    public static final class MyBean {
+
+        public String callA(@PropertyInject("greeting") String greeting, String body) {
+            return greeting + " " + body;
+        }
+
+    }
+
+    public static final class MyOtherBean {
+
+        public String callA(@PropertyInject(value = "bye", defaultValue = "Goodbye") String bye, String body) {
+            return bye + " " + body;
+        }
+
+    }
+
+    public static final class MyCounterBean {
+
+        public String callA(@PropertyInject("times") int times, String body) {
+            StringBuilder sb = new StringBuilder();
+            for (int i = 0; i < times; i++) {
+                sb.append(body);
+            }
+            return sb.toString();
+        }
+
+    }
+
+}