KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > hibernate > cache > JBCCache


1 package org.jboss.hibernate.cache;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Set JavaDoc;
7
8 import javax.transaction.SystemException JavaDoc;
9 import javax.transaction.Transaction JavaDoc;
10 import javax.transaction.TransactionManager JavaDoc;
11
12 import org.hibernate.cache.Cache;
13 import org.hibernate.cache.CacheException;
14 import org.jboss.cache.Fqn;
15 import org.jboss.cache.InvocationContext;
16 import org.jboss.cache.Node;
17 import org.jboss.cache.config.Option;
18 import org.jboss.cache.lock.TimeoutException;
19 import org.jboss.logging.Logger;
20
21 /**
22  * {@link Cache} implementation that uses a 2.x or later release of JBoss Cache.
23  *
24  * @author <a HREF="brian.stansberry@jboss.com">Brian Stansberry</a>
25  * @version $Revision: 1.1 $
26  */

27 public class JBCCache implements Cache
28 {
29     
30     private static final Logger log = Logger.getLogger(JBCCache.class);
31
32     private static final String JavaDoc ITEM = "item";
33
34     private org.jboss.cache.Cache cache;
35     private final String JavaDoc regionName;
36     private final Fqn regionFqn;
37     private final TransactionManager JavaDoc transactionManager;
38
39     public JBCCache(org.jboss.cache.Cache cache, String JavaDoc regionName, TransactionManager JavaDoc transactionManager)
40     throws CacheException {
41         this.cache = cache;
42         this.regionName = regionName;
43         this.regionFqn = Fqn.fromString( regionName.replace( '.', '/' ) );
44         this.transactionManager = transactionManager;
45     }
46
47     public Object JavaDoc get(Object JavaDoc key) throws CacheException {
48         Transaction JavaDoc tx = suspend();
49         try {
50             return read(key);
51         }
52         finally {
53             resume( tx );
54         }
55     }
56     
57     public Object JavaDoc read(Object JavaDoc key) throws CacheException {
58         try {
59             return cache.get( new Fqn( regionFqn, key ), ITEM );
60         }
61         catch (Exception JavaDoc e) {
62             throw new CacheException(e);
63         }
64     }
65
66     public void update(Object JavaDoc key, Object JavaDoc value) throws CacheException {
67         try {
68             cache.put( new Fqn( regionFqn, key ), ITEM, value );
69         }
70         catch (Exception JavaDoc e) {
71             throw new CacheException(e);
72         }
73     }
74
75     public void put(Object JavaDoc key, Object JavaDoc value) throws CacheException {
76         Transaction JavaDoc tx = suspend();
77         try {
78             //do the failfast put outside the scope of the JTA txn
79
cache.putForExternalRead(new Fqn( regionFqn, key ), ITEM, value);
80         }
81         catch (TimeoutException te) {
82             //ignore!
83
log.debug("ignoring write lock acquisition failure");
84         }
85         catch (Exception JavaDoc e) {
86             throw new CacheException(e);
87         }
88         finally {
89             resume( tx );
90         }
91     }
92
93     private void resume(Transaction JavaDoc tx) {
94         try {
95             if (tx!=null) transactionManager.resume(tx);
96         }
97         catch (Exception JavaDoc e) {
98             throw new CacheException("Could not resume transaction", e);
99         }
100     }
101
102     private Transaction JavaDoc suspend() {
103         Transaction JavaDoc tx = null;
104         try {
105             if ( transactionManager!=null ) {
106                 tx = transactionManager.suspend();
107             }
108         }
109         catch (SystemException JavaDoc se) {
110             throw new CacheException("Could not suspend transaction", se);
111         }
112         return tx;
113     }
114
115     public void remove(Object JavaDoc key) throws CacheException {
116         try {
117             cache.remove( new Fqn( regionFqn, key ) );
118         }
119         catch (Exception JavaDoc e) {
120             throw new CacheException(e);
121         }
122     }
123
124     public void clear() throws CacheException {
125         try {
126             cache.remove( regionFqn );
127         }
128         catch (Exception JavaDoc e) {
129             throw new CacheException(e);
130         }
131     }
132
133     public void destroy() throws CacheException {
134         try {
135             // NOTE : evict() operates locally only (i.e., does not propogate
136
// to any other nodes in the potential cluster). This is
137
// exactly what is needed when we destroy() here; destroy() is used
138
// as part of the process of shutting down a SessionFactory; thus
139
// these removals should not be propogated
140
// FIXME NPE bug in 2.0.0.ALPHA1, so we don't use evict 'til fixed
141
// cache.evict( regionFqn, true );
142
InvocationContext ctx = cache.getInvocationContext();
143         Option opt = new Option();
144         opt.setCacheModeLocal(true);
145         ctx.setOptionOverrides(opt);
146         cache.removeNode( regionFqn );
147         }
148         catch( Exception JavaDoc e ) {
149             throw new CacheException( e );
150         }
151     }
152
153     public void lock(Object JavaDoc key) throws CacheException {
154         throw new UnsupportedOperationException JavaDoc( "TreeCache is a fully transactional cache" + regionName );
155     }
156
157     public void unlock(Object JavaDoc key) throws CacheException {
158         throw new UnsupportedOperationException JavaDoc( "TreeCache is a fully transactional cache: " + regionName );
159     }
160
161     public long nextTimestamp() {
162         return System.currentTimeMillis() / 100;
163     }
164
165     public int getTimeout() {
166         return 600; //60 seconds
167
}
168
169     public String JavaDoc getRegionName() {
170         return regionName;
171     }
172
173     public long getSizeInMemory() {
174         return -1;
175     }
176
177     public long getElementCountInMemory() {
178         try {
179             Set JavaDoc children = getChildrenNames();
180             return children == null ? 0 : children.size();
181         }
182         catch (Exception JavaDoc e) {
183             throw new CacheException(e);
184         }
185     }
186
187     public long getElementCountOnDisk() {
188         return 0;
189     }
190     
191     public Map JavaDoc toMap() {
192         try {
193             Map JavaDoc result = new HashMap JavaDoc();
194             Set JavaDoc childrenNames = getChildrenNames();
195             if (childrenNames != null) {
196                 Iterator JavaDoc iter = childrenNames.iterator();
197                 while ( iter.hasNext() ) {
198                     Object JavaDoc key = iter.next();
199                     result.put(
200                             key,
201                             cache.get( new Fqn( regionFqn, key ), ITEM )
202                         );
203                 }
204             }
205             return result;
206         }
207         catch (Exception JavaDoc e) {
208             throw new CacheException(e);
209         }
210     }
211     
212     private Set JavaDoc getChildrenNames()
213     {
214        try {
215           Node base = cache.getChild( regionFqn );
216           return (base == null ? null : base.getChildrenNames());
217        }
218        catch (Exception JavaDoc e) {
219           throw new CacheException(e);
220        }
221     }
222     
223     public String JavaDoc toString() {
224         return "JBCCache(" + regionName + ')';
225     }
226 }
227
Popular Tags