Updated Branches: refs/heads/master 5763c7998 -> 687edfcdc
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/687edfcd Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/687edfcd Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/687edfcd Branch: refs/heads/master Commit: 687edfcdc21af44a7686594ad5ffe7ac4d79b3b0 Parents: 5763c79 Author: Akitoshi Yoshida <a...@apache.org> Authored: Thu Jul 25 21:43:19 2013 +0200 Committer: Akitoshi Yoshida <a...@apache.org> Committed: Thu Jul 25 21:48:17 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 ++++++++++++ 5 files changed, 206 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/687edfcd/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/687edfcd/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/687edfcd/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 3167f95..0f42e9e 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("Error creating CacheManager from file: " + fileName, exception); } http://git-wip-us.apache.org/repos/asf/camel/blob/687edfcd/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/687edfcd/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)); + } +}