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/d7e84ed0 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d7e84ed0 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d7e84ed0 Branch: refs/heads/camel-2.18.x Commit: d7e84ed092721fc425f008b1e917df5ad7cf51db Parents: 2dae076 Author: Zoran Regvart <zo...@regvart.com> Authored: Fri Jan 20 20:34:51 2017 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Jan 20 22:27:19 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/d7e84ed0/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)); } }