camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] git commit: CAMEL-6340: Added cache for compiled groovy scripts to speedy performance. Thanks to Antoine DESSAIGNE for the patch.
Date Wed, 08 May 2013 08:59:29 GMT
CAMEL-6340: Added cache for compiled groovy scripts to speedy performance. Thanks to Antoine
DESSAIGNE for the patch.


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

Branch: refs/heads/camel-2.10.x
Commit: fe19edcb1988b6ecb664eb657c45eff7fc5cbf0d
Parents: c6ec319
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed May 8 10:52:57 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed May 8 10:59:21 2013 +0200

----------------------------------------------------------------------
 .../camel/language/groovy/GroovyExpression.java    |   54 ++++++++------
 .../camel/language/groovy/GroovyLanguage.java      |   17 ++++-
 2 files changed, 47 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/fe19edcb/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
----------------------------------------------------------------------
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 3f41f9e..616e3ee 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,14 +16,14 @@
  */
 package org.apache.camel.language.groovy;
 
-import java.util.AbstractMap;
-import java.util.Collections;
-import java.util.Set;
+import java.util.HashMap;
+import java.util.Map;
 
 import groovy.lang.Binding;
 import groovy.lang.GroovyShell;
 import groovy.lang.Script;
 import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.support.ExpressionSupport;
 import org.apache.camel.util.ExchangeHelper;
 
@@ -47,30 +47,38 @@ public class GroovyExpression extends ExpressionSupport {
     }
 
     public <T> T evaluate(Exchange exchange, Class<T> type) {
-        // use application classloader if given
-        ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
-        GroovyShell shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
-
-        // need to re-parse script due thread-safe with binding
-        Script script = shell.parse(text);
-        configure(exchange, script.getBinding());
-        Object value = script.evaluate(text);
+        Script script = instantiateScript(exchange);
+        script.setBinding(createBinding(exchange));
+        Object value = script.run();
 
         return exchange.getContext().getTypeConverter().convertTo(type, value);
     }
 
-    private void configure(Exchange exchange, final Binding binding) {
-        ExchangeHelper.populateVariableMap(exchange, new AbstractMap<String, Object>()
{
-            @Override
-            public Object put(String key, Object value) {
-                binding.setProperty(key, value);
-                return null;
-            }
+    @SuppressWarnings("unchecked")
+    private Script instantiateScript(Exchange exchange) {
+        // Get the script from the cache, or create a new instance
+        GroovyLanguage language = (GroovyLanguage) exchange.getContext().resolveLanguage("groovy");
+        Class<Script> scriptClass = language.getScriptFromCache(text);
+        if (scriptClass == null) {
+            ClassLoader cl = exchange.getContext().getApplicationContextClassLoader();
+            GroovyShell shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
+            scriptClass = shell.getClassLoader().parseClass(text);
+            language.addScriptToCache(text, scriptClass);
+        }
+
+        // New instance of the script
+        try {
+            return scriptClass.newInstance();
+        } catch (InstantiationException e) {
+            throw new RuntimeCamelException(e);
+        } catch (IllegalAccessException e) {
+            throw new RuntimeCamelException(e);
+        }
+    }
 
-            @Override
-            public Set<Entry<String, Object>> entrySet() {
-                return Collections.emptySet();
-            }
-        });
+    private Binding createBinding(Exchange exchange) {
+        Map<String, Object> variables = new HashMap<String, Object>();
+        ExchangeHelper.populateVariableMap(exchange, variables);
+        return new Binding(variables);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/fe19edcb/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 241c4fb..73d89a4 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
@@ -18,12 +18,18 @@ package org.apache.camel.language.groovy;
 
 import org.apache.camel.IsSingleton;
 import org.apache.camel.spi.Language;
+import org.apache.camel.util.LRUSoftCache;
+
+import groovy.lang.Script;
 
 /**
- * @version 
+ * @version
  */
 public class GroovyLanguage implements Language, IsSingleton {
 
+    // Cache used to stores the compiled scripts (aka their classes)
+    private final LRUSoftCache<String, Class<Script>> scriptCache = new LRUSoftCache<String,
Class<Script>>(1000);
+
     public static GroovyExpression groovy(String expression) {
         return new GroovyLanguage().createExpression(expression);
     }
@@ -39,4 +45,13 @@ public class GroovyLanguage implements Language, IsSingleton {
     public boolean isSingleton() {
         return true;
     }
+
+    Class<Script> getScriptFromCache(String script) {
+        return scriptCache.get(script);
+    }
+
+    void addScriptToCache(String script, Class<Script> scriptClass) {
+        scriptCache.put(script, scriptClass);
+    }
+
 }


Mime
View raw message