1 5 package com.opensymphony.oscache.base; 6 7 import com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache; 8 import com.opensymphony.oscache.base.events.*; 9 import com.opensymphony.oscache.base.persistence.PersistenceListener; 10 import com.opensymphony.oscache.util.StringUtil; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 import java.util.*; 16 17 import javax.swing.event.EventListenerList ; 18 19 33 public abstract class AbstractCacheAdministrator implements java.io.Serializable { 34 private static transient final Log log = LogFactory.getLog(AbstractCacheAdministrator.class); 35 36 41 public final static String CACHE_MEMORY_KEY = "cache.memory"; 42 43 49 public final static String CACHE_CAPACITY_KEY = "cache.capacity"; 50 51 60 public final static String CACHE_ALGORITHM_KEY = "cache.algorithm"; 61 62 68 public final static String CACHE_DISK_UNLIMITED_KEY = "cache.unlimited.disk"; 69 70 76 public final static String CACHE_BLOCKING_KEY = "cache.blocking"; 77 78 82 public static final String PERSISTENCE_CLASS_KEY = "cache.persistence.class"; 83 84 88 public static final String CACHE_PERSISTENCE_OVERFLOW_KEY = "cache.persistence.overflow.only"; 89 90 95 public static final String CACHE_ENTRY_EVENT_LISTENERS_KEY = "cache.event.listeners"; 96 protected Config config = null; 97 98 102 protected EventListenerList listenerList = new EventListenerList (); 103 104 108 protected String algorithmClass = null; 109 110 114 protected int cacheCapacity = -1; 115 116 121 private boolean blocking = false; 122 123 127 private boolean memoryCaching = true; 128 129 134 private boolean overflowPersistence; 135 136 140 private boolean unlimitedDiskCache; 141 142 146 protected AbstractCacheAdministrator() { 147 this(null); 148 } 149 150 155 protected AbstractCacheAdministrator(Properties p) { 156 loadProps(p); 157 initCacheParameters(); 158 159 if (log.isDebugEnabled()) { 160 log.debug("Constructed AbstractCacheAdministrator()"); 161 } 162 } 163 164 173 public void setAlgorithmClass(String newAlgorithmClass) { 174 algorithmClass = newAlgorithmClass; 175 } 176 177 183 public boolean isBlocking() { 184 return blocking; 185 } 186 187 194 protected void setCacheCapacity(int newCacheCapacity) { 195 cacheCapacity = newCacheCapacity; 196 } 197 198 205 public boolean isMemoryCaching() { 206 return memoryCaching; 207 } 208 209 215 public String getProperty(String key) { 216 return config.getProperty(key); 217 } 218 219 222 public boolean isUnlimitedDiskCache() { 223 return unlimitedDiskCache; 224 } 225 226 231 public boolean isOverflowPersistence() { 232 return this.overflowPersistence; 233 } 234 235 240 public void setOverflowPersistence(boolean overflowPersistence) { 241 this.overflowPersistence = overflowPersistence; 242 } 243 244 248 protected CacheEventListener[] getCacheEventListeners() { 249 CacheEventListener[] listeners = null; 250 251 List classes = StringUtil.split(config.getProperty(CACHE_ENTRY_EVENT_LISTENERS_KEY), ','); 252 listeners = new CacheEventListener[classes.size()]; 253 254 for (int i = 0; i < classes.size(); i++) { 255 String className = (String ) classes.get(i); 256 257 try { 258 Class clazz = Class.forName(className); 259 260 if (!CacheEventListener.class.isAssignableFrom(clazz)) { 261 log.error("Specified listener class '" + className + "' does not implement CacheEventListener. Ignoring this listener."); 262 } else { 263 listeners[i] = (CacheEventListener) clazz.newInstance(); 264 } 265 } catch (ClassNotFoundException e) { 266 log.error("CacheEventListener class '" + className + "' not found. Ignoring this listener.", e); 267 } catch (InstantiationException e) { 268 log.error("CacheEventListener class '" + className + "' could not be instantiated because it is not a concrete class. Ignoring this listener.", e); 269 } catch (IllegalAccessException e) { 270 log.error("CacheEventListener class '" + className + "' could not be instantiated because it is not public. Ignoring this listener.", e); 271 } 272 } 273 274 return listeners; 275 } 276 277 290 protected Cache setPersistenceListener(Cache cache) { 291 String persistenceClassname = config.getProperty(PERSISTENCE_CLASS_KEY); 292 293 try { 294 Class clazz = Class.forName(persistenceClassname); 295 PersistenceListener persistenceListener = (PersistenceListener) clazz.newInstance(); 296 297 cache.setPersistenceListener(persistenceListener.configure(config)); 298 } catch (ClassNotFoundException e) { 299 log.error("PersistenceListener class '" + persistenceClassname + "' not found. Check your configuration.", e); 300 } catch (Exception e) { 301 log.error("Error instantiating class '" + persistenceClassname + "'", e); 302 } 303 304 return cache; 305 } 306 307 315 protected Cache configureStandardListeners(Cache cache) { 316 if (config.getProperty(PERSISTENCE_CLASS_KEY) != null) { 317 cache = setPersistenceListener(cache); 318 } 319 320 if (config.getProperty(CACHE_ENTRY_EVENT_LISTENERS_KEY) != null) { 321 CacheEventListener[] listeners = getCacheEventListeners(); 325 326 for (int i = 0; i < listeners.length; i++) { 327 if (listeners[i] instanceof LifecycleAware) { 329 try { 330 ((LifecycleAware) listeners[i]).initialize(cache, config); 331 } catch (InitializationException e) { 332 log.error("Could not initialize listener '" + listeners[i].getClass().getName() + "'. Listener ignored.", e); 333 334 continue; 335 } 336 } 337 338 if (listeners[i] instanceof CacheEntryEventListener) { 339 cache.addCacheEventListener(listeners[i], CacheEntryEventListener.class); 340 } 341 342 if (listeners[i] instanceof CacheMapAccessEventListener) { 343 cache.addCacheEventListener(listeners[i], CacheMapAccessEventListener.class); 344 } 345 } 346 } 347 348 return cache; 349 } 350 351 356 protected void finalizeListeners(Cache cache) { 357 if (cache == null) { 359 return; 360 } 361 362 Object [] listeners = cache.listenerList.getListenerList(); 363 364 for (int i = listeners.length - 2; i >= 0; i -= 2) { 365 if (listeners[i + 1] instanceof LifecycleAware) { 366 try { 367 ((LifecycleAware) listeners[i + 1]).finialize(); 368 } catch (FinalizationException e) { 369 log.error("Listener could not be finalized", e); 370 } 371 } 372 } 373 } 374 375 386 private void initCacheParameters() { 387 algorithmClass = getProperty(CACHE_ALGORITHM_KEY); 388 389 blocking = "true".equalsIgnoreCase(getProperty(CACHE_BLOCKING_KEY)); 390 391 String cacheMemoryStr = getProperty(CACHE_MEMORY_KEY); 392 393 if ((cacheMemoryStr != null) && cacheMemoryStr.equalsIgnoreCase("false")) { 394 memoryCaching = false; 395 } 396 397 unlimitedDiskCache = Boolean.valueOf(config.getProperty(CACHE_DISK_UNLIMITED_KEY)).booleanValue(); 398 overflowPersistence = Boolean.valueOf(config.getProperty(CACHE_PERSISTENCE_OVERFLOW_KEY)).booleanValue(); 399 400 String cacheSize = getProperty(CACHE_CAPACITY_KEY); 401 402 try { 403 if ((cacheSize != null) && (cacheSize.length() > 0)) { 404 cacheCapacity = Integer.parseInt(cacheSize); 405 } 406 } catch (NumberFormatException e) { 407 log.error("The value supplied for the cache capacity, '" + cacheSize + "', is not a valid number. The cache capacity setting is being ignored."); 408 } 409 } 410 411 414 private void loadProps(Properties p) { 415 config = new Config(p); 416 } 417 } 418 | Popular Tags |