1 18 19 package org.apache.roller.util.cache; 20 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.roller.RollerException; 30 import org.apache.roller.business.runnable.ContinuousWorkerThread; 31 import org.apache.roller.business.runnable.Job; 32 import org.apache.roller.config.RollerConfig; 33 import org.apache.roller.model.RollerFactory; 34 import org.apache.roller.model.UserManager; 35 import org.apache.roller.pojos.BookmarkData; 36 import org.apache.roller.pojos.CommentData; 37 import org.apache.roller.pojos.FolderData; 38 import org.apache.roller.pojos.RefererData; 39 import org.apache.roller.pojos.UserData; 40 import org.apache.roller.pojos.WeblogCategoryData; 41 import org.apache.roller.pojos.WeblogEntryData; 42 import org.apache.roller.pojos.WeblogTemplate; 43 import org.apache.roller.pojos.WebsiteData; 44 45 46 59 public class CacheManager { 60 61 private static Log log = LogFactory.getLog(CacheManager.class); 62 63 private static final String DEFAULT_FACTORY = 64 "org.apache.roller.util.cache.ExpiringLRUCacheFactoryImpl"; 65 66 private static CacheFactory cacheFactory = null; 68 69 private static Set cacheHandlers = new HashSet (); 71 72 private static Map caches = new HashMap (); 74 75 private static ContinuousWorkerThread futureInvalidationsThread = null; 76 77 78 static { 79 String classname = RollerConfig.getProperty("cache.defaultFactory"); 81 82 try { 84 Class factoryClass = Class.forName(classname); 85 cacheFactory = (CacheFactory) factoryClass.newInstance(); 86 } catch(ClassCastException cce) { 87 log.error("It appears that your factory does not implement "+ 88 "the CacheFactory interface",cce); 89 } catch(Exception e) { 90 log.error("Unable to instantiate cache factory ["+classname+"]"+ 91 " falling back on default", e); 92 } 93 94 if(cacheFactory == null) try { 95 Class factoryClass = Class.forName(DEFAULT_FACTORY); 98 cacheFactory = (CacheFactory) factoryClass.newInstance(); 99 } catch(Exception e) { 100 log.fatal("Failed to instantiate a cache factory", e); 101 throw new RuntimeException (e); 102 } 103 104 log.info("Cache Manager Initialized."); 105 log.info("Cache Factory = "+cacheFactory.getClass().getName()); 106 107 108 String customHandlers = RollerConfig.getProperty("cache.customHandlers"); 110 if(customHandlers != null && customHandlers.trim().length() > 0) { 111 112 String [] cHandlers = customHandlers.split(","); 113 for(int i=0; i < cHandlers.length; i++) { 114 try { 116 Class handlerClass = Class.forName(cHandlers[i]); 117 CacheHandler customHandler = 118 (CacheHandler) handlerClass.newInstance(); 119 120 cacheHandlers.add(customHandler); 121 } catch(ClassCastException cce) { 122 log.error("It appears that your handler does not implement "+ 123 "the CacheHandler interface",cce); 124 } catch(Exception e) { 125 log.error("Unable to instantiate cache handler ["+cHandlers[i]+"]", e); 126 } 127 } 128 } 129 130 Integer peerTime = new Integer (5); 132 String peerTimeString = RollerConfig.getProperty("cache.futureInvalidations.peerTime"); 133 try { 134 peerTime = new Integer (peerTimeString); 135 } catch(NumberFormatException nfe) { 136 } 138 139 int threadTime = (peerTime.intValue() * 60 * 1000) - (10 * 1000); 143 144 futureInvalidationsThread = new ContinuousWorkerThread("future invalidations thread", threadTime); 146 Job futureInvalidationsJob = new FuturePostingsInvalidationJob(); 147 148 Map inputs = new HashMap (); 150 inputs.put("peerTime", peerTime); 151 futureInvalidationsJob.input(inputs); 152 153 futureInvalidationsThread.setJob(futureInvalidationsJob); 155 futureInvalidationsThread.start(); 156 } 157 158 159 private CacheManager() {} 161 162 163 181 public static Cache constructCache(CacheHandler handler, Map properties) { 182 183 log.debug("Constructing new cache with props "+properties); 184 185 Cache cache = null; 186 187 if(properties != null && properties.containsKey("factory")) { 188 String classname = (String ) properties.get("factory"); 190 191 try { 192 Class factoryClass = Class.forName(classname); 194 CacheFactory factory = (CacheFactory) factoryClass.newInstance(); 195 196 cache = factory.constructCache(properties); 198 } catch(ClassCastException cce) { 199 log.error("It appears that your factory ["+classname+ 200 "] does not implement the CacheFactory interface",cce); 201 } catch(Exception e) { 202 log.error("Unable to instantiate cache factory ["+classname+ 203 "] falling back on default", e); 204 } 205 } 206 207 if(cache == null) { 208 cache = cacheFactory.constructCache(properties); 210 } 211 212 if(cache != null) { 213 caches.put(cache.getId(), cache); 214 215 if(handler != null) { 217 cacheHandlers.add(handler); 218 } 219 } 220 221 return cache; 222 } 223 224 225 236 public static void registerHandler(CacheHandler handler) { 237 238 log.debug("Registering handler "+handler); 239 240 if(handler != null) { 241 cacheHandlers.add(handler); 242 } 243 } 244 245 246 public static void invalidate(WeblogEntryData entry) { 247 248 log.debug("invalidating entry = "+entry.getAnchor()); 249 250 Iterator handlers = cacheHandlers.iterator(); 251 while(handlers.hasNext()) { 252 ((CacheHandler) handlers.next()).invalidate(entry); 253 } 254 } 255 256 257 public static void invalidate(WebsiteData website) { 258 259 log.debug("invalidating website = "+website.getHandle()); 260 261 Iterator handlers = cacheHandlers.iterator(); 262 while(handlers.hasNext()) { 263 ((CacheHandler) handlers.next()).invalidate(website); 264 } 265 } 266 267 268 public static void invalidate(BookmarkData bookmark) { 269 270 log.debug("invalidating bookmark = "+bookmark.getId()); 271 272 Iterator handlers = cacheHandlers.iterator(); 273 while(handlers.hasNext()) { 274 ((CacheHandler) handlers.next()).invalidate(bookmark); 275 } 276 } 277 278 279 public static void invalidate(FolderData folder) { 280 281 log.debug("invalidating folder = "+folder.getId()); 282 283 Iterator handlers = cacheHandlers.iterator(); 284 while(handlers.hasNext()) { 285 ((CacheHandler) handlers.next()).invalidate(folder); 286 } 287 } 288 289 290 public static void invalidate(CommentData comment) { 291 292 log.debug("invalidating comment = "+comment.getId()); 293 294 Iterator handlers = cacheHandlers.iterator(); 295 while(handlers.hasNext()) { 296 ((CacheHandler) handlers.next()).invalidate(comment); 297 } 298 } 299 300 301 public static void invalidate(RefererData referer) { 302 303 log.debug("invalidating referer = "+referer.getId()); 304 305 309 Iterator handlers = cacheHandlers.iterator(); 310 while(handlers.hasNext()) { 311 ((CacheHandler) handlers.next()).invalidate(referer); 312 } 313 } 314 315 316 public static void invalidate(UserData user) { 317 318 log.debug("invalidating user = "+user.getUserName()); 319 320 Iterator handlers = cacheHandlers.iterator(); 321 while(handlers.hasNext()) { 322 ((CacheHandler) handlers.next()).invalidate(user); 323 } 324 } 325 326 327 public static void invalidate(WeblogCategoryData category) { 328 329 log.debug("invalidating category = "+category.getId()); 330 331 Iterator handlers = cacheHandlers.iterator(); 332 while(handlers.hasNext()) { 333 ((CacheHandler) handlers.next()).invalidate(category); 334 } 335 } 336 337 338 public static void invalidate(WeblogTemplate template) { 339 340 log.debug("invalidating template = "+template.getId()); 341 342 Iterator handlers = cacheHandlers.iterator(); 343 while(handlers.hasNext()) { 344 ((CacheHandler) handlers.next()).invalidate(template); 345 } 346 } 347 348 349 352 public static void clear() { 353 354 Cache cache = null; 356 Iterator cachesIT = caches.values().iterator(); 357 while(cachesIT.hasNext()) { 358 cache = (Cache) cachesIT.next(); 359 360 cache.clear(); 361 } 362 } 363 364 365 368 public static void clear(String cacheId) { 369 370 Cache cache = (Cache) caches.get(cacheId); 371 if(cache != null) { 372 cache.clear(); 373 } 374 } 375 376 377 385 public static Map getStats() { 386 387 Map allStats = new HashMap (); 388 389 Cache cache = null; 390 Iterator cachesIT = caches.values().iterator(); 391 while(cachesIT.hasNext()) { 392 cache = (Cache) cachesIT.next(); 393 394 allStats.put(cache.getId(), cache.getStats()); 395 } 396 397 return allStats; 398 } 399 400 401 404 public static void shutdown() { 405 406 if(futureInvalidationsThread != null) { 408 futureInvalidationsThread.interrupt(); 409 } 410 } 411 412 } 413 | Popular Tags |