From: <hib...@li...> - 2006-05-05 19:27:24
|
Author: epbernard Date: 2006-05-05 15:27:17 -0400 (Fri, 05 May 2006) New Revision: 9895 Added: trunk/Hibernate3/lib/ehcache-1.2.jar trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/ trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/EhCacheTest.java trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/ehcache.xml Removed: trunk/Hibernate3/lib/ehcache-1.1.jar Modified: trunk/Hibernate3/src/org/hibernate/cache/EhCache.java trunk/Hibernate3/src/org/hibernate/cache/EhCacheProvider.java trunk/Hibernate3/src/org/hibernate/cache/OptimisticTreeCacheProvider.java trunk/Hibernate3/src/org/hibernate/cache/TreeCacheProvider.java trunk/Hibernate3/src/org/hibernate/cfg/Environment.java trunk/Hibernate3/test/org/hibernate/test/cache/BaseCacheProviderTestCase.java trunk/Hibernate3/test/org/hibernate/test/cache/CacheSuite.java trunk/Hibernate3/test/org/hibernate/test/cache/treecache/optimistic/OptimisticTreeCacheTest.java trunk/Hibernate3/test/org/hibernate/test/cache/treecache/pessimistic/TreeCacheTest.java Log: HHH-1670 ehcache 1.2 and bugfixes in the provider Support for Environment.CACHE_PROVIDER_CONFIG Deleted: trunk/Hibernate3/lib/ehcache-1.1.jar =================================================================== (Binary files differ) Added: trunk/Hibernate3/lib/ehcache-1.2.jar =================================================================== (Binary files differ) Property changes on: trunk/Hibernate3/lib/ehcache-1.2.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/Hibernate3/src/org/hibernate/cache/EhCache.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/cache/EhCache.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/src/org/hibernate/cache/EhCache.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -1,56 +1,18 @@ //$Id$ -/* ==================================================================== - * The Apache Software License, Version 1.1 +/** + * Copyright 2003-2006 Greg Luck, Jboss Inc * - * Copyright (c) 2003 - 2004 Greg Luck. All rights reserved. + * Licensed 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 * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * http://www.apache.org/licenses/LICENSE-2.0 * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, if - * any, must include the following acknowlegement: - * "This product includes software developed by Greg Luck - * (http://sourceforge.net/users/gregluck) and contributors. - * See http://sourceforge.net/project/memberlist.php?group_id=93232 - * for a list of contributors" - * Alternately, this acknowledgement may appear in the software itself, - * if and wherever such third-party acknowlegements normally appear. - * - * 4. The names "EHCache" must not be used to endorse or promote products - * derived from this software without prior written permission. For written - * permission, please contact Greg Luck (gregluck at users.sourceforge.net). - * - * 5. Products derived from this software may not be called "EHCache" - * nor may "EHCache" appear in their names without prior written - * permission of Greg Luck. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL GREG LUCK OR OTHER - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by contributors - * individuals on behalf of the EHCache project. For more - * information on EHCache, please see <http://ehcache.sourceforge.net/>. - * + * 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.hibernate.cache; @@ -62,207 +24,223 @@ import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * EHCache plugin for Hibernate - * + * <p/> * EHCache uses a {@link net.sf.ehcache.store.MemoryStore} and a - * {@link net.sf.ehcache.store.DiskStore}. The {@link net.sf.ehcache.store.DiskStore} - * requires that both keys and values be {@link Serializable}. For this reason - * this plugin throws Exceptions when either of these are not castable to {@link Serializable}. + * {@link net.sf.ehcache.store.DiskStore}. + * The {@link net.sf.ehcache.store.DiskStore} requires that both keys and values be {@link java.io.Serializable}. + * However the MemoryStore does not and in ehcache-1.2 nonSerializable Objects are permitted. They are discarded + * if an attempt it made to overflow them to Disk or to replicate them to remote cache peers. * - * @version Taken from EhCache 0.9 * @author Greg Luck * @author Emmanuel Bernard */ public class EhCache implements Cache { - private static final Log log = LogFactory.getLog(EhCache.class); + private static final Log log = LogFactory.getLog( EhCache.class ); - private net.sf.ehcache.Cache cache; + private static final int SIXTY_THOUSAND_MS = 60000; - /** - * Creates a new Hibernate pluggable cache based on a cache name. - * <p> - * @param cache The underlying EhCache instance to use. - */ - public EhCache(net.sf.ehcache.Cache cache) { - this.cache = cache; - } + private net.sf.ehcache.Cache cache; - /** - * Gets a value of an element which matches the given key. - * @param key the key of the element to return. - * @return The value placed into the cache with an earlier put, or null if not found or expired - * @throws CacheException - */ - public Object get(Object key) throws CacheException { - try { - if ( log.isDebugEnabled() ) { - log.debug("key: " + key); - } - if (key == null) { - return null; - } - else { - Element element = cache.get( (Serializable) key ); - if (element == null) { - if ( log.isDebugEnabled() ) { - log.debug("Element for " + key + " is null"); - } - return null; - } - else { - return element.getValue(); - } - } - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException(e); - } - } - + /** + * Creates a new Hibernate pluggable cache based on a cache name. + * <p/> + * + * @param cache The underlying EhCache instance to use. + */ + public EhCache(net.sf.ehcache.Cache cache) { + this.cache = cache; + } + + /** + * Gets a value of an element which matches the given key. + * + * @param key the key of the element to return. + * @return The value placed into the cache with an earlier put, or null if not found or expired + * @throws CacheException + */ + public Object get(Object key) throws CacheException { + try { + if ( log.isDebugEnabled() ) { + log.debug( "key: " + key ); + } + if ( key == null ) { + return null; + } + else { + Element element = cache.get( key ); + if ( element == null ) { + if ( log.isDebugEnabled() ) { + log.debug( "Element for " + key + " is null" ); + } + return null; + } + else { + return element.getObjectValue(); + } + } + } + catch (net.sf.ehcache.CacheException e) { + throw new CacheException( e ); + } + } + public Object read(Object key) throws CacheException { - return get(key); + return get( key ); } - /** - * Puts an object into the cache. - * @param key a {@link Serializable} key - * @param value a {@link Serializable} value - * @throws CacheException if the parameters are not {@link Serializable}, the {@link CacheManager} - * is shutdown or another {@link Exception} occurs. - */ - public void update(Object key, Object value) throws CacheException { - put(key, value); - } - - /** - * Puts an object into the cache. - * @param key a {@link Serializable} key - * @param value a {@link Serializable} value - * @throws CacheException if the parameters are not {@link Serializable}, the {@link CacheManager} - * is shutdown or another {@link Exception} occurs. - */ + /** + * Puts an object into the cache. + * + * @param key a {@link Serializable} key + * @param value a {@link Serializable} value + * @throws CacheException if the parameters are not {@link Serializable}, the {@link CacheManager} + * is shutdown or another {@link Exception} occurs. + */ + public void update(Object key, Object value) throws CacheException { + put( key, value ); + } + + /** + * Puts an object into the cache. + * + * @param key a {@link Serializable} key + * @param value a {@link Serializable} value + * @throws CacheException if the parameters are not {@link Serializable}, the {@link CacheManager} + * is shutdown or another {@link Exception} occurs. + */ public void put(Object key, Object value) throws CacheException { - try { - Element element = new Element( (Serializable) key, (Serializable) value ); - cache.put(element); - } - catch (IllegalArgumentException e) { - throw new CacheException(e); - } - catch (IllegalStateException e) { - throw new CacheException(e); - } + try { + Element element = new Element( key, value ); + cache.put( element ); + } + catch (IllegalArgumentException e) { + throw new CacheException( e ); + } + catch (IllegalStateException e) { + throw new CacheException( e ); + } - } + } - /** - * Removes the element which matches the key. - * <p> - * If no element matches, nothing is removed and no Exception is thrown. - * @param key the key of the element to remove - * @throws CacheException - */ - public void remove(Object key) throws CacheException { - try { - cache.remove( (Serializable) key ); - } - catch (ClassCastException e) { - throw new CacheException(e); - } - catch (IllegalStateException e) { - throw new CacheException(e); - } - } + /** + * Removes the element which matches the key. + * <p/> + * If no element matches, nothing is removed and no Exception is thrown. + * + * @param key the key of the element to remove + * @throws CacheException + */ + public void remove(Object key) throws CacheException { + try { + cache.remove( key ); + } + catch (ClassCastException e) { + throw new CacheException( e ); + } + catch (IllegalStateException e) { + throw new CacheException( e ); + } + } - /** - * Remove all elements in the cache, but leave the cache - * in a useable state. - * @throws CacheException - */ - public void clear() throws CacheException { - try { - cache.removeAll(); - } - catch (IllegalStateException e) { - throw new CacheException(e); - } - catch (IOException e) { - throw new CacheException(e); - } - } + /** + * Remove all elements in the cache, but leave the cache + * in a useable state. + * + * @throws CacheException + */ + public void clear() throws CacheException { + try { + cache.removeAll(); + } + catch (IllegalStateException e) { + throw new CacheException( e ); + } + catch (IOException e) { + throw new CacheException( e ); + } + } - /** - * Remove the cache and make it unuseable. - * @throws CacheException - */ - public void destroy() throws CacheException { - try { - CacheManager.getInstance().removeCache( cache.getName() ); - } - catch (IllegalStateException e) { - throw new CacheException(e); - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException(e); - } - } + /** + * Remove the cache and make it unuseable. + * + * @throws CacheException + */ + public void destroy() throws CacheException { + try { + cache.getCacheManager().removeCache( cache.getName() ); + } + catch (IllegalStateException e) { + throw new CacheException( e ); + } + catch (net.sf.ehcache.CacheException e) { + throw new CacheException( e ); + } + } - /** - * Calls to this method should perform there own synchronization. - * It is provided for distributed caches. Because EHCache is not distributed - * this method does nothing. - */ - public void lock(Object key) throws CacheException { - } + /** + * Calls to this method should perform there own synchronization. + * It is provided for distributed caches. Because EHCache is not distributed + * this method does nothing. + */ + public void lock(Object key) throws CacheException { + } - /** - * Calls to this method should perform there own synchronization. - * It is provided for distributed caches. Because EHCache is not distributed - * this method does nothing. - */ - public void unlock(Object key) throws CacheException { - } + /** + * Calls to this method should perform there own synchronization. + * It is provided for distributed caches. Because EHCache is not distributed + * this method does nothing. + */ + public void unlock(Object key) throws CacheException { + } - /** - * Gets the next timestamp; - */ - public long nextTimestamp() { - return Timestamper.next(); - } + /** + * Gets the next timestamp; + */ + public long nextTimestamp() { + return Timestamper.next(); + } - /** - * Returns the lock timeout for this cache. - */ - public int getTimeout() { - // 60 second lock timeout - return Timestamper.ONE_MS * 60000; - } + /** + * Returns the lock timeout for this cache. + */ + public int getTimeout() { + // 60 second lock timeout + return Timestamper.ONE_MS * SIXTY_THOUSAND_MS; + } public String getRegionName() { return cache.getName(); } + /** + * Warning: This method can be very expensive to run. Allow approximately 1 second + * per 1MB of entries. Running this method could create liveness problems + * because the object lock is held for a long period + * <p/> + * + * @return the approximate size of memory ehcache is using for the MemoryStore for this cache + */ public long getSizeInMemory() { try { return cache.calculateInMemorySize(); } - catch(Throwable t) { + catch (Throwable t) { return -1; } } public long getElementCountInMemory() { try { - return cache.getSize(); + return cache.getMemoryStoreSize(); } catch (net.sf.ehcache.CacheException ce) { - throw new CacheException(ce); + throw new CacheException( ce ); } } @@ -281,7 +259,7 @@ return result; } catch (Exception e) { - throw new CacheException(e); + throw new CacheException( e ); } } Modified: trunk/Hibernate3/src/org/hibernate/cache/EhCacheProvider.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/cache/EhCacheProvider.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/src/org/hibernate/cache/EhCacheProvider.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -1,65 +1,30 @@ //$Id$ -/* ==================================================================== - * The Apache Software License, Version 1.1 +/** + * Copyright 2003-2006 Greg Luck, Jboss Inc * - * Copyright (c) 2003 - 2004 Greg Luck. All rights reserved. + * Licensed 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 * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * http://www.apache.org/licenses/LICENSE-2.0 * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, if - * any, must include the following acknowlegement: - * "This product includes software developed by Greg Luck - * (http://sourceforge.net/users/gregluck) and contributors. - * See http://sourceforge.net/project/memberlist.php?group_id=93232 - * for a list of contributors" - * Alternately, this acknowledgement may appear in the software itself, - * if and wherever such third-party acknowlegements normally appear. - * - * 4. The names "EHCache" must not be used to endorse or promote products - * derived from this software without prior written permission. For written - * permission, please contact Greg Luck (gregluck at users.sourceforge.net). - * - * 5. Products derived from this software may not be called "EHCache" - * nor may "EHCache" appear in their names without prior written - * permission of Greg Luck. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL GREG LUCK OR OTHER - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by contributors - * individuals on behalf of the EHCache project. For more - * information on EHCache, please see <http://ehcache.sourceforge.net/>. - * + * 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.hibernate.cache; -import net.sf.ehcache.CacheManager; - import java.util.Properties; +import java.net.URL; +import net.sf.ehcache.CacheManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.hibernate.cfg.Environment; +import org.hibernate.util.StringHelper; +import org.hibernate.util.ConfigHelper; /** * Cache Provider plugin for Hibernate @@ -71,16 +36,34 @@ * @author Greg Luck * @author Emmanuel Bernard */ +/** + * Cache Provider plugin for ehcache-1.2. New in this provider are ehcache support for multiple + * Hibernate session factories, each with its own ehcache configuration, and non Serializable keys and values. + * Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply + * by configurion in ehcache.xml. + * <p/> + * Use <code>hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider</code> in the Hibernate configuration + * to enable this provider for Hibernate's second level cache. + * <p/> + * When configuring multiple ehcache CacheManagers, as you would where you have multiple Hibernate Configurations and + * multiple SessionFactories, specify in each Hibernate configuration the ehcache configuration using + * the property <code>hibernate.cache.provider_configuration_file_resource_path</code> An example to set an ehcache configuration + * called ehcache-2.xml would be <code>hibernate.cache.provider_configuration_file_resource_path=/ehcache-2.xml</code>. If the leading + * slash is not there one will be added. The configuration file will be looked for in the root of the classpath. + * <p/> + * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier + * is not in the classpath or it will not work. + * <p/> + * See http://ehcache.sf.net for documentation on ehcache + * <p/> + * + * @author Greg Luck + * @author Emmanuel Bernard + */ public class EhCacheProvider implements CacheProvider { private static final Log log = LogFactory.getLog(EhCacheProvider.class); - // CacheManager.create() actually returns a singleton reference, which is causing - // problems with users attempting to use multiple SessionFactories all using the - // EhCacheProvider in the same classloader. The work-around is to use simple reference - // counting here... - private static int referenceCount = 0; - private CacheManager manager; /** @@ -126,26 +109,54 @@ * @param properties current configuration settings. */ public void start(Properties properties) throws CacheException { - try { - manager = CacheManager.create(); - referenceCount++; - } - catch (net.sf.ehcache.CacheException e) { - throw new CacheException(e); + if (manager != null) { + log.warn("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " + + " between repeated calls to buildSessionFactory. Using previously created EhCacheProvider." + + " If this behaviour is required, consider using net.sf.ehcache.hibernate.SingletonEhCacheProvider."); + return; } + try { + String configurationResourceName = null; + if (properties != null) { + configurationResourceName = (String) properties.get( Environment.CACHE_PROVIDER_CONFIG ); + } + if ( StringHelper.isEmpty( configurationResourceName ) ) { + manager = new CacheManager(); + } else { + URL url = loadResource(configurationResourceName); + manager = new CacheManager(url); + } + } catch (net.sf.ehcache.CacheException e) { + //yukky! Don't you have subclasses for that! + if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " + + "CacheManager using the diskStorePath")) { + throw new CacheException("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " + + " between repeated calls to buildSessionFactory. Consider using net.sf.ehcache.hibernate.SingletonEhCacheProvider. Error from " + + " ehcache was: " + e.getMessage()); + } else { + throw e; + } + } } + private URL loadResource(String configurationResourceName) { + URL url = ConfigHelper.locateConfig( configurationResourceName ); + if (log.isDebugEnabled()) { + log.debug("Creating EhCacheProvider from a specified resource: " + + configurationResourceName + " Resolved to URL: " + url); + } + return url; + } + /** * Callback to perform any necessary cleanup of the underlying cache implementation * during SessionFactory.close(). */ public void stop() { - if ( manager != null ) { - if ( --referenceCount == 0 ) { - manager.shutdown(); - } - manager = null; - } + if (manager != null) { + manager.shutdown(); + manager = null; + } } public boolean isMinimalPutsEnabledByDefault() { Modified: trunk/Hibernate3/src/org/hibernate/cache/OptimisticTreeCacheProvider.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/cache/OptimisticTreeCacheProvider.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/src/org/hibernate/cache/OptimisticTreeCacheProvider.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -1,16 +1,16 @@ //$Id$ package org.hibernate.cache; -import org.jboss.cache.PropertyConfigurator; +import java.util.Properties; +import javax.transaction.TransactionManager; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.cfg.Environment; import org.hibernate.transaction.TransactionManagerLookup; import org.hibernate.transaction.TransactionManagerLookupFactory; -import org.hibernate.cfg.Environment; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.jboss.cache.PropertyConfigurator; -import javax.transaction.TransactionManager; -import java.util.Properties; - /** * Support for a standalone JBossCache TreeCache instance utilizing TreeCache's * optimistic locking capabilities. This capability was added in JBossCache @@ -25,6 +25,9 @@ */ public class OptimisticTreeCacheProvider implements CacheProvider { + /** + * @deprecated use {@link Environment.CACHE_PROVIDER_CONFIG} + */ public static final String CONFIG_RESOURCE = "hibernate.cache.opt_tree_cache.config"; public static final String DEFAULT_CONFIG = "treecache.xml"; @@ -58,7 +61,10 @@ * Indicates a problem preparing cache for use. */ public void start(Properties properties) { - String resource = properties.getProperty( CONFIG_RESOURCE ); + String resource = properties.getProperty( Environment.CACHE_PROVIDER_CONFIG ); + if (resource == null) { + resource = properties.getProperty( CONFIG_RESOURCE ); + } if ( resource == null ) { resource = DEFAULT_CONFIG; } Modified: trunk/Hibernate3/src/org/hibernate/cache/TreeCacheProvider.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/cache/TreeCacheProvider.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/src/org/hibernate/cache/TreeCacheProvider.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -1,15 +1,16 @@ //$Id$ package org.hibernate.cache; -import org.jboss.cache.PropertyConfigurator; +import java.util.Properties; +import javax.transaction.TransactionManager; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.hibernate.transaction.TransactionManagerLookup; import org.hibernate.transaction.TransactionManagerLookupFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.hibernate.cfg.Environment; +import org.jboss.cache.PropertyConfigurator; -import javax.transaction.TransactionManager; -import java.util.Properties; - /** * Support for a standalone JBossCache (TreeCache) instance. The JBossCache is configured * via a local config resource. @@ -18,6 +19,9 @@ */ public class TreeCacheProvider implements CacheProvider { + /** + * @deprecated use {@link org.hibernate.cfg.Environment.CACHE_PROVIDER_CONFIG} + */ public static final String CONFIG_RESOURCE = "hibernate.cache.tree_cache.config"; public static final String DEFAULT_CONFIG = "treecache.xml"; @@ -50,8 +54,12 @@ * @throws CacheException Indicates a problem preparing cache for use. */ public void start(Properties properties) { - String resource = properties.getProperty( CONFIG_RESOURCE ); + String resource = properties.getProperty( Environment.CACHE_PROVIDER_CONFIG ); + if ( resource == null ) { + resource = properties.getProperty( CONFIG_RESOURCE ); + } + if ( resource == null ) { resource = DEFAULT_CONFIG; } log.debug( "Configuring TreeCache from resource [" + resource + "]" ); Modified: trunk/Hibernate3/src/org/hibernate/cfg/Environment.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/cfg/Environment.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/src/org/hibernate/cfg/Environment.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -366,6 +366,10 @@ */ public static final String CACHE_PROVIDER = "hibernate.cache.provider_class"; /** + * The <tt>CacheProvider</tt> implementation class + */ + public static final String CACHE_PROVIDER_CONFIG = "hibernate.cache.provider_configuration_file_resource_path"; + /** * The <tt>CacheProvider</tt> JNDI namespace, if pre-bound to JNDI. */ public static final String CACHE_NAMESPACE = "hibernate.cache.jndi"; Modified: trunk/Hibernate3/test/org/hibernate/test/cache/BaseCacheProviderTestCase.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/cache/BaseCacheProviderTestCase.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/test/org/hibernate/test/cache/BaseCacheProviderTestCase.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -1,10 +1,13 @@ package org.hibernate.test.cache; +import java.util.Map; +import java.util.HashMap; + import org.hibernate.Session; import org.hibernate.Transaction; +import org.hibernate.cache.ReadWriteCache; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; -import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.stat.SecondLevelCacheStatistics; import org.hibernate.stat.Statistics; import org.hibernate.test.TestCase; @@ -12,17 +15,15 @@ import org.hibernate.test.tm.DummyTransactionManagerLookup; import org.hibernate.transaction.JDBCTransactionFactory; -import java.util.Map; - /** * Common requirement testing for each {@link org.hibernate.cache.CacheProvider} impl. * * @author Steve Ebersole */ public abstract class BaseCacheProviderTestCase extends TestCase { - - // note that a lot of the fucntionality here is intended to be used - // in creating specific tests for each CacheProvider that would extend + + // note that a lot of the fucntionality here is intended to be used + // in creating specific tests for each CacheProvider that would extend // from a base test case (this) for common requirement testing... public BaseCacheProviderTestCase(String x) { @@ -50,7 +51,7 @@ cfg.setProperty( Environment.TRANSACTION_MANAGER_STRATEGY, DummyTransactionManagerLookup.class.getName() ); } else { - cfg.setProperty( Environment.TRANSACTION_MANAGER_STRATEGY, JDBCTransactionFactory.class.getName() ); + cfg.setProperty( Environment.TRANSACTION_STRATEGY, JDBCTransactionFactory.class.getName() ); } } @@ -116,7 +117,15 @@ assertEquals( slcs.getPutCount(), 2 ); - Map map = (Map) slcs.getEntries().get( i.getId() ); + Object entry = slcs.getEntries().get( i.getId() ); + Map map; + if ( entry instanceof ReadWriteCache.Item ) { + map = new HashMap(); + map = (Map) ( (ReadWriteCache.Item) entry ).getValue(); + } + else { + map = (Map) entry; + } assertTrue( map.get("description").equals("A bog standard item") ); assertTrue( map.get("name").equals("widget") ); @@ -183,10 +192,19 @@ // check the version value in the cache... SecondLevelCacheStatistics slcs = sfi().getStatistics() .getSecondLevelCacheStatistics( VersionedItem.class.getName() ); - Map map = ( Map ) slcs.getEntries().get( item.getId() ); - Long cachedVersionValue = ( Long ) map.get( "_version" ); - assertEquals( initialVersion.longValue(), cachedVersionValue.longValue() ); + Object entry = slcs.getEntries().get( item.getId() ); + Long cachedVersionValue; + if ( entry instanceof ReadWriteCache.Lock ) { + //FIXME don't know what to test here + cachedVersionValue = new Long( ( (ReadWriteCache.Lock) entry).getUnlockTimestamp() ); + } + else { + cachedVersionValue = ( Long ) ( (Map) entry ).get( "_version" ); + assertEquals( initialVersion.longValue(), cachedVersionValue.longValue() ); + } + + // cleanup s = openSession(); txn = s.beginTransaction(); Modified: trunk/Hibernate3/test/org/hibernate/test/cache/CacheSuite.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/cache/CacheSuite.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/test/org/hibernate/test/cache/CacheSuite.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -4,6 +4,7 @@ import junit.framework.TestSuite; import org.hibernate.test.cache.treecache.optimistic.OptimisticTreeCacheTest; import org.hibernate.test.cache.treecache.pessimistic.TreeCacheTest; +import org.hibernate.test.cache.ehcache.EhCacheTest; /** * @author Steve Ebersole @@ -14,6 +15,7 @@ TestSuite suite = new TestSuite( "CacheProvider tests"); suite.addTest( OptimisticTreeCacheTest.suite() ); suite.addTest( TreeCacheTest.suite() ); + suite.addTest( EhCacheTest.suite() ); return suite; } } Added: trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/EhCacheTest.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/EhCacheTest.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/EhCacheTest.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -0,0 +1,47 @@ +//$Id: $ +package org.hibernate.test.cache.ehcache; + +import junit.framework.Test; +import junit.framework.TestSuite; +import org.hibernate.cache.EhCacheProvider; +import org.hibernate.cfg.Environment; +import org.hibernate.test.cache.BaseCacheProviderTestCase; + +/** + * @author Emmanuel Bernard + */ +public class EhCacheTest extends BaseCacheProviderTestCase { + + // note that a lot of the fucntionality here is intended to be used + // in creating specific tests for each CacheProvider that would extend + // from a base test case (this) for common requirement testing... + + public EhCacheTest(String x) { + super( x ); + } + + public static Test suite() { + return new TestSuite( EhCacheTest.class ); + } + + public String getCacheConcurrencyStrategy() { + return "read-write"; + } + + protected Class getCacheProvider() { + return EhCacheProvider.class; + } + + protected String getConfigResourceKey() { + return Environment.CACHE_PROVIDER_CONFIG; + } + + protected String getConfigResourceLocation() { + return "org/hibernate/test/cache/ehcache/ehcache.xml"; + } + + protected boolean useTransactionManager() { + return false; + } + +} Added: trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/ehcache.xml =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/ehcache.xml 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/test/org/hibernate/test/cache/ehcache/ehcache.xml 2006-05-05 19:27:17 UTC (rev 9895) @@ -0,0 +1,88 @@ +<ehcache> + + <!-- Sets the path to the directory where cache .data files are created. + + If the path is a Java System Property it is replaced by + its value in the running VM. + + The following properties are translated: + user.home - User's home directory + user.dir - User's current working directory + java.io.tmpdir - Default temp file path --> + <diskStore path="java.io.tmpdir"/> + + + <!--Default Cache configuration. These will applied to caches programmatically created through + the CacheManager. + + The following attributes are required for defaultCache: + + maxInMemory - Sets the maximum number of objects that will be created in memory + eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element + is never expired. + timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used + if the element is not eternal. Idle time is now - last accessed time + timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used + if the element is not eternal. TTL is now - creation time + overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache + has reached the maxInMemory limit. + + --> + <defaultCache + maxElementsInMemory="10000" + eternal="false" + timeToIdleSeconds="120" + timeToLiveSeconds="120" + overflowToDisk="true" + /> + + <!--Predefined caches. Add your cache configuration settings here. + If you do not have a configuration for your cache a WARNING will be issued when the + CacheManager starts + + The following attributes are required for defaultCache: + + name - Sets the name of the cache. This is used to identify the cache. It must be unique. + maxInMemory - Sets the maximum number of objects that will be created in memory + eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element + is never expired. + timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used + if the element is not eternal. Idle time is now - last accessed time + timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used + if the element is not eternal. TTL is now - creation time + overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache + has reached the maxInMemory limit. + + --> + + <!-- Sample cache named sampleCache1 + This cache contains a maximum in memory of 10000 elements, and will expire + an element if it is idle for more than 5 minutes and lives for more than + 10 minutes. + + If there are more than 10000 elements it will overflow to the + disk cache, which in this configuration will go to wherever java.io.tmp is + defined on your system. On a standard Linux system this will be /tmp" + --> + <cache name="sampleCache1" + maxElementsInMemory="10000" + eternal="false" + timeToIdleSeconds="300" + timeToLiveSeconds="600" + overflowToDisk="true" + /> + + <!-- Sample cache named sampleCache2 + This cache contains 1000 elements. Elements will always be held in memory. + They are not expired. --> + <cache name="sampleCache2" + maxElementsInMemory="1000" + eternal="true" + timeToIdleSeconds="0" + timeToLiveSeconds="0" + overflowToDisk="false" + /> --> + + <!-- Place configuration for your caches following --> + +</ehcache> Modified: trunk/Hibernate3/test/org/hibernate/test/cache/treecache/optimistic/OptimisticTreeCacheTest.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/cache/treecache/optimistic/OptimisticTreeCacheTest.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/test/org/hibernate/test/cache/treecache/optimistic/OptimisticTreeCacheTest.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -1,15 +1,15 @@ package org.hibernate.test.cache.treecache.optimistic; +import junit.framework.Test; +import junit.framework.TestSuite; +import org.hibernate.cache.OptimisticTreeCacheProvider; +import org.hibernate.cfg.Environment; import org.hibernate.test.cache.BaseCacheProviderTestCase; import org.hibernate.test.tm.DummyTransactionManager; -import org.hibernate.cache.OptimisticTreeCacheProvider; -import org.hibernate.engine.SessionFactoryImplementor; +import org.jboss.cache.Fqn; import org.jboss.cache.TreeCache; -import org.jboss.cache.Fqn; +import org.jboss.cache.config.Option; import org.jboss.cache.optimistic.DataVersion; -import org.jboss.cache.config.Option; -import junit.framework.Test; -import junit.framework.TestSuite; /** * @author Steve Ebersole @@ -37,7 +37,7 @@ } protected String getConfigResourceKey() { - return OptimisticTreeCacheProvider.CONFIG_RESOURCE; + return Environment.CACHE_PROVIDER_CONFIG; } protected String getConfigResourceLocation() { Modified: trunk/Hibernate3/test/org/hibernate/test/cache/treecache/pessimistic/TreeCacheTest.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/cache/treecache/pessimistic/TreeCacheTest.java 2006-05-05 18:01:32 UTC (rev 9894) +++ trunk/Hibernate3/test/org/hibernate/test/cache/treecache/pessimistic/TreeCacheTest.java 2006-05-05 19:27:17 UTC (rev 9895) @@ -2,6 +2,7 @@ import org.hibernate.test.cache.BaseCacheProviderTestCase; import org.hibernate.cache.TreeCacheProvider; +import org.hibernate.cfg.Environment; import junit.framework.Test; import junit.framework.TestSuite; @@ -31,7 +32,7 @@ } protected String getConfigResourceKey() { - return TreeCacheProvider.CONFIG_RESOURCE; + return Environment.CACHE_PROVIDER_CONFIG; } protected String getConfigResourceLocation() { |