You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/01/20 21:29:04 UTC

[3/3] camel git commit: CAMEL-10732 Remove from all caches when Groovy ...

CAMEL-10732 Remove from all caches when Groovy ...

...script is removed from Camel script cache

This commit wraps the `Class<Script>` in a `org.apache.camel.Service`
before placing it in the cache so that on removal from the cache `stop`
method would be invoked in which Groovy's `InvokerHelper` is used to
cleanup three other caches that still hold references to the Script
Class in question.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ff9ee2a5
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ff9ee2a5
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ff9ee2a5

Branch: refs/heads/camel-2.17.x
Commit: ff9ee2a55f613f0a43011175170fad065a7a9bf6
Parents: 4b1aafc
Author: Zoran Regvart <zo...@regvart.com>
Authored: Fri Jan 20 20:34:51 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Jan 20 22:27:38 2017 +0100

----------------------------------------------------------------------
 .../camel/language/groovy/GroovyLanguage.java   | 33 ++++++++++++++++++--
 1 file changed, 30 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ff9ee2a5/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
----------------------------------------------------------------------
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 a82cd0f..d9a8226 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
@@ -17,8 +17,10 @@
 package org.apache.camel.language.groovy;
 
 import groovy.lang.Script;
+import org.apache.camel.Service;
 import org.apache.camel.support.LanguageSupport;
 import org.apache.camel.util.LRUSoftCache;
+import org.codehaus.groovy.runtime.InvokerHelper;
 
 /**
  * @version
@@ -26,7 +28,26 @@ import org.apache.camel.util.LRUSoftCache;
 public class GroovyLanguage extends LanguageSupport {
 
     // Cache used to stores the compiled scripts (aka their classes)
-    private final LRUSoftCache<String, Class<Script>> scriptCache = new LRUSoftCache<String, Class<Script>>(1000);
+    private final LRUSoftCache<String, GroovyClassService> scriptCache = new LRUSoftCache<String, GroovyClassService>(16, 1000, true);
+
+    private static final class GroovyClassService implements Service {
+
+        private final Class<Script> script;
+
+        private GroovyClassService(Class<Script> script) {
+            this.script = script;
+        }
+
+        @Override
+        public void start() throws Exception {
+        }
+
+        @Override
+        public void stop() throws Exception {
+            InvokerHelper.removeClass(script);
+        }
+
+    }
 
     public static GroovyExpression groovy(String expression) {
         return new GroovyLanguage().createExpression(expression);
@@ -42,11 +63,17 @@ public class GroovyLanguage extends LanguageSupport {
     }
 
     Class<Script> getScriptFromCache(String script) {
-        return scriptCache.get(script);
+        final GroovyClassService cached = scriptCache.get(script);
+
+        if (cached == null) {
+            return null;
+        }
+
+        return cached.script;
     }
 
     void addScriptToCache(String script, Class<Script> scriptClass) {
-        scriptCache.put(script, scriptClass);
+        scriptCache.put(script, new GroovyClassService(scriptClass));
     }
 
 }