diff --git a/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java b/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java index 80958effdeb83..21f1bf1f4799a 100755 --- a/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java +++ b/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java @@ -30,7 +30,7 @@ public class CacheComponent extends DefaultComponent { private CacheConfiguration configuration; private CacheManagerFactory cacheManagerFactory; - private String configurationFile = "classpath:ehcache.xml"; + private String configurationFile; public CacheComponent() { configuration = new CacheConfiguration(); @@ -99,7 +99,7 @@ protected void doStart() throws Exception { if (configurationFile != null) { is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), configurationFile); } - cacheManagerFactory = new DefaultCacheManagerFactory(is); + cacheManagerFactory = new DefaultCacheManagerFactory(is, configurationFile); } ServiceHelper.startService(cacheManagerFactory); } diff --git a/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java b/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java index f0cec7c7f87f0..69cf3f713ce2c 100644 --- a/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java +++ b/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java @@ -19,25 +19,36 @@ import java.io.InputStream; import net.sf.ehcache.CacheManager; + +import org.apache.camel.impl.DefaultComponent; import org.apache.camel.util.IOHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DefaultCacheManagerFactory extends CacheManagerFactory { + private static final Logger LOG = LoggerFactory.getLogger(DefaultCacheManagerFactory.class); private InputStream is; + private String configurationFile; + public DefaultCacheManagerFactory() { - this(null); + this(null, null); } - public DefaultCacheManagerFactory(InputStream is) { + public DefaultCacheManagerFactory(InputStream is, String configurationFile) { this.is = is; + this.configurationFile = configurationFile; } @Override protected CacheManager createCacheManagerInstance() { if (is == null) { - is = getClass().getResourceAsStream("/ehcache.xml"); + // it will still look for "/ehcache.xml" before defaulting to "/ehcache-failsafe.xml" + LOG.info("Creating CacheManager using Ehcache defaults"); + return EHCacheUtil.createCacheManager(); } + LOG.info("Creating CacheManager using camel-cache configuration: {}", configurationFile); return EHCacheUtil.createCacheManager(is); } diff --git a/components/camel-cache/src/main/resources/ehcache.xml b/components/camel-cache/src/main/resources/ehcache.xml deleted file mode 100755 index 15eaef9f39a61..0000000000000 --- a/components/camel-cache/src/main/resources/ehcache.xml +++ /dev/null @@ -1,510 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/components/camel-cache/src/test/java/org/apache/camel/component/cache/DefaultCacheManagerFactoryTest.java b/components/camel-cache/src/test/java/org/apache/camel/component/cache/DefaultCacheManagerFactoryTest.java index ac33ac09cf9b5..af8e786f8de85 100644 --- a/components/camel-cache/src/test/java/org/apache/camel/component/cache/DefaultCacheManagerFactoryTest.java +++ b/components/camel-cache/src/test/java/org/apache/camel/component/cache/DefaultCacheManagerFactoryTest.java @@ -24,13 +24,14 @@ import org.junit.Assert; import org.junit.Test; +import static org.hamcrest.CoreMatchers.*; + /** * */ public class DefaultCacheManagerFactoryTest extends Assert { @Test - public void testEHCacheCompatiblity() throws Exception { // get the default cache manager CacheManagerFactory factory = new DefaultCacheManagerFactory(); @@ -56,4 +57,22 @@ public void testEHCacheCompatiblity() throws Exception { // the default cache manger is shutdown assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus()); } + + @Test + public void testNoProvidedConfiguration() throws Exception { + CacheManagerFactory factory = new DefaultCacheManagerFactory(getClass().getResourceAsStream("/ehcache.xml"), "/ehcache.xml"); + CacheManager manager = factory.getInstance(); + // CAMEL-7195 + assertThat("There should be no peer providers configured", manager.getCacheManagerPeerProviders().size(), is(0)); + assertThat("There should be no /ehcache.xml resource by default", getClass().getResourceAsStream("/ehcache.xml"), nullValue()); + } + + @Test + public void testFailSafeEHCacheManager() throws Exception { + CacheManagerFactory factory1 = new DefaultCacheManagerFactory(null, null); + CacheManagerFactory factory2 = new DefaultCacheManagerFactory(); + assertSame("The cache managers should be the same, loaded from fallback ehcache config", + factory1.getInstance(), factory2.getInstance()); + } + }