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/23 19:00:40 UTC

[camel] 01/01: CAMEL-19189: camel-groovy - Adapt the code for native mode

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

nfilotto pushed a commit to branch CAMEL-19189/groovy-language-native-support
in repository https://gitbox.apache.org/repos/asf/camel.git

commit b6eaa742c31394e0cf1c09645524d415ece9ada1
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Thu Mar 23 19:59:45 2023 +0100

    CAMEL-19189: camel-groovy - Adapt the code for native mode
---
 .../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);
+        }
+    }
 }