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 16:11:32 UTC

[camel] branch main updated: CAMEL-19220: camel-groovy - Avoid setting variables to initialize the binding (#9692)

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

nfilotto 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 d708ff6d12e CAMEL-19220: camel-groovy - Avoid setting variables to initialize the binding (#9692)
d708ff6d12e is described below

commit d708ff6d12e8b3877a4b252462ca7c2c128e231b
Author: Nicolas Filotto <es...@users.noreply.github.com>
AuthorDate: Wed Mar 29 18:11:25 2023 +0200

    CAMEL-19220: camel-groovy - Avoid setting variables to initialize the binding (#9692)
    
    ## Motivation
    
    The changes made for [CAMEL-19212](https://issues.apache.org/jira/browse/CAMEL-19212) cause runtime errors in native mode https://github.com/apache/camel-quarkus/issues/4712.
    
    ## Modifications:
    
    * Rewrite the code to avoid calling `setVariable` on 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);
     }
 }