Repository: camel Updated Branches: refs/heads/master 8304351fc -> 84aab93c0
[Spring Boot] Added Camel annotations support. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/84aab93c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/84aab93c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/84aab93c Branch: refs/heads/master Commit: 84aab93c00c3521f59b7523a551c9f53da273aac Parents: 8304351 Author: Henryk Konsek <hekon...@gmail.com> Authored: Tue Jan 20 12:25:40 2015 +0100 Committer: Henryk Konsek <hekon...@gmail.com> Committed: Tue Jan 20 12:25:47 2015 +0100 ---------------------------------------------------------------------- .../spring/boot/CamelAutoConfiguration.java | 19 +++++- .../camel/spring/boot/RoutesCollector.java | 31 ++++++--- .../camel/spring/boot/CamelAnnotationsTest.java | 69 ++++++++++++++++++++ 3 files changed, 107 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/84aab93c/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 6bfbe39..7210f87 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 @@ -16,12 +16,17 @@ */ package org.apache.camel.spring.boot; +import java.util.ArrayList; +import java.util.Collection; + import org.apache.camel.CamelContext; import org.apache.camel.ConsumerTemplate; import org.apache.camel.ProducerTemplate; import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.component.properties.PropertiesParser; +import org.apache.camel.spring.CamelBeanPostProcessor; import org.apache.camel.spring.SpringCamelContext; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -50,8 +55,10 @@ public class CamelAutoConfiguration { } @Bean - RoutesCollector routesCollector() { - return new RoutesCollector(); + @ConditionalOnMissingBean + RoutesCollector routesCollector(ApplicationContext applicationContext) { + Collection<CamelContextConfiguration> configurations = applicationContext.getBeansOfType(CamelContextConfiguration.class).values(); + return new RoutesCollector(applicationContext, new ArrayList<CamelContextConfiguration>(configurations)); } /** @@ -84,4 +91,12 @@ public class CamelAutoConfiguration { return properties; } + @Bean + CamelBeanPostProcessor camelBeanPostProcessor(CamelContext camelContext, ApplicationContext applicationContext) { + CamelBeanPostProcessor processor = new CamelBeanPostProcessor(); + processor.setCamelContext(camelContext); + processor.setApplicationContext(applicationContext); + return processor; + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/84aab93c/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 f9db4dd..1d1c805 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,24 +16,36 @@ */ package org.apache.camel.spring.boot; +import java.util.List; + import org.apache.camel.CamelContext; +import org.apache.camel.Ordered; import org.apache.camel.RoutesBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; +import org.springframework.core.PriorityOrdered; -public class RoutesCollector implements BeanPostProcessor, ApplicationContextAware { +public class RoutesCollector implements BeanPostProcessor, PriorityOrdered { private static final Logger LOG = LoggerFactory.getLogger(RoutesCollector.class); - private ApplicationContext applicationContext; + // Collaborators + + private final ApplicationContext applicationContext; + + private final List<CamelContextConfiguration> camelContextConfigurations; + + // Constructors - @Autowired(required = false) - private CamelContextConfiguration[] camelContextConfigurations; + public RoutesCollector(ApplicationContext applicationContext, List<CamelContextConfiguration> camelContextConfigurations) { + this.applicationContext = applicationContext; + this.camelContextConfigurations = camelContextConfigurations; + } + + // Overridden @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { @@ -64,9 +76,8 @@ public class RoutesCollector implements BeanPostProcessor, ApplicationContextAwa } @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - LOG.debug("Injecting Spring application context into RoutesCollector: {}"); - this.applicationContext = applicationContext; + public int getOrder() { + return Ordered.LOWEST; } -} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/84aab93c/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java new file mode 100644 index 0000000..dbe5720 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java @@ -0,0 +1,69 @@ +/** + * 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; + +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.RoutesBuilder; +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; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@EnableAutoConfiguration +@SpringApplicationConfiguration(classes = {CamelAnnotationsTest.class, CamelAnnotationsTestConfig.class}) +@IntegrationTest +public class CamelAnnotationsTest extends Assert { + + @Autowired + ProducerTemplate producerTemplate; + + @EndpointInject(uri = "mock:test") + MockEndpoint mockEndpoint; + + @Test + public void shouldInjectEndpoint() throws InterruptedException { + mockEndpoint.setExpectedMessageCount(1); + producerTemplate.sendBody("direct:test", "msg"); + mockEndpoint.assertIsSatisfied(); + } + +} + +@Configuration +class CamelAnnotationsTestConfig { + + @Bean + RoutesBuilder route() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:test").to("mock:test"); + } + }; + } + +}