CAMEL-11297: camel-ehcache-starter : auto discovery cache manager
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1a2fdfd0 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1a2fdfd0 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1a2fdfd0 Branch: refs/heads/master Commit: 1a2fdfd02277f9f4327083192820cab1b7ad6f2d Parents: 3eeb97a Author: lburgazzoli <lburgazz...@gmail.com> Authored: Thu May 18 16:53:25 2017 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Fri May 19 13:52:16 2017 +0200 ---------------------------------------------------------------------- .../customizer/CacheManagerCustomizer.java | 77 ++++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + ...heManagerConfigurerNotEnabledGlobalTest.java | 37 ++++++++++ ...agerConfigurerNotEnabledOnComponentTest.java | 37 ++++++++++ .../CacheManagerConfigurerNotEnabledTest.java | 37 ++++++++++ ...acheManagerConfigurerNotEnabledTestBase.java | 48 ++++++++++++ .../springboot/CacheManagerConfigurerTest.java | 63 ++++++++++++++++ .../src/test/resources/application.properties | 0 .../src/test/resources/logback.xml | 41 +++++++++++ 9 files changed, 341 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/customizer/CacheManagerCustomizer.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/customizer/CacheManagerCustomizer.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/customizer/CacheManagerCustomizer.java new file mode 100644 index 0000000..f3a8e59 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/customizer/CacheManagerCustomizer.java @@ -0,0 +1,77 @@ +/** + * 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.component.ehcache.springboot.customizer; + +import org.apache.camel.component.ehcache.EhcacheComponent; +import org.apache.camel.component.ehcache.springboot.EhcacheComponentAutoConfiguration; +import org.apache.camel.spi.ComponentCustomizer; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.ehcache.CacheManager; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.AllNestedConditions; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; + +/** + * A simple implementation of {@link ComponentCustomizer} that auto discovers a + * {@link CacheManager} instance and bind it to the {@link EhcacheComponent} + * component. + * + * This configurer can be disabled either by disable all the + */ +@Order(Ordered.LOWEST_PRECEDENCE) +@Configuration +@ConditionalOnProperty(name = "camel.component.ehcache.configurer.cache-manager.enabled", matchIfMissing = true) +@AutoConfigureAfter(CamelAutoConfiguration.class) +@AutoConfigureBefore(EhcacheComponentAutoConfiguration.class) +public class CacheManagerCustomizer extends AllNestedConditions implements ComponentCustomizer<EhcacheComponent> { + @Autowired + private CacheManager cacheManager; + + public CacheManagerCustomizer() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @Override + public void customize(EhcacheComponent component) { + component.setCacheManager(cacheManager); + } + + // ************************************************************************* + // By default ConditionalOnBean works using an OR operation so if you list + // a number of classes, the condition succeeds if a single instance of the + // classes is found. + // + // A workaround is to use AllNestedConditions and creates some dummy classes + // annotated @ConditionalOnBean + // + // This should be fixed in spring-boot 2.0 where ConditionalOnBean uses and + // AND operation instead of the OR as it does today. + // ************************************************************************* + + @ConditionalOnBean(CacheManager.class) + static class OnCacheManager { + } + @ConditionalOnBean(CamelAutoConfiguration.class) + static class OnCamelAutoConfiguration { + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/resources/META-INF/spring.factories ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/resources/META-INF/spring.factories b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/resources/META-INF/spring.factories index 9fb37bf..3e2aa80 100644 --- a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/resources/META-INF/spring.factories +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/resources/META-INF/spring.factories @@ -15,4 +15,5 @@ ## limitations under the License. ## --------------------------------------------------------------------------- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.apache.camel.component.ehcache.springboot.customizer.CacheManagerCustomizer,\ org.apache.camel.component.ehcache.springboot.EhcacheComponentAutoConfiguration http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledGlobalTest.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledGlobalTest.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledGlobalTest.java new file mode 100644 index 0000000..303ba06 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledGlobalTest.java @@ -0,0 +1,37 @@ +/** + * 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.component.ehcache.springboot; + +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DirtiesContext +@SpringBootApplication +@SpringBootTest( + classes = { + CacheManagerConfigurerNotEnabledTestBase.TestConfiguration.class + }, + properties = { + "debug=false", + "camel.component.configurer.enabled=false" + }) +public class CacheManagerConfigurerNotEnabledGlobalTest extends CacheManagerConfigurerNotEnabledTestBase { +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledOnComponentTest.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledOnComponentTest.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledOnComponentTest.java new file mode 100644 index 0000000..444d76b --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledOnComponentTest.java @@ -0,0 +1,37 @@ +/** + * 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.component.ehcache.springboot; + +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DirtiesContext +@SpringBootApplication +@SpringBootTest( + classes = { + CacheManagerConfigurerNotEnabledTestBase.TestConfiguration.class + }, + properties = { + "debug=false", + "camel.component.ehcache.configurer.enabled=false" + }) +public class CacheManagerConfigurerNotEnabledOnComponentTest extends CacheManagerConfigurerNotEnabledTestBase { +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledTest.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledTest.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledTest.java new file mode 100644 index 0000000..e43a635 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledTest.java @@ -0,0 +1,37 @@ +/** + * 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.component.ehcache.springboot; + +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DirtiesContext +@SpringBootApplication +@SpringBootTest( + classes = { + CacheManagerConfigurerNotEnabledTestBase.TestConfiguration.class + }, + properties = { + "debug=false", + "camel.component.ehcache.configurer.cache-manager.enabled=false" + }) +public class CacheManagerConfigurerNotEnabledTest extends CacheManagerConfigurerNotEnabledTestBase { +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledTestBase.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledTestBase.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledTestBase.java new file mode 100644 index 0000000..fb65b4b --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerNotEnabledTestBase.java @@ -0,0 +1,48 @@ +/** + * 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.component.ehcache.springboot; + +import org.apache.camel.component.ehcache.EhcacheComponent; +import org.ehcache.CacheManager; +import org.ehcache.config.builders.CacheManagerBuilder; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +class CacheManagerConfigurerNotEnabledTestBase { + @Autowired + CacheManager cacheManager; + @Autowired + EhcacheComponent component; + + @Test + public void testComponentConfiguration() throws Exception { + Assert.assertNotNull(cacheManager); + Assert.assertNotNull(component); + Assert.assertNull(component.getCacheManager()); + } + + @Configuration + public static class TestConfiguration { + @Bean(initMethod = "init", destroyMethod = "close") + public CacheManager cacheManager() { + return CacheManagerBuilder.newCacheManagerBuilder().build(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerTest.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerTest.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerTest.java new file mode 100644 index 0000000..3a15d44 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/java/org/apache/camel/component/ehcache/springboot/CacheManagerConfigurerTest.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.component.ehcache.springboot; + +import org.apache.camel.component.ehcache.EhcacheComponent; +import org.ehcache.CacheManager; +import org.ehcache.config.builders.CacheManagerBuilder; +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.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DirtiesContext +@SpringBootApplication +@SpringBootTest( + classes = { + CacheManagerConfigurerTest.TestConfiguration.class + }, + properties = { + "debug=false", + }) +public class CacheManagerConfigurerTest { + @Autowired + CacheManager cacheManager; + @Autowired + EhcacheComponent component; + + @Test + public void testComponentConfiguration() throws Exception { + Assert.assertNotNull(cacheManager); + Assert.assertNotNull(component); + Assert.assertEquals(cacheManager, component.getCacheManager()); + } + + @Configuration + public static class TestConfiguration { + @Bean(initMethod = "init", destroyMethod = "close") + public CacheManager cacheManager() { + return CacheManagerBuilder.newCacheManagerBuilder().build(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/resources/application.properties ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/resources/application.properties b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/resources/application.properties new file mode 100644 index 0000000..e69de29 http://git-wip-us.apache.org/repos/asf/camel/blob/1a2fdfd0/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/resources/logback.xml ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/resources/logback.xml b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/resources/logback.xml new file mode 100644 index 0000000..1d7aa0c --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/test/resources/logback.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<configuration> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-15.15thread] %-5level %-30.30logger - %msg%n</pattern> + </encoder> + </appender> + + <appender name="FILE" class="ch.qos.logback.core.FileAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-15.15thread] %-5level %-30.30logger - %msg%n</pattern> + </encoder> + <file>target/camel-ehcache-starter-test.log</file> + </appender> + + <logger name="org.apache.camel.component.ehcache.springboot" level="DEBUG"/> + + <root level="INFO"> + <appender-ref ref="FILE"/> + </root> + +</configuration> \ No newline at end of file