Updated Branches: refs/heads/camel-2.10.x 3d85a1920 -> a18abe08c
CAMEL-6573: adjust to the changed behavior in ehcache's CacheManager instance creation Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a18abe08 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a18abe08 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a18abe08 Branch: refs/heads/camel-2.10.x Commit: a18abe08ca18ec9a82b5a41d70455d8b84f42134 Parents: 3d85a19 Author: Akitoshi Yoshida <a...@apache.org> Authored: Thu Jul 25 21:43:19 2013 +0200 Committer: Akitoshi Yoshida <akitoshi.yosh...@sap.com> Committed: Tue Jul 30 15:31:31 2013 +0200 ---------------------------------------------------------------------- .../cache/DefaultCacheManagerFactory.java | 2 +- .../camel/component/cache/EHCacheUtil.java | 91 ++++++++++++++++++++ .../cache/FileCacheManagerFactory.java | 2 +- .../cache/DefaultCacheManagerFactoryTest.java | 59 +++++++++++++ .../camel/component/cache/EHCacheUtilTest.java | 54 ++++++++++++ .../src/test/resources/test-ehcache.xml | 32 +++++++ 6 files changed, 238 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a18abe08/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java ---------------------------------------------------------------------- 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 f942725..1c8d539 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 @@ -22,7 +22,7 @@ public class DefaultCacheManagerFactory extends CacheManagerFactory { @Override protected CacheManager createCacheManagerInstance() { - return CacheManager.create(getClass().getResourceAsStream("/ehcache.xml")); + return EHCacheUtil.createCacheManager(getClass().getResourceAsStream("/ehcache.xml")); } } http://git-wip-us.apache.org/repos/asf/camel/blob/a18abe08/components/camel-cache/src/main/java/org/apache/camel/component/cache/EHCacheUtil.java ---------------------------------------------------------------------- diff --git a/components/camel-cache/src/main/java/org/apache/camel/component/cache/EHCacheUtil.java b/components/camel-cache/src/main/java/org/apache/camel/component/cache/EHCacheUtil.java new file mode 100644 index 0000000..2a0aad6 --- /dev/null +++ b/components/camel-cache/src/main/java/org/apache/camel/component/cache/EHCacheUtil.java @@ -0,0 +1,91 @@ +/** + * 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.cache; + +import java.io.InputStream; +import java.net.URL; + +import net.sf.ehcache.CacheException; +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.config.Configuration; + +/** + * A utility class for ehcache + */ +final class EHCacheUtil { + + private static boolean useCreateNewMethod; + + static { + // to support ehcache's version range given in camel-cache (e.g., ehcache 2.5.1, 2.7.2, etc), + // if method newInstance is found, use the newInstance methods; otherwise use the old create methods. + // no reflection used for the actual call as the code is compiled against the newer ehcache. + try { + CacheManager.class.getMethod("newInstance", (Class<?>[])null); + useCreateNewMethod = true; + + } catch (NoSuchMethodException e) { + // ignore + } + } + + private EHCacheUtil() { + // + } + + static CacheManager createCacheManager() throws CacheException { + if (useCreateNewMethod) { + return CacheManager.newInstance(); + } else { + return CacheManager.create(); + } + } + + static CacheManager createCacheManager(String configurationFileName) throws CacheException { + if (useCreateNewMethod) { + return CacheManager.newInstance(configurationFileName); + } else { + return CacheManager.create(configurationFileName); + } + } + + static CacheManager createCacheManager(URL configurationFileURL) throws CacheException { + if (useCreateNewMethod) { + return CacheManager.newInstance(configurationFileURL); + } else { + return CacheManager.create(configurationFileURL); + } + } + + static CacheManager createCacheManager(InputStream inputStream) throws CacheException { + if (useCreateNewMethod) { + return CacheManager.newInstance(inputStream); + } else { + return CacheManager.create(inputStream); + } + } + + static CacheManager createCacheManager(Configuration conf) throws CacheException { + if (useCreateNewMethod) { + return CacheManager.newInstance(conf); + } else { + return CacheManager.create(conf); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a18abe08/components/camel-cache/src/main/java/org/apache/camel/component/cache/FileCacheManagerFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-cache/src/main/java/org/apache/camel/component/cache/FileCacheManagerFactory.java b/components/camel-cache/src/main/java/org/apache/camel/component/cache/FileCacheManagerFactory.java index 623619c..67ba920 100644 --- a/components/camel-cache/src/main/java/org/apache/camel/component/cache/FileCacheManagerFactory.java +++ b/components/camel-cache/src/main/java/org/apache/camel/component/cache/FileCacheManagerFactory.java @@ -36,7 +36,7 @@ public class FileCacheManagerFactory extends CacheManagerFactory { @Override protected CacheManager createCacheManagerInstance() { try { - return CacheManager.create(new FileInputStream(fileName)); + return EHCacheUtil.createCacheManager(new FileInputStream(fileName)); } catch (Exception exception) { throw new RuntimeCamelException(exception); } http://git-wip-us.apache.org/repos/asf/camel/blob/a18abe08/components/camel-cache/src/test/java/org/apache/camel/component/cache/DefaultCacheManagerFactoryTest.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..ac33ac0 --- /dev/null +++ b/components/camel-cache/src/test/java/org/apache/camel/component/cache/DefaultCacheManagerFactoryTest.java @@ -0,0 +1,59 @@ +/** + * 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.cache; + +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Status; +import net.sf.ehcache.config.Configuration; +import net.sf.ehcache.config.ConfigurationFactory; +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +public class DefaultCacheManagerFactoryTest extends Assert { + + @Test + + public void testEHCacheCompatiblity() throws Exception { + // get the default cache manager + CacheManagerFactory factory = new DefaultCacheManagerFactory(); + CacheManager manager = factory.getInstance(); + assertEquals(Status.STATUS_ALIVE, manager.getStatus()); + + // create another unrelated cache manager + Configuration conf = + ConfigurationFactory.parseConfiguration(DefaultCacheManagerFactory.class.getResource("/test-ehcache.xml")); + assertNotNull(conf); + conf.setName("otherCache"); + CacheManager other = CacheManager.create(conf); + assertEquals(Status.STATUS_ALIVE, other.getStatus()); + + // shutdown this unrelated cache manager + other.shutdown(); + assertEquals(Status.STATUS_SHUTDOWN, other.getStatus()); + + // the default cache manager should be still running + assertEquals(Status.STATUS_ALIVE, manager.getStatus()); + + factory.doStop(); + // the default cache manger is shutdown + assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus()); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a18abe08/components/camel-cache/src/test/java/org/apache/camel/component/cache/EHCacheUtilTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cache/src/test/java/org/apache/camel/component/cache/EHCacheUtilTest.java b/components/camel-cache/src/test/java/org/apache/camel/component/cache/EHCacheUtilTest.java new file mode 100644 index 0000000..4699d1d --- /dev/null +++ b/components/camel-cache/src/test/java/org/apache/camel/component/cache/EHCacheUtilTest.java @@ -0,0 +1,54 @@ +/** + * 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.cache; + +import java.net.URL; + +import net.sf.ehcache.config.Configuration; +import net.sf.ehcache.config.ConfigurationFactory; + +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +public class EHCacheUtilTest extends Assert { + @Test + public void testCreateCacheManagers() throws Exception { + // no arg + assertNotNull("create with no arg", EHCacheUtil.createCacheManager()); + + URL configURL = EHCacheUtil.class.getResource("/test-ehcache.xml"); + assertNotNull(configURL); + + // string + assertNotNull("create with string", EHCacheUtil.createCacheManager(configURL.getPath())); + + // url + assertNotNull("create with url", EHCacheUtil.createCacheManager(configURL)); + + // inputstream + assertNotNull("create with inputstream", EHCacheUtil.createCacheManager(configURL.openStream())); + + // config + Configuration conf = ConfigurationFactory.parseConfiguration(configURL); + assertNotNull(conf); + assertNotNull("create with configuration", EHCacheUtil.createCacheManager(conf)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a18abe08/components/camel-cache/src/test/resources/test-ehcache.xml ---------------------------------------------------------------------- diff --git a/components/camel-cache/src/test/resources/test-ehcache.xml b/components/camel-cache/src/test/resources/test-ehcache.xml new file mode 100755 index 0000000..7360578 --- /dev/null +++ b/components/camel-cache/src/test/resources/test-ehcache.xml @@ -0,0 +1,32 @@ +<?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. +--> +<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="ehcache.xsd" > + + <!-- a very simple ehachace configuration only used for unit testing --> + <diskStore path="java.io.tmpdir"/> + <defaultCache + maxElementsInMemory="10000" + eternal="false" + timeToIdleSeconds="120" + timeToLiveSeconds="120" + overflowToDisk="false" + memoryStoreEvictionPolicy="LRU" + /> + +</ehcache>