Repository: camel
Updated Branches:
  refs/heads/camel-2.15.x a9201e41e -> 21107ffe4


[CAMEL-8460] Fixed double context refresh issue.

(cherry picked from commit 98902f02d143b6e81c8d7f0b8048b1788ecbcb50)


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

Branch: refs/heads/camel-2.15.x
Commit: 21107ffe44ff88828a4f8bcc777ec7797e885c5f
Parents: a9201e4
Author: Henryk Konsek <hekon...@gmail.com>
Authored: Tue May 12 18:12:01 2015 +0200
Committer: Henryk Konsek <hekon...@gmail.com>
Committed: Tue May 12 18:12:01 2015 +0200

----------------------------------------------------------------------
 .../spring/boot/CamelAutoConfiguration.java     |  2 +-
 .../camel/spring/boot/RoutesCollector.java      | 66 ++++++++++++++------
 .../parent/SpringBootWithParentContextTest.java | 51 +++++++++++++++
 3 files changed, 99 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/21107ffe/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 dc9abc8..0f444ac 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
@@ -68,7 +68,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/21107ffe/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..2b0a050 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
@@ -16,57 +16,85 @@
  */
 package org.apache.camel.spring.boot;
 
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
 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> {
 
+    // Static collaborators
+
     private static final Logger LOG = 
LoggerFactory.getLogger(RoutesCollector.class);
 
     // Collaborators
 
-    private final ApplicationContext applicationContext;
-
     private final List<CamelContextConfiguration> camelContextConfigurations;
 
     // Constructors
 
-    public RoutesCollector(ApplicationContext applicationContext, 
List<CamelContextConfiguration> camelContextConfigurations) {
-        this.applicationContext = applicationContext;
-        this.camelContextConfigurations = camelContextConfigurations;
+    public RoutesCollector(List<CamelContextConfiguration> 
camelContextConfigurations) {
+        this.camelContextConfigurations = new 
ArrayList<CamelContextConfiguration>(camelContextConfigurations);
     }
 
     // Overridden
 
     @Override
     public void onApplicationEvent(ContextRefreshedEvent 
contextRefreshedEvent) {
-        CamelContext camelContext = 
contextRefreshedEvent.getApplicationContext().getBean(CamelContext.class);
-        LOG.debug("Post-processing CamelContext bean: {}", 
camelContext.getName());
-        for (RoutesBuilder routesBuilder : 
applicationContext.getBeansOfType(RoutesBuilder.class).values()) {
+        ApplicationContext applicationContext = 
contextRefreshedEvent.getApplicationContext();
+        if (applicationContext.getParent() == null) {
+            CamelContext camelContext = 
contextRefreshedEvent.getApplicationContext().getBean(CamelContext.class);
+            LOG.debug("Post-processing CamelContext bean: {}", 
camelContext.getName());
+            for (RoutesBuilder routesBuilder : 
applicationContext.getBeansOfType(RoutesBuilder.class).values()) {
+                try {
+                    LOG.debug("Injecting following route into the 
CamelContext: {}", routesBuilder);
+                    camelContext.addRoutes(routesBuilder);
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+
+            loadXmlRoutes(applicationContext, camelContext);
+
+            if (camelContextConfigurations != null) {
+                for (CamelContextConfiguration camelContextConfiguration : 
camelContextConfigurations) {
+                    LOG.debug("CamelContextConfiguration found. Invoking: {}", 
camelContextConfiguration);
+                    
camelContextConfiguration.beforeApplicationStart(camelContext);
+                }
+            }
             try {
-                LOG.debug("Injecting following route into the CamelContext: 
{}", routesBuilder);
-                camelContext.addRoutes(routesBuilder);
+                camelContext.start();
             } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        }
-        if (camelContextConfigurations != null) {
-            for (CamelContextConfiguration camelContextConfiguration : 
camelContextConfigurations) {
-                LOG.debug("CamelContextConfiguration found. Invoking: {}", 
camelContextConfiguration);
-                camelContextConfiguration.beforeApplicationStart(camelContext);
+                throw new CamelSpringBootInitializationException(e);
             }
+        } else {
+            LOG.debug("Not at root context - defer adding routes");
         }
+    }
+
+    // Helpers
+
+    private void loadXmlRoutes(ApplicationContext applicationContext, 
CamelContext camelContext) {
+        LOG.debug("Started XML routes detection. Scanning classpath 
(/camel/*.xml)...");
         try {
-            camelContext.start();
+            Resource[] xmlRoutes = 
applicationContext.getResources("classpath:camel/*.xml");
+            for (Resource xmlRoute : xmlRoutes) {
+                RoutesDefinition xmlDefinition = 
camelContext.loadRoutesDefinition(xmlRoute.getInputStream());
+                camelContext.addRouteDefinitions(xmlDefinition.getRoutes());
+            }
+        } catch (FileNotFoundException e) {
+            LOG.debug("No XMl routes found in the classpath (/camel/*.xml). 
Skipping XML routes detection.");
         } catch (Exception e) {
-            throw new CamelSpringBootInitializationException(e);
+            throw new RuntimeException(e);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/21107ffe/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.java
 
b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.java
new file mode 100644
index 0000000..6a1c16a
--- /dev/null
+++ 
b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.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.spring.boot.parent;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.support.GenericApplicationContext;
+
+public class SpringBootWithParentContextTest {
+
+    @Test
+    public void shouldCollectRoutesOnlyInRootContext() {
+        GenericApplicationContext parent = new GenericApplicationContext();
+        parent.refresh();
+        new 
SpringApplicationBuilder(Configuration.class).web(false).parent(parent).run();
+    }
+
+}
+
+@SpringBootApplication
+class Configuration {
+
+    @Bean
+    RoutesBuilder routes() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("seda:test").to("mock:test");
+            }
+        };
+    }
+
+}
\ No newline at end of file

Reply via email to