1 17 18 package org.sape.carbon.services.cache.total; 19 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.Comparator ; 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.InvocationTargetException ; 28 29 import org.sape.carbon.core.component.ComponentConfiguration; 30 import org.sape.carbon.core.component.lifecycle.Configurable; 31 import org.sape.carbon.services.cache.CacheLoadException; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 57 public class ReadOnlyCache 58 extends AbstractTotalCache 59 implements Configurable { 60 61 62 private Log log = LogFactory.getLog(this.getClass()); 63 64 67 private Map activeMap; 68 69 73 private Map holdingMap; 74 75 76 private TotalCacheDataLoader dataLoader; 77 78 85 public synchronized void refreshAll() throws CacheLoadException { 86 87 if (log.isTraceEnabled()) { 88 log.trace("Refreshing Cache"); 89 } 90 91 this.initializeHoldingCache(); 92 this.holdingMap.putAll(this.dataLoader.loadAllData()); 93 this.activateHoldingCache(); 94 } 95 96 99 public void configure(ComponentConfiguration configuration) { 100 101 TotalCacheConfiguration config = 102 (TotalCacheConfiguration) configuration; 103 this.dataLoader = config.getDataLoader(); 104 } 105 106 111 public Set entrySet() { 112 return Collections.unmodifiableSet(super.entrySet()); 113 } 114 115 120 public Set keySet() { 121 return Collections.unmodifiableSet(super.keySet()); 122 } 123 124 129 public Collection values() { 130 return Collections.unmodifiableCollection(super.values()); 131 } 132 133 139 public void clear() { 140 throw new UnsupportedOperationException ( 141 "Read-only cache does not support clear operation"); 142 } 143 144 153 public Object put(Object key, Object value) { 154 throw new UnsupportedOperationException ( 155 "Read-only cache does not support put operation"); 156 } 157 158 165 public void putAll(Map t) { 166 throw new UnsupportedOperationException ( 167 "Read-only cache does not support putAll operation"); 168 } 169 170 178 public Object remove(Object key) { 179 throw new UnsupportedOperationException ( 180 "Read-only cache does not support remove operation"); 181 } 182 183 186 protected Map getCacheMap() { 187 return this.activeMap; 188 } 189 190 193 private void activateHoldingCache() { 194 if (log.isTraceEnabled()) { 195 log.trace("Activating Holding Cache"); 196 } 197 this.activeMap = this.holdingMap; 198 this.holdingMap = null; 199 } 200 201 204 private void initializeHoldingCache() { 205 if (log.isTraceEnabled()) { 206 log.trace("Initializing Holding Cache"); 207 } 208 209 if (this.dataLoader instanceof ConfigurableMapTypeCacheDataLoader) { 212 this.holdingMap = 213 ((ConfigurableMapTypeCacheDataLoader)this.dataLoader). 214 getMapInstance(); 215 if (log.isTraceEnabled()) { 216 log.trace("Map instance created using " 217 + (this.holdingMap.getClass()).getName()); 218 } 219 } else { 220 if (log.isTraceEnabled()) { 221 log.trace("Dataloader does not provide method for obtaining " 222 + "instance of Map. Creating default instance as HashMap"); 223 } 224 this.holdingMap = new HashMap (); 225 } 226 227 228 } 229 } | Popular Tags |