1 25 26 package com.rift.coad.lib.bean; 28 29 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import javax.rmi.PortableRemoteObject ; 35 36 import org.apache.log4j.Logger; 38 39 import com.rift.coad.lib.cache.Cache; 41 import com.rift.coad.lib.cache.CacheEntry; 42 import com.rift.coad.lib.configuration.ConfigurationFactory; 43 import com.rift.coad.lib.configuration.Configuration; 44 import com.rift.coad.lib.thread.ThreadStateMonitor; 45 46 53 public class BeanCache implements Cache { 54 55 private final static String CACHE_EXPIRY_TIME = "bean_cache_expiry"; 57 private final static long CACHE_EXPIRY_TIME_DEFAULT = 30 * 60 * 1000; 58 59 protected static Logger log = 61 Logger.getLogger(BeanCache.class.getName()); 62 63 private Map entries = new HashMap (); 65 private long defaultCacheExpiryTime = 0; 66 private ThreadStateMonitor status = new ThreadStateMonitor(); 67 68 73 public BeanCache() throws BeanException { 74 try { 75 Configuration config = ConfigurationFactory.getInstance(). 76 getConfig(BeanCache.class); 77 defaultCacheExpiryTime = config.getLong(CACHE_EXPIRY_TIME, 78 CACHE_EXPIRY_TIME_DEFAULT); 79 } catch (Exception ex) { 80 log.error("Failed to start the BeanCache object because : " + 81 ex.getMessage(),ex); 82 throw new BeanException( 83 "Failed to start the BeanCache object because : " + 84 ex.getMessage(),ex); 85 } 86 } 87 88 89 92 public void garbageCollect() { 93 Map entries = new HashMap (); 95 synchronized (this.entries) { 96 entries.putAll(this.entries); 97 } 98 99 Date expiryDate = new Date (); 101 for (Iterator iter = entries.keySet().iterator();iter.hasNext();) { 102 Object cacheKey = iter.next(); 103 BeanCacheEntry beanCacheEntry = 104 (BeanCacheEntry)entries.get(cacheKey); 105 if (beanCacheEntry.isExpired(expiryDate)) { 106 synchronized (this.entries) { 107 if (beanCacheEntry.getCacheEntry() != null) 108 { 109 try { 110 PortableRemoteObject.unexportObject( 111 (java.rmi.Remote ) 112 beanCacheEntry.getCacheEntry()); 113 this.entries.remove(cacheKey); 115 beanCacheEntry.cacheRelease(); 116 } catch (java.rmi.NoSuchObjectException ex) { 117 log.warn("The object was never exported : " + 118 ex.getMessage(),ex); 119 this.entries.remove(cacheKey); 121 beanCacheEntry.cacheRelease(); 122 } catch (Exception ex) { 123 log.error("Failed to un-export this object : " + 124 ex.getMessage(),ex); 125 } 126 } else { 127 this.entries.remove(cacheKey); 130 beanCacheEntry.cacheRelease(); 131 } 132 } 133 } 134 } 135 } 136 137 138 141 public void clear() { 142 Map entries = new HashMap (); 144 synchronized (this.entries) { 145 entries.putAll(this.entries); 146 this.entries.clear(); 147 status.terminate(false); 148 } 149 150 for (Iterator iter = entries.keySet().iterator();iter.hasNext();) { 152 Object cacheKey = iter.next(); 153 BeanCacheEntry beanCacheEntry = 154 (BeanCacheEntry)entries.get(cacheKey); 155 if (beanCacheEntry.getCacheEntry() != null) 156 { 157 try { 158 PortableRemoteObject.unexportObject( 159 (java.rmi.Remote ) 160 beanCacheEntry.getCacheEntry()); 161 } catch (java.rmi.NoSuchObjectException ex) { 162 log.warn("The cache object was not bound : " + 163 ex.getMessage(),ex); 164 } catch (Exception ex) { 165 log.error("Failed to un-export the cached object : " + 166 ex.getMessage(),ex); 167 } 168 } 169 beanCacheEntry.cacheRelease(); 170 } 171 } 172 173 174 180 public boolean contains(Object cacheKey) { 181 synchronized (entries) { 182 return entries.containsKey(cacheKey); 183 } 184 } 185 186 187 193 public BeanCacheEntry getEntry(Object cacheKey) throws BeanException { 194 synchronized (entries) { 195 checkStatus(); 196 BeanCacheEntry beanCacheEntry = 197 (BeanCacheEntry)entries.get(cacheKey); 198 if (beanCacheEntry != null) { 199 beanCacheEntry.touch(); 200 } 201 return beanCacheEntry; 202 } 203 } 204 205 206 213 public void addCacheEntry(long timeout,Object cacheKey, Object wrappedObject, 214 CacheEntry entry) throws BeanException { 215 synchronized(entries) { 216 checkStatus(); 217 long cacheTimeout = timeout; 218 if (timeout == -1) { 219 cacheTimeout = defaultCacheExpiryTime; 220 } 221 BeanCacheEntry beanCacheEntry = getEntry(cacheKey); 223 if (beanCacheEntry != null) { 224 try { 225 PortableRemoteObject.unexportObject( 226 (java.rmi.Remote ) 227 beanCacheEntry.getCacheEntry()); 228 } catch (java.rmi.NoSuchObjectException ex) { 229 log.warn("The cache object was not bound : " + 230 ex.getMessage(),ex); 231 } 232 } 233 entries.put(cacheKey,new BeanCacheEntry(cacheTimeout,cacheKey, 234 wrappedObject, entry)); 235 } 236 } 237 238 239 247 public void addCacheEntry(long timeout, Object cacheKey, Object wrappedObject, 248 Object proxy, CacheEntry handle) throws BeanException { 249 synchronized (entries) { 250 checkStatus(); 251 long cacheTimeout = timeout; 252 if (timeout == -1) { 253 cacheTimeout = defaultCacheExpiryTime; 254 } 255 BeanCacheEntry beanCacheEntry = getEntry(cacheKey); 257 if (beanCacheEntry != null) { 258 try { 259 PortableRemoteObject.unexportObject( 260 (java.rmi.Remote ) 261 beanCacheEntry.getCacheEntry()); 262 } catch (java.rmi.NoSuchObjectException ex) { 263 log.warn("The cache object was not bound : " + 264 ex.getMessage(),ex); 265 } 266 } 267 entries.put(cacheKey,new BeanCacheEntry(cacheTimeout,cacheKey, 268 wrappedObject, proxy, handle)); 269 } 270 } 271 272 273 278 public void removeCacheEntry(Object cacheKey) throws BeanException { 279 synchronized (entries) { 280 checkStatus(); 281 entries.remove(cacheKey); 282 } 283 } 284 285 286 289 private void checkStatus() throws BeanException { 290 if (status.isTerminated()) { 291 throw new BeanException("Bean cache has been terminated."); 292 } 293 } 294 } 295 | Popular Tags |