You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2018/09/11 08:45:23 UTC

[GitHub] nicolaferraro closed pull request #19: runtime: add support for groovy

nicolaferraro closed pull request #19: runtime: add support for groovy
URL: https://github.com/apache/camel-k/pull/19
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/runtime/jvm/pom.xml b/runtime/jvm/pom.xml
index 678a468..1aeb457 100644
--- a/runtime/jvm/pom.xml
+++ b/runtime/jvm/pom.xml
@@ -41,6 +41,12 @@
             <artifactId>commons-lang3</artifactId>
             <version>${commons-lang.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.codehaus.groovy</groupId>
+            <artifactId>groovy</artifactId>
+            <version>${groovy.version}</version>
+            <optional>true</optional>
+        </dependency>
 
         <dependency>
             <groupId>junit</groupId>
diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RouteLoaders.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RouteLoaders.java
index c5b5b99..77cd3bc 100644
--- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RouteLoaders.java
+++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RouteLoaders.java
@@ -19,6 +19,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.Reader;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.function.Function;
@@ -27,20 +28,23 @@
 import javax.script.ScriptEngineManager;
 import javax.script.SimpleBindings;
 
+import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
+import groovy.util.DelegatingScript;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.codehaus.groovy.control.CompilerConfiguration;
 import org.joor.Reflect;
 
 public enum RouteLoaders implements RoutesLoader {
     JavaClass {
         @Override
         public boolean test(String resource) {
-            return !resource.endsWith(".java") && !resource.endsWith(".js")
-                && (resource.startsWith(Application.SCHEME_CLASSPATH) || resource.startsWith(Application.SCHEME_FILE));
+            return !isScripting(resource) && hasSupportedScheme(resource);
         }
 
         @Override
@@ -58,8 +62,7 @@ public RouteBuilder load(String resource) throws Exception {
     JavaSource {
         @Override
         public boolean test(String resource) {
-            return resource.endsWith(".java")
-                && (resource.startsWith(Application.SCHEME_CLASSPATH) || resource.startsWith(Application.SCHEME_FILE));
+            return isScripting(resource, "java") && hasSupportedScheme(resource);
         }
 
         @Override
@@ -76,8 +79,7 @@ public RouteBuilder load(String resource) throws Exception {
     JavaScript {
         @Override
         public boolean test(String resource) {
-            return resource.endsWith(".js")
-                && (resource.startsWith(Application.SCHEME_CLASSPATH) || resource.startsWith(Application.SCHEME_FILE));
+            return isScripting(resource, "js") && hasSupportedScheme(resource);
         }
 
         @Override
@@ -102,8 +104,36 @@ public void configure() throws Exception {
                 }
             };
         }
-    };
+    },
+    Groovy {
+        @Override
+        public boolean test(String resource) {
+            return isScripting(resource, "groovy") && hasSupportedScheme(resource);
+        }
+
+        @Override
+        public RouteBuilder load(String resource) throws Exception {
+            return new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    CompilerConfiguration cc = new CompilerConfiguration();
+                    cc.setScriptBaseClass(DelegatingScript.class.getName());
+
+                    ClassLoader cl = Thread.currentThread().getContextClassLoader();
+                    GroovyShell sh = new GroovyShell(cl, new Binding(), cc);
 
+                    try (InputStream is = is(resource)) {
+                        Reader reader = new InputStreamReader(is);
+                        DelegatingScript script = (DelegatingScript) sh.parse(reader);
+
+                        // set the delegate target
+                        script.setDelegate(new ScriptingDsl(this));
+                        script.run();
+                    }
+                }
+            };
+        }
+    };
 
     // ********************************
     //
@@ -112,6 +142,18 @@ public void configure() throws Exception {
     // TODO: move to a dedicate class
     // ********************************
 
+    public static boolean isScripting(String resource, String type) {
+        return type.startsWith(".") ? resource.endsWith(type) : resource.endsWith("." + type);
+    }
+
+    public static boolean isScripting(String resource) {
+        return resource.endsWith(".java") || resource.endsWith(".js") || resource.endsWith(".groovy");
+    }
+
+    public static boolean hasSupportedScheme(String resource) {
+        return resource.startsWith(Application.SCHEME_CLASSPATH) || resource.startsWith(Application.SCHEME_FILE);
+    }
+
     public static RoutesLoader loaderFor(String resource) {
         for (RoutesLoader loader: RouteLoaders.values()) {
             if (loader.test(resource)) {
@@ -160,4 +202,21 @@ public Component make(String scheme, String type) {
             return instance;
         }
     }
+
+    private static class ScriptingDsl {
+        private final RouteBuilder builder;
+
+        public final CamelContext context;
+        public final Components components;
+
+        public ScriptingDsl(RouteBuilder builder) {
+            this.builder = builder;
+            this.context = builder.getContext();
+            this.components = new Components(builder.getContext());
+        }
+
+        public RouteDefinition from(String endpoint) {
+            return builder.from(endpoint);
+        }
+    }
 }
diff --git a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RouteLoadersTest.java b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RouteLoadersTest.java
index f9cf4c7..4dcea3b 100644
--- a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RouteLoadersTest.java
+++ b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RouteLoadersTest.java
@@ -42,7 +42,6 @@ public void testLoadClass() throws Exception {
         assertThat(routes).hasSize(1);
         assertThat(routes.get(0).getInputs().get(0).getEndpointUri()).isEqualTo("timer:tick");
         assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
-        assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
     }
 
     @Test
@@ -60,11 +59,10 @@ public void testLoadJava() throws Exception {
         assertThat(routes).hasSize(1);
         assertThat(routes.get(0).getInputs().get(0).getEndpointUri()).isEqualTo("timer:tick");
         assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
-        assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
     }
 
     @Test
-    public void testLoadScript() throws Exception {
+    public void testLoadJavaScript() throws Exception {
         String resource = "classpath:/routes.js";
         RoutesLoader loader = RouteLoaders.loaderFor(resource);
         RouteBuilder builder = loader.load(resource);
@@ -80,6 +78,23 @@ public void testLoadScript() throws Exception {
         assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
     }
 
+    @Test
+    public void testLoadGroovy() throws Exception {
+        String resource = "classpath:/routes.groovy";
+        RoutesLoader loader = RouteLoaders.loaderFor(resource);
+        RouteBuilder builder = loader.load(resource);
+
+        assertThat(loader).isSameAs(RouteLoaders.Groovy);
+        assertThat(builder).isNotNull();
+
+        builder.configure();
+
+        List<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
+        assertThat(routes).hasSize(1);
+        assertThat(routes.get(0).getInputs().get(0).getEndpointUri()).isEqualTo("timer:tick");
+        assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
+    }
+
     @Test(expected = IllegalArgumentException.class)
     public void testResourceWithoutScheme() {
         RouteLoaders.loaderFor("routes.js");
diff --git a/runtime/jvm/src/test/resources/routes.groovy b/runtime/jvm/src/test/resources/routes.groovy
new file mode 100644
index 0000000..0f5600d
--- /dev/null
+++ b/runtime/jvm/src/test/resources/routes.groovy
@@ -0,0 +1,3 @@
+
+from('timer:tick')
+    .to('log:info')
\ No newline at end of file
diff --git a/runtime/pom.xml b/runtime/pom.xml
index 8cba9f5..bcef12e 100644
--- a/runtime/pom.xml
+++ b/runtime/pom.xml
@@ -23,6 +23,7 @@
         <assertj.version>3.11.1</assertj.version>
         <log4j2.version>2.11.0</log4j2.version>
         <slf4j.version>1.7.25</slf4j.version>
+        <groovy.version>2.5.2</groovy.version>
 
         <fabric8-maven-plugin.version>3.5.40</fabric8-maven-plugin.version>
     </properties>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services