You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by he...@apache.org on 2015/03/20 16:43:20 UTC

camel git commit: [CAMEL-8523] Spring Boot should automagically load XML routes definitions from classpath.

Repository: camel
Updated Branches:
  refs/heads/master 9b1b92cf8 -> 2b6bf5f8c


[CAMEL-8523] Spring Boot should automagically load XML routes definitions from classpath.


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

Branch: refs/heads/master
Commit: 2b6bf5f8ca0c509b565a36c98e3e63219b77b7f0
Parents: 9b1b92c
Author: Henryk Konsek <he...@gmail.com>
Authored: Fri Mar 20 16:43:11 2015 +0100
Committer: Henryk Konsek <he...@gmail.com>
Committed: Fri Mar 20 16:43:11 2015 +0100

----------------------------------------------------------------------
 .../spring/boot/CamelAutoConfiguration.java     |  2 +-
 .../camel/spring/boot/RoutesCollector.java      | 25 ++++++++++++++++----
 .../spring/boot/CamelAutoConfigurationTest.java | 18 +++++++++++++-
 .../src/test/resources/camel/camelContext.xml   |  7 ++++++
 4 files changed, 46 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/2b6bf5f8/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
index aba9623..5fdac34 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
@@ -63,7 +63,7 @@ public class CamelAutoConfiguration {
     @ConditionalOnMissingBean(RoutesCollector.class)
     RoutesCollector routesCollector(ApplicationContext applicationContext) {
         Collection<CamelContextConfiguration> configurations = applicationContext.getBeansOfType(CamelContextConfiguration.class).values();
-        return new RoutesCollector(applicationContext, new ArrayList<CamelContextConfiguration>(configurations));
+        return new RoutesCollector(new ArrayList<CamelContextConfiguration>(configurations));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/2b6bf5f8/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
index b0c501a..72c4eda 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
@@ -20,11 +20,13 @@ import java.util.List;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.RoutesBuilder;
+import org.apache.camel.model.RoutesDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationListener;
 import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.core.io.Resource;
 
 public class RoutesCollector implements ApplicationListener<ContextRefreshedEvent> {
 
@@ -32,14 +34,11 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
 
     // Collaborators
 
-    private final ApplicationContext applicationContext;
-
     private final List<CamelContextConfiguration> camelContextConfigurations;
 
     // Constructors
 
-    public RoutesCollector(ApplicationContext applicationContext, List<CamelContextConfiguration> camelContextConfigurations) {
-        this.applicationContext = applicationContext;
+    public RoutesCollector(List<CamelContextConfiguration> camelContextConfigurations) {
         this.camelContextConfigurations = camelContextConfigurations;
     }
 
@@ -47,6 +46,7 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
 
     @Override
     public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
+        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
         CamelContext camelContext = contextRefreshedEvent.getApplicationContext().getBean(CamelContext.class);
         LOG.debug("Post-processing CamelContext bean: {}", camelContext.getName());
         for (RoutesBuilder routesBuilder : applicationContext.getBeansOfType(RoutesBuilder.class).values()) {
@@ -57,6 +57,9 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
                 throw new RuntimeException(e);
             }
         }
+
+        loadXmlRoutes(applicationContext, camelContext);
+
         if (camelContextConfigurations != null) {
             for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
                 LOG.debug("CamelContextConfiguration found. Invoking: {}", camelContextConfiguration);
@@ -70,4 +73,18 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
         }
     }
 
+    // Helpers
+
+    private void loadXmlRoutes(ApplicationContext applicationContext, CamelContext camelContext) {
+        try {
+            Resource[] xmlRoutes = applicationContext.getResources("classpath:camel/*.xml");
+            for (Resource xmlRoute : xmlRoutes) {
+                RoutesDefinition xmlDefinition = camelContext.loadRoutesDefinition(xmlRoute.getInputStream());
+                camelContext.addRouteDefinitions(xmlDefinition.getRoutes());
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/2b6bf5f8/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java
index ae04337..2b09479 100644
--- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java
@@ -18,10 +18,12 @@ package org.apache.camel.spring.boot;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.EndpointInject;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.Route;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -65,7 +67,8 @@ public class CamelAutoConfigurationTest extends Assert {
 
     // Spring context fixtures
 
-
+    @EndpointInject(uri = "mock:xmlAutoLoading")
+    MockEndpoint xmlAutoLoadingMock;
 
     // Tests
 
@@ -148,6 +151,19 @@ public class CamelAutoConfigurationTest extends Assert {
         assertEquals(message, receivedMessage);
     }
 
+    @Test
+    public void shouldLoadXmlRoutes() throws InterruptedException {
+        // Given
+        String message = "msg";
+        xmlAutoLoadingMock.expectedBodiesReceived(message);
+
+        // When
+        producerTemplate.sendBody("direct:xmlAutoLoading", message);
+
+        // Then
+        xmlAutoLoadingMock.assertIsSatisfied();
+    }
+
 }
 
 @Configuration

http://git-wip-us.apache.org/repos/asf/camel/blob/2b6bf5f8/components/camel-spring-boot/src/test/resources/camel/camelContext.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/test/resources/camel/camelContext.xml b/components/camel-spring-boot/src/test/resources/camel/camelContext.xml
new file mode 100644
index 0000000..21b424b
--- /dev/null
+++ b/components/camel-spring-boot/src/test/resources/camel/camelContext.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<routes xmlns="http://camel.apache.org/schema/spring">
+    <route>
+        <from uri="direct:xmlAutoLoading"/>
+        <to uri="mock:xmlAutoLoading"/>
+    </route>
+</routes>
\ No newline at end of file