1 package org.jboss.hibernate.cache; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.Map ; 6 import java.util.Set ; 7 8 import javax.transaction.SystemException ; 9 import javax.transaction.Transaction ; 10 import javax.transaction.TransactionManager ; 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 27 public class JBCCache implements Cache 28 { 29 30 private static final Logger log = Logger.getLogger(JBCCache.class); 31 32 private static final String ITEM = "item"; 33 34 private org.jboss.cache.Cache cache; 35 private final String regionName; 36 private final Fqn regionFqn; 37 private final TransactionManager transactionManager; 38 39 public JBCCache(org.jboss.cache.Cache cache, String regionName, TransactionManager 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 get(Object key) throws CacheException { 48 Transaction tx = suspend(); 49 try { 50 return read(key); 51 } 52 finally { 53 resume( tx ); 54 } 55 } 56 57 public Object read(Object key) throws CacheException { 58 try { 59 return cache.get( new Fqn( regionFqn, key ), ITEM ); 60 } 61 catch (Exception e) { 62 throw new CacheException(e); 63 } 64 } 65 66 public void update(Object key, Object value) throws CacheException { 67 try { 68 cache.put( new Fqn( regionFqn, key ), ITEM, value ); 69 } 70 catch (Exception e) { 71 throw new CacheException(e); 72 } 73 } 74 75 public void put(Object key, Object value) throws CacheException { 76 Transaction tx = suspend(); 77 try { 78 cache.putForExternalRead(new Fqn( regionFqn, key ), ITEM, value); 80 } 81 catch (TimeoutException te) { 82 log.debug("ignoring write lock acquisition failure"); 84 } 85 catch (Exception e) { 86 throw new CacheException(e); 87 } 88 finally { 89 resume( tx ); 90 } 91 } 92 93 private void resume(Transaction tx) { 94 try { 95 if (tx!=null) transactionManager.resume(tx); 96 } 97 catch (Exception e) { 98 throw new CacheException("Could not resume transaction", e); 99 } 100 } 101 102 private Transaction suspend() { 103 Transaction tx = null; 104 try { 105 if ( transactionManager!=null ) { 106 tx = transactionManager.suspend(); 107 } 108 } 109 catch (SystemException se) { 110 throw new CacheException("Could not suspend transaction", se); 111 } 112 return tx; 113 } 114 115 public void remove(Object key) throws CacheException { 116 try { 117 cache.remove( new Fqn( regionFqn, key ) ); 118 } 119 catch (Exception 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 e) { 129 throw new CacheException(e); 130 } 131 } 132 133 public void destroy() throws CacheException { 134 try { 135 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 e ) { 149 throw new CacheException( e ); 150 } 151 } 152 153 public void lock(Object key) throws CacheException { 154 throw new UnsupportedOperationException ( "TreeCache is a fully transactional cache" + regionName ); 155 } 156 157 public void unlock(Object key) throws CacheException { 158 throw new UnsupportedOperationException ( "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; } 168 169 public String getRegionName() { 170 return regionName; 171 } 172 173 public long getSizeInMemory() { 174 return -1; 175 } 176 177 public long getElementCountInMemory() { 178 try { 179 Set children = getChildrenNames(); 180 return children == null ? 0 : children.size(); 181 } 182 catch (Exception e) { 183 throw new CacheException(e); 184 } 185 } 186 187 public long getElementCountOnDisk() { 188 return 0; 189 } 190 191 public Map toMap() { 192 try { 193 Map result = new HashMap (); 194 Set childrenNames = getChildrenNames(); 195 if (childrenNames != null) { 196 Iterator iter = childrenNames.iterator(); 197 while ( iter.hasNext() ) { 198 Object 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 e) { 208 throw new CacheException(e); 209 } 210 } 211 212 private Set getChildrenNames() 213 { 214 try { 215 Node base = cache.getChild( regionFqn ); 216 return (base == null ? null : base.getChildrenNames()); 217 } 218 catch (Exception e) { 219 throw new CacheException(e); 220 } 221 } 222 223 public String toString() { 224 return "JBCCache(" + regionName + ')'; 225 } 226 } 227 | Popular Tags |