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