1 package org.hibernate.cache; 3 4 import org.jboss.cache.PropertyConfigurator; 5 import org.hibernate.transaction.TransactionManagerLookup; 6 import org.hibernate.transaction.TransactionManagerLookupFactory; 7 8 import javax.transaction.TransactionManager ; 9 import java.util.Properties ; 10 11 17 public class TreeCacheProvider implements CacheProvider { 18 19 private org.jboss.cache.TreeCache cache; 20 private TransactionManager transactionManager; 21 22 30 public Cache buildCache(String regionName, Properties properties) throws CacheException { 31 return new TreeCache(cache, regionName, transactionManager); 32 } 33 34 public long nextTimestamp() { 35 return System.currentTimeMillis() / 100; 36 } 37 38 45 public void start(Properties properties) { 46 try { 47 cache = new org.jboss.cache.TreeCache(); 48 PropertyConfigurator config = new PropertyConfigurator(); 49 config.configure(cache, "treecache.xml"); 50 TransactionManagerLookup transactionManagerLookup = TransactionManagerLookupFactory.getTransactionManagerLookup(properties); 51 if (transactionManagerLookup!=null) { 52 cache.setTransactionManagerLookup( new TransactionManagerLookupAdaptor(transactionManagerLookup, properties) ); 53 transactionManager = transactionManagerLookup.getTransactionManager(properties); 54 } 55 cache.start(); 56 } 57 catch (Exception e) { 58 throw new CacheException(e); 59 } 60 } 61 62 public void stop() { 63 if (cache!=null) { 64 cache.stop(); 65 cache.destroy(); 66 cache=null; 67 } 68 } 69 70 public boolean isMinimalPutsEnabledByDefault() { 71 return true; 72 } 73 74 static final class TransactionManagerLookupAdaptor implements org.jboss.cache.TransactionManagerLookup { 75 private final TransactionManagerLookup tml; 76 private final Properties props; 77 TransactionManagerLookupAdaptor(TransactionManagerLookup tml, Properties props) { 78 this.tml=tml; 79 this.props=props; 80 } 81 public TransactionManager getTransactionManager() throws Exception { 82 return tml.getTransactionManager(props); 83 } 84 } 85 86 } 87 | Popular Tags |