1 25 26 package com.rift.coad.lib.bean; 28 29 import java.util.Date ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.Vector ; 35 36 37 import org.apache.log4j.Logger; 39 40 import com.rift.coad.lib.cache.Cache; 42 import com.rift.coad.lib.cache.CacheEntry; 43 import com.rift.coad.lib.common.RandomGuid; 44 import com.rift.coad.lib.configuration.ConfigurationFactory; 45 import com.rift.coad.lib.configuration.Configuration; 46 import com.rift.coad.lib.thread.ThreadStateMonitor; 47 48 55 public class ProxyCache implements Cache { 56 57 private final static String CACHE_EXPIRY_TIME = "proxy_cache_expiry"; 59 private final static long CACHE_EXPIRY_TIME_DEFAULT = 60 * 1000; 60 61 protected static Logger log = 63 Logger.getLogger(ProxyCache.class.getName()); 64 65 private Map cacheEntries = new HashMap (); 67 private long defaultCacheExpiryTime = 0; 68 private ThreadStateMonitor status = new ThreadStateMonitor(); 69 70 71 76 public ProxyCache() throws BeanException { 77 try { 78 Configuration config = ConfigurationFactory.getInstance(). 79 getConfig(ProxyCache.class); 80 defaultCacheExpiryTime = config.getLong(CACHE_EXPIRY_TIME, 81 CACHE_EXPIRY_TIME_DEFAULT); 82 } catch (Exception ex) { 83 log.error("Failed to start the ProxyCache object because : " + 84 ex.getMessage(),ex); 85 throw new BeanException( 86 "Failed to start the ProxyCache object because : " + 87 ex.getMessage(),ex); 88 } 89 } 90 91 92 95 public void garbageCollect() { 96 Map cacheEntries = new HashMap (); 97 synchronized(this.cacheEntries) { 98 cacheEntries.putAll(this.cacheEntries); 99 } 100 101 Date currentTime = new Date (); 103 for (Iterator iter = cacheEntries.keySet().iterator(); iter.hasNext();) { 104 Object key = iter.next(); 105 ProxyCacheEntry cacheEntry = 106 (ProxyCacheEntry)cacheEntries.get(key); 107 if (cacheEntry.isExpired(currentTime)) { 108 synchronized(this.cacheEntries) { 109 this.cacheEntries.remove(key); 110 } 111 cacheEntry.cacheRelease(); 112 } 113 } 114 } 115 116 117 120 public void clear() { 121 Map cacheEntries = new HashMap (); 122 synchronized(this.cacheEntries) { 123 status.terminate(false); 124 cacheEntries.putAll(this.cacheEntries); 125 this.cacheEntries.clear(); 126 } 127 128 for (Iterator iter = cacheEntries.keySet().iterator(); iter.hasNext();) { 129 ProxyCacheEntry cacheEntry = 130 (ProxyCacheEntry)cacheEntries.get(iter.next()); 131 cacheEntry.cacheRelease(); 132 } 133 } 134 135 136 142 public boolean contains(Object cacheEntry) { 143 synchronized(cacheEntries) { 145 return cacheEntries.containsKey(cacheEntry); 146 } 147 } 148 149 150 157 public void addCacheEntry(long timeout, Object proxy, CacheEntry handler) 158 throws BeanException { 159 synchronized(cacheEntries) { 160 checkStatus(); 161 long cacheTimeout = timeout; 162 if (timeout == -1) { 163 cacheTimeout = defaultCacheExpiryTime; 164 } 165 cacheEntries.put(handler,new ProxyCacheEntry(cacheTimeout, proxy, 166 handler)); 167 } 168 } 169 170 171 176 private void checkStatus() throws BeanException { 177 if (status.isTerminated()) { 178 throw new BeanException("The proxy cache has been shut down."); 179 } 180 } 181 } 182 | Popular Tags |