1 package org.hibernate.cache; 3 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 import java.util.Set ; 8 9 import javax.transaction.SystemException ; 10 import javax.transaction.Transaction ; 11 import javax.transaction.TransactionManager ; 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 23 public class TreeCache implements Cache { 24 25 private static final Log log = LogFactory.getLog(TreeCache.class); 26 27 private static final String ITEM = "item"; 28 29 private org.jboss.cache.TreeCache cache; 30 private final String regionName; 31 private final String userRegionName; 32 private final TransactionManager transactionManager; 33 34 public TreeCache(org.jboss.cache.TreeCache cache, String regionName, TransactionManager 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 get(Object key) throws CacheException { 43 Transaction tx = suspend(); 44 try { 45 return read(key); 46 } 47 finally { 48 resume( tx ); 49 } 50 } 51 52 public Object read(Object key) throws CacheException { 53 try { 54 return cache.get( new Fqn( new Object [] { regionName, key } ), ITEM ); 55 } 56 catch (Exception e) { 57 throw new CacheException(e); 58 } 59 } 60 61 public void update(Object key, Object value) throws CacheException { 62 try { 63 cache.put( new Fqn( new Object [] { regionName, key } ), ITEM, value ); 64 } 65 catch (Exception e) { 66 throw new CacheException(e); 67 } 68 } 69 70 public void put(Object key, Object value) throws CacheException { 71 Transaction tx = suspend(); 72 try { 73 cache.putFailFast( new Fqn( new Object [] { regionName, key } ), ITEM, value, 0 ); 75 } 76 catch (TimeoutException te) { 77 log.debug("ignoring write lock acquisition failure"); 79 } 80 catch (Exception e) { 81 throw new CacheException(e); 82 } 83 finally { 84 resume( tx ); 85 } 86 } 87 88 private void resume(Transaction tx) { 89 try { 90 if (tx!=null) transactionManager.resume(tx); 91 } 92 catch (Exception e) { 93 throw new CacheException("Could not resume transaction", e); 94 } 95 } 96 97 private Transaction suspend() { 98 Transaction tx = null; 99 try { 100 if ( transactionManager!=null ) { 101 tx = transactionManager.suspend(); 102 } 103 } 104 catch (SystemException se) { 105 throw new CacheException("Could not suspend transaction", se); 106 } 107 return tx; 108 } 109 110 public void remove(Object key) throws CacheException { 111 try { 112 cache.remove( new Fqn( new Object [] { regionName, key } ) ); 113 } 114 catch (Exception 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 e) { 124 throw new CacheException(e); 125 } 126 } 127 128 public void destroy() throws CacheException { 129 clear(); 130 } 131 132 public void lock(Object key) throws CacheException { 133 throw new UnsupportedOperationException ("TreeCache is a fully transactional cache" + regionName); 134 } 135 136 public void unlock(Object key) throws CacheException { 137 throw new UnsupportedOperationException ("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; } 147 148 public String getRegionName() { 149 return userRegionName; 150 } 151 152 public long getSizeInMemory() { 153 return -1; 154 } 155 156 public long getElementCountInMemory() { 157 try { 158 Set children = cache.getChildrenNames( new Fqn(regionName) ); 159 return children == null ? 0 : children.size(); 160 } 161 catch (Exception e) { 162 throw new CacheException(e); 163 } 164 } 165 166 public long getElementCountOnDisk() { 167 return 0; 168 } 169 170 public Map toMap() { 171 try { 172 Map result = new HashMap (); 173 Set childrens = cache.getChildrenNames( new Fqn(regionName) ); 174 if (childrens != null) { 175 Iterator iter = childrens.iterator(); 176 while ( iter.hasNext() ) { 177 Object key = iter.next(); 178 result.put( key, cache.get( new Fqn( new Object [] { regionName, key } ), ITEM ) ); 179 } 180 } 181 return result; 182 } 183 catch (Exception e) { 184 throw new CacheException(e); 185 } 186 } 187 188 public String toString() { 189 return "TreeCache(" + userRegionName + ')'; 190 } 191 192 } 193 | Popular Tags |