Repository: camel
Updated Branches:
  refs/heads/master a174d7858 -> 0511f48cb


[CAMEL-8259] [Groovy] Added SPI hook to support customized creation of the 
GroovyShell.


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

Branch: refs/heads/master
Commit: 0511f48cb8d5a3744a8fe00b74efc0dc83196270
Parents: a174d78
Author: Henryk Konsek <[email protected]>
Authored: Tue Jan 20 22:10:03 2015 +0100
Committer: Henryk Konsek <[email protected]>
Committed: Tue Jan 20 22:10:03 2015 +0100

----------------------------------------------------------------------
 components/camel-groovy/pom.xml                 |  5 ++
 .../camel/language/groovy/GroovyExpression.java | 13 ++++-
 .../language/groovy/GroovyShellFactory.java     | 26 ++++++++++
 .../language/groovy/GroovyShellFactoryTest.java | 51 ++++++++++++++++++++
 4 files changed, 93 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0511f48c/components/camel-groovy/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-groovy/pom.xml b/components/camel-groovy/pom.xml
index 92d3886..73ba3e0 100644
--- a/components/camel-groovy/pom.xml
+++ b/components/camel-groovy/pom.xml
@@ -70,6 +70,11 @@
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   
   <build>

http://git-wip-us.apache.org/repos/asf/camel/blob/0511f48c/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 616e3ee..fe3a52a 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
@@ -18,6 +18,7 @@ package org.apache.camel.language.groovy;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
 import groovy.lang.Binding;
 import groovy.lang.GroovyShell;
@@ -60,8 +61,16 @@ public class GroovyExpression extends ExpressionSupport {
         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();
+            GroovyShell shell;
+            Set<GroovyShellFactory> shellFactories = 
exchange.getContext().getRegistry().findByType(GroovyShellFactory.class);
+            if (shellFactories.size() > 1) {
+                throw new IllegalStateException("Too many GroovyShellFactory 
instances found: " + shellFactories.size());
+            } else if (shellFactories.size() == 1) {
+                shell = 
shellFactories.iterator().next().createGroovyShell(exchange);
+            } else {
+                ClassLoader cl = 
exchange.getContext().getApplicationContextClassLoader();
+                shell = cl != null ? new GroovyShell(cl) : new GroovyShell();
+            }
             scriptClass = shell.getClassLoader().parseClass(text);
             language.addScriptToCache(text, scriptClass);
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/0511f48c/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyShellFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyShellFactory.java
 
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyShellFactory.java
new file mode 100644
index 0000000..4be114d
--- /dev/null
+++ 
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyShellFactory.java
@@ -0,0 +1,26 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.language.groovy;
+
+import groovy.lang.GroovyShell;
+import org.apache.camel.Exchange;
+
+public interface GroovyShellFactory {
+
+    GroovyShell createGroovyShell(Exchange exchange);
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/0511f48c/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyShellFactoryTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyShellFactoryTest.java
 
b/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyShellFactoryTest.java
new file mode 100644
index 0000000..31f8674
--- /dev/null
+++ 
b/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyShellFactoryTest.java
@@ -0,0 +1,51 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.language.groovy;
+
+import groovy.lang.GroovyShell;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.SimpleRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class GroovyShellFactoryTest extends CamelTestSupport {
+
+    @Test
+    public void testExpressionReturnsTheCorrectValue() {
+        // Given
+        GroovyShellFactory groovyShellFactory = mock(GroovyShellFactory.class);
+        
given(groovyShellFactory.createGroovyShell(any(Exchange.class))).willReturn(new 
GroovyShell());
+        SimpleRegistry registry = new SimpleRegistry();
+        registry.put("groovyShellFactory", groovyShellFactory);
+        CamelContext camelContext = new DefaultCamelContext(registry);
+
+        // When
+        assertExpression(GroovyLanguage.groovy("exchange.in.body"), new 
DefaultExchange(camelContext), null);
+
+        // Then
+        verify(groovyShellFactory).createGroovyShell(any(Exchange.class));
+    }
+
+}

Reply via email to