camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nferr...@apache.org
Subject [camel-k] branch master updated: runtime: add support for groovy
Date Tue, 11 Sep 2018 08:45:23 GMT
This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new 9851138  runtime: add support for groovy
9851138 is described below

commit 9851138be93901e74f225f8da561e0ed0b83316a
Author: lburgazzoli <lburgazzoli@gmail.com>
AuthorDate: Tue Sep 11 10:39:22 2018 +0200

    runtime: add support for groovy
---
 runtime/jvm/pom.xml                                |  6 ++
 .../java/org/apache/camel/k/jvm/RouteLoaders.java  | 73 +++++++++++++++++++---
 .../org/apache/camel/k/jvm/RouteLoadersTest.java   | 21 ++++++-
 runtime/jvm/src/test/resources/routes.groovy       |  3 +
 runtime/pom.xml                                    |  1 +
 5 files changed, 94 insertions(+), 10 deletions(-)

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 @@ package org.apache.camel.k.jvm;
 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.ScriptEngine;
 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 enum RouteLoaders implements RoutesLoader {
     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 enum RouteLoaders implements RoutesLoader {
     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 enum RouteLoaders implements RoutesLoader {
                 }
             };
         }
-    };
+    },
+    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 enum RouteLoaders implements RoutesLoader {
     // 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 enum RouteLoaders implements RoutesLoader {
             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 class RouteLoadersTest {
         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 class RouteLoadersTest {
         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 class RouteLoadersTest {
         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>


Mime
View raw message