This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 2d05e1a90d7 CAMEL-20150: camel-jq - Optimize to load functions once 
and use child… (#12191)
2d05e1a90d7 is described below

commit 2d05e1a90d725f056f8509931d93cebf599547cd
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 24 10:52:23 2023 +0100

    CAMEL-20150: camel-jq - Optimize to load functions once and use child… 
(#12191)
    
    * CAMEL-20150: camel-jq - Optimize to load functions once and use child 
scopes per exchange
    
    * Fix xref by not using xref
---
 .../org/apache/camel/language/jq/JqExpression.java | 47 +++++++++-------------
 .../org/apache/camel/language/jq/JqLanguage.java   | 25 +++++++++++-
 .../apache/camel/language/jq/JqExpressionTest.java | 14 -------
 .../modules/languages/pages/simple-language.adoc   |  2 +-
 4 files changed, 44 insertions(+), 44 deletions(-)

diff --git 
a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java
 
b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java
index 06689ca9bfd..f66c0f8f1ed 100644
--- 
a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java
+++ 
b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java
@@ -29,12 +29,12 @@ import net.thisptr.jackson.jq.Versions;
 import net.thisptr.jackson.jq.exception.JsonQueryException;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
+import org.apache.camel.ExpressionIllegalSyntaxException;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.NoSuchHeaderOrPropertyException;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.spi.ExpressionResultTypeAware;
-import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.support.MessageHelper;
 
@@ -62,35 +62,28 @@ public class JqExpression extends ExpressionAdapter 
implements ExpressionResultT
 
     @Override
     public void init(CamelContext camelContext) {
-        super.init(camelContext);
-
-        this.typeConverter = camelContext.getTypeConverter();
-
-        if (this.scope == null) {
-            this.scope = CamelContextHelper.findSingleByType(camelContext, 
Scope.class);
-        }
-
-        if (this.scope == null) {
-            // if no scope is explicit set or no scope is found in the 
registry,
-            // then a local scope is created and functions are loaded from the
-            // jackson-jq library and the component.
-            this.scope = Scope.newEmptyScope();
+        // avoid initializing multiple times
+        if (this.query == null) {
+            super.init(camelContext);
 
-            JqFunctions.load(camelContext, scope);
-            JqFunctions.loadLocal(scope);
-        }
+            if (this.scope == null) {
+                JqLanguage lan = (JqLanguage) 
camelContext.resolveLanguage("jq");
+                this.scope = Scope.newChildScope(lan.getRootScope());
+            }
 
-        try {
-            this.query = JsonQuery.compile(this.expression, Versions.JQ_1_6);
-        } catch (JsonQueryException e) {
-            throw new RuntimeException(e);
-        }
+            this.typeConverter = camelContext.getTypeConverter();
+            try {
+                this.query = JsonQuery.compile(this.expression, 
Versions.JQ_1_6);
+            } catch (JsonQueryException e) {
+                throw new ExpressionIllegalSyntaxException(this.expression, e);
+            }
 
-        if (resultTypeName != null && (resultType == null || resultType == 
Object.class)) {
-            resultType = 
camelContext.getClassResolver().resolveClass(resultTypeName);
-        }
-        if (resultType == null || resultType == Object.class) {
-            resultType = JsonNode.class;
+            if (resultTypeName != null && (resultType == null || resultType == 
Object.class)) {
+                resultType = 
camelContext.getClassResolver().resolveClass(resultTypeName);
+            }
+            if (resultType == null || resultType == Object.class) {
+                resultType = JsonNode.class;
+            }
         }
     }
 
diff --git 
a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqLanguage.java
 
b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqLanguage.java
index 273adf78734..c5ef627cf27 100644
--- 
a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqLanguage.java
+++ 
b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqLanguage.java
@@ -16,16 +16,37 @@
  */
 package org.apache.camel.language.jq;
 
+import net.thisptr.jackson.jq.Scope;
+import net.thisptr.jackson.jq.module.loaders.BuiltinModuleLoader;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.StaticService;
 import org.apache.camel.spi.annotations.Language;
 import org.apache.camel.support.ExpressionToPredicateAdapter;
 import org.apache.camel.support.SingleInputTypedLanguageSupport;
+import org.apache.camel.util.ObjectHelper;
 
 @Language("jq")
 public class JqLanguage extends SingleInputTypedLanguageSupport implements 
StaticService {
 
+    private Scope rootScope;
+
+    @Override
+    public void init() {
+        ObjectHelper.notNull(getCamelContext(), "CamelContext", this);
+
+        if (this.rootScope == null) {
+            this.rootScope = Scope.newEmptyScope();
+            this.rootScope.setModuleLoader(BuiltinModuleLoader.getInstance());
+            JqFunctions.load(getCamelContext(), rootScope);
+            JqFunctions.loadLocal(rootScope);
+        }
+    }
+
+    public Scope getRootScope() {
+        return rootScope;
+    }
+
     @Override
     public void start() {
         // noop
@@ -48,7 +69,7 @@ public class JqLanguage extends 
SingleInputTypedLanguageSupport implements Stati
 
     @Override
     public Expression createExpression(String expression) {
-        JqExpression answer = new JqExpression(expression);
+        JqExpression answer = new JqExpression(Scope.newChildScope(rootScope), 
expression);
         answer.setResultType(getResultType());
         answer.setHeaderName(getHeaderName());
         answer.setPropertyName(getPropertyName());
@@ -58,7 +79,7 @@ public class JqLanguage extends 
SingleInputTypedLanguageSupport implements Stati
 
     @Override
     public Expression createExpression(String expression, Object[] properties) 
{
-        JqExpression answer = new JqExpression(expression);
+        JqExpression answer = new JqExpression(Scope.newChildScope(rootScope), 
expression);
         answer.setResultType(property(Class.class, properties, 0, 
getResultType()));
         answer.setHeaderName(property(String.class, properties, 1, 
getHeaderName()));
         answer.setPropertyName(property(String.class, properties, 2, 
getPropertyName()));
diff --git 
a/components/camel-jq/src/test/java/org/apache/camel/language/jq/JqExpressionTest.java
 
b/components/camel-jq/src/test/java/org/apache/camel/language/jq/JqExpressionTest.java
index ae61fc93bdb..9e7fe8928cd 100644
--- 
a/components/camel-jq/src/test/java/org/apache/camel/language/jq/JqExpressionTest.java
+++ 
b/components/camel-jq/src/test/java/org/apache/camel/language/jq/JqExpressionTest.java
@@ -262,18 +262,4 @@ public class JqExpressionTest {
         }
     }
 
-    @Test
-    public void customScopeFromRegistry() throws Exception {
-        try (CamelContext context = new DefaultCamelContext()) {
-            Scope root = Scope.newEmptyScope();
-            context.getRegistry().bind("scope", root);
-
-            JqExpression expression = new JqExpression(".foo");
-            expression.init(context);
-
-            assertThat(expression.getScope()).isSameAs(root);
-            assertThat(expression.getScope().getParentScope()).isNull();
-            
assertThat(expression.getScope().getLocalFunctions()).doesNotContainKeys("header/1",
 "header/2");
-        }
-    }
 }
diff --git 
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
 
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
index 4325100dd0f..b2f43ee25c2 100644
--- 
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
+++ 
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
@@ -252,7 +252,7 @@ If no type is given the default is used. It is also 
possible to use a custom `Uu
 and bind the bean to the xref:manual::registry.adoc[Registry] with an id. For 
example `${uuid(myGenerator}`
 where the ID is _myGenerator_.
 
-|jq(exp) | Object | When working with JSon data, then this allows to use the 
xref:languages::jq-language.adoc[JQ] language
+|jq(exp) | Object | When working with JSon data, then this allows to use the 
JQ language
 for example to extract data from the message body (in JSon format). This 
requires having camel-jq JAR on the classpath.
 
 |=======================================================================

Reply via email to