1 64 65 package com.jcorporate.expresso.core.cache; 66 67 import com.jcorporate.expresso.core.misc.ConfigManager; 68 import com.jcorporate.expresso.kernel.ComponentLifecycle; 69 import com.jcorporate.expresso.kernel.management.ExpressoRuntimeMap; 70 import com.jcorporate.expresso.kernel.util.LocatorUtils; 71 import org.apache.commons.collections.FastHashMap; 72 import org.apache.log4j.Logger; 73 74 import java.util.Enumeration ; 75 import java.util.Iterator ; 76 import java.util.Vector ; 77 78 98 public class CacheManager { 99 100 101 102 private volatile static FastHashMap cacheLists = null; 103 104 107 private static CacheManager theInstance = null; 108 109 110 private static String thisContextPath = null; 111 112 115 private static Logger log = Logger.getLogger(CacheManager.class); 116 117 118 122 private static boolean runtimeInitialized = false; 123 124 private static CacheCleaner cacheCleaner; 125 126 130 public CacheManager() { 131 super(); 132 133 runtimeInitialized = (ExpressoRuntimeMap.getDefaultRuntime() != null); 134 135 if (!runtimeInitialized) { 136 cacheLists = new FastHashMap(); 137 thisContextPath = ConfigManager.getWebAppDir(); 138 cacheCleaner = new CacheCleaner(); 139 cacheLists.setFast(false); 140 141 for (Enumeration e = ConfigManager.getAllConfigKeys(); e.hasMoreElements();) { 142 String oneContext = (String ) e.nextElement(); 143 DefaultCacheManager dcm = new DefaultCacheManager(); 144 dcm.initialize(); 145 cacheLists.put(oneContext, dcm); 146 cacheCleaner.registerCacheSystem(dcm); 147 } 148 cacheLists.setFast(true); 149 cacheCleaner.setDaemon(true); 150 cacheCleaner.setName("Cache Cleaner"); 151 cacheCleaner.setPriority(Thread.MIN_PRIORITY); 152 cacheCleaner.start(); 153 } 154 } 155 156 166 public synchronized static CacheManager getInstance() { 167 if (theInstance == null) { 168 theInstance = new CacheManager(); 169 } 170 171 return theInstance; 172 } 173 174 181 public final static CacheSystem getCacheSystem(String dataContext) { 182 if (dataContext == null || dataContext.length() == 0) { 183 dataContext = "default"; 184 } 185 186 if (CacheManager.runtimeInitialized) { 187 LocatorUtils lc = new LocatorUtils(ExpressoRuntimeMap.getDefaultRuntime()); 188 return (CacheSystem) lc.locateComponent(dataContext + ".Cache"); 189 } else { 190 return (CacheSystem) cacheLists.get(dataContext); 191 } 192 } 193 194 198 public synchronized static void destroy() { 199 if (runtimeInitialized) { 200 return; 202 } else { 203 cacheCleaner.interrupt(); 204 try { 205 cacheCleaner.join(); 206 } catch (InterruptedException ex) { 207 log.info("Interrupted while waiting for cache cleaner to exit"); 208 } 209 210 for (Iterator i = cacheLists.values().iterator(); i.hasNext();) { 211 CacheSystem cs = (CacheSystem) i.next(); 212 if (cs instanceof ComponentLifecycle) { 213 ((ComponentLifecycle) cs).destroy(); 214 } 215 } 216 217 cacheLists.clear(); 218 } 219 220 System.gc(); } 222 223 231 public static void addItem(String dataContext, String cacheName, 232 Cacheable newItem) 233 throws CacheException { 234 CacheSystem cs = getCacheSystem(dataContext); 235 if (cs == null) { 236 return; 237 } else { 238 cs.addItem(cacheName, newItem); 239 } 240 } 241 242 243 253 public static void addItem(String dataContext, String cacheName, 254 Cacheable newItem, long expiry) 255 throws CacheException { 256 CacheSystem cs = getCacheSystem(dataContext); 257 if (cs == null) { 258 return; 259 } else { 260 cs.addItem(cacheName, newItem, expiry); 261 } 262 } 263 264 265 274 public static void addListener(String listener, String listenTo) { 275 276 for (Enumeration e = ConfigManager.getAllConfigKeys(); e.hasMoreElements();) { 277 String oneContext = (String ) e.nextElement(); 278 CacheSystem cs = getCacheSystem(oneContext); 279 if (cs == null) { 280 continue; 281 } else { 282 cs.addListener(listener, listenTo); 283 } 284 } 285 } 286 287 294 public static void clear(String dataContext) 295 throws CacheException { 296 CacheSystem cs = getCacheSystem(dataContext); 297 if (cs == null) { 298 log.warn("Unable to locate cache system for data context " + dataContext); 299 return; 300 } else { 301 cs.clear(); 302 } 303 } 304 305 306 311 public static void clearNoNotify(String dataContext) { 312 CacheSystem cs = getCacheSystem(dataContext); 313 if (cs == null) { 314 log.warn("Unable to locate cache system for data context " + dataContext); 315 return; 316 } else { 317 cs.clearNoNotify(); 318 } 319 } 320 321 322 329 public static void clear(String dataContext, String cacheName) 330 throws CacheException { 331 CacheSystem cs = getCacheSystem(dataContext); 332 if (cs == null) { 333 log.warn("Unable to locate cache system for data context " + dataContext); 334 return; 335 } else { 336 cs.clear(cacheName); 337 } 338 } 339 340 347 public static void clearNoNotify(String dataContext, String cacheName) { 348 CacheSystem cs = getCacheSystem(dataContext); 349 if (cs == null) { 350 return; 351 } else { 352 cs.clearNoNotify(cacheName); 353 } 354 } 355 356 357 367 public static Cache createCache(String dataContext, String cacheName, 368 boolean ordered) 369 throws CacheException { 370 CacheSystem cs = getCacheSystem(dataContext); 371 if (cs == null) { 372 log.warn("Unable to locate cache system for data context " + dataContext); 373 return null; 374 } else { 375 return cs.createCache(cacheName, ordered); 376 } 377 378 } 379 380 381 392 public static Cache createCache(String dataContext, String cacheName, 393 boolean ordered, int maxSize) 394 throws CacheException { 395 396 CacheSystem cs = getCacheSystem(dataContext); 397 if (cs == null) { 398 log.warn("Unable to locate cache system for data context " + dataContext); 399 return null; 400 } else { 401 return cs.createCache(cacheName, ordered, maxSize); 402 } 403 404 } 405 406 407 412 public static void displayStatus() { 413 Runtime r = Runtime.getRuntime(); 414 double avail = r.totalMemory(); 415 double free = r.freeMemory(); 416 double pfree = ((free / avail) * 100.000); 417 418 if (log.isInfoEnabled()) { 419 log.info("Cache Status"); 420 log.info("Free memory " + free + ", Available " + avail + 421 ", Percent free " + pfree + "%"); 422 } 423 424 String oneConfigKey = null; 425 426 for (Enumeration e = ConfigManager.getAllConfigKeys(); 427 e.hasMoreElements();) { 428 oneConfigKey = (String ) e.nextElement(); 429 CacheSystem cs = CacheManager.getCacheSystem(oneConfigKey); 430 if (cs != null) { 431 cs.displayStatus(); 432 } else { 433 continue; 434 } 435 } 437 438 } 439 440 449 public static boolean existsCache(String dataContext, String cacheName) { 450 CacheSystem cs = getCacheSystem(dataContext); 451 if (cs == null) { 452 return false; 453 } else { 454 return cs.existsCache(cacheName); 455 } 456 } 458 459 465 public static Iterator getAllCacheNamesIterator(String dataContext) { 466 CacheSystem cs = getCacheSystem(dataContext); 467 if (cs == null) { 468 return new java.util.ArrayList (0).iterator(); 469 } else { 470 return cs.getAllCacheNames().iterator(); 471 } 472 } 473 474 475 483 public static Cacheable getItem(String dataContext, String cacheName, 484 String valueKey) { 485 CacheSystem cs = getCacheSystem(dataContext); 486 if (cs == null) { 487 return null; 488 } else { 489 return cs.getItem(cacheName, valueKey); 490 } 491 492 } 494 495 505 public static Vector getItems(String dataContext, String cacheName) { 506 CacheSystem cs = getCacheSystem(dataContext); 507 if (cs == null) { 508 return null; 509 } else { 510 java.util.List l = getCacheSystem(dataContext).getItems(cacheName); 511 if (l == null) { 512 return null; 513 } else { 514 if (l instanceof java.util.Vector ) { 515 return (Vector ) l; 516 } else { 517 return new Vector (l); 518 } 519 } 520 } 521 522 } 530 531 539 public static int getItemCount(String dataContext, String cacheName) { 540 CacheSystem cs = getCacheSystem(dataContext); 541 if (cs == null) { 542 return 0; 543 } else { 544 return cs.getItemCount(cacheName); 545 } 546 547 } 549 550 551 558 public static void removeItem(String dataContext, String cacheName, 559 Cacheable itemToRemove) 560 throws CacheException { 561 CacheSystem cs = getCacheSystem(dataContext); 562 if (cs == null) { 563 return; 564 } else { 565 cs.removeItem(cacheName, itemToRemove); 566 } 567 569 } 570 571 572 580 public static void removeItemNoNotify(String dataContext, String cacheName, 581 Cacheable itemToRemove) 582 throws CacheException { 583 CacheSystem cs = getCacheSystem(dataContext); 584 if (cs == null) { 585 return; 586 } else { 587 cs.removeItemNoNotify(cacheName, itemToRemove); 588 } 589 590 } 592 593 594 602 public static void setItems(String dataContext, String cacheName, 603 Vector itemList) 604 throws CacheException { 605 CacheSystem cs = getCacheSystem(dataContext); 606 if (cs == null) { 607 return; 608 } else { 609 cs.setItems(cacheName, itemList); 610 } 611 612 } 614 } 615 616 617 | Popular Tags |