1 package org.hibernate.cache; 3 4 import java.util.Comparator ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 9 13 public class ReadOnlyCache implements CacheConcurrencyStrategy { 14 15 private Cache cache; 16 private static final Log log = LogFactory.getLog(ReadOnlyCache.class); 17 18 public ReadOnlyCache() {} 19 20 public void setCache(Cache cache) { 21 this.cache=cache; 22 } 23 24 public Cache getCache() { 25 return cache; 26 } 27 28 public String getRegionName() { 29 return cache.getRegionName(); 30 } 31 32 public synchronized Object get(Object key, long timestamp) throws CacheException { 33 Object result = cache.get(key); 34 if ( result!=null && log.isDebugEnabled() ) log.debug("Cache hit: " + key); 35 return result; 36 } 37 38 41 public SoftLock lock(Object key, Object version) { 42 log.error("Application attempted to edit read only item: " + key); 43 throw new UnsupportedOperationException ("Can't write to a readonly object"); 44 } 45 46 public synchronized boolean put( 47 Object key, 48 Object value, 49 long timestamp, 50 Object version, 51 Comparator versionComparator, 52 boolean minimalPut) 53 throws CacheException { 54 if ( minimalPut && cache.get(key)!=null ) { 55 if ( log.isDebugEnabled() ) log.debug("item already cached: " + key); 56 return false; 57 } 58 if ( log.isDebugEnabled() ) log.debug("Caching: " + key); 59 cache.put(key, value); 60 return true; 61 } 62 63 66 public void release(Object key, SoftLock lock) { 67 log.error("Application attempted to edit read only item: " + key); 68 } 70 71 public void clear() throws CacheException { 72 cache.clear(); 73 } 74 75 public void remove(Object key) throws CacheException { 76 cache.remove(key); 77 } 78 79 public void destroy() { 80 try { 81 cache.destroy(); 82 } 83 catch (Exception e) { 84 log.warn("could not destroy cache", e); 85 } 86 } 87 88 91 public boolean afterUpdate(Object key, Object value, Object version, SoftLock lock) throws CacheException { 92 log.error("Application attempted to edit read only item: " + key); 93 throw new UnsupportedOperationException ("Can't write to a readonly object"); 94 } 95 96 99 public boolean afterInsert(Object key, Object value, Object version) throws CacheException { 100 if ( log.isDebugEnabled() ) log.debug("Caching after insert: " + key); 101 cache.update(key, value); 102 return true; 103 } 104 105 108 public void evict(Object key) throws CacheException { 109 } 111 112 115 public boolean insert(Object key, Object value) throws CacheException { 116 return false; 117 } 118 119 122 public boolean update(Object key, Object value) throws CacheException { 123 log.error("Application attempted to edit read only item: " + key); 124 throw new UnsupportedOperationException ("Can't write to a readonly object"); 125 } 126 127 public String toString() { 128 return cache + "(read-only)"; 129 } 130 131 } 132 133 134 135 136 137 138 | Popular Tags |