Repository: camel Updated Branches: refs/heads/master a4828fa28 -> f9429f632
CAMEL-6098: Add script to DSL Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2e1cb1be Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2e1cb1be Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2e1cb1be Branch: refs/heads/master Commit: 2e1cb1bea1659e9a5f5c57175c5857b1eb2ce6ad Parents: a4828fa Author: Claus Ibsen <davscl...@apache.org> Authored: Sat Jul 11 10:24:57 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sat Jul 11 10:40:14 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/model/ProcessorDefinition.java | 28 ++++++- .../apache/camel/model/ScriptDefinition.java | 70 ++++++++++++++++ .../apache/camel/processor/ScriptProcessor.java | 86 ++++++++++++++++++++ .../camel/processor/TransformProcessor.java | 2 +- .../resources/org/apache/camel/model/jaxb.index | 1 + .../org/apache/camel/model/XmlParseTest.java | 8 ++ .../org/apache/camel/processor/ScriptTest.java | 54 ++++++++++++ .../resources/org/apache/camel/model/script.xml | 26 ++++++ .../spring/processor/SpringScriptTest.java | 50 ++++++++++++ .../apache/camel/spring/processor/script.xml | 37 +++++++++ 10 files changed, 360 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java index 5a0b79e..8aec8b9 100644 --- a/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java @@ -2840,7 +2840,7 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> * @return a expression builder clause to set the body */ public ExpressionClause<ProcessorDefinition<Type>> transform() { - ExpressionClause<ProcessorDefinition<Type>> clause = + ExpressionClause<ProcessorDefinition<Type>> clause = new ExpressionClause<ProcessorDefinition<Type>>((ProcessorDefinition<Type>) this); TransformDefinition answer = new TransformDefinition(clause); addOutput(answer); @@ -2848,6 +2848,32 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> } /** + * Executes a script (do not change the message body). + * + * @param expression the expression used as the script. + * @return the builder + */ + @SuppressWarnings("unchecked") + public Type script(Expression expression) { + ScriptDefinition answer = new ScriptDefinition(expression); + addOutput(answer); + return (Type) this; + } + + /** + * Executes a script (do not change the message body). + * + * @return a expression builder clause to use as script. + */ + public ExpressionClause<ProcessorDefinition<Type>> script() { + ExpressionClause<ProcessorDefinition<Type>> clause = + new ExpressionClause<ProcessorDefinition<Type>>((ProcessorDefinition<Type>) this); + ScriptDefinition answer = new ScriptDefinition(clause); + addOutput(answer); + return clause; + } + + /** * Adds a processor which sets the body on the FAULT message * * @param expression the expression used to set the body http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/camel-core/src/main/java/org/apache/camel/model/ScriptDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/ScriptDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ScriptDefinition.java new file mode 100644 index 0000000..93ded97 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/model/ScriptDefinition.java @@ -0,0 +1,70 @@ +/** + * 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.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +import org.apache.camel.Expression; +import org.apache.camel.Processor; +import org.apache.camel.model.language.ExpressionDefinition; +import org.apache.camel.processor.ScriptProcessor; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.RouteContext; + +/** + * Executes a script from a language which does not change the message body. + */ +@Metadata(label = "eip,transformation") +@XmlRootElement(name = "script") +@XmlAccessorType(XmlAccessType.FIELD) +public class ScriptDefinition extends NoOutputExpressionNode { + + public ScriptDefinition() { + } + + public ScriptDefinition(Expression expression) { + super(expression); + } + + @Override + public String toString() { + return "Script[" + getExpression() + "]"; + } + + @Override + public String getLabel() { + return "script[" + getExpression() + "]"; + } + + @Override + public Processor createProcessor(RouteContext routeContext) throws Exception { + Expression expr = getExpression().createExpression(routeContext); + return new ScriptProcessor(expr); + } + + /** + * Expression to return the transformed message body (the new message body to use) + */ + @Override + public void setExpression(ExpressionDefinition expression) { + // override to include javadoc what the expression is used for + super.setExpression(expression); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/camel-core/src/main/java/org/apache/camel/processor/ScriptProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/ScriptProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/ScriptProcessor.java new file mode 100644 index 0000000..0b545d4 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/processor/ScriptProcessor.java @@ -0,0 +1,86 @@ +/** + * 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.AsyncCallback; +import org.apache.camel.AsyncProcessor; +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.Traceable; +import org.apache.camel.spi.IdAware; +import org.apache.camel.support.ServiceSupport; +import org.apache.camel.util.AsyncProcessorHelper; +import org.apache.camel.util.ObjectHelper; + +/** + * A processor which executes the script as an expression and does not change the message body. + */ +public class ScriptProcessor extends ServiceSupport implements AsyncProcessor, Traceable, IdAware { + private String id; + private final Expression expression; + + public ScriptProcessor(Expression expression) { + ObjectHelper.notNull(expression, "expression", this); + this.expression = expression; + } + + public void process(Exchange exchange) throws Exception { + AsyncProcessorHelper.process(this, exchange); + } + + public boolean process(Exchange exchange, AsyncCallback callback) { + try { + expression.evaluate(exchange, Object.class); + } catch (Throwable e) { + exchange.setException(e); + } + + callback.done(true); + return true; + } + + @Override + public String toString() { + return "Script(" + expression + ")"; + } + + public String getTraceLabel() { + return "script[" + expression + "]"; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Expression getExpression() { + return expression; + } + + @Override + protected void doStart() throws Exception { + // noop + } + + @Override + protected void doStop() throws Exception { + // noop + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/camel-core/src/main/java/org/apache/camel/processor/TransformProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/TransformProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/TransformProcessor.java index fe82780..0b5e862 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/TransformProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/processor/TransformProcessor.java @@ -30,7 +30,7 @@ import org.apache.camel.util.ExchangeHelper; import org.apache.camel.util.ObjectHelper; /** - * A processor which sets the body on the OUT message with an {@link Expression} + * A processor which sets the body on the OUT message with an {@link Expression}. */ public class TransformProcessor extends ServiceSupport implements AsyncProcessor, Traceable, IdAware { private String id; http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/camel-core/src/main/resources/org/apache/camel/model/jaxb.index ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/org/apache/camel/model/jaxb.index b/camel-core/src/main/resources/org/apache/camel/model/jaxb.index index fc79da2..7bd585a 100644 --- a/camel-core/src/main/resources/org/apache/camel/model/jaxb.index +++ b/camel-core/src/main/resources/org/apache/camel/model/jaxb.index @@ -70,6 +70,7 @@ RouteContextRefDefinition RoutesDefinition RoutingSlipDefinition SamplingDefinition +ScriptDefinition SetBodyDefinition SetExchangePatternDefinition SetFaultBodyDefinition http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java b/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java index 7d20d5f..118e8d2 100644 --- a/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java +++ b/camel-core/src/test/java/org/apache/camel/model/XmlParseTest.java @@ -94,6 +94,14 @@ public class XmlParseTest extends XmlTestSupport { assertChildTo(route, "mock:end", 1); } + public void testParseScriptXml() throws Exception { + RouteDefinition route = assertOneRoute("script.xml"); + assertFrom(route, "direct:start"); + ScriptDefinition node = assertNthProcessorInstanceOf(ScriptDefinition.class, route, 0); + assertExpression(node.getExpression(), "groovy", "System.out.println(\"groovy was here\")"); + assertChildTo(route, "mock:end", 1); + } + public void testParseSetBodyXml() throws Exception { RouteDefinition route = assertOneRoute("setBody.xml"); assertFrom(route, "direct:start"); http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/camel-core/src/test/java/org/apache/camel/processor/ScriptTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/ScriptTest.java b/camel-core/src/test/java/org/apache/camel/processor/ScriptTest.java new file mode 100644 index 0000000..38af9553 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/ScriptTest.java @@ -0,0 +1,54 @@ +/** + * 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 ScriptTest extends ContextTestSupport { + protected MockEndpoint resultEndpoint; + + public void testScript() throws Exception { + resultEndpoint.expectedBodiesReceived("Hello"); + + sendBody("direct:start", "Hello"); + + resultEndpoint.assertIsSatisfied(); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + resultEndpoint = getMockEndpoint("mock:result"); + } + + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:start") + // should not affect the body + .script(body().append(" World!")) + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/camel-core/src/test/resources/org/apache/camel/model/script.xml ---------------------------------------------------------------------- diff --git a/camel-core/src/test/resources/org/apache/camel/model/script.xml b/camel-core/src/test/resources/org/apache/camel/model/script.xml new file mode 100644 index 0000000..88bbe03 --- /dev/null +++ b/camel-core/src/test/resources/org/apache/camel/model/script.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<routes id="camel" xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <script> + <groovy>System.out.println("groovy was here")</groovy> + </script> + <to uri="mock:end"/> + </route> +</routes> http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringScriptTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringScriptTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringScriptTest.java new file mode 100644 index 0000000..2d80510 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringScriptTest.java @@ -0,0 +1,50 @@ +/** + * 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.spring.processor; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.component.mock.MockEndpoint; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +/** + * @version + */ +public class SpringScriptTest extends ContextTestSupport { + + protected MockEndpoint resultEndpoint; + + public void testScript() throws Exception { + resultEndpoint.expectedBodiesReceived("Hello"); + + sendBody("direct:start", "Hello"); + + resultEndpoint.assertIsSatisfied(); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + resultEndpoint = getMockEndpoint("mock:result"); + } + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/spring/processor/script.xml"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/2e1cb1be/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/script.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/script.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/script.xml new file mode 100644 index 0000000..ce4122a --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/script.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <!-- START SNIPPET: example --> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <script> + <simple>Hello ${body}</simple> + </script> + <to uri="mock:result"/> + </route> + </camelContext> + <!-- END SNIPPET: example --> + +</beans>