/*
* 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.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;
import javax.transaction.TransactionManager;
import org.hibernate.cache.Cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.CacheProvider;
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
* @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
*/
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";
/**
* @deprecated deprecared default configuration, use {@link DEFAULT_CONFIG} instead.
*/
public static final String LEGACY_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 = configFileExists(DEFAULT_CONFIG) ? DEFAULT_CONFIG : null;
}
if (resource == null)
{
/* If new default does not exists, try to lookup legacy default config file.
* If found, then use legacy default config file, if not, revert back
* to default config, which will fail but at least will show current default. */
resource = configFileExists(LEGACY_DEFAULT_CONFIG) ? LEGACY_DEFAULT_CONFIG : 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.startService();
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.stopService();
cache = null;
}
}
public boolean isMinimalPutsEnabledByDefault()
{
return true;
}
public org.jboss.cache.TreeCache getUnderlyingCache()
{
return cache;
}
protected boolean configFileExists(String configFile)
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = cl.getResourceAsStream(configFile);
if (is == null)
{
try
{
is = new FileInputStream(configFile);
}
catch (FileNotFoundException e)
{
log.debug("Config file not found: " + configFile);
}
}
if (is == null)
{
return false;
}
log.debug("Using configuration file: " + configFile);
return true;
}
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);
}
}
}