1 23 24 package com.rift.coad.lib.bean; 26 27 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 32 import com.rift.coad.lib.cache.Cache; 34 import com.rift.coad.lib.cache.CacheEntry; 35 import com.rift.coad.lib.thread.ThreadStateMonitor; 36 37 42 public class BeanCacheManager implements Cache { 43 44 private Map beanCaches = new HashMap (); 46 private ThreadStateMonitor status = new ThreadStateMonitor(); 47 48 51 public BeanCacheManager() { 52 53 } 54 55 56 63 public BeanCache getBeanCache(Object ref) throws BeanException { 64 checkStatus(); 65 synchronized (beanCaches) { 66 if (!beanCaches.containsKey(ref)) { 67 beanCaches.put(ref,new BeanCache()); 68 } 69 return (BeanCache)beanCaches.get(ref); 70 } 71 } 72 73 74 77 public void garbageCollect() { 78 Map beanCaches = new HashMap (); 79 synchronized (this.beanCaches) { 80 beanCaches.putAll(this.beanCaches); 81 } 82 for (Iterator iter = beanCaches.keySet().iterator(); iter.hasNext();) { 83 BeanCache beanCache = (BeanCache)beanCaches.get(iter.next()); 84 beanCache.garbageCollect(); 85 } 86 } 87 88 89 92 public void clear() { 93 status.terminate(true); 94 Map beanCaches = new HashMap (); 95 synchronized (this.beanCaches) { 96 beanCaches.putAll(this.beanCaches); 97 this.beanCaches.clear(); 98 } 99 for (Iterator iter = beanCaches.keySet().iterator(); iter.hasNext();) { 100 BeanCache beanCache = (BeanCache)beanCaches.get(iter.next()); 101 beanCache.clear(); 102 } 103 } 104 105 106 112 public boolean contains(Object cacheEntry) { 113 synchronized (beanCaches) { 114 return beanCaches.containsKey(cacheEntry); 115 } 116 } 117 118 119 124 private void checkStatus() throws BeanException { 125 if (status.isTerminated()) { 126 throw new BeanException("Bean cache manager has been terminated."); 127 } 128 } 129 } 130 | Popular Tags |