You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2018/09/14 07:30:46 UTC

[camel-k] branch master updated: Add support for XML routes #70

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 ea382b1  Add support for XML routes #70
ea382b1 is described below

commit ea382b1a966699466ea81ba91745feec6a0ea6e8
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Fri Sep 14 08:50:03 2018 +0200

    Add support for XML routes #70
---
 .../main/java/org/apache/camel/k/jvm/Routes.java   |  2 +-
 .../java/org/apache/camel/k/jvm/RoutesLoaders.java | 30 ++++++++++++++++++++++
 .../org/apache/camel/k/jvm/RoutesLoadersTest.java  | 17 ++++++++++++
 runtime/jvm/src/test/resources/routes.xml          |  7 +++++
 4 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Routes.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Routes.java
index 03c91e4..cfe0f13 100644
--- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Routes.java
+++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Routes.java
@@ -36,7 +36,7 @@ public final class Routes {
     }
 
     public static boolean isScripting(String resource) {
-        return resource.endsWith(".java") || resource.endsWith(".js") || resource.endsWith(".groovy");
+        return resource.endsWith(".java") || resource.endsWith(".js") || resource.endsWith(".groovy") || resource.endsWith(".xml");
     }
 
     public static RoutesLoader loaderForLanguage(String language) {
diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java
index 8f9409e..c0489ec 100644
--- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java
+++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java
@@ -35,6 +35,7 @@ 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.camel.model.RoutesDefinition;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.codehaus.groovy.control.CompilerConfiguration;
@@ -168,6 +169,35 @@ public enum RoutesLoaders implements RoutesLoader {
                 }
             };
         }
+    },
+    Xml {
+        @Override
+        public List<String> getSupportedLanguages() {
+            return Arrays.asList("xml");
+        }
+
+        @Override
+        public boolean test(String resource) {
+            String ext = StringUtils.substringAfterLast(resource, ".");
+            List<String> langs = getSupportedLanguages();
+
+            return langs.contains(ext);
+        }
+
+        @Override
+        public RouteBuilder load(String resource) throws Exception {
+            return new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    try (InputStream is = Routes.loadResourceAsInputStream(resource)) {
+                        final CamelContext context = getContext();
+                        final RoutesDefinition definitions = context.loadRoutesDefinition(is);
+
+                        setRouteCollection(definitions);
+                    }
+                }
+            };
+        }
     };
 
     // ********************************
diff --git a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
index e083464..d281085 100644
--- a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
+++ b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
@@ -112,6 +112,23 @@ public class RoutesLoadersTest {
         assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
     }
 
+    @Test
+    public void testLoadXml() throws Exception {
+        String resource = "classpath:routes.xml";
+        RoutesLoader loader = Routes.loaderForResource(resource);
+        RouteBuilder builder = loader.load(resource);
+
+        assertThat(loader).isSameAs(RoutesLoaders.Xml);
+        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() {
         Routes.loaderForResource("routes.js");
diff --git a/runtime/jvm/src/test/resources/routes.xml b/runtime/jvm/src/test/resources/routes.xml
new file mode 100644
index 0000000..b6672f1
--- /dev/null
+++ b/runtime/jvm/src/test/resources/routes.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<routes xmlns="http://camel.apache.org/schema/spring">
+  <route>
+    <from uri="timer:tick"/>
+    <to uri="log:info"/>
+  </route>
+</routes>
\ No newline at end of file