1 39 40 package org.jahia.services.cache; 41 42 import java.util.HashMap ; 43 import java.util.Iterator ; 44 import java.util.Map ; 45 import java.util.Set ; 46 47 import org.apache.commons.collections.FastHashMap; 48 import org.apache.log4j.Logger; 49 import org.jahia.exceptions.JahiaException; 50 import org.jahia.exceptions.JahiaInitializationException; 51 import org.jahia.mbeans.JahiaMBeanServer; 52 import org.jahia.services.JahiaInitializableService; 53 import org.jahia.services.cache.simplecache.PersistenceCache; 54 import org.jahia.services.cache.simplecache.SimpleCache; 55 import org.jahia.services.cache.simplecache.SimpleHtmlCache; 56 import org.jahia.settings.SettingsBean; 57 58 70 public class CacheFactory extends JahiaInitializableService { 71 72 73 protected static CacheFactory instance; 74 75 76 private static final Logger logger = Logger.getLogger(CacheFactory.class); 77 78 79 final protected HashMap caches = new FastHashMap(40); 80 81 82 private JMSHub jmsHub = null; 83 84 private Map cacheLimits = null; 86 87 private boolean JMXEnabled = true; 88 89 protected boolean keyHierarchyEnabled = false; 90 91 95 protected CacheFactory() { 96 } 97 98 107 public void init(SettingsBean jSettings) throws JahiaInitializationException { 108 if (jSettings == null) 110 return; 111 112 if (jSettings.lookupBoolean(SettingsBean.JMS_CACHE_ACTIVATION)) { 113 jmsHub = new JMSHub(); 114 jmsHub.init(jSettings, this); 115 } 116 117 cacheLimits = jSettings.getJahiaMaxCachedValues(); 118 } 119 120 public synchronized void shutdown() throws JahiaException { 122 super.shutdown(); 123 124 caches.clear(); 129 130 if (jmsHub != null) { 131 jmsHub.disconnect(); 134 } 135 } 136 137 142 public static synchronized CacheFactory getInstance() { 143 if (instance == null) { 144 instance = new CacheFactory(); 145 } 146 return instance; 147 } 148 149 166 public synchronized Cache createCacheInstance(String name) throws JahiaInitializationException { 167 if (name == null) 169 return null; 170 171 Cache cache = getCache(name); 173 if (cache != null) { 174 return cache; 175 } 176 177 CacheFactory factory = this; 179 if (factory == null) { 180 logger.warn("Could not get the CacheFactory instance! "); 181 return null; 182 } 183 184 cache = new SimpleCache(name, factory.jmsHub); 186 if (factory.cacheLimits.containsKey(name)) { 187 String limitStr = (String ) factory.cacheLimits.get(name); 188 cache.setCacheLimit(Integer.parseInt(limitStr)); 189 } 190 logger.debug("Created cache instance [" + name + "]"); 191 192 if (registerCache(cache)) { 193 return cache; 194 } 195 196 cache = null; 197 return null; 198 } 199 200 229 public synchronized PersistenceCache createCacheInstance(String name, PersistenceCacheable persistenceCacheable) 230 throws JahiaInitializationException { 231 if (name == null) 233 return null; 234 235 Cache cache = getCache(name); 237 if (cache != null) { 238 if (cache instanceof PersistenceCache) { 239 return (PersistenceCache) cache; 240 241 } else { 242 logger.error("Cannot create new cache [" + name 243 + "] because another non PersistenceCache cache holds already this name!"); 244 return null; 245 } 246 } 247 248 CacheFactory factory = this; 250 if (factory == null) { 251 logger.warn("Could not get the CacheFactory instance! "); 252 return null; 253 } 254 255 cache = new PersistenceCache(name, factory.jmsHub, persistenceCacheable); 256 if (factory.cacheLimits.containsKey(name)) { 257 Integer limit = (Integer ) factory.cacheLimits.get(name); 258 cache.setCacheLimit(limit.intValue()); 259 } 260 logger.debug("Created persistence cache instance [" + name + "]"); 261 262 if (registerCache(cache)) { 263 return (PersistenceCache) cache; 264 } 265 266 cache = null; 267 return null; 268 } 269 270 public static synchronized Cache createCache(String name) throws JahiaInitializationException { 271 return CacheFactory.getInstance().createCacheInstance(name); 272 } 273 274 public static synchronized PersistenceCache createCache(String name, PersistenceCacheable persistenceCacheable) 275 throws JahiaInitializationException { 276 return CacheFactory.getInstance().createCacheInstance(name, persistenceCacheable); 277 } 278 279 private boolean registerCache(Cache cache) { 280 caches.put(cache.getName(), cache); 282 283 if (isJMXEnabled()) { 284 JahiaMBeanServer.getInstance().registerManagedInstance(cache, "Cache", cache.getName()); 286 } 287 return true; 288 } 289 290 306 public static HtmlCache getHtmlCache() throws JahiaInitializationException { 307 308 HtmlCache cache = (HtmlCache) getCache(CacheFactory.getInstance(), HtmlCache.HTML_CACHE); 310 if (cache != null) 311 return cache; 312 313 return CacheFactory.getInstance().createHtmlCacheInstance(); 314 } 315 316 public HtmlCache createHtmlCacheInstance() throws JahiaInitializationException { 317 318 SimpleHtmlCache cache = new SimpleHtmlCache(this.jmsHub); 320 if (this.cacheLimits.containsKey(HtmlCache.HTML_CACHE)) { 321 String limitStr = (String ) this.cacheLimits.get(HtmlCache.HTML_CACHE); 322 cache.setCacheLimit(Integer.parseInt(limitStr)); 323 } 324 this.caches.put(cache.getName(), cache); 325 326 return cache; 327 } 328 329 public Cache getCache(String name) { 330 if (name == null) { 331 return null; 332 } 333 return (Cache) caches.get(name); 334 } 335 336 346 public static Cache getCache(CacheFactory factory, String name) { 347 348 if (factory == null) { 349 logger.warn("Could not get the CacheFactory instance! "); 350 return null; 351 } 352 353 return factory.getCache(name); 354 } 355 356 363 public Set getNames() { 364 return caches.keySet(); 365 } 366 367 378 public synchronized void flushAllCaches() { 379 380 Iterator cacheNames = getNames().iterator(); 381 while (cacheNames.hasNext()) { 382 String curCacheName = (String ) cacheNames.next(); 383 Cache cache = (Cache) caches.get(curCacheName); 384 385 cache.flush(); 386 } 387 388 logger.info("Flushed all caches."); 389 } 390 391 398 public boolean isJMSEnabled() { 399 return (jmsHub != null); 400 } 401 402 413 public void enableJMSSynchronization() throws JahiaInitializationException { 414 if (jmsHub != null) 415 jmsHub.connect(); 416 } 417 418 public void syncCachesNow() { 419 if (jmsHub != null) 420 jmsHub.sendMessagesNow(); 421 } 422 423 429 public void disableJMSSynchronization() { 430 if (jmsHub != null) 431 jmsHub.disconnect(); 432 } 433 434 public boolean isJMXEnabled() { 435 return JMXEnabled; 436 } 437 438 public void setJMXEnabled(boolean isJMXEnabled) { 439 this.JMXEnabled = isJMXEnabled; 440 } 441 442 public boolean isKeyHierarchyEnabled() { 443 return keyHierarchyEnabled; 444 } 445 } 446 | Popular Tags |