1 17 18 package com.whirlycott.cache.component.store; 19 20 import java.io.IOException ; 21 import java.util.Enumeration ; 22 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.thread.ThreadSafe; 29 import org.apache.excalibur.store.Store; 30 31 import com.whirlycott.cache.Cache; 32 import com.whirlycott.cache.CacheConfiguration; 33 import com.whirlycott.cache.CacheDecorator; 34 import com.whirlycott.cache.CacheMaintenancePolicy; 35 import com.whirlycott.cache.Constants; 36 import com.whirlycott.cache.ManagedCache; 37 import com.whirlycott.cache.Messages; 38 39 45 public class WhirlycacheStore implements Store, ThreadSafe, Configurable, Initializable, Disposable { 46 47 50 private Cache cache; 51 52 55 private CacheConfiguration cacheConfiguration; 56 57 60 private Class cacheMaintenancePolicyClass; 61 62 65 private Class managedCacheClass; 66 67 70 public void clear() { 71 cache.clear(); 72 } 73 74 75 78 public void configure(final Configuration configuration) throws ConfigurationException { 79 final Configuration backend = configuration.getChild(Constants.CONFIG_BACKEND); 80 final Configuration policy = configuration.getChild(Constants.CONFIG_POLICY); 81 82 cacheConfiguration = new CacheConfiguration(); 83 84 cacheConfiguration.setName(configuration.getChild(Constants.CONFIG_NAME).getValue(toString())); 85 cacheConfiguration.setBackend(backend.getValue()); 86 cacheConfiguration.setMaxSize(configuration.getChild(Constants.CONFIG_MAXSIZE).getValueAsInteger()); 87 cacheConfiguration.setPolicy(policy.getValue()); 88 cacheConfiguration.setTunerSleepTime(configuration.getChild(Constants.CONFIG_TUNER_SLEEPTIME).getValueAsInteger()); 89 90 managedCacheClass = loadManagedCacheClass(backend.getLocation()); 91 cacheMaintenancePolicyClass = loadCacheMaintenancePolicyClass(policy.getLocation()); 92 } 93 94 95 98 public boolean containsKey(final Object key) { 99 boolean retval = false; 100 if (cache.retrieve(key) != null) 101 retval = true; 102 return retval; 103 } 104 105 106 private CacheMaintenancePolicy[] createCacheMaintenancePolicies(final ManagedCache cache) 107 throws IllegalAccessException , InstantiationException { 108 final CacheMaintenancePolicy policy = (CacheMaintenancePolicy)cacheMaintenancePolicyClass.newInstance(); 109 110 policy.setCache(cache); 111 policy.setConfiguration(cacheConfiguration); 112 113 return new CacheMaintenancePolicy[]{policy}; 114 } 115 116 119 public void dispose() { 120 cache.clear(); 121 125 ((CacheDecorator)cache).shutdown(); 126 } 127 128 129 132 public void free() { 133 } 135 136 139 public Object get(final Object key) { 140 return cache.retrieve(key); 141 } 142 143 144 147 public void initialize() throws Exception { 148 final ManagedCache managedCache = (ManagedCache)managedCacheClass.newInstance(); 149 cache = new CacheDecorator(managedCache, cacheConfiguration, createCacheMaintenancePolicies(managedCache)); 150 } 151 152 155 public Enumeration keys() { 156 throw new UnsupportedOperationException (); 157 } 158 159 160 166 private Class loadCacheMaintenancePolicyClass(final String _location) throws ConfigurationException { 167 final String policy = cacheConfiguration.getPolicy(); 168 169 try { 170 final Class clazz = Thread.currentThread().getContextClassLoader().loadClass(policy); 171 172 if (!CacheMaintenancePolicy.class.isAssignableFrom(clazz)) { 173 final Object [] args = { 174 policy, 175 _location 176 }; 177 throw new ConfigurationException(Messages.getCompoundString("WhirlycacheStore.not_cache_maintenance_policy", args)); } 179 180 return clazz; 181 } catch (final ClassNotFoundException e) { 182 final Object [] args = { 183 policy, 184 _location 185 }; 186 throw new ConfigurationException(Messages.getCompoundString("WhirlycacheStore.cannot_load_policy", args), e); } 188 } 189 190 196 private Class loadManagedCacheClass(final String _location) throws ConfigurationException { 197 final String backend = cacheConfiguration.getBackend(); 198 199 try { 200 final Class clazz = Thread.currentThread().getContextClassLoader().loadClass(backend); 201 202 if (!ManagedCache.class.isAssignableFrom(clazz)) { 203 final Object [] args = { 204 backend, 205 _location 206 }; 207 throw new ConfigurationException(Messages.getCompoundString("WhirlycacheStore.not_managed_cache", args)); } 209 210 return clazz; 211 } catch (final ClassNotFoundException e) { 212 final Object [] args = { 213 backend, 214 _location 215 }; 216 throw new ConfigurationException(Messages.getCompoundString("WhirlycacheStore.cannot_load_backend", args), e); } 218 } 219 220 221 224 public void remove(final Object key) { 225 cache.remove(key); 226 } 227 228 229 232 public int size() { 233 return cache.size(); 234 } 235 236 237 240 public void store(final Object key, final Object value) throws IOException { 241 cache.store(key, value); 242 } 243 } | Popular Tags |