You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/03/29 14:04:58 UTC

[camel] branch CAMEL-19220/global-variables-without-setting-variables created (now 12a3788a6de)

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

nfilotto pushed a change to branch CAMEL-19220/global-variables-without-setting-variables
in repository https://gitbox.apache.org/repos/asf/camel.git


      at 12a3788a6de CAMEL-19220: camel-groovy - Avoid setting variables to initialize the binding

This branch includes the following new commits:

     new 12a3788a6de CAMEL-19220: camel-groovy - Avoid setting variables to initialize the binding

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[camel] 01/01: CAMEL-19220: camel-groovy - Avoid setting variables to initialize the binding

Posted by nf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch CAMEL-19220/global-variables-without-setting-variables
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 12a3788a6dee0d0f75f1c861c40367d545e14a03
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Wed Mar 29 16:04:25 2023 +0200

    CAMEL-19220: camel-groovy - Avoid setting variables to initialize the binding
---
 .../camel/language/groovy/GroovyExpression.java    | 27 ++++++++--------------
 1 file changed, 9 insertions(+), 18 deletions(-)

diff --git a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
index 3e118f68d3e..355b8084027 100644
--- a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
+++ b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.language.groovy;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
@@ -48,25 +47,25 @@ public class GroovyExpression extends ExpressionSupport {
 
     @Override
     public <T> T evaluate(Exchange exchange, Class<T> type) {
-        Script script = instantiateScript(exchange);
-        populateBinding(script.getBinding(), exchange);
+        Map<String, Object> globalVariables = new HashMap<>();
+        Script script = instantiateScript(exchange, globalVariables);
+        script.setBinding(createBinding(exchange, globalVariables));
         Object value = script.run();
 
         return exchange.getContext().getTypeConverter().convertTo(type, value);
     }
 
     @SuppressWarnings("unchecked")
-    private Script instantiateScript(Exchange exchange) {
+    private Script instantiateScript(Exchange exchange, Map<String, Object> globalVariables) {
         // Get the script from the cache, or create a new instance
         GroovyLanguage language = (GroovyLanguage) exchange.getContext().resolveLanguage("groovy");
         Set<GroovyShellFactory> shellFactories = exchange.getContext().getRegistry().findByType(GroovyShellFactory.class);
         GroovyShellFactory shellFactory = null;
         String fileName = null;
-        Map<String, Object> variables = Collections.emptyMap();
         if (shellFactories.size() == 1) {
             shellFactory = shellFactories.iterator().next();
             fileName = shellFactory.getFileName(exchange);
-            variables = shellFactory.getVariables(exchange);
+            globalVariables.putAll(shellFactory.getVariables(exchange));
         }
         final String key = fileName != null ? fileName + text : text;
         Class<Script> scriptClass = language.getScriptFromCache(key);
@@ -79,20 +78,12 @@ public class GroovyExpression extends ExpressionSupport {
             language.addScriptToCache(key, scriptClass);
         }
         // New instance of the script
-        Script script = ObjectHelper.newInstance(scriptClass, Script.class);
-        Binding binding = script.getBinding();
-        for (Map.Entry<String, Object> variableEntry : variables.entrySet()) {
-            binding.setVariable(variableEntry.getKey(), variableEntry.getValue());
-        }
-
-        return script;
+        return ObjectHelper.newInstance(scriptClass, Script.class);
     }
 
-    private void populateBinding(Binding binding, Exchange exchange) {
-        Map<String, Object> variables = new HashMap<>();
+    private Binding createBinding(Exchange exchange, Map<String, Object> globalVariables) {
+        Map<String, Object> variables = new HashMap<>(globalVariables);
         ExchangeHelper.populateVariableMap(exchange, variables, true);
-        for (Map.Entry<String, Object> variableEntry : variables.entrySet()) {
-            binding.setVariable(variableEntry.getKey(), variableEntry.getValue());
-        }
+        return new Binding(variables);
     }
 }