This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.x by this push:
new 291d7365a46 provide a way to define global groovy variables (#9648)
291d7365a46 is described below
commit 291d7365a46559ffc25909f41c5ac968d95b87f9
Author: EvanMi <[email protected]>
AuthorDate: Tue Mar 28 13:18:15 2023 +0800
provide a way to define global groovy variables (#9648)
* 1. provide a way to define global groovy variables, without pollute the
camelContext and exchange.
2. don't need to new a binding, the binding is provided already.
* add javadoc on the interface method
* 1. fix java.util.Map import order code-style check problem
2. make getVariables method return empty map, not a null
* use empty map not the null value
---------
Co-authored-by: mipengcheng3 <[email protected]>
---
.../camel/language/groovy/GroovyExpression.java | 20 ++++++++++++----
.../camel/language/groovy/GroovyShellFactory.java | 12 ++++++++++
.../language/groovy/GroovyShellFactoryTest.java | 28 ++++++++++++++++++++++
3 files changed, 55 insertions(+), 5 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 a384c975cbd..3e118f68d3e 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,6 +16,7 @@
*/
package org.apache.camel.language.groovy;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -48,7 +49,7 @@ public class GroovyExpression extends ExpressionSupport {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
Script script = instantiateScript(exchange);
- script.setBinding(createBinding(exchange));
+ populateBinding(script.getBinding(), exchange);
Object value = script.run();
return exchange.getContext().getTypeConverter().convertTo(type, value);
@@ -61,9 +62,11 @@ public class GroovyExpression extends ExpressionSupport {
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);
}
final String key = fileName != null ? fileName + text : text;
Class<Script> scriptClass = language.getScriptFromCache(key);
@@ -75,14 +78,21 @@ public class GroovyExpression extends ExpressionSupport {
? shell.getClassLoader().parseClass(text, fileName) :
shell.getClassLoader().parseClass(text);
language.addScriptToCache(key, scriptClass);
}
-
// New instance of the script
- return ObjectHelper.newInstance(scriptClass, Script.class);
+ 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;
}
- private Binding createBinding(Exchange exchange) {
+ private void populateBinding(Binding binding, Exchange exchange) {
Map<String, Object> variables = new HashMap<>();
ExchangeHelper.populateVariableMap(exchange, variables, true);
- return new Binding(variables);
+ for (Map.Entry<String, Object> variableEntry : variables.entrySet()) {
+ binding.setVariable(variableEntry.getKey(),
variableEntry.getValue());
+ }
}
}
diff --git
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyShellFactory.java
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyShellFactory.java
index 491ea2f25a2..e3bc9ec4af6 100644
---
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyShellFactory.java
+++
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyShellFactory.java
@@ -16,6 +16,9 @@
*/
package org.apache.camel.language.groovy;
+import java.util.Collections;
+import java.util.Map;
+
import groovy.lang.GroovyShell;
import org.apache.camel.Exchange;
@@ -27,4 +30,13 @@ public interface GroovyShellFactory {
return null;
}
+ /**
+ * This method provide a way to define some global variables that will be
applied to all the groovy script context.
+ *
+ * @param exchange the camel exchange in process.
+ * @return the global variables that will be applied to all the groovy
script context.
+ */
+ default Map<String, Object> getVariables(Exchange exchange) {
+ return Collections.emptyMap();
+ }
}
diff --git
a/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyShellFactoryTest.java
b/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyShellFactoryTest.java
index e47c06ba75c..bd452e4d9d9 100644
---
a/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyShellFactoryTest.java
+++
b/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyShellFactoryTest.java
@@ -17,6 +17,8 @@
package org.apache.camel.language.groovy;
import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
import groovy.lang.GroovyShell;
import org.apache.camel.CamelContext;
@@ -30,6 +32,7 @@ import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class GroovyShellFactoryTest extends CamelTestSupport {
@@ -75,6 +78,31 @@ public class GroovyShellFactoryTest extends CamelTestSupport
{
.anyMatch(stackTraceElement ->
"Test.groovy".equals(stackTraceElement.getFileName())));
}
+ @Test
+ public void testGroovyShellFactoryVariables() {
+ SimpleRegistry registry = new SimpleRegistry();
+ registry.bind("groovyShellFactory", new GroovyShellFactory() {
+ @Override
+ public GroovyShell createGroovyShell(Exchange exchange) {
+ return new GroovyShell();
+ }
+
+ @Override
+ public Map<String, Object> getVariables(Exchange exchange) {
+ Map<String, Object> map = new HashMap<>();
+ map.put("key", "testValue");
+ return map;
+ }
+ });
+
+ CamelContext camelContext = new DefaultCamelContext(registry);
+
+ final DefaultExchange exchange = new DefaultExchange(camelContext);
+
+ final String res = GroovyLanguage.groovy("key").evaluate(exchange,
String.class);
+ assertEquals("testValue", res);
+ }
+
public static class Utils {
public static String dummy() {
return "foo";