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

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

commit b7a28a434634dfa8fed84ac35cd2871c1519eadc
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Fri Feb 2 10:42:36 2024 +0100

    CAMEL-20378: Languages should be thread-safe and be configured only via 
properties array, all in the same way.
---
 .../apache/camel/jsonpath/JsonPathLanguage.java    | 162 ++++-----------------
 .../camel/jsonpath/JsonPathLanguageTest.java       |  14 +-
 .../camel/builder/ExpressionClauseSupport.java     |  76 +++++++---
 3 files changed, 85 insertions(+), 167 deletions(-)

diff --git 
a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
 
b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
index b8879cfcbec..6732297483f 100644
--- 
a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
+++ 
b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
@@ -21,125 +21,63 @@ import java.util.List;
 
 import com.jayway.jsonpath.JsonPath;
 import com.jayway.jsonpath.Option;
-import org.apache.camel.CamelContext;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.jsonpath.easypredicate.EasyPredicateParser;
-import org.apache.camel.spi.PropertyConfigurer;
 import org.apache.camel.spi.annotations.Language;
+import org.apache.camel.support.ExpressionToPredicateAdapter;
 import org.apache.camel.support.SingleInputTypedLanguageSupport;
-import org.apache.camel.support.component.PropertyConfigurerSupport;
 
 @Language("jsonpath")
-public class JsonPathLanguage extends SingleInputTypedLanguageSupport 
implements PropertyConfigurer {
-
-    private boolean suppressExceptions;
-    private boolean allowSimple = true;
-    private boolean allowEasyPredicate = true;
-    private boolean writeAsString;
-    private boolean unpackArray;
-    private Option[] options;
-
-    public boolean isSuppressExceptions() {
-        return suppressExceptions;
-    }
-
-    public void setSuppressExceptions(boolean suppressExceptions) {
-        this.suppressExceptions = suppressExceptions;
-    }
-
-    public boolean isAllowSimple() {
-        return allowSimple;
-    }
-
-    public void setAllowSimple(boolean allowSimple) {
-        this.allowSimple = allowSimple;
-    }
-
-    public boolean isAllowEasyPredicate() {
-        return allowEasyPredicate;
-    }
-
-    public void setAllowEasyPredicate(boolean allowEasyPredicate) {
-        this.allowEasyPredicate = allowEasyPredicate;
-    }
-
-    public boolean isWriteAsString() {
-        return writeAsString;
-    }
-
-    public void setWriteAsString(boolean writeAsString) {
-        this.writeAsString = writeAsString;
-    }
-
-    public boolean isUnpackArray() {
-        return unpackArray;
-    }
-
-    public void setUnpackArray(boolean unpackArray) {
-        this.unpackArray = unpackArray;
-    }
-
-    public Option[] getOptions() {
-        return options;
-    }
-
-    public void setOptions(Option... options) {
-        this.options = options;
-    }
+public class JsonPathLanguage extends SingleInputTypedLanguageSupport {
 
     @Override
     public Predicate createPredicate(String expression) {
-        JsonPathExpression answer = (JsonPathExpression) 
createExpression(expression);
-        answer.setPredicate(true);
-        return answer;
+        return 
ExpressionToPredicateAdapter.toPredicate(createExpression(expression));
     }
 
     @Override
     public Expression createExpression(String expression) {
-        JsonPathExpression answer = new JsonPathExpression(expression);
-        answer.setResultType(getResultType());
-        answer.setSuppressExceptions(suppressExceptions);
-        answer.setAllowSimple(allowSimple);
-        answer.setAllowEasyPredicate(allowEasyPredicate);
-        answer.setWriteAsString(writeAsString);
-        answer.setUnpackArray(unpackArray);
-        answer.setVariableName(getVariableName());
-        answer.setHeaderName(getHeaderName());
-        answer.setPropertyName(getPropertyName());
-        answer.setOptions(options);
-        answer.init(getCamelContext());
-        return answer;
+        return createExpression(expression, null);
     }
 
     @Override
     public Predicate createPredicate(String expression, Object[] properties) {
-        JsonPathExpression json = (JsonPathExpression) 
createExpression(expression, properties);
-        json.setPredicate(true);
-        return json;
+        return 
ExpressionToPredicateAdapter.toPredicate(doCreateJsonPathExpression(expression, 
properties, true));
     }
 
     @Override
     public Expression createExpression(String expression, Object[] properties) 
{
+        return doCreateJsonPathExpression(expression, properties, false);
+    }
+
+    protected Expression doCreateJsonPathExpression(String expression, 
Object[] properties, boolean predicate) {
         JsonPathExpression answer = new JsonPathExpression(expression);
+        answer.setPredicate(predicate);
         answer.setResultType(property(Class.class, properties, 0, 
getResultType()));
-        answer.setSuppressExceptions(property(boolean.class, properties, 1, 
suppressExceptions));
-        answer.setAllowSimple(property(boolean.class, properties, 2, 
allowSimple));
-        answer.setAllowEasyPredicate(property(boolean.class, properties, 3, 
allowEasyPredicate));
-        answer.setWriteAsString(property(boolean.class, properties, 4, 
writeAsString));
-        answer.setUnpackArray(property(boolean.class, properties, 5, 
unpackArray));
+        answer.setSuppressExceptions(property(boolean.class, properties, 1, 
false));
+        answer.setAllowSimple(property(boolean.class, properties, 2, true));
+        answer.setAllowEasyPredicate(property(boolean.class, properties, 3, 
true));
+        answer.setWriteAsString(property(boolean.class, properties, 4, false));
+        answer.setUnpackArray(property(boolean.class, properties, 5, false));
         answer.setHeaderName(property(String.class, properties, 6, 
getHeaderName()));
-        String option = (String) properties[7];
+        Object option = property(Object.class, properties, 7, null);
         if (option != null) {
             List<Option> list = new ArrayList<>();
-            for (String s : option.split(",")) {
-                
list.add(getCamelContext().getTypeConverter().convertTo(Option.class, s));
+            if (option instanceof String str) {
+                for (String s : str.split(",")) {
+                    
list.add(getCamelContext().getTypeConverter().convertTo(Option.class, s));
+                }
+            } else if (option instanceof Option opt) {
+                list.add(opt);
             }
             answer.setOptions(list.toArray(new Option[0]));
         }
         answer.setPropertyName(property(String.class, properties, 8, 
getPropertyName()));
         answer.setVariableName(property(String.class, properties, 9, 
getVariableName()));
-        answer.init(getCamelContext());
+        if (getCamelContext() != null) {
+            answer.init(getCamelContext());
+        }
         return answer;
     }
 
@@ -157,54 +95,4 @@ public class JsonPathLanguage extends 
SingleInputTypedLanguageSupport implements
         return true;
     }
 
-    @Override
-    public boolean configure(CamelContext camelContext, Object target, String 
name, Object value, boolean ignoreCase) {
-        if (target != this) {
-            throw new IllegalStateException("Can only configure our own 
instance !");
-        }
-
-        switch (ignoreCase ? name.toLowerCase() : name) {
-            case "resulttype":
-            case "resultType":
-                setResultType(PropertyConfigurerSupport.property(camelContext, 
Class.class, value));
-                return true;
-            case "suppressexceptions":
-            case "suppressExceptions":
-                
setSuppressExceptions(PropertyConfigurerSupport.property(camelContext, 
boolean.class, value));
-                return true;
-            case "allowsimple":
-            case "allowSimple":
-                
setAllowSimple(PropertyConfigurerSupport.property(camelContext, boolean.class, 
value));
-                return true;
-            case "alloweasypredicate":
-            case "allowEasyPredicate":
-                
setAllowEasyPredicate(PropertyConfigurerSupport.property(camelContext, 
boolean.class, value));
-                return true;
-            case "variablename":
-            case "variableName":
-                
setVariableName(PropertyConfigurerSupport.property(camelContext, String.class, 
value));
-                return true;
-            case "headername":
-            case "headerName":
-                setHeaderName(PropertyConfigurerSupport.property(camelContext, 
String.class, value));
-                return true;
-            case "propertyname":
-            case "propertyName":
-                
setPropertyName(PropertyConfigurerSupport.property(camelContext, String.class, 
value));
-                return true;
-            case "writeasstring":
-            case "writeAsString":
-                
setWriteAsString(PropertyConfigurerSupport.property(camelContext, 
boolean.class, value));
-                return true;
-            case "unpackarray":
-            case "unpackArray":
-                
setUnpackArray(PropertyConfigurerSupport.property(camelContext, boolean.class, 
value));
-                return true;
-            case "options":
-                setOptions(PropertyConfigurerSupport.property(camelContext, 
Option[].class, value));
-                return true;
-            default:
-                return false;
-        }
-    }
 }
diff --git 
a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
 
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
index 37cae41c8fa..7ae9fea32d0 100644
--- 
a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
+++ 
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
@@ -129,9 +129,9 @@ public class JsonPathLanguageTest extends CamelTestSupport {
         exchange.getIn().setBody(new File("src/test/resources/type.json"));
 
         JsonPathLanguage lan = (JsonPathLanguage) 
context.resolveLanguage("jsonpath");
-        lan.setOptions(Option.SUPPRESS_EXCEPTIONS);
 
-        Expression exp = lan.createExpression("$.foo");
+        Expression exp = lan.createExpression("$.foo",
+                new Object[] { null, null, null, null, null, null, null, 
Option.SUPPRESS_EXCEPTIONS });
         String nofoo = exp.evaluate(exchange, String.class);
 
         assertNull(nofoo);
@@ -143,10 +143,9 @@ public class JsonPathLanguageTest extends CamelTestSupport 
{
         exchange.getIn().setBody(new 
File("src/test/resources/expensive.json"));
 
         JsonPathLanguage language = (JsonPathLanguage) 
context.resolveLanguage("jsonpath");
-        language.setUnpackArray(true);
-        language.setResultType(String.class);
 
-        JsonPathExpression expression = (JsonPathExpression) 
language.createExpression("$.store.book");
+        JsonPathExpression expression = (JsonPathExpression) 
language.createExpression("$.store.book",
+                new Object[] { String.class, null, null, null, null, true });
         String json = (String) expression.evaluate(exchange);
 
         // check that a single json object is returned, not an array
@@ -159,10 +158,9 @@ public class JsonPathLanguageTest extends CamelTestSupport 
{
         exchange.getIn().setBody(new 
File("src/test/resources/expensive.json"));
 
         JsonPathLanguage language = (JsonPathLanguage) 
context.resolveLanguage("jsonpath");
-        language.setUnpackArray(false);
-        language.setResultType(String.class);
 
-        JsonPathExpression expression = (JsonPathExpression) 
language.createExpression("$.store.book");
+        JsonPathExpression expression = (JsonPathExpression) 
language.createExpression("$.store.book",
+                new Object[] { String.class, null, null, null, false });
         String json = (String) expression.evaluate(exchange);
 
         // check that an array is returned, not a single object
diff --git 
a/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
 
b/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
index fd3e07ad584..1054eec3ed6 100644
--- 
a/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
+++ 
b/core/camel-core-model/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
@@ -546,7 +546,9 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpath(String text, boolean suppressExceptions) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
         return expression(expression);
     }
 
@@ -560,8 +562,12 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpath(String text, boolean suppressExceptions, boolean 
allowSimple) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
-        expression.setAllowSimple(Boolean.toString(allowSimple));
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
+        if (allowSimple) {
+            expression.setAllowSimple("true");
+        }
         return expression(expression);
     }
 
@@ -589,7 +595,9 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpath(String text, boolean suppressExceptions, Class<?> 
resultType) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
         expression.setResultType(resultType);
         expression(expression);
         return result;
@@ -606,8 +614,12 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpath(String text, boolean suppressExceptions, boolean 
allowSimple, Class<?> resultType) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
-        expression.setAllowSimple(Boolean.toString(allowSimple));
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
+        if (allowSimple) {
+            expression.setAllowSimple("true");
+        }
         expression.setResultType(resultType);
         expression(expression);
         return result;
@@ -625,8 +637,12 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpath(String text, boolean suppressExceptions, boolean 
allowSimple, Class<?> resultType, String headerName) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
-        expression.setAllowSimple(Boolean.toString(allowSimple));
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
+        if (allowSimple) {
+            expression.setAllowSimple("true");
+        }
         expression.setResultType(resultType);
         expression.setHeaderName(headerName);
         expression(expression);
@@ -663,8 +679,10 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpathWriteAsString(String text, boolean suppressExceptions) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setWriteAsString(Boolean.toString(true));
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
+        expression.setWriteAsString("true");
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
         return expression(expression);
     }
 
@@ -678,8 +696,10 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpathWriteAsString(String text, boolean suppressExceptions, 
Class<?> resultType) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setWriteAsString(Boolean.toString(true));
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
+        expression.setWriteAsString("true");
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
         expression.setResultType(resultType);
         return expression(expression);
     }
@@ -694,9 +714,13 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpathWriteAsString(String text, boolean suppressExceptions, 
boolean allowSimple) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setWriteAsString(Boolean.toString(true));
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
-        expression.setAllowSimple(Boolean.toString(allowSimple));
+        expression.setWriteAsString("true");
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
+        if (allowSimple) {
+            expression.setAllowSimple("true");
+        }
         return expression(expression);
     }
 
@@ -711,9 +735,13 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpathWriteAsString(String text, boolean suppressExceptions, 
boolean allowSimple, String headerName) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setWriteAsString(Boolean.toString(true));
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
-        expression.setAllowSimple(Boolean.toString(allowSimple));
+        expression.setWriteAsString("true");
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
+        if (allowSimple) {
+            expression.setAllowSimple("true");
+        }
         expression.setHeaderName(headerName);
         return expression(expression);
     }
@@ -731,9 +759,13 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
     public T jsonpathWriteAsString(
             String text, boolean suppressExceptions, boolean allowSimple, 
String headerName, Class<?> resultType) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setWriteAsString(Boolean.toString(true));
-        expression.setSuppressExceptions(Boolean.toString(suppressExceptions));
-        expression.setAllowSimple(Boolean.toString(allowSimple));
+        expression.setWriteAsString("true");
+        if (suppressExceptions) {
+            expression.setSuppressExceptions("true");
+        }
+        if (allowSimple) {
+            expression.setAllowSimple("true");
+        }
         expression.setHeaderName(headerName);
         expression.setResultType(resultType);
         return expression(expression);
@@ -749,7 +781,7 @@ public class ExpressionClauseSupport<T> implements 
ExpressionFactoryAware, Predi
      */
     public T jsonpathUnpack(String text, Class<?> resultType) {
         JsonPathExpression expression = new JsonPathExpression(text);
-        expression.setUnpackArray(Boolean.toString(true));
+        expression.setUnpackArray("true");
         expression.setResultType(resultType);
         return expression(expression);
     }

Reply via email to