Author: davsclaus
Date: Mon Oct 22 12:40:45 2012
New Revision: 1400863

URL: http://svn.apache.org/viewvc?rev=1400863&view=rev
Log:
CAMEL-4171: Fixed groovy language to work in OSGi.

Modified:
    camel/trunk/components/camel-groovy/pom.xml
    
camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
    
camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java

Modified: camel/trunk/components/camel-groovy/pom.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-groovy/pom.xml?rev=1400863&r1=1400862&r2=1400863&view=diff
==============================================================================
--- camel/trunk/components/camel-groovy/pom.xml (original)
+++ camel/trunk/components/camel-groovy/pom.xml Mon Oct 22 12:40:45 2012
@@ -30,7 +30,7 @@
   <description>Camel Groovy support</description>
 
   <properties>
-       
<camel.osgi.export.pkg>org.apache.camel.language.groovy.*</camel.osgi.export.pkg>
+         
<camel.osgi.export.pkg>org.apache.camel.language.groovy.*</camel.osgi.export.pkg>
   </properties>
 
   <dependencies>

Modified: 
camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java?rev=1400863&r1=1400862&r2=1400863&view=diff
==============================================================================
--- 
camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
 (original)
+++ 
camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyExpression.java
 Mon Oct 22 12:40:45 2012
@@ -21,8 +21,8 @@ import java.util.Collections;
 import java.util.Set;
 
 import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
 import groovy.lang.Script;
-
 import org.apache.camel.Exchange;
 import org.apache.camel.support.ExpressionSupport;
 import org.apache.camel.util.ExchangeHelper;
@@ -31,11 +31,9 @@ import org.apache.camel.util.ExchangeHel
  * @version 
  */
 public class GroovyExpression extends ExpressionSupport {
-    private Class<Script> scriptType;
-    private String text;
+    private final String text;
 
-    public GroovyExpression(Class<Script> scriptType, String text) {
-        this.scriptType = scriptType;
+    public GroovyExpression(String text) {
         this.text = text;
     }
 
@@ -49,15 +47,19 @@ public class GroovyExpression extends Ex
     }
 
     public <T> T evaluate(Exchange exchange, Class<T> type) {
-        Script script = ExchangeHelper.newInstance(exchange, scriptType);
-        // lets configure the script
-        configure(exchange, script);
-        Object value = script.run();
+        // 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);
+
         return exchange.getContext().getTypeConverter().convertTo(type, value);
     }
 
-    private void configure(Exchange exchange, Script script) {
-        final Binding binding = script.getBinding();
+    private void configure(Exchange exchange, final Binding binding) {
         ExchangeHelper.populateVariableMap(exchange, new AbstractMap<String, 
Object>() {
             @Override
             public Object put(String key, Object value) {

Modified: 
camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java?rev=1400863&r1=1400862&r2=1400863&view=diff
==============================================================================
--- 
camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
 (original)
+++ 
camel/trunk/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyLanguage.java
 Mon Oct 22 12:40:45 2012
@@ -16,8 +16,6 @@
  */
 package org.apache.camel.language.groovy;
 
-import groovy.lang.GroovyClassLoader;
-import groovy.lang.Script;
 import org.apache.camel.IsSingleton;
 import org.apache.camel.spi.Language;
 
@@ -35,13 +33,7 @@ public class GroovyLanguage implements L
     }
 
     public GroovyExpression createExpression(String expression) {
-        Class<Script> scriptType = parseExpression(expression);
-        return new GroovyExpression(scriptType, expression);
-    }
-
-    @SuppressWarnings("unchecked")
-    protected Class<Script> parseExpression(String expression) {
-        return new GroovyClassLoader().parseClass(expression);
+        return new GroovyExpression(expression);
     }
 
     public boolean isSingleton() {


Reply via email to