KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > entity > JBCCache


1 package org.jboss.ejb3.entity;
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.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.hibernate.cache.Cache;
15 import org.hibernate.cache.CacheException;
16 import org.hibernate.cache.OptimisticCache;
17 import org.jboss.cache.Fqn;
18 import org.jboss.cache.InvocationContext;
19 import org.jboss.cache.Node;
20 import org.jboss.cache.config.Option;
21 import org.jboss.cache.lock.TimeoutException;
22
23 /**
24  * {@link Cache} implementation that uses a 2.x or later release of JBoss Cache.
25  *
26  * @author <a HREF="brian.stansberry@jboss.com">Brian Stansberry</a>
27  * @version $Revision: 1.1 $
28  */

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