Hibernate SVN: r15196 - in search/trunk/src/java/org/hibernate/search: jpa and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: sannegrinovero
Date: 2008-09-14 14:25:52 -0400 (Sun, 14 Sep 2008)
New Revision: 15196
Modified:
search/trunk/src/java/org/hibernate/search/FullTextSession.java
search/trunk/src/java/org/hibernate/search/jpa/FullTextEntityManager.java
Log:
javadoc clarifications for index() - by forum request.
Modified: search/trunk/src/java/org/hibernate/search/FullTextSession.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/FullTextSession.java 2008-09-14 09:00:05 UTC (rev 15195)
+++ search/trunk/src/java/org/hibernate/search/FullTextSession.java 2008-09-14 18:25:52 UTC (rev 15196)
@@ -20,8 +20,8 @@
/**
* Force the (re)indexing of a given <b>managed</b> object.
- * Indexation is batched per transaction
- * Non indexable entities are ignored
+ * Indexation is batched per transaction: if a transaction is active, the operation
+ * will not affect the index at least until commit.
*
* @param entity The entity to index - must not be <code>null</code>.
* @throws IllegalArgumentException if entity is null or not an @Indexed entity
Modified: search/trunk/src/java/org/hibernate/search/jpa/FullTextEntityManager.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/jpa/FullTextEntityManager.java 2008-09-14 09:00:05 UTC (rev 15195)
+++ search/trunk/src/java/org/hibernate/search/jpa/FullTextEntityManager.java 2008-09-14 18:25:52 UTC (rev 15196)
@@ -21,8 +21,9 @@
/**
* Force the (re)indexing of a given <b>managed</b> object.
- * Indexation is batched per transaction
- *
+ * Indexation is batched per transaction: if a transaction is active, the operation
+ * will not affect the index at least until commit.
+ *
* @throws IllegalArgumentException if entity is null or not an @Indexed entity
*/
void index(Object entity);
16 years, 3 months
Hibernate SVN: r15195 - in search/trunk/src: test/org/hibernate/search/test/shards and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: sannegrinovero
Date: 2008-09-14 05:00:05 -0400 (Sun, 14 Sep 2008)
New Revision: 15195
Added:
search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java
Modified:
search/trunk/src/java/org/hibernate/search/store/IdHashShardingStrategy.java
Log:
HSEARCH-237 IdHashShardingStrategy fails on IDs having negative hashcode
Modified: search/trunk/src/java/org/hibernate/search/store/IdHashShardingStrategy.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/store/IdHashShardingStrategy.java 2008-09-12 07:14:40 UTC (rev 15194)
+++ search/trunk/src/java/org/hibernate/search/store/IdHashShardingStrategy.java 2008-09-14 09:00:05 UTC (rev 15195)
@@ -12,6 +12,7 @@
* @author Emmanuel Bernard
*/
public class IdHashShardingStrategy implements IndexShardingStrategy {
+
private DirectoryProvider<?>[] providers;
public void initialize(Properties properties, DirectoryProvider<?>[] providers) {
this.providers = providers;
@@ -27,17 +28,17 @@
public DirectoryProvider<?>[] getDirectoryProvidersForDeletion(Class<?> entity, Serializable id, String idInString) {
if ( idInString == null ) return providers;
- return new DirectoryProvider[] { providers[ hashKey(idInString) ] };
+ return new DirectoryProvider[] { providers[hashKey( idInString )] };
}
private int hashKey(String key) {
- //reproduce the hashCode implementaiton of String as documented in the javadoc
+ // reproduce the hashCode implementation of String as documented in the javadoc
// to be safe cross Java version (in case it changes some day)
int hash = 0;
int length = key.length();
- for (int index = 0 ; index < length ; index++) {
- hash = 31*hash + key.charAt( index );
+ for ( int index = 0; index < length; index++ ) {
+ hash = 31 * hash + key.charAt( index );
}
- return hash % providers.length;
+ return Math.abs( hash % providers.length );
}
}
Added: search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java 2008-09-14 09:00:05 UTC (rev 15195)
@@ -0,0 +1,40 @@
+// $Id$
+package org.hibernate.search.test.shards;
+
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.store.IdHashShardingStrategy;
+import org.hibernate.search.store.RAMDirectoryProvider;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Sanne Grinovero
+ */
+public class IdShardingStrategyTest extends TestCase {
+
+ private IdHashShardingStrategy shardStrategy;
+
+ protected void setUp() throws Exception {
+ shardStrategy = new IdHashShardingStrategy();
+ shardStrategy.initialize( null, new DirectoryProvider[] {
+ new RAMDirectoryProvider(), new RAMDirectoryProvider() } );
+ }
+
+ public void testHashOverflow() {
+ String key = String.valueOf( Integer.MAX_VALUE - 1 );
+ // any key will do as long as it's hash is negative
+ assertTrue( key.hashCode() < 0 );
+ assertAcceptableId( key );
+ }
+
+ private void assertAcceptableId(String id) {
+ try {
+ shardStrategy.getDirectoryProviderForAddition( null, id, id, null );
+ shardStrategy.getDirectoryProvidersForDeletion( null, id, id );
+ }
+ catch ( Exception e ) {
+ fail( "Couldn't get directory for id " + id );
+ }
+ }
+
+}
\ No newline at end of file
Property changes on: search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
16 years, 3 months
Hibernate SVN: r15194 - core/trunk/core/src/main/java/org/hibernate/impl.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2008-09-12 03:14:40 -0400 (Fri, 12 Sep 2008)
New Revision: 15194
Modified:
core/trunk/core/src/main/java/org/hibernate/impl/StatelessSessionImpl.java
Log:
HHH-3414 : use LoadQueryInfluencers.NONE from StatelessSession
Modified: core/trunk/core/src/main/java/org/hibernate/impl/StatelessSessionImpl.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/impl/StatelessSessionImpl.java 2008-09-12 07:00:40 UTC (rev 15193)
+++ core/trunk/core/src/main/java/org/hibernate/impl/StatelessSessionImpl.java 2008-09-12 07:14:40 UTC (rev 15194)
@@ -625,7 +625,7 @@
}
public LoadQueryInfluencers getLoadQueryInfluencers() {
- return null;
+ return LoadQueryInfluencers.NONE;
}
public void setFetchProfile(String name) {}
16 years, 3 months
Hibernate SVN: r15193 - core/trunk/core/src/main/java/org/hibernate/engine.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2008-09-12 03:00:40 -0400 (Fri, 12 Sep 2008)
New Revision: 15193
Modified:
core/trunk/core/src/main/java/org/hibernate/engine/LoadQueryInfluencers.java
Log:
HHH-3414 : made LoadQueryInfluencers Serializable
Modified: core/trunk/core/src/main/java/org/hibernate/engine/LoadQueryInfluencers.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/engine/LoadQueryInfluencers.java 2008-09-12 06:17:00 UTC (rev 15192)
+++ core/trunk/core/src/main/java/org/hibernate/engine/LoadQueryInfluencers.java 2008-09-12 07:00:40 UTC (rev 15193)
@@ -29,6 +29,7 @@
import java.util.Iterator;
import java.util.HashMap;
import java.util.HashSet;
+import java.io.Serializable;
import org.hibernate.Filter;
import org.hibernate.UnknownProfileException;
@@ -36,7 +37,7 @@
import org.hibernate.impl.FilterImpl;
/**
- * Centralize all options which can influence the SQL query needed to load and
+ * Centralize all options which can influence the SQL query needed to load an
* entity. Currently such influencers are defined as:<ul>
* <li>filters</li>
* <li>fetch profiles</li>
@@ -45,7 +46,7 @@
*
* @author Steve Ebersole
*/
-public class LoadQueryInfluencers {
+public class LoadQueryInfluencers implements Serializable {
/**
* Static reference useful for cases where we are creating load SQL
* outside the context of any influencers. One such example is
16 years, 3 months
Hibernate SVN: r15192 - in core/trunk/core/src/main/java/org/hibernate: loader and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2008-09-12 02:17:00 -0400 (Fri, 12 Sep 2008)
New Revision: 15192
Modified:
core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java
core/trunk/core/src/main/java/org/hibernate/loader/Loader.java
Log:
HHH-2592 : Dialect.forceLimitUsage
Modified: core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java 2008-09-12 06:16:31 UTC (rev 15191)
+++ core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java 2008-09-12 06:17:00 UTC (rev 15192)
@@ -768,16 +768,6 @@
}
/**
- * Generally, if there is no limit applied to a Hibernate query we do not apply any limits
- * to the SQL query. This option forces that the limit be written to the SQL query.
- *
- * @return True to force limit into SQL query even if none specified in Hibernate query; false otherwise.
- */
- public boolean forceLimitUsage() {
- return true;
- }
-
- /**
* Does this dialect's LIMIT support (if any) additionally
* support specifying an offset?
*
@@ -838,6 +828,16 @@
}
/**
+ * Generally, if there is no limit applied to a Hibernate query we do not apply any limits
+ * to the SQL query. This option forces that the limit be written to the SQL query.
+ *
+ * @return True to force limit into SQL query even if none specified in Hibernate query; false otherwise.
+ */
+ public boolean forceLimitUsage() {
+ return false;
+ }
+
+ /**
* Given a limit and an offset, apply the limit clause to the query.
*
* @param query The query to which to apply the limit.
@@ -846,7 +846,7 @@
* @return The modified query statement with the limit applied.
*/
public String getLimitString(String query, int offset, int limit) {
- return getLimitString( query, offset > 0 );
+ return getLimitString( query, ( offset > 0 || forceLimitUsage() ) );
}
/**
Modified: core/trunk/core/src/main/java/org/hibernate/loader/Loader.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/Loader.java 2008-09-12 06:16:31 UTC (rev 15191)
+++ core/trunk/core/src/main/java/org/hibernate/loader/Loader.java 2008-09-12 06:17:00 UTC (rev 15192)
@@ -1526,7 +1526,7 @@
* LIMIT clause.
*/
private static boolean useLimit(final RowSelection selection, final Dialect dialect) {
- return dialect.supportsLimit() && ( hasMaxRows( selection ) || dialect.forceLimitUsage() );
+ return dialect.supportsLimit() && hasMaxRows( selection );
}
/**
@@ -1659,7 +1659,7 @@
}
int firstRow = getFirstRow( selection );
int lastRow = getMaxOrLimit( selection, dialect );
- boolean hasFirstRow = firstRow > 0 && dialect.supportsLimitOffset();
+ boolean hasFirstRow = dialect.supportsLimitOffset() && ( firstRow > 0 || dialect.forceLimitUsage() );
boolean reverse = dialect.bindLimitParametersInReverseOrder();
if ( hasFirstRow ) {
statement.setInt( index + ( reverse ? 1 : 0 ), firstRow );
16 years, 3 months
Hibernate SVN: r15191 - in core/branches/Branch_3_3/core/src/main/java/org/hibernate: loader and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2008-09-12 02:16:31 -0400 (Fri, 12 Sep 2008)
New Revision: 15191
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/dialect/Dialect.java
core/branches/Branch_3_3/core/src/main/java/org/hibernate/loader/Loader.java
Log:
HHH-2592 - Dialect.forceLimitUsage
Modified: core/branches/Branch_3_3/core/src/main/java/org/hibernate/dialect/Dialect.java
===================================================================
--- core/branches/Branch_3_3/core/src/main/java/org/hibernate/dialect/Dialect.java 2008-09-12 04:53:33 UTC (rev 15190)
+++ core/branches/Branch_3_3/core/src/main/java/org/hibernate/dialect/Dialect.java 2008-09-12 06:16:31 UTC (rev 15191)
@@ -768,16 +768,6 @@
}
/**
- * Generally, if there is no limit applied to a Hibernate query we do not apply any limits
- * to the SQL query. This option forces that the limit be written to the SQL query.
- *
- * @return True to force limit into SQL query even if none specified in Hibernate query; false otherwise.
- */
- public boolean forceLimitUsage() {
- return true;
- }
-
- /**
* Does this dialect's LIMIT support (if any) additionally
* support specifying an offset?
*
@@ -838,6 +828,16 @@
}
/**
+ * Generally, if there is no limit applied to a Hibernate query we do not apply any limits
+ * to the SQL query. This option forces that the limit be written to the SQL query.
+ *
+ * @return True to force limit into SQL query even if none specified in Hibernate query; false otherwise.
+ */
+ public boolean forceLimitUsage() {
+ return false;
+ }
+
+ /**
* Given a limit and an offset, apply the limit clause to the query.
*
* @param query The query to which to apply the limit.
@@ -846,7 +846,7 @@
* @return The modified query statement with the limit applied.
*/
public String getLimitString(String query, int offset, int limit) {
- return getLimitString( query, offset > 0 );
+ return getLimitString( query, ( offset > 0 || forceLimitUsage() ) );
}
/**
Modified: core/branches/Branch_3_3/core/src/main/java/org/hibernate/loader/Loader.java
===================================================================
--- core/branches/Branch_3_3/core/src/main/java/org/hibernate/loader/Loader.java 2008-09-12 04:53:33 UTC (rev 15190)
+++ core/branches/Branch_3_3/core/src/main/java/org/hibernate/loader/Loader.java 2008-09-12 06:16:31 UTC (rev 15191)
@@ -1526,7 +1526,7 @@
* LIMIT clause.
*/
private static boolean useLimit(final RowSelection selection, final Dialect dialect) {
- return dialect.supportsLimit() && ( hasMaxRows( selection ) || dialect.forceLimitUsage() );
+ return dialect.supportsLimit() && hasMaxRows( selection );
}
/**
@@ -1659,7 +1659,7 @@
}
int firstRow = getFirstRow( selection );
int lastRow = getMaxOrLimit( selection, dialect );
- boolean hasFirstRow = firstRow > 0 && dialect.supportsLimitOffset();
+ boolean hasFirstRow = dialect.supportsLimitOffset() && ( firstRow > 0 || dialect.forceLimitUsage() );
boolean reverse = dialect.bindLimitParametersInReverseOrder();
if ( hasFirstRow ) {
statement.setInt( index + ( reverse ? 1 : 0 ), firstRow );
16 years, 3 months
Hibernate SVN: r15190 - in core/trunk/core/src/main/java/org/hibernate: loader and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2008-09-12 00:53:33 -0400 (Fri, 12 Sep 2008)
New Revision: 15190
Modified:
core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java
core/trunk/core/src/main/java/org/hibernate/loader/Loader.java
Log:
HHH-2592 : apply limit even when no limit specified :/
Modified: core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java 2008-09-12 04:53:18 UTC (rev 15189)
+++ core/trunk/core/src/main/java/org/hibernate/dialect/Dialect.java 2008-09-12 04:53:33 UTC (rev 15190)
@@ -768,6 +768,16 @@
}
/**
+ * Generally, if there is no limit applied to a Hibernate query we do not apply any limits
+ * to the SQL query. This option forces that the limit be written to the SQL query.
+ *
+ * @return True to force limit into SQL query even if none specified in Hibernate query; false otherwise.
+ */
+ public boolean forceLimitUsage() {
+ return true;
+ }
+
+ /**
* Does this dialect's LIMIT support (if any) additionally
* support specifying an offset?
*
Modified: core/trunk/core/src/main/java/org/hibernate/loader/Loader.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/loader/Loader.java 2008-09-12 04:53:18 UTC (rev 15189)
+++ core/trunk/core/src/main/java/org/hibernate/loader/Loader.java 2008-09-12 04:53:33 UTC (rev 15190)
@@ -1526,7 +1526,7 @@
* LIMIT clause.
*/
private static boolean useLimit(final RowSelection selection, final Dialect dialect) {
- return dialect.supportsLimit() && hasMaxRows( selection );
+ return dialect.supportsLimit() && ( hasMaxRows( selection ) || dialect.forceLimitUsage() );
}
/**
16 years, 3 months
Hibernate SVN: r15189 - in core/branches/Branch_3_3/core/src/main/java/org/hibernate: loader and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2008-09-12 00:53:18 -0400 (Fri, 12 Sep 2008)
New Revision: 15189
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/dialect/Dialect.java
core/branches/Branch_3_3/core/src/main/java/org/hibernate/loader/Loader.java
Log:
HHH-2592 : apply limit even when no limit specified :/
Modified: core/branches/Branch_3_3/core/src/main/java/org/hibernate/dialect/Dialect.java
===================================================================
--- core/branches/Branch_3_3/core/src/main/java/org/hibernate/dialect/Dialect.java 2008-09-12 04:39:09 UTC (rev 15188)
+++ core/branches/Branch_3_3/core/src/main/java/org/hibernate/dialect/Dialect.java 2008-09-12 04:53:18 UTC (rev 15189)
@@ -768,6 +768,16 @@
}
/**
+ * Generally, if there is no limit applied to a Hibernate query we do not apply any limits
+ * to the SQL query. This option forces that the limit be written to the SQL query.
+ *
+ * @return True to force limit into SQL query even if none specified in Hibernate query; false otherwise.
+ */
+ public boolean forceLimitUsage() {
+ return true;
+ }
+
+ /**
* Does this dialect's LIMIT support (if any) additionally
* support specifying an offset?
*
Modified: core/branches/Branch_3_3/core/src/main/java/org/hibernate/loader/Loader.java
===================================================================
--- core/branches/Branch_3_3/core/src/main/java/org/hibernate/loader/Loader.java 2008-09-12 04:39:09 UTC (rev 15188)
+++ core/branches/Branch_3_3/core/src/main/java/org/hibernate/loader/Loader.java 2008-09-12 04:53:18 UTC (rev 15189)
@@ -1526,7 +1526,7 @@
* LIMIT clause.
*/
private static boolean useLimit(final RowSelection selection, final Dialect dialect) {
- return dialect.supportsLimit() && hasMaxRows( selection );
+ return dialect.supportsLimit() && ( hasMaxRows( selection ) || dialect.forceLimitUsage() );
}
/**
16 years, 3 months
Hibernate SVN: r15188 - core/trunk/cache-ehcache/src/main/java/org/hibernate/cache.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2008-09-12 00:39:09 -0400 (Fri, 12 Sep 2008)
New Revision: 15188
Added:
core/trunk/cache-ehcache/src/main/java/org/hibernate/cache/SingletonEhCacheProvider.java
Modified:
core/trunk/cache-ehcache/src/main/java/org/hibernate/cache/EhCache.java
Log:
HHH-3450 : SingletonEhCacheProvider
Modified: core/trunk/cache-ehcache/src/main/java/org/hibernate/cache/EhCache.java
===================================================================
--- core/trunk/cache-ehcache/src/main/java/org/hibernate/cache/EhCache.java 2008-09-12 04:38:43 UTC (rev 15187)
+++ core/trunk/cache-ehcache/src/main/java/org/hibernate/cache/EhCache.java 2008-09-12 04:39:09 UTC (rev 15188)
@@ -49,7 +49,7 @@
private static final int SIXTY_THOUSAND_MS = 60000;
- private net.sf.ehcache.Cache cache;
+ private net.sf.ehcache.Ehcache cache;
/**
* Creates a new Hibernate pluggable cache based on a cache name.
@@ -57,7 +57,7 @@
*
* @param cache The underlying EhCache instance to use.
*/
- public EhCache(net.sf.ehcache.Cache cache) {
+ public EhCache(net.sf.ehcache.Ehcache cache) {
this.cache = cache;
}
Added: core/trunk/cache-ehcache/src/main/java/org/hibernate/cache/SingletonEhCacheProvider.java
===================================================================
--- core/trunk/cache-ehcache/src/main/java/org/hibernate/cache/SingletonEhCacheProvider.java (rev 0)
+++ core/trunk/cache-ehcache/src/main/java/org/hibernate/cache/SingletonEhCacheProvider.java 2008-09-12 04:39:09 UTC (rev 15188)
@@ -0,0 +1,202 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ *
+ */
+package org.hibernate.cache;
+
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.util.ClassLoaderUtil;
+
+import java.net.URL;
+import java.util.Properties;
+import java.util.logging.Logger;
+import java.util.logging.Level;
+
+/**
+ * Singleton cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider is support for
+ * non Serializable keys and values. This provider works as a Singleton. No matter how many Hibernate Configurations
+ * you have, only one ehcache CacheManager is used. See EhCacheProvider for a non-singleton implementation.
+ * <p/>
+ * 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=net.sf.ehcache.hibernate.SingletonEhCacheProvider</code> in the Hibernate configuration
+ * to enable this provider for Hibernate's second level cache.
+ * <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
+ * @version $Id: SingletonEhCacheProvider.java 744 2008-08-16 20:10:49Z gregluck $
+ */
+public final class SingletonEhCacheProvider implements CacheProvider {
+
+ /**
+ * The Hibernate system property specifying the location of the ehcache configuration file name.
+ * <p/
+ * If not set, ehcache.xml will be looked for in the root of the classpath.
+ * <p/>
+ * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath.
+ */
+ public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName";
+
+ private static final Logger LOG = Logger.getLogger( SingletonEhCacheProvider.class.getName() );
+
+ /**
+ * To be backwardly compatible with a lot of Hibernate code out there, allow multiple starts and stops on the
+ * one singleton CacheManager. Keep a count of references to only stop on when only one reference is held.
+ */
+ private static int referenceCount;
+
+ private CacheManager manager;
+
+
+ /**
+ * Builds a Cache.
+ * <p/>
+ * Even though this method provides properties, they are not used.
+ * Properties for EHCache are specified in the ehcache.xml file.
+ * Configuration will be read from ehcache.xml for a cache declaration
+ * where the name attribute matches the name parameter in this builder.
+ *
+ * @param name the name of the cache. Must match a cache configured in ehcache.xml
+ * @param properties not used
+ *
+ * @return a newly built cache will be built and initialised
+ *
+ * @throws org.hibernate.cache.CacheException
+ * inter alia, if a cache of the same name already exists
+ */
+ public final Cache buildCache(String name, Properties properties) throws CacheException {
+ try {
+ net.sf.ehcache.Ehcache cache = manager.getEhcache( name );
+ if ( cache == null ) {
+ SingletonEhCacheProvider.LOG.warning(
+ "Could not find a specific ehcache configuration for cache named ["
+ + name + "]; using defaults."
+ );
+ manager.addCache( name );
+ cache = manager.getEhcache( name );
+ SingletonEhCacheProvider.LOG.fine( "started EHCache region: " + name );
+ }
+ return new EhCache( cache );
+ }
+ catch ( net.sf.ehcache.CacheException e ) {
+ throw new CacheException( e );
+ }
+ }
+
+ /**
+ * Returns the next timestamp.
+ */
+ public final long nextTimestamp() {
+ return Timestamper.next();
+ }
+
+ /**
+ * Callback to perform any necessary initialization of the underlying cache implementation
+ * during SessionFactory construction.
+ * <p/>
+ *
+ * @param properties current configuration settings.
+ */
+ public final void start(Properties properties) throws CacheException {
+ String configurationResourceName = null;
+ if ( properties != null ) {
+ configurationResourceName = ( String ) properties.get( NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME );
+ }
+ if ( configurationResourceName == null || configurationResourceName.length() == 0 ) {
+ manager = CacheManager.create();
+ referenceCount++;
+ }
+ else {
+ if ( !configurationResourceName.startsWith( "/" ) ) {
+ configurationResourceName = "/" + configurationResourceName;
+ if ( LOG.isLoggable( Level.FINE ) ) {
+ LOG.fine(
+ "prepending / to " + configurationResourceName + ". It should be placed in the root"
+ + "of the classpath rather than in a package."
+ );
+ }
+ }
+ URL url = loadResource( configurationResourceName );
+ manager = CacheManager.create( url );
+ referenceCount++;
+ }
+ }
+
+ private URL loadResource(String configurationResourceName) {
+ ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader();
+ URL url = null;
+ if ( standardClassloader != null ) {
+ url = standardClassloader.getResource( configurationResourceName );
+ }
+ if ( url == null ) {
+ url = this.getClass().getResource( configurationResourceName );
+ }
+ if ( LOG.isLoggable( Level.FINE ) ) {
+ LOG.fine(
+ "Creating EhCacheProvider from a specified resource: "
+ + configurationResourceName + " Resolved to URL: " + url
+ );
+ }
+ if ( url == null ) {
+ if ( LOG.isLoggable( Level.WARNING ) ) {
+ LOG.warning(
+ "A configurationResourceName was set to " + configurationResourceName +
+ " but the resource could not be loaded from the classpath." +
+ "Ehcache will configure itself using defaults."
+ );
+ }
+ }
+ return url;
+ }
+
+ /**
+ * Callback to perform any necessary cleanup of the underlying cache implementation
+ * during SessionFactory.close().
+ */
+ public void stop() {
+ if ( manager != null ) {
+ referenceCount--;
+ if ( referenceCount == 0 ) {
+ manager.shutdown();
+ }
+ manager = null;
+ }
+ }
+
+ /**
+ * Not sure what this is supposed to do.
+ *
+ * @return false to be safe
+ */
+ public final boolean isMinimalPutsEnabledByDefault() {
+ return false;
+ }
+
+}
16 years, 3 months
Hibernate SVN: r15187 - core/branches/Branch_3_2/src/org/hibernate/cache.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2008-09-12 00:38:43 -0400 (Fri, 12 Sep 2008)
New Revision: 15187
Added:
core/branches/Branch_3_2/src/org/hibernate/cache/SingletonEhCacheProvider.java
Modified:
core/branches/Branch_3_2/src/org/hibernate/cache/EhCache.java
Log:
HHH-3450 : SingletonEhCacheProvider
Modified: core/branches/Branch_3_2/src/org/hibernate/cache/EhCache.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/cache/EhCache.java 2008-09-12 04:36:50 UTC (rev 15186)
+++ core/branches/Branch_3_2/src/org/hibernate/cache/EhCache.java 2008-09-12 04:38:43 UTC (rev 15187)
@@ -43,7 +43,7 @@
private static final int SIXTY_THOUSAND_MS = 60000;
- private net.sf.ehcache.Cache cache;
+ private net.sf.ehcache.Ehcache cache;
/**
* Creates a new Hibernate pluggable cache based on a cache name.
@@ -51,7 +51,7 @@
*
* @param cache The underlying EhCache instance to use.
*/
- public EhCache(net.sf.ehcache.Cache cache) {
+ public EhCache(net.sf.ehcache.Ehcache cache) {
this.cache = cache;
}
@@ -272,4 +272,4 @@
return "EHCache(" + getRegionName() + ')';
}
-}
\ No newline at end of file
+}
Added: core/branches/Branch_3_2/src/org/hibernate/cache/SingletonEhCacheProvider.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/cache/SingletonEhCacheProvider.java (rev 0)
+++ core/branches/Branch_3_2/src/org/hibernate/cache/SingletonEhCacheProvider.java 2008-09-12 04:38:43 UTC (rev 15187)
@@ -0,0 +1,202 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ *
+ */
+package org.hibernate.cache;
+
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.util.ClassLoaderUtil;
+
+import java.net.URL;
+import java.util.Properties;
+import java.util.logging.Logger;
+import java.util.logging.Level;
+
+/**
+ * Singleton cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider is support for
+ * non Serializable keys and values. This provider works as a Singleton. No matter how many Hibernate Configurations
+ * you have, only one ehcache CacheManager is used. See EhCacheProvider for a non-singleton implementation.
+ * <p/>
+ * 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=net.sf.ehcache.hibernate.SingletonEhCacheProvider</code> in the Hibernate configuration
+ * to enable this provider for Hibernate's second level cache.
+ * <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
+ * @version $Id: SingletonEhCacheProvider.java 744 2008-08-16 20:10:49Z gregluck $
+ */
+public final class SingletonEhCacheProvider implements CacheProvider {
+
+ /**
+ * The Hibernate system property specifying the location of the ehcache configuration file name.
+ * <p/
+ * If not set, ehcache.xml will be looked for in the root of the classpath.
+ * <p/>
+ * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath.
+ */
+ public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName";
+
+ private static final Logger LOG = Logger.getLogger( SingletonEhCacheProvider.class.getName() );
+
+ /**
+ * To be backwardly compatible with a lot of Hibernate code out there, allow multiple starts and stops on the
+ * one singleton CacheManager. Keep a count of references to only stop on when only one reference is held.
+ */
+ private static int referenceCount;
+
+ private CacheManager manager;
+
+
+ /**
+ * Builds a Cache.
+ * <p/>
+ * Even though this method provides properties, they are not used.
+ * Properties for EHCache are specified in the ehcache.xml file.
+ * Configuration will be read from ehcache.xml for a cache declaration
+ * where the name attribute matches the name parameter in this builder.
+ *
+ * @param name the name of the cache. Must match a cache configured in ehcache.xml
+ * @param properties not used
+ *
+ * @return a newly built cache will be built and initialised
+ *
+ * @throws org.hibernate.cache.CacheException
+ * inter alia, if a cache of the same name already exists
+ */
+ public final Cache buildCache(String name, Properties properties) throws CacheException {
+ try {
+ net.sf.ehcache.Ehcache cache = manager.getEhcache( name );
+ if ( cache == null ) {
+ SingletonEhCacheProvider.LOG.warning(
+ "Could not find a specific ehcache configuration for cache named ["
+ + name + "]; using defaults."
+ );
+ manager.addCache( name );
+ cache = manager.getEhcache( name );
+ SingletonEhCacheProvider.LOG.fine( "started EHCache region: " + name );
+ }
+ return new EhCache( cache );
+ }
+ catch ( net.sf.ehcache.CacheException e ) {
+ throw new CacheException( e );
+ }
+ }
+
+ /**
+ * Returns the next timestamp.
+ */
+ public final long nextTimestamp() {
+ return Timestamper.next();
+ }
+
+ /**
+ * Callback to perform any necessary initialization of the underlying cache implementation
+ * during SessionFactory construction.
+ * <p/>
+ *
+ * @param properties current configuration settings.
+ */
+ public final void start(Properties properties) throws CacheException {
+ String configurationResourceName = null;
+ if ( properties != null ) {
+ configurationResourceName = ( String ) properties.get( NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME );
+ }
+ if ( configurationResourceName == null || configurationResourceName.length() == 0 ) {
+ manager = CacheManager.create();
+ referenceCount++;
+ }
+ else {
+ if ( !configurationResourceName.startsWith( "/" ) ) {
+ configurationResourceName = "/" + configurationResourceName;
+ if ( LOG.isLoggable( Level.FINE ) ) {
+ LOG.fine(
+ "prepending / to " + configurationResourceName + ". It should be placed in the root"
+ + "of the classpath rather than in a package."
+ );
+ }
+ }
+ URL url = loadResource( configurationResourceName );
+ manager = CacheManager.create( url );
+ referenceCount++;
+ }
+ }
+
+ private URL loadResource(String configurationResourceName) {
+ ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader();
+ URL url = null;
+ if ( standardClassloader != null ) {
+ url = standardClassloader.getResource( configurationResourceName );
+ }
+ if ( url == null ) {
+ url = this.getClass().getResource( configurationResourceName );
+ }
+ if ( LOG.isLoggable( Level.FINE ) ) {
+ LOG.fine(
+ "Creating EhCacheProvider from a specified resource: "
+ + configurationResourceName + " Resolved to URL: " + url
+ );
+ }
+ if ( url == null ) {
+ if ( LOG.isLoggable( Level.WARNING ) ) {
+ LOG.warning(
+ "A configurationResourceName was set to " + configurationResourceName +
+ " but the resource could not be loaded from the classpath." +
+ "Ehcache will configure itself using defaults."
+ );
+ }
+ }
+ return url;
+ }
+
+ /**
+ * Callback to perform any necessary cleanup of the underlying cache implementation
+ * during SessionFactory.close().
+ */
+ public void stop() {
+ if ( manager != null ) {
+ referenceCount--;
+ if ( referenceCount == 0 ) {
+ manager.shutdown();
+ }
+ manager = null;
+ }
+ }
+
+ /**
+ * Not sure what this is supposed to do.
+ *
+ * @return false to be safe
+ */
+ public final boolean isMinimalPutsEnabledByDefault() {
+ return false;
+ }
+
+}
16 years, 3 months