Repository: camel Updated Branches: refs/heads/master b01ba794b -> 578625d72
[Spring Boot] Added converter bridge. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/578625d7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/578625d7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/578625d7 Branch: refs/heads/master Commit: 578625d72b9fdd43ce860126b89fd524a648c24e Parents: b01ba79 Author: Henryk Konsek <hekon...@gmail.com> Authored: Thu Jan 1 20:54:19 2015 +0100 Committer: Henryk Konsek <hekon...@gmail.com> Committed: Thu Jan 1 20:54:19 2015 +0100 ---------------------------------------------------------------------- .../spring/boot/CamelAutoConfiguration.java | 8 ++- .../SpringConversionServiceConfiguration.java | 45 ++++++++++++++ .../camel/spring/boot/SpringTypeConverter.java | 42 +++++++++++++ .../boot/SpringConverterDelegationTest.java | 63 ++++++++++++++++++++ 4 files changed, 156 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/578625d7/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 9f1e9fa..4f24f7c 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 @@ -23,11 +23,11 @@ import org.apache.camel.TypeConverter; import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.component.properties.PropertiesParser; import org.apache.camel.spring.SpringCamelContext; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; /** * <p> @@ -102,6 +102,7 @@ import org.springframework.context.annotation.Configuration; */ @Configuration @EnableConfigurationProperties(CamelConfigurationProperties.class) +@Import(SpringConversionServiceConfiguration.class) public class CamelAutoConfiguration { /** @@ -110,9 +111,12 @@ public class CamelAutoConfiguration { */ @Bean CamelContext camelContext(ApplicationContext applicationContext, - CamelConfigurationProperties configurationProperties) { + CamelConfigurationProperties configurationProperties, + SpringTypeConverter springTypeConverter) { CamelContext camelContext = new SpringCamelContext(applicationContext); + camelContext.getTypeConverterRegistry().addFallbackTypeConverter(springTypeConverter, true); + if (!configurationProperties.isJmxEnabled()) { camelContext.disableJMX(); } http://git-wip-us.apache.org/repos/asf/camel/blob/578625d7/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringConversionServiceConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringConversionServiceConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringConversionServiceConfiguration.java new file mode 100644 index 0000000..eb0dab0 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringConversionServiceConfiguration.java @@ -0,0 +1,45 @@ +/** + * 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.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.support.DefaultConversionService; + +@Configuration +public class SpringConversionServiceConfiguration { + + @ConditionalOnMissingBean + @Bean + ConversionService conversionService(ApplicationContext applicationContext) { + DefaultConversionService service = new DefaultConversionService(); + for (Converter converter : applicationContext.getBeansOfType(Converter.class).values()) { + service.addConverter(converter); + } + return service; + } + + @Bean + SpringTypeConverter springTypeConverter(ConversionService conversionService) { + return new SpringTypeConverter(conversionService); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/578625d7/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringTypeConverter.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringTypeConverter.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringTypeConverter.java new file mode 100644 index 0000000..8748ef9 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringTypeConverter.java @@ -0,0 +1,42 @@ +/** + * 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.Exchange; +import org.apache.camel.TypeConversionException; +import org.apache.camel.support.TypeConverterSupport; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.convert.ConversionService; + +public class SpringTypeConverter extends TypeConverterSupport { + + private final ConversionService conversionService; + + @Autowired + public SpringTypeConverter(ConversionService conversionService) { + this.conversionService = conversionService; + } + + @Override + public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException { + if (conversionService.canConvert(value.getClass(), type)) { + return conversionService.convert(value, type); + } + return null; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/578625d7/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java new file mode 100644 index 0000000..58a5b70 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java @@ -0,0 +1,63 @@ +/** + * 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.TypeConverter; +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.core.convert.converter.Converter; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@EnableAutoConfiguration +@SpringApplicationConfiguration(classes = SpringConverterDelegationTest.class) +@IntegrationTest +public class SpringConverterDelegationTest extends Assert { + + @Autowired + TypeConverter typeConverter; + + @Test + public void shouldConvertUsingSpringConverter() { + String result = typeConverter.convertTo(String.class, new Convertable()); + assertEquals("converted!", result); + } + + @Bean + ConvertableConverter convertableConverter() { + return new ConvertableConverter(); + } + +} + +class Convertable { +} + +class ConvertableConverter implements Converter<Convertable, String> { + + @Override + public String convert(Convertable source) { + return "converted!"; + } + +} \ No newline at end of file