You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/04/24 16:54:08 UTC

svn commit: r531967 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ test/java/org/apache/camel/processor/

Author: jstrachan
Date: Tue Apr 24 07:54:06 2007
New Revision: 531967

URL: http://svn.apache.org/viewvc?view=rev&rev=531967
Log:
added the ability to use transformers in the DSL without having to be explicit with a Processor implementation

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformViaDSLTest.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?view=diff&rev=531967&r1=531966&r2=531967
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Tue Apr 24 07:54:06 2007
@@ -229,6 +229,23 @@
         };
     }
 
+
+    /**
+     * Appends the String evaluations of the two expressions together
+     */
+    public static <E extends Exchange> Expression<E> append(final Expression<E> left, final Expression<E> right) {
+        return new Expression<E>() {
+            public Object evaluate(E exchange) {
+                return evaluateStringExpression(left, exchange) + evaluateStringExpression(right, exchange);
+            }
+
+            @Override
+            public String toString() {
+                return "append(" + left + ", " + right + ")";
+            }
+        };
+    }
+
     /**
      * Evaluates the expression on the given exchange and returns the String representation
      *

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java?view=diff&rev=531967&r1=531966&r2=531967
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java Tue Apr 24 07:54:06 2007
@@ -145,16 +145,6 @@
     }
 
     /**
-     * Adds the custom processor to this destination which could be a final destination, or could be a transformation in a pipeline
-     */
-    @Fluent
-    public FromBuilder<E> process(@FluentArg("ref")Processor<E> processor) {
-        ConstantProcessorBuilder<E> answer = new ConstantProcessorBuilder<E>(processor);
-        addProcessBuilder(answer);
-        return this;
-    }
-
-    /**
      * Creates a predicate which is applied and only if it is true then
      * the exchange is forwarded to the destination
      *
@@ -262,6 +252,74 @@
         return answer.target();
     }
 
+    // Transformers
+    //-------------------------------------------------------------------------
+
+    /**
+     * Adds the custom processor to this destination which could be a final destination, or could be a transformation in a pipeline
+     */
+    @Fluent
+    public FromBuilder<E> process(@FluentArg("ref")Processor<E> processor) {
+        addProcessorBuilder(processor);
+        return this;
+    }
+
+    /**
+     * Adds a processor which sets the body on the IN message
+     */
+    @Fluent
+    public FromBuilder<E> setBody(Expression<E> expression) {
+        addProcessorBuilder(ProcessorBuilder.setBody(expression));
+        return this;
+    }
+
+    /**
+     * Adds a processor which sets the body on the IN message
+     */
+    @Fluent
+    public FromBuilder<E> setBody(ExpressionFactory<E> expressionFactory) {
+        return setBody(expressionFactory.createExpression());
+    }
+
+    /**
+     * Adds a processor which sets the body on the OUT message
+     */
+    @Fluent
+    public FromBuilder<E> setOutBody(Expression<E> expression) {
+        addProcessorBuilder(ProcessorBuilder.setOutBody(expression));
+        return this;
+    }
+
+    /**
+     * Adds a processor which sets the header on the IN message
+     */
+    @Fluent
+    public FromBuilder<E> setHeader(String name, Expression<E> expression) {
+        addProcessorBuilder(ProcessorBuilder.setHeader(name, expression));
+        return this;
+    }
+
+    /**
+     * Adds a processor which sets the header on the OUT message
+     */
+    @Fluent
+    public FromBuilder<E> setOutHeader(String name, Expression<E> expression) {
+        addProcessorBuilder(ProcessorBuilder.setOutHeader(name, expression));
+        return this;
+    }
+
+
+    /**
+     * Adds a processor which sets the exchange property
+     */
+    @Fluent
+    public FromBuilder<E> setProperty(String name, Expression<E> expression) {
+        addProcessorBuilder(ProcessorBuilder.setProperty(name, expression));
+        return this;
+    }
+
+
+
     // Properties
     //-------------------------------------------------------------------------
     public RouteBuilder<E> getBuilder() {
@@ -275,6 +333,10 @@
     public ProcessorFactory<E> addProcessBuilder(ProcessorFactory<E> processFactory) {
         processFactories.add(processFactory);
         return processFactory;
+    }
+
+    protected void addProcessorBuilder(Processor<E> processor) {
+        addProcessBuilder(new ConstantProcessorBuilder<E>(processor));
     }
 
     public void addProcessor(Processor<E> processor) {

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java?view=auto&rev=531967
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java Tue Apr 24 07:54:06 2007
@@ -0,0 +1,115 @@
+/**
+ *
+ * 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.builder;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Processor;
+
+/**
+ * A builder of a number of different {@link Processor} implementations
+ *
+ * @version $Revision: 1.1 $
+ */
+public class ProcessorBuilder {
+
+    /**
+     * Creates a processor which sets the body of the IN message to the value of the expression
+     */
+    public static <E extends Exchange> Processor<E> setBody(final Expression<E> expression) {
+        return new Processor<E>() {
+            public void process(E exchange) {
+                Object newBody = expression.evaluate(exchange);
+                exchange.getIn().setBody(newBody);
+            }
+
+            @Override
+            public String toString() {
+                return "setBody(" + expression + ")";
+            }
+        };
+    }
+
+    /**
+     * Creates a processor which sets the body of the IN message to the value of the expression
+     */
+    public static <E extends Exchange> Processor<E> setOutBody(final Expression<E> expression) {
+        return new Processor<E>() {
+            public void process(E exchange) {
+                Object newBody = expression.evaluate(exchange);
+                exchange.getOut().setBody(newBody);
+            }
+
+            @Override
+            public String toString() {
+                return "setOutBody(" + expression + ")";
+            }
+        };
+    }
+
+    /**
+     * Sets the header on the IN message
+     */
+    public static <E extends Exchange> Processor<E> setHeader(final String name, final Expression<E> expression) {
+        return new Processor<E>() {
+            public void process(E exchange) {
+                Object value = expression.evaluate(exchange);
+                exchange.getIn().setHeader(name, value);
+            }
+
+            @Override
+            public String toString() {
+                return "setHeader(" + name + ", " + expression + ")";
+            }
+        };
+    }
+
+    /**
+     * Sets the header on the OUT message
+     */
+    public static <E extends Exchange> Processor<E> setOutHeader(final String name, final Expression<E> expression) {
+        return new Processor<E>() {
+            public void process(E exchange) {
+                Object value = expression.evaluate(exchange);
+                exchange.getOut().setHeader(name, value);
+            }
+
+            @Override
+            public String toString() {
+                return "setOutHeader(" + name + ", " + expression + ")";
+            }
+        };
+    }
+
+    /**
+     * Sets the property on the exchange
+     */
+    public static <E extends Exchange> Processor<E> setProperty(final String name, final Expression<E> expression) {
+        return new Processor<E>() {
+            public void process(E exchange) {
+                Object value = expression.evaluate(exchange);
+                exchange.setProperty(name, value);
+            }
+
+            @Override
+            public String toString() {
+                return "setProperty(" + name + ", " + expression + ")";
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ProcessorBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?view=diff&rev=531967&r1=531966&r2=531967
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java Tue Apr 24 07:54:06 2007
@@ -25,6 +25,7 @@
  *
  * @version $Revision: $
  */
+//public class ValueBuilder<E extends Exchange> implements Expression<E>, ExpressionFactory<E> {
 public class ValueBuilder<E extends Exchange> implements ExpressionFactory<E> {
     private Expression<E> expression;
 
@@ -32,6 +33,10 @@
         this.expression = expression;
     }
 
+    public Object evaluate(E exchange) {
+        return expression.evaluate(exchange);
+    }
+
     public Expression<E> getExpression() {
         return expression;
     }
@@ -111,7 +116,7 @@
     }
 
 
-    // Transformers
+    // Expression builders
     //-------------------------------------------------------------------------
 
     @Fluent
@@ -175,6 +180,16 @@
         return convertTo(String.class);
     }
 
+    /**
+     * Appends the string evaluation of this expression with the given value
+     * @param value the value or expression to append
+     * @return the current builder
+     */
+    @Fluent
+    public ValueBuilder<E> append(@FluentArg("value") Object value) {
+        return new ValueBuilder<E>(ExpressionBuilder.append(expression, asExpression(value)));
+    }
+
     
     // Implementation methods
     //-------------------------------------------------------------------------
@@ -199,5 +214,4 @@
             return ExpressionBuilder.constantExpression(value);
         }
     }
-
 }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformViaDSLTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformViaDSLTest.java?view=auto&rev=531967
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformViaDSLTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformViaDSLTest.java Tue Apr 24 07:54:06 2007
@@ -0,0 +1,55 @@
+/**
+ *
+ * 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.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class TransformViaDSLTest extends ContextTestSupport {
+    protected MockEndpoint resultEndpoint;
+
+    public void testSendingAMessageUsingMulticastReceivesItsOwnExchange() throws Exception {
+        resultEndpoint.expectedBodiesReceived("Hello World!");
+
+        send("direct:start", "Hello");
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        resultEndpoint = (MockEndpoint) resolveMandatoryEndpoint("mock:result");
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder<Exchange>() {
+            public void configure() {
+                // START SNIPPET: example
+                from("direct:start").setBody(body().append(" World!")).to("mock:result");
+                // END SNIPPET: example
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TransformViaDSLTest.java
------------------------------------------------------------------------------
    svn:eol-style = native