2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
24
package org.hibernate.cache;
26
import java.util.Properties;
27
import javax.transaction.TransactionManager;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31
import org.hibernate.transaction.TransactionManagerLookup;
32
import org.hibernate.transaction.TransactionManagerLookupFactory;
33
import org.hibernate.cfg.Environment;
34
import org.jboss.cache.PropertyConfigurator;
37
* Support for a standalone JBossCache (TreeCache) instance. The JBossCache is configured
38
* via a local config resource.
42
public class TreeCacheProvider implements CacheProvider {
45
* @deprecated use {@link org.hibernate.cfg.Environment#CACHE_PROVIDER_CONFIG}
47
public static final String CONFIG_RESOURCE = "hibernate.cache.tree_cache.config";
48
public static final String DEFAULT_CONFIG = "treecache-optimistic.xml";
50
private static final Logger log = LoggerFactory.getLogger( TreeCacheProvider.class );
52
private org.jboss.cache.TreeCache cache;
53
private TransactionManager transactionManager;
56
* Construct and configure the Cache representation of a named cache region.
58
* @param regionName the name of the cache region
59
* @param properties configuration settings
60
* @return The Cache representation of the named cache region.
61
* @throws CacheException Indicates an error building the cache region.
63
public Cache buildCache(String regionName, Properties properties) throws CacheException {
64
return new TreeCache(cache, regionName, transactionManager);
67
public long nextTimestamp() {
68
return System.currentTimeMillis() / 100;
72
* Prepare the underlying JBossCache TreeCache instance.
74
* @param properties All current config settings.
76
* @throws CacheException Indicates a problem preparing cache for use.
78
public void start(Properties properties) {
79
String resource = properties.getProperty( Environment.CACHE_PROVIDER_CONFIG );
81
if ( resource == null ) {
82
resource = properties.getProperty( CONFIG_RESOURCE );
84
if ( resource == null ) {
85
resource = DEFAULT_CONFIG;
87
log.debug( "Configuring TreeCache from resource [" + resource + "]" );
89
cache = new org.jboss.cache.TreeCache();
90
PropertyConfigurator config = new PropertyConfigurator();
91
config.configure( cache, resource );
92
TransactionManagerLookup transactionManagerLookup = TransactionManagerLookupFactory.getTransactionManagerLookup(properties);
93
if (transactionManagerLookup!=null) {
94
cache.setTransactionManagerLookup( new TransactionManagerLookupAdaptor(transactionManagerLookup, properties) );
95
transactionManager = transactionManagerLookup.getTransactionManager(properties);
100
throw new CacheException(e);
112
public boolean isMinimalPutsEnabledByDefault() {
116
static final class TransactionManagerLookupAdaptor implements org.jboss.cache.TransactionManagerLookup {
117
private final TransactionManagerLookup tml;
118
private final Properties props;
119
TransactionManagerLookupAdaptor(TransactionManagerLookup tml, Properties props) {
123
public TransactionManager getTransactionManager() throws Exception {
124
return tml.getTransactionManager(props);
128
public org.jboss.cache.TreeCache getUnderlyingCache() {