KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > TreeCacheProvider


1 //$Id: TreeCacheProvider.java,v 1.6 2005/03/16 06:01:17 oneovthafew Exp $
2
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 JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 /**
12  * Support for a standalone JBossCache (TreeCache) instance. The JBossCache is configured
13  * via a local config resource.
14  *
15  * @author Gavin King
16  */

17 public class TreeCacheProvider implements CacheProvider {
18
19     private org.jboss.cache.TreeCache cache;
20     private TransactionManager JavaDoc transactionManager;
21
22     /**
23      * Construct and configure the Cache representation of a named cache region.
24      *
25      * @param regionName the name of the cache region
26      * @param properties configuration settings
27      * @return The Cache representation of the named cache region.
28      * @throws CacheException Indicates an error building the cache region.
29      */

30     public Cache buildCache(String JavaDoc regionName, Properties JavaDoc properties) throws CacheException {
31         return new TreeCache(cache, regionName, transactionManager);
32     }
33
34     public long nextTimestamp() {
35         return System.currentTimeMillis() / 100;
36     }
37
38     /**
39      * Prepare the underlying JBossCache TreeCache instance.
40      *
41      * @param properties All current config settings.
42      *
43      * @throws CacheException Indicates a problem preparing cache for use.
44      */

45     public void start(Properties JavaDoc 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 JavaDoc 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 JavaDoc props;
77         TransactionManagerLookupAdaptor(TransactionManagerLookup tml, Properties JavaDoc props) {
78             this.tml=tml;
79             this.props=props;
80         }
81         public TransactionManager JavaDoc getTransactionManager() throws Exception JavaDoc {
82             return tml.getTransactionManager(props);
83         }
84     }
85
86 }
87
Popular Tags