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 4efe6d2c974 CAMEL-19189: camel-groovy - Adapt the code for native mode
(#9620)
4efe6d2c974 is described below
commit 4efe6d2c974a63685b976ddeebd1c05a504b6624
Author: Nicolas Filotto <[email protected]>
AuthorDate: Thu Mar 23 21:57:28 2023 +0100
CAMEL-19189: camel-groovy - Adapt the code for native mode (#9620)
## Motivation
To use the Groovy language in native, we need to adapt the current code to
be able to provide all the pre-compiled groovy classes at build time.
## Modifications
* Add a `Builder` class to be able to create an immutable instance of
`GroovyLanguage` with all the precompiled Groovy expressions
---
.../camel/language/groovy/GroovyLanguage.java | 37 ++++++++++++++++++++--
1 file changed, 34 insertions(+), 3 deletions(-)
diff --git
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
index 145a752147f..77bf2ca8d42 100644
---
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
+++
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.language.groovy;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Map;
import groovy.lang.Binding;
@@ -32,8 +34,24 @@ import org.codehaus.groovy.runtime.InvokerHelper;
@Language("groovy")
public class GroovyLanguage extends TypedLanguageSupport implements
ScriptingLanguage {
- // Cache used to stores the compiled scripts (aka their classes)
- private final Map<String, GroovyClassService> scriptCache =
LRUCacheFactory.newLRUSoftCache(16, 1000, true);
+ /**
+ * In case the expression is referring to an external resource, it
indicates whether it is still needed to load the
+ * resource.
+ */
+ private final boolean loadExternalResource;
+ /**
+ * Cache used to stores the compiled scripts (aka their classes)
+ */
+ private final Map<String, GroovyClassService> scriptCache;
+
+ private GroovyLanguage(Map<String, GroovyClassService> scriptCache,
boolean loadExternalResource) {
+ this.scriptCache = scriptCache;
+ this.loadExternalResource = loadExternalResource;
+ }
+
+ public GroovyLanguage() {
+ this(LRUCacheFactory.newLRUSoftCache(16, 1000, true), true);
+ }
private static final class GroovyClassService implements Service {
@@ -72,7 +90,9 @@ public class GroovyLanguage extends TypedLanguageSupport
implements ScriptingLan
@Override
@SuppressWarnings("unchecked")
public <T> T evaluate(String script, Map<String, Object> bindings,
Class<T> resultType) {
- script = loadResource(script);
+ if (loadExternalResource) {
+ script = loadResource(script);
+ }
Class<Script> clazz = getScriptFromCache(script);
if (clazz == null) {
ClassLoader cl =
getCamelContext().getApplicationContextClassLoader();
@@ -103,4 +123,15 @@ public class GroovyLanguage extends TypedLanguageSupport
implements ScriptingLan
scriptCache.put(script, new GroovyClassService(scriptClass));
}
+ public static class Builder {
+ private final Map<String, GroovyClassService> cache = new HashMap<>();
+
+ public void addScript(String content, Class<Script> scriptClass) {
+ cache.put(content, new GroovyClassService(scriptClass));
+ }
+
+ public GroovyLanguage build() {
+ return new GroovyLanguage(Collections.unmodifiableMap(cache),
false);
+ }
+ }
}