KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: TreeCache.java,v 1.16 2005/07/08 01:49:47 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import javax.transaction.SystemException JavaDoc;
10 import javax.transaction.Transaction JavaDoc;
11 import javax.transaction.TransactionManager JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.Fqn;
16 import org.jboss.cache.lock.TimeoutException;
17
18 /**
19  * Represents a particular region within the given JBossCache TreeCache.
20  *
21  * @author Gavin King
22  */

23 public class TreeCache implements Cache {
24     
25     private static final Log log = LogFactory.getLog(TreeCache.class);
26
27     private static final String JavaDoc ITEM = "item";
28
29     private org.jboss.cache.TreeCache cache;
30     private final String JavaDoc regionName;
31     private final String JavaDoc userRegionName;
32     private final TransactionManager JavaDoc transactionManager;
33
34     public TreeCache(org.jboss.cache.TreeCache cache, String JavaDoc regionName, TransactionManager JavaDoc transactionManager)
35     throws CacheException {
36         this.cache = cache;
37         userRegionName = regionName;
38         this.regionName = regionName.replace('.', '/');
39         this.transactionManager = transactionManager;
40     }
41
42     public Object JavaDoc get(Object JavaDoc key) throws CacheException {
43         Transaction JavaDoc tx = suspend();
44         try {
45             return read(key);
46         }
47         finally {
48             resume( tx );
49         }
50     }
51     
52     public Object JavaDoc read(Object JavaDoc key) throws CacheException {
53         try {
54             return cache.get( new Fqn( new Object JavaDoc[] { regionName, key } ), ITEM );
55         }
56         catch (Exception JavaDoc e) {
57             throw new CacheException(e);
58         }
59     }
60
61     public void update(Object JavaDoc key, Object JavaDoc value) throws CacheException {
62         try {
63             cache.put( new Fqn( new Object JavaDoc[] { regionName, key } ), ITEM, value );
64         }
65         catch (Exception JavaDoc e) {
66             throw new CacheException(e);
67         }
68     }
69
70     public void put(Object JavaDoc key, Object JavaDoc value) throws CacheException {
71         Transaction JavaDoc tx = suspend();
72         try {
73             //do the failfast put outside the scope of the JTA txn
74
cache.putFailFast( new Fqn( new Object JavaDoc[] { regionName, key } ), ITEM, value, 0 );
75         }
76         catch (TimeoutException te) {
77             //ignore!
78
log.debug("ignoring write lock acquisition failure");
79         }
80         catch (Exception JavaDoc e) {
81             throw new CacheException(e);
82         }
83         finally {
84             resume( tx );
85         }
86     }
87
88     private void resume(Transaction JavaDoc tx) {
89         try {
90             if (tx!=null) transactionManager.resume(tx);
91         }
92         catch (Exception JavaDoc e) {
93             throw new CacheException("Could not resume transaction", e);
94         }
95     }
96
97     private Transaction JavaDoc suspend() {
98         Transaction JavaDoc tx = null;
99         try {
100             if ( transactionManager!=null ) {
101                 tx = transactionManager.suspend();
102             }
103         }
104         catch (SystemException JavaDoc se) {
105             throw new CacheException("Could not suspend transaction", se);
106         }
107         return tx;
108     }
109
110     public void remove(Object JavaDoc key) throws CacheException {
111         try {
112             cache.remove( new Fqn( new Object JavaDoc[] { regionName, key } ) );
113         }
114         catch (Exception JavaDoc e) {
115             throw new CacheException(e);
116         }
117     }
118
119     public void clear() throws CacheException {
120         try {
121             cache.remove( new Fqn(regionName) );
122         }
123         catch (Exception JavaDoc e) {
124             throw new CacheException(e);
125         }
126     }
127
128     public void destroy() throws CacheException {
129         clear();
130     }
131
132     public void lock(Object JavaDoc key) throws CacheException {
133         throw new UnsupportedOperationException JavaDoc("TreeCache is a fully transactional cache" + regionName);
134     }
135
136     public void unlock(Object JavaDoc key) throws CacheException {
137         throw new UnsupportedOperationException JavaDoc("TreeCache is a fully transactional cache: " + regionName);
138     }
139
140     public long nextTimestamp() {
141         return System.currentTimeMillis() / 100;
142     }
143
144     public int getTimeout() {
145         return 600; //60 seconds
146
}
147
148     public String JavaDoc getRegionName() {
149         return userRegionName;
150     }
151
152     public long getSizeInMemory() {
153         return -1;
154     }
155
156     public long getElementCountInMemory() {
157         try {
158             Set JavaDoc children = cache.getChildrenNames( new Fqn(regionName) );
159             return children == null ? 0 : children.size();
160         }
161         catch (Exception JavaDoc e) {
162             throw new CacheException(e);
163         }
164     }
165
166     public long getElementCountOnDisk() {
167         return 0;
168     }
169     
170     public Map JavaDoc toMap() {
171         try {
172             Map JavaDoc result = new HashMap JavaDoc();
173             Set JavaDoc childrens = cache.getChildrenNames( new Fqn(regionName) );
174             if (childrens != null) {
175                 Iterator JavaDoc iter = childrens.iterator();
176                 while ( iter.hasNext() ) {
177                     Object JavaDoc key = iter.next();
178                     result.put( key, cache.get( new Fqn( new Object JavaDoc[] { regionName, key } ), ITEM ) );
179                 }
180             }
181             return result;
182         }
183         catch (Exception JavaDoc e) {
184             throw new CacheException(e);
185         }
186     }
187     
188     public String JavaDoc toString() {
189         return "TreeCache(" + userRegionName + ')';
190     }
191     
192 }
193
Popular Tags