[jboss-cvs] JBossAS SVN: r77607 - projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Aug 28 12:12:37 EDT 2008
Author: bstansberry at jboss.com
Date: 2008-08-28 12:12:37 -0400 (Thu, 28 Aug 2008)
New Revision: 77607
Added:
projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider/OptimisticTreeCacheProvider.java
projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider/TreeCacheProvider.java
Log:
[JBCLUSTER-206] Add provider versions that instantiate JBC from config file
Added: projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider/OptimisticTreeCacheProvider.java
===================================================================
--- projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider/OptimisticTreeCacheProvider.java (rev 0)
+++ projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider/OptimisticTreeCacheProvider.java 2008-08-28 16:12:37 UTC (rev 77607)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.hibernate.jbc.cacheprovider;
+
+import java.util.Properties;
+
+import org.hibernate.cache.CacheException;
+import org.jboss.logging.Logger;
+
+/**
+ * Trivial {@link TreeCacheProvider} subclass that logs a warning in
+ * {@link #start(Properties) start} if the underlying JBoss Cache
+ * is not configured for optimistic locking. Like the superclass,
+ * will provide working Cache implementations whether JBoss Cache is
+ * configured for optimistic locking or not; the only added behavior
+ * is the logging of the warning if the JBoss Cache configuration doesn't
+ * match the intent implied by the use of this class.
+ *
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
+ * @author Brian Stansberry
+ * @deprecated TreeCacheProvider supports both optimistic and pessimistic
+ * cache configurations.
+ */
+public class OptimisticTreeCacheProvider extends TreeCacheProvider
+{
+ private static final Logger log = Logger.getLogger(OptimisticTreeCacheProvider.class);
+
+ public void start(Properties properties) throws CacheException
+ {
+ super.start(properties);
+
+ if (isOptimistic() == false)
+ {
+ log.warn("JBoss Cache is not configured for optimistic locking; " +
+ "provided Cache implementations therefore will not implement OptimisticCache");
+ }
+ }
+
+}
Added: projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider/TreeCacheProvider.java
===================================================================
--- projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider/TreeCacheProvider.java (rev 0)
+++ projects/cluster/hibernate-jbc-cacheprovider/trunk/src/main/java/org/jboss/hibernate/jbc/cacheprovider/TreeCacheProvider.java 2008-08-28 16:12:37 UTC (rev 77607)
@@ -0,0 +1,171 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.hibernate.jbc.cacheprovider;
+
+import java.util.Properties;
+
+import javax.transaction.TransactionManager;
+
+import org.hibernate.cache.Cache;
+import org.hibernate.cache.CacheException;
+import org.hibernate.cfg.Environment;
+import org.hibernate.transaction.TransactionManagerLookup;
+import org.hibernate.transaction.TransactionManagerLookupFactory;
+import org.jboss.cache.PropertyConfigurator;
+import org.jboss.logging.Logger;
+
+/**
+ * Support for a standalone JBoss Cache (TreeCache) instance. The JBoss Cache
+ * is instantiated by this provider and is configured via a local config resource.
+ *
+ * @author Brian Stansberry
+ */
+public class TreeCacheProvider
+{
+ /**
+ * @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-optimistic.xml";
+
+ private static final Logger log = Logger.getLogger( TreeCacheProvider.class );
+
+ private org.jboss.cache.TreeCache cache;
+ private TransactionManager transactionManager;
+ private boolean optimistic;
+
+ /**
+ * Construct and configure the Cache representation of a named cache region.
+ *
+ * @param regionName the name of the cache region
+ * @param properties configuration settings
+ * @return The Cache representation of the named cache region.
+ * @throws CacheException Indicates an error building the cache region.
+ */
+ public Cache buildCache(String regionName, Properties properties) throws CacheException
+ {
+ CacheProperties cacheProperties = new CacheProperties(properties);
+
+ if (optimistic)
+ {
+ return new OptimisticJBCCache(getUnderlyingCache(), regionName, cacheProperties);
+ }
+ else
+ {
+ /* removed dependency on ejb3's TxUtil */
+ return new JBCCache(getUnderlyingCache(), regionName, cacheProperties, transactionManager);
+ }
+ }
+
+ public long nextTimestamp() {
+ return System.currentTimeMillis() / 100;
+ }
+
+ public boolean isOptimistic()
+ {
+ return optimistic;
+ }
+
+ /**
+ * Prepare the underlying JBossCache TreeCache instance.
+ *
+ * @param properties All current config settings.
+ *
+ * @throws CacheException Indicates a problem preparing cache for use.
+ */
+ public void start(Properties properties) {
+ 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 + "]" );
+ try
+ {
+ cache = new org.jboss.cache.TreeCache();
+ PropertyConfigurator config = new PropertyConfigurator();
+ config.configure( cache, resource );
+ TransactionManagerLookup transactionManagerLookup = TransactionManagerLookupFactory.getTransactionManagerLookup(properties);
+ if (transactionManagerLookup!=null)
+ {
+ cache.setTransactionManagerLookup( new TransactionManagerLookupAdaptor(transactionManagerLookup, properties) );
+ transactionManager = transactionManagerLookup.getTransactionManager(properties);
+ }
+ cache.start();
+
+ if ("OPTIMISTIC".equals(cache.getNodeLockingScheme()))
+ {
+ optimistic = true;
+ log.debug("JBoss Cache is configured for optimistic locking; " +
+ "provided Cache implementations will also implement OptimisticCache");
+ }
+
+ }
+ catch (Exception e) {
+ throw new CacheException(e);
+ }
+ }
+
+ public void stop()
+ {
+ if (cache!=null)
+ {
+ cache.stop();
+ cache.destroy();
+ cache=null;
+ }
+ }
+
+ public boolean isMinimalPutsEnabledByDefault()
+ {
+ return true;
+ }
+
+ public org.jboss.cache.TreeCache getUnderlyingCache()
+ {
+ return cache;
+ }
+
+ static final class TransactionManagerLookupAdaptor implements org.jboss.cache.TransactionManagerLookup
+ {
+ private final TransactionManagerLookup tml;
+ private final Properties props;
+
+ TransactionManagerLookupAdaptor(TransactionManagerLookup tml, Properties props)
+ {
+ this.tml=tml;
+ this.props=props;
+ }
+
+ public TransactionManager getTransactionManager() throws Exception
+ {
+ return tml.getTransactionManager(props);
+ }
+ }
+
+}
More information about the jboss-cvs-commits
mailing list