1 24 25 package com.rift.coad.lib.cache; 27 28 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import org.apache.log4j.Logger; 37 38 import com.rift.coad.lib.configuration.ConfigurationFactory; 40 import com.rift.coad.lib.configuration.Configuration; 41 import com.rift.coad.lib.thread.BasicThread; 42 import com.rift.coad.lib.thread.ThreadStateMonitor; 43 import com.rift.coad.lib.thread.CoadunationThreadGroup; 44 45 52 public class CacheRegistry { 53 54 57 public class CacheListManager extends BasicThread { 58 59 private ClassLoader loader = null; 61 private Map caches = new HashMap (); 62 private ThreadStateMonitor threadStateMonitor = null; 63 64 65 70 public CacheListManager(ClassLoader loader) throws Exception { 71 this.loader = loader; 72 threadStateMonitor = new ThreadStateMonitor(delay); 73 } 74 75 76 81 public void process() throws Exception { 82 while(!threadStateMonitor.isTerminated()) { 83 threadStateMonitor.monitor(); 84 Set keySet = new HashSet (); 85 synchronized (caches) { 86 keySet.addAll(caches.keySet()); 87 } 88 Iterator iter = keySet.iterator(); 89 while(iter.hasNext()) { 90 Object key = iter.next(); 91 Cache cache = null; 92 synchronized (caches) { 93 cache = (Cache)caches.get(key); 94 } 95 try { 96 cache.garbageCollect(); 97 } catch (Exception ex) { 98 log.error("Failed to clear the garbage collect : " + 99 ex.getMessage(),ex); 100 } 101 if (threadStateMonitor.isTerminated()) 102 { 103 break; 104 } 105 } 106 } 107 108 Iterator iter = caches.keySet().iterator(); 110 while(iter.hasNext()) { 111 Object key = iter.next(); 112 Cache cache = (Cache)caches.get(key); 113 try { 114 cache.clear(); 115 } catch (Exception ex) { 116 log.error("Failed to clear the cache : " + 117 ex.getMessage(),ex); 118 } 119 } 120 } 121 122 123 127 public void terminate() { 128 threadStateMonitor.terminate(false); 129 } 130 131 132 139 public Cache getCache(Class cacheClass) throws CacheException { 140 if (threadStateMonitor.isTerminated()) { 141 throw new CacheException( 142 "The cache has been closed cannot add anymore entries."); 143 } 144 try 146 { 147 synchronized (caches) { 148 if (caches.containsKey(cacheClass)) { 149 return (Cache)caches.get(cacheClass); 150 } 151 Cache cache = (Cache)cacheClass.newInstance(); 152 caches.put(cacheClass,cache); 153 return cache; 154 } 155 } catch (Exception ex) { 156 157 throw new CacheException("Failed to retrieve the cache entry"); 158 } 159 } 160 } 161 162 private static final String DELAY = "cache_process_delay"; 164 private static final long DELAY_DEFAULT = 500; 165 private static final String USER = "cache_user"; 166 167 protected Logger log = 169 Logger.getLogger(CacheRegistry.class.getName()); 170 171 private static CacheRegistry singleton = null; 173 private CoadunationThreadGroup threadGroup = null; 174 private String threadUser = null; 175 private Map cacheManagers = new HashMap (); 176 private Configuration config = null; 177 private long delay = 0; 178 private ThreadStateMonitor stateMonitor = new ThreadStateMonitor(); 179 180 181 187 private CacheRegistry(CoadunationThreadGroup threadGroup) throws CacheException { 188 try { 189 this.threadGroup = threadGroup.createThreadGroup(); 190 config = ConfigurationFactory. 191 getInstance().getConfig(CacheRegistry.class); 192 delay = config.getLong(DELAY,DELAY_DEFAULT); 193 threadUser = config.getString(USER); 194 } catch (Exception ex) { 195 log.error("Failed to instanciate the cache registry : " + 196 ex.getMessage(),ex); 197 throw new CacheException("Failed to instanciate the cache registry : " 198 + ex.getMessage(),ex); 199 } 200 } 201 202 203 211 public static synchronized CacheRegistry init(CoadunationThreadGroup threadGroup) throws 212 CacheException { 213 if (singleton == null) { 215 singleton = new CacheRegistry(threadGroup); 216 } 217 return singleton; 218 } 219 220 221 227 public static synchronized CacheRegistry getInstance() throws CacheException { 228 if (singleton == null) { 229 throw new CacheException( 230 "The cache registry has not been initialized"); 231 } 232 return singleton; 233 } 234 235 236 240 public void initCache() throws CacheException { 241 if (stateMonitor.isTerminated()) { 242 throw new CacheException("Cache is terminated"); 243 } 244 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 245 CacheListManager cacheListManager = null; 246 synchronized (cacheManagers) { 247 if (cacheManagers.containsKey(loader)) { 248 throw new CacheException("There is already a cache entry for " + 249 "this loader"); 250 } 251 try { 252 cacheListManager = new CacheListManager(loader); 253 } catch (Exception ex) { 254 throw new CacheException ( 255 "Failed to instanciate the cace list manager : " + 256 ex.getMessage(),ex); 257 } 258 cacheManagers.put(loader,cacheListManager); 259 } 260 try { 261 threadGroup.addThread(cacheListManager,threadUser); 262 cacheListManager.start(); 263 cacheListManager.setContextClassLoader(loader); 264 } catch (Exception ex) { 265 synchronized (cacheManagers) { 267 cacheManagers.remove(loader); 268 } 269 log.error("Failed to start the cache list manager : " + 270 ex.getMessage(),ex); 271 throw new CacheException("Failed to init the cache : " + 272 ex.getMessage(),ex); 273 } 274 275 } 276 277 278 286 public Cache getCache(Class cacheClass) throws CacheException { 287 if (stateMonitor.isTerminated()) { 288 throw new CacheException("Cache is terminated"); 289 } 290 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 291 CacheListManager cacheListManager = null; 292 synchronized (cacheManagers) { 293 if (!cacheManagers.containsKey(loader)) { 294 throw new CacheException("The is no cache list manager for " + 295 "this loader"); 296 } 297 cacheListManager = (CacheListManager)cacheManagers.get(loader); 298 } 299 return cacheListManager.getCache(cacheClass); 300 } 301 302 303 308 public void terminateCache() throws CacheException { 309 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 310 CacheListManager cacheListManager = null; 311 synchronized (cacheManagers) { 312 if (!cacheManagers.containsKey(loader)) { 313 log.info("The is no cache list manager for this loader. " + 314 "The deploy was not completed successfully"); 315 return; 316 } 317 cacheListManager = (CacheListManager)cacheManagers.get(loader); 318 cacheManagers.remove(loader); 319 } 320 cacheListManager.terminate(); 321 } 322 323 324 329 public void shutdown() throws CacheException { 330 stateMonitor.terminate(false); 331 try { 332 threadGroup.terminate(); 333 } catch (Exception ex) { 334 log.error("Failed to terminate the cache thread managers : " + 335 ex.getMessage(),ex); 336 } 337 synchronized(cacheManagers) { 338 cacheManagers.clear(); 339 } 340 } 341 } 342 | Popular Tags |