You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/10/16 20:28:38 UTC

[camel] branch master updated: CAMEL-15697: camel-joor - Camel expression langauge using jOOR runtime java compiled.

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 6be0383  CAMEL-15697: camel-joor - Camel expression langauge using jOOR runtime java compiled.
6be0383 is described below

commit 6be038320241179ae1e8f9214e6857ff2126574d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Oct 16 22:28:02 2020 +0200

    CAMEL-15697: camel-joor - Camel expression langauge using jOOR runtime java compiled.
---
 .../apache/camel/language/joor/JoorExpression.java | 32 +++++++++++++++-------
 .../joor/JoorExpressionEvaluationException.java    | 26 ++++++++++++++++++
 2 files changed, 48 insertions(+), 10 deletions(-)

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 0f0e941..c024c23 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
@@ -16,11 +16,12 @@
  */
 package org.apache.camel.language.joor;
 
+import java.lang.reflect.Method;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
-import org.apache.camel.ExpressionEvaluationException;
+import org.apache.camel.Message;
 import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.support.ScriptHelper;
 import org.joor.Reflect;
@@ -34,8 +35,10 @@ public class JoorExpression extends ExpressionAdapter {
     private static Boolean JAVA8;
 
     private final String text;
+    private String className;
     private String code;
     private Reflect compiled;
+    private Method method;
 
     private Class<?> resultType;
     private boolean preCompile = true;
@@ -79,19 +82,22 @@ public class JoorExpression extends ExpressionAdapter {
         try {
             Reflect ref = compiled;
             if (ref == null) {
-                String fqn = nextFQN();
-                String eval = evalCode(exchange.getContext(), fqn, text);
-                ref = compile(fqn, eval);
+                this.className = nextFQN();
+                this.code = evalCode(exchange.getContext(), className, text);
+                LOG.trace(code);
+                ref = compile(className, code);
+                method = ref.type().getMethod("evaluate", CamelContext.class, Exchange.class, Message.class, Object.class);
             }
-            Object out = ref
-                    .call("evaluate", exchange.getContext(), exchange, exchange.getIn(), exchange.getIn().getBody()).get();
+            // 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 out = method.invoke(null, exchange.getContext(), exchange, exchange.getIn(), exchange.getIn().getBody());
             if (out != null && resultType != null) {
                 return exchange.getContext().getTypeConverter().convertTo(resultType, exchange, out);
             } else {
                 return out;
             }
         } catch (Exception e) {
-            throw new ExpressionEvaluationException(this, exchange, e);
+            throw new JoorExpressionEvaluationException(this, className, code, exchange, e);
         }
     }
 
@@ -107,10 +113,16 @@ public class JoorExpression extends ExpressionAdapter {
         }
 
         if (preCompile) {
-            String fqn = nextFQN();
-            this.code = evalCode(context, fqn, text);
+            this.className = nextFQN();
+            this.code = evalCode(context, className, text);
             LOG.debug(code);
-            this.compiled = compile(fqn, code);
+            try {
+                this.compiled = compile(className, code);
+                this.method = compiled.type().getMethod("evaluate", CamelContext.class, Exchange.class, Message.class,
+                        Object.class);
+            } catch (NoSuchMethodException e) {
+                throw new JoorCompilationException(className, code, e);
+            }
         }
     }
 
diff --git a/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorExpressionEvaluationException.java b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorExpressionEvaluationException.java
new file mode 100644
index 0000000..93ecf59
--- /dev/null
+++ b/components/camel-joor/src/main/java/org/apache/camel/language/joor/JoorExpressionEvaluationException.java
@@ -0,0 +1,26 @@
+package org.apache.camel.language.joor;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.ExpressionEvaluationException;
+
+public class JoorExpressionEvaluationException extends ExpressionEvaluationException {
+
+    private final String className;
+    private final String code;
+
+    public JoorExpressionEvaluationException(Expression expression, String className, String code, Exchange exchange,
+                                             Throwable cause) {
+        super(expression, "jOOR evaluation error for class: " + className + " with code:\n" + code, exchange, cause);
+        this.className = className;
+        this.code = code;
+    }
+
+    public String getClassName() {
+        return className;
+    }
+
+    public String getCode() {
+        return code;
+    }
+}