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
The following commit(s) were added to refs/heads/master by this push:
new dc40969 CAMEL-15697: camel-joor - Camel expression langauge using
jOOR runtime java compiled.
dc40969 is described below
commit dc40969d2fd7ec02aadc4da044bb05d445ebfe97
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Oct 19 14:58:45 2020 +0200
CAMEL-15697: camel-joor - Camel expression langauge using jOOR runtime java
compiled.
---
.../apache/camel/catalog/docs/joor-language.adoc | 11 +++--
.../camel-joor/src/main/docs/joor-language.adoc | 11 +++--
.../apache/camel/language/joor/JoorCompiler.java | 9 +++-
.../apache/camel/language/joor/JoorExpression.java | 6 ++-
.../org/apache/camel/language/joor/JoorHelper.java | 14 ++++++
.../language/joor/JoorOptionalPredicateTest.java | 54 +++++++++++++++++++++
.../language/joor/JoorStreamPredicateTest.java | 56 ++++++++++++++++++++++
.../modules/languages/pages/joor-language.adoc | 11 +++--
8 files changed, 160 insertions(+), 12 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/joor-language.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/joor-language.adoc
index f1f20a0..2031f66 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/joor-language.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/joor-language.adoc
@@ -45,6 +45,7 @@ The jOOR language allows the following variables to be used
in the script:
| exchange | Exchange | The Camel Exchange
| message | Message | The Camel message
| body | Object | The message body
+| optionalBody | The body wrapped in `java.util.Optional`.
|===
== Functions
@@ -54,9 +55,12 @@ The jOOR language allows the following functions to be used
in the script:
[width="100%",cols="2,1m,7",options="header"]
|===
| Function | Description
-| bodyAs(type) | To convert the body to the given type
-| headerAs(name, type) | To convert the header with the name to the given type
-| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type
+| bodyAs(type) | To convert the body to the given type.
+| headerAs(name, type) | To convert the header with the name to the given type.
+| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type.
+| optionalBodyAs(type) | To convert the body to the given type returned
wrapped in `java.util.Optional`.
+| optionalHeaderAs(name, type) | To convert the header with the name to the
given type returned wrapped in `java.util.Optional`.
+| optionalExchangePropertyAs(name, type) | To convert the exchange property
with the name to the given type returned wrapped in `java.util.Optional`.
|===
These functions are convenient for getting the message body, header or
exchange properties as a specific Java type.
@@ -95,6 +99,7 @@ The jOOR language will automatic import from:
----
import java.util.*;
import java.util.concurrent.*;
+import java.util.stream.*;
import org.apache.camel.*;
import org.apache.camel.util.*;
----
diff --git a/components/camel-joor/src/main/docs/joor-language.adoc
b/components/camel-joor/src/main/docs/joor-language.adoc
index f1f20a0..2031f66 100644
--- a/components/camel-joor/src/main/docs/joor-language.adoc
+++ b/components/camel-joor/src/main/docs/joor-language.adoc
@@ -45,6 +45,7 @@ The jOOR language allows the following variables to be used
in the script:
| exchange | Exchange | The Camel Exchange
| message | Message | The Camel message
| body | Object | The message body
+| optionalBody | The body wrapped in `java.util.Optional`.
|===
== Functions
@@ -54,9 +55,12 @@ The jOOR language allows the following functions to be used
in the script:
[width="100%",cols="2,1m,7",options="header"]
|===
| Function | Description
-| bodyAs(type) | To convert the body to the given type
-| headerAs(name, type) | To convert the header with the name to the given type
-| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type
+| bodyAs(type) | To convert the body to the given type.
+| headerAs(name, type) | To convert the header with the name to the given type.
+| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type.
+| optionalBodyAs(type) | To convert the body to the given type returned
wrapped in `java.util.Optional`.
+| optionalHeaderAs(name, type) | To convert the header with the name to the
given type returned wrapped in `java.util.Optional`.
+| optionalExchangePropertyAs(name, type) | To convert the exchange property
with the name to the given type returned wrapped in `java.util.Optional`.
|===
These functions are convenient for getting the message body, header or
exchange properties as a specific Java type.
@@ -95,6 +99,7 @@ The jOOR language will automatic import from:
----
import java.util.*;
import java.util.concurrent.*;
+import java.util.stream.*;
import org.apache.camel.*;
import org.apache.camel.util.*;
----
diff --git
a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorCompiler.java
b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorCompiler.java
index c13ba31..01623d6 100644
---
a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorCompiler.java
+++
b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorCompiler.java
@@ -18,6 +18,7 @@ package org.apache.camel.language.joor;
import java.lang.reflect.Method;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
@@ -37,6 +38,8 @@ import org.slf4j.LoggerFactory;
public class JoorCompiler extends ServiceSupport implements StaticService {
+ // TODO: OptionalXXX
+
private static final Pattern BODY_AS_PATTERN =
Pattern.compile("bodyAs\\(([A-Za-z0-9.$]*)(.class)\\)");
private static final Pattern BODY_AS_PATTERN_NO_CLASS =
Pattern.compile("bodyAs\\(([A-Za-z0-9.$]*)\\)");
private static final Pattern HEADER_AS_PATTERN
@@ -88,7 +91,8 @@ public class JoorCompiler extends ServiceSupport implements
StaticService {
try {
LOG.trace(code);
Reflect ref = Reflect.compile(className, code);
- answer = ref.type().getMethod("evaluate", CamelContext.class,
Exchange.class, Message.class, Object.class);
+ answer = ref.type().getMethod("evaluate", CamelContext.class,
Exchange.class, Message.class, Object.class,
+ Optional.class);
} catch (Exception e) {
throw new JoorCompilationException(className, code, e);
}
@@ -114,6 +118,7 @@ public class JoorCompiler extends ServiceSupport implements
StaticService {
sb.append("\n");
sb.append("import java.util.*;\n");
sb.append("import java.util.concurrent.*;\n");
+ sb.append("import java.util.stream.*;\n");
sb.append("\n");
sb.append("import org.apache.camel.*;\n");
sb.append("import org.apache.camel.util.*;\n");
@@ -131,7 +136,7 @@ public class JoorCompiler extends ServiceSupport implements
StaticService {
sb.append("public class ").append(name).append(" {\n");
sb.append("\n");
sb.append(
- " public static Object evaluate(CamelContext context,
Exchange exchange, Message message, Object body) throws Exception {\n");
+ " public static Object evaluate(CamelContext context,
Exchange exchange, Message message, Object body, Optional optionalBody) throws
Exception {\n");
sb.append(" ");
if (!script.contains("return ")) {
sb.append("return ");
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 39aaddf..594d5ec 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
@@ -17,6 +17,7 @@
package org.apache.camel.language.joor;
import java.lang.reflect.Method;
+import java.util.Optional;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
@@ -82,8 +83,11 @@ public class JoorExpression extends ExpressionAdapter {
}
// optimize as we call the same method all the time so we dont want to
find the method every time as joor would do
// if you use its call method
+ Object body = exchange.getIn().getBody();
+ // in the rare case the body is already an optional
+ boolean optional = body instanceof Optional;
Object out = ObjectHelper.invokeMethod(method, null,
exchange.getContext(), exchange, exchange.getIn(),
- exchange.getIn().getBody());
+ body, optional ? body : Optional.ofNullable(body));
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/JoorHelper.java
b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorHelper.java
index 950431a..df0a7b7 100644
---
a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorHelper.java
+++
b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorHelper.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.language.joor;
+import java.util.Optional;
+
import org.apache.camel.Exchange;
/**
@@ -30,12 +32,24 @@ public final class JoorHelper {
return exchange.getMessage().getBody(type);
}
+ public static <T> Optional<T> optionalBodyAs(Exchange exchange, Class<T>
type) {
+ return Optional.ofNullable(exchange.getMessage().getBody(type));
+ }
+
public static <T> T headerAs(Exchange exchange, String name, Class<T>
type) {
return exchange.getMessage().getHeader(name, type);
}
+ public static <T> Optional<T> optionalHeaderAs(Exchange exchange, String
name, Class<T> type) {
+ return Optional.ofNullable(exchange.getMessage().getHeader(name,
type));
+ }
+
public static <T> T exchangePropertyAs(Exchange exchange, String name,
Class<T> type) {
return exchange.getProperty(name, type);
}
+ public static <T> Optional<T> optionalExchangePropertyAs(Exchange
exchange, String name, Class<T> type) {
+ return Optional.ofNullable(exchange.getProperty(name, type));
+ }
+
}
diff --git
a/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorOptionalPredicateTest.java
b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorOptionalPredicateTest.java
new file mode 100644
index 0000000..4296380
--- /dev/null
+++
b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorOptionalPredicateTest.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 JoorOptionalPredicateTest extends CamelTestSupport {
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .choice()
+ .when()
+ .joor("optionalBody.isPresent()")
+ .to("mock:body")
+ .otherwise()
+ .to("mock:null");
+ }
+ };
+ }
+
+ @Test
+ public void testPredicate() throws Exception {
+ getMockEndpoint("mock:body").expectedMessageCount(1);
+ getMockEndpoint("mock:null").expectedMessageCount(3);
+
+ template.sendBody("direct:start", null);
+ template.sendBody("direct:start", 5);
+ template.sendBody("direct:start", null);
+ template.sendBody("direct:start", null);
+
+ assertMockEndpointsSatisfied();
+ }
+
+}
diff --git
a/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorStreamPredicateTest.java
b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorStreamPredicateTest.java
new file mode 100644
index 0000000..e5ccfa1
--- /dev/null
+++
b/components/camel-joor/src/test/java/org/apache/camel/language/joor/JoorStreamPredicateTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.util.stream.Stream;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class JoorStreamPredicateTest extends CamelTestSupport {
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .choice()
+ .when()
+ .joor("Stream<Integer> s = (Stream<Integer>) body;
return s.filter((n) -> n > 10).findAny().isPresent()")
+ .to("mock:high")
+ .otherwise()
+ .to("mock:low");
+ }
+ };
+ }
+
+ @Test
+ public void testPredicate() throws Exception {
+ getMockEndpoint("mock:high").expectedMessageCount(3);
+ getMockEndpoint("mock:low").expectedMessageCount(1);
+
+ template.sendBody("direct:start", Stream.of(8, 6, 22, 6));
+ template.sendBody("direct:start", Stream.of(2, 3));
+ template.sendBody("direct:start", Stream.of(7, 2, 122, 444));
+ template.sendBody("direct:start", Stream.of(1, 2, 3, 99, 4));
+
+ assertMockEndpointsSatisfied();
+ }
+
+}
diff --git a/docs/components/modules/languages/pages/joor-language.adoc
b/docs/components/modules/languages/pages/joor-language.adoc
index 13f611e..d7b5790 100644
--- a/docs/components/modules/languages/pages/joor-language.adoc
+++ b/docs/components/modules/languages/pages/joor-language.adoc
@@ -47,6 +47,7 @@ The jOOR language allows the following variables to be used
in the script:
| exchange | Exchange | The Camel Exchange
| message | Message | The Camel message
| body | Object | The message body
+| optionalBody | The body wrapped in `java.util.Optional`.
|===
== Functions
@@ -56,9 +57,12 @@ The jOOR language allows the following functions to be used
in the script:
[width="100%",cols="2,1m,7",options="header"]
|===
| Function | Description
-| bodyAs(type) | To convert the body to the given type
-| headerAs(name, type) | To convert the header with the name to the given type
-| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type
+| bodyAs(type) | To convert the body to the given type.
+| headerAs(name, type) | To convert the header with the name to the given type.
+| exchangePropertyAs(name, type) | To convert the exchange property with the
name to the given type.
+| optionalBodyAs(type) | To convert the body to the given type returned
wrapped in `java.util.Optional`.
+| optionalHeaderAs(name, type) | To convert the header with the name to the
given type returned wrapped in `java.util.Optional`.
+| optionalExchangePropertyAs(name, type) | To convert the exchange property
with the name to the given type returned wrapped in `java.util.Optional`.
|===
These functions are convenient for getting the message body, header or
exchange properties as a specific Java type.
@@ -97,6 +101,7 @@ The jOOR language will automatic import from:
----
import java.util.*;
import java.util.concurrent.*;
+import java.util.stream.*;
import org.apache.camel.*;
import org.apache.camel.util.*;
----