This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 1331a75b9484cd8331e1c4ba7a0d14b4c670d397 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Oct 16 14:00:34 2020 +0200 CAMEL-15697: camel-joor - Camel expression langauge using jOOR runtime java compiled. --- .../camel-joor/src/main/docs/joor-language.adoc | 1 + .../java/org/apache/camel/language/joor/Joor.java | 10 +++- .../joor/JoorAnnotationExpressionFactory.java | 49 ++++++++++++++++++++ .../apache/camel/language/joor/JoorExpression.java | 4 +- .../apache/camel/language/joor/JoorLanguage.java | 2 +- .../camel/language/joor/JoorPredicateTest.java | 54 ++++++++++++++++++++++ .../camel/language/joor/JoorTransformTest.java | 47 +++++++++++++++++++ .../org/apache/camel/builder/ExpressionClause.java | 14 ++++++ 8 files changed, 177 insertions(+), 4 deletions(-) diff --git a/components/camel-joor/src/main/docs/joor-language.adoc b/components/camel-joor/src/main/docs/joor-language.adoc index 573bfea..f1a75c8 100644 --- a/components/camel-joor/src/main/docs/joor-language.adoc +++ b/components/camel-joor/src/main/docs/joor-language.adoc @@ -43,6 +43,7 @@ The JOOR language allows the following variables to be used in the script | context | Context | The CamelContext | exchange | Exchange | The Camel Exchange | message | Message | The Camel message +| body | Object | The message body |=== === Sample diff --git a/components/camel-joor/src/main/java/org/apache/camel/language/joor/Joor.java b/components/camel-joor/src/main/java/org/apache/camel/language/joor/Joor.java index 4dbd65d..a781c23 100644 --- a/components/camel-joor/src/main/java/org/apache/camel/language/joor/Joor.java +++ b/components/camel-joor/src/main/java/org/apache/camel/language/joor/Joor.java @@ -30,7 +30,15 @@ import org.apache.camel.support.language.LanguageAnnotation; @Retention(RetentionPolicy.RUNTIME) @Documented @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) -@LanguageAnnotation(language = "joor") +@LanguageAnnotation(language = "joor", factory = JoorAnnotationExpressionFactory.class) public @interface Joor { + String value(); + + /** + * Whether single quotes can be used as replacement for double quotes. This is convenient when you need to work with + * strings inside strings. + */ + boolean singleQuotes() default true; + } diff --git a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorAnnotationExpressionFactory.java b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorAnnotationExpressionFactory.java new file mode 100644 index 0000000..9691fbc --- /dev/null +++ b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorAnnotationExpressionFactory.java @@ -0,0 +1,49 @@ +/* + * 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.joor; + +import java.lang.annotation.Annotation; + +import org.apache.camel.CamelContext; +import org.apache.camel.Expression; +import org.apache.camel.support.language.DefaultAnnotationExpressionFactory; +import org.apache.camel.support.language.LanguageAnnotation; + +public class JoorAnnotationExpressionFactory extends DefaultAnnotationExpressionFactory { + + @Override + public Expression createExpression( + CamelContext camelContext, Annotation annotation, + LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) { + + String expression = getExpressionFromAnnotation(annotation); + JoorExpression answer = new JoorExpression(JoorLanguage.nextFQN(), expression); + + if (expressionReturnType != null) { + answer.setResultType(expressionReturnType); + } + + if (annotation instanceof Joor) { + Joor joorAnnotation = (Joor) annotation; + answer.setSingleQuotes(joorAnnotation.singleQuotes()); + } + + answer.init(camelContext); + return answer; + } + +} diff --git a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorExpression.java b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorExpression.java index b33b8ed..026c8c8 100644 --- a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorExpression.java +++ b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorExpression.java @@ -87,7 +87,7 @@ public class JoorExpression extends ExpressionAdapter { sb.append("\n"); sb.append("\n"); sb.append( - " public static Object evaluate(CamelContext context, Exchange exchange, Message message) throws Exception {\n"); + " public static Object evaluate(CamelContext context, Exchange exchange, Message message, Object body) throws Exception {\n"); sb.append(" "); if (!text.contains("return ")) { sb.append("return "); @@ -116,7 +116,7 @@ public class JoorExpression extends ExpressionAdapter { @Override public Object evaluate(Exchange exchange) { try { - Object out = compiled.call("evaluate", exchange.getContext(), exchange, exchange.getIn()).get(); + Object out = compiled.call("evaluate", exchange.getContext(), exchange, exchange.getIn(), exchange.getIn().getBody()).get(); if (out != null && resultType != null) { return exchange.getContext().getTypeConverter().convertTo(resultType, exchange, out); } else { diff --git a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorLanguage.java b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorLanguage.java index 19933f5..8f9db80 100644 --- a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorLanguage.java +++ b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorLanguage.java @@ -76,7 +76,7 @@ public class JoorLanguage extends LanguageSupport { return exp; } - private static String nextFQN() { + static String nextFQN() { return "org.apache.camel.language.joor.compiled.JoorLanguage" + COUNTER.incrementAndGet(); } } diff --git a/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorPredicateTest.java b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorPredicateTest.java new file mode 100644 index 0000000..3e74a96 --- /dev/null +++ b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorPredicateTest.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.language.joor; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +public class JoorPredicateTest extends CamelTestSupport { + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .choice() + .when().joor("((int) body) / 2 > 10") + .to("mock:high") + .otherwise() + .to("mock:low"); + } + }; + } + + @Test + public void testPredicate() throws Exception { + getMockEndpoint("mock:high").expectedBodiesReceived(44, 123); + getMockEndpoint("mock:low").expectedBodiesReceived(1, 18, 6); + + template.sendBody("direct:start", 44); + template.sendBody("direct:start", 1); + template.sendBody("direct:start", 18); + template.sendBody("direct:start", 123); + template.sendBody("direct:start", 6); + + assertMockEndpointsSatisfied(); + } + +} diff --git a/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorTransformTest.java b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorTransformTest.java new file mode 100644 index 0000000..00d2591 --- /dev/null +++ b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorTransformTest.java @@ -0,0 +1,47 @@ +/* + * 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.joor; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +public class JoorTransformTest extends CamelTestSupport { + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .transform().joor("'Hello ' + body") + .to("mock:result"); + } + }; + } + + @Test + public void testHello() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World", "Hello Camel"); + + template.sendBody("direct:start", "World"); + template.sendBody("direct:start", "Camel"); + + assertMockEndpointsSatisfied(); + } + +} diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/builder/ExpressionClause.java b/core/camel-core-engine/src/main/java/org/apache/camel/builder/ExpressionClause.java index e3fd6b1..0f8d3d3 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/builder/ExpressionClause.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/builder/ExpressionClause.java @@ -330,6 +330,20 @@ public class ExpressionClause<T> implements Expression, Predicate { } /** + * Returns a JOOR expression value builder + */ + public T joor(String value) { + return delegate.joor(value); + } + + /** + * Returns a JOOR expression value builder + */ + public T joor(String value, Class<?> resultType) { + return delegate.joor(value, resultType); + } + + /** * Evaluates a <a href="http://camel.apache.org/jsonpath.html">Json Path expression</a> * * @param text the expression to be evaluated
