You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2023/03/22 11:32:22 UTC

[camel] 04/05: CAMEL-19014 - Fixing issue about concurrence on the SimpleLanguage

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

acosentino pushed a commit to branch camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit aeb95f016e0ce46981d8e5783d5c9aa49b7f220b
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Mar 22 12:03:40 2023 +0100

    CAMEL-19014 - Fixing issue about concurrence on the SimpleLanguage
---
 .../camel/support/builder/ExpressionBuilder.java   | 36 +++++++++++++---------
 1 file changed, 21 insertions(+), 15 deletions(-)

diff --git a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
index 1ec39556bf1..4fcad2a08c5 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
@@ -20,7 +20,6 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
@@ -1594,14 +1593,12 @@ public class ExpressionBuilder {
      * @return an expression which when evaluated will return the concatenated values
      */
     public static Expression concatExpression(final Collection<Expression> expressions, final String description) {
-
         for (Expression expression : expressions) {
-            if(expression instanceof ConstantExpressionAdapter){
+            if (expression instanceof ConstantExpressionAdapter) {
                 return concatExpressionOptimized(expressions, description);
             }
         }
-
-        return concatExpressionUnoptimized(expressions,description);
+        return concatExpressionUnoptimized(expressions, description);
     }
 
     /**
@@ -1654,14 +1651,15 @@ public class ExpressionBuilder {
      * @return an expression which when evaluated will return the concatenated values
      */
     private static Expression concatExpressionOptimized(final Collection<Expression> expressions, final String description) {
-
-
         return new ExpressionAdapter() {
-
             private Collection<Object> optimized;
+            private String optimizedValue;
 
             @Override
             public Object evaluate(Exchange exchange) {
+                if (optimizedValue != null) {
+                    return optimizedValue;
+                }
                 StringBuilder buffer = new StringBuilder();
                 Collection<?> col = optimized != null ? optimized : expressions;
                 for (Object obj : col) {
@@ -1671,8 +1669,8 @@ public class ExpressionBuilder {
                         if (text != null) {
                             buffer.append(text);
                         }
-                    } else {
-                        buffer.append((String) obj);
+                    } else if (obj != null) {
+                        buffer.append(obj);
                     }
                 }
                 return buffer.toString();
@@ -1680,8 +1678,9 @@ public class ExpressionBuilder {
 
             @Override
             public void init(CamelContext context) {
-                if(optimized == null) {
+                if (optimized == null) {
                     Collection<Object> preprocessedExpression = new ArrayList<>(expressions.size());
+                    boolean constantsOnly = true;
                     for (Expression expression : expressions) {
                         expression.init(context);
                         if (expression instanceof ConstantExpressionAdapter) {
@@ -1689,16 +1688,23 @@ public class ExpressionBuilder {
                             preprocessedExpression.add(value.toString());
                         } else {
                             preprocessedExpression.add(expression);
+                            constantsOnly = false;
                         }
                     }
-                    optimized = Collections.unmodifiableCollection(preprocessedExpression);
-                }
-                else{
+                    if (constantsOnly) {
+                        StringBuilder sb = new StringBuilder();
+                        for (Object o : preprocessedExpression) {
+                            sb.append(o);
+                        }
+                        optimizedValue = sb.toString();
+                    } else {
+                        optimized = preprocessedExpression;
+                    }
+                } else {
                     for (Expression expression : expressions) {
                         expression.init(context);
                     }
                 }
-
             }
 
             @Override