camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [09/10] git commit: CAMEL-6990: Added language to Java DSL builder so it can be used more easily in the DSL. See ticket for example.
Date Thu, 21 Nov 2013 12:56:32 GMT
CAMEL-6990: Added language to Java DSL builder so it can be used more easily in the DSL. See
ticket for example.


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

Branch: refs/heads/camel-2.12.x
Commit: 9c980dda2c80e1eaf28751799b820725b31add82
Parents: 2b21094
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Nov 21 12:22:13 2013 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Nov 21 13:10:10 2013 +0100

----------------------------------------------------------------------
 .../java/org/apache/camel/builder/Builder.java  |  8 ++++
 .../apache/camel/builder/BuilderSupport.java    |  9 +++-
 .../apache/camel/builder/ExpressionBuilder.java | 35 ++++++++++++++
 .../camel/processor/LanguageBuilderTest.java    | 48 ++++++++++++++++++++
 4 files changed, 99 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9c980dda/camel-core/src/main/java/org/apache/camel/builder/Builder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/Builder.java b/camel-core/src/main/java/org/apache/camel/builder/Builder.java
index b1ca550..850fc04 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/Builder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/Builder.java
@@ -89,6 +89,14 @@ public final class Builder {
     }
     
     /**
+     * Returns a constant expression
+     */
+    public static ValueBuilder language(String language, String expression) {
+        Expression exp = ExpressionBuilder.languageExpression(language, expression);
+        return new ValueBuilder(exp);
+    }
+
+    /**
      * Returns a simple expression  
      */
     public static ValueBuilder simple(String value) {

http://git-wip-us.apache.org/repos/asf/camel/blob/9c980dda/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java b/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
index 5de21d2..81b1101 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
@@ -137,7 +137,14 @@ public abstract class BuilderSupport {
     public ValueBuilder constant(Object value) {
         return Builder.constant(value);
     }
-    
+
+    /**
+     * Returns a language expression value builder
+     */
+    public ValueBuilder language(String language, String expression) {
+        return Builder.language(language, expression);
+    }
+
     /**
      * Returns a simple expression value builder
      */

http://git-wip-us.apache.org/repos/asf/camel/blob/9c980dda/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 33bffd9..cb81bc8 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
@@ -37,6 +37,7 @@ import org.apache.camel.Expression;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.Message;
 import org.apache.camel.NoSuchEndpointException;
+import org.apache.camel.NoSuchLanguageException;
 import org.apache.camel.Producer;
 import org.apache.camel.component.bean.BeanInvocation;
 import org.apache.camel.component.properties.PropertiesComponent;
@@ -693,6 +694,40 @@ public final class ExpressionBuilder {
     }
 
     /**
+     * Returns an expression for evaluating the expression/predicate using the given language
+     *
+     * @param expression  the expression or predicate
+     * @return an expression object which will evaluate the expression/predicate using the
given language
+     */
+    public static Expression languageExpression(final String language, final String expression)
{
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                Language lan = exchange.getContext().resolveLanguage(language);
+                if (lan != null) {
+                    return lan.createExpression(expression).evaluate(exchange, Object.class);
+                } else {
+                    throw new NoSuchLanguageException(language);
+                }
+            }
+
+            @Override
+            public boolean matches(Exchange exchange) {
+                Language lan = exchange.getContext().resolveLanguage(language);
+                if (lan != null) {
+                    return lan.createPredicate(expression).matches(exchange);
+                } else {
+                    throw new NoSuchLanguageException(language);
+                }
+            }
+
+            @Override
+            public String toString() {
+                return "language[" + language + ":" + expression + "]";
+            }
+        };
+    }
+
+    /**
      * Returns an expression for a type value
      *
      * @param name the type name

http://git-wip-us.apache.org/repos/asf/camel/blob/9c980dda/camel-core/src/test/java/org/apache/camel/processor/LanguageBuilderTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/LanguageBuilderTest.java
b/camel-core/src/test/java/org/apache/camel/processor/LanguageBuilderTest.java
new file mode 100644
index 0000000..ee7a20e
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/LanguageBuilderTest.java
@@ -0,0 +1,48 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version 
+ */
+public class LanguageBuilderTest extends ContextTestSupport {
+
+    public void testCamel() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:camel");
+        mock.expectedBodiesReceived("Hello Camel");
+
+        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:start", "Hello Camel");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .filter(language("simple", "${body} contains 'Camel'")).to("mock:camel");
+            }
+        };
+    }
+}


Mime
View raw message