1 package org.jahia.services.cache.treecache; 2 3 import java.util.Iterator ; 4 5 import org.apache.log4j.Logger; 6 import org.jahia.exceptions.JahiaException; 7 import org.jahia.exceptions.JahiaInitializationException; 8 import org.jahia.mbeans.JahiaMBeanServer; 9 import org.jahia.services.cache.Cache; 10 import org.jahia.services.cache.CacheFactory; 11 import org.jahia.services.cache.HtmlCache; 12 import org.jahia.services.cache.simplecache.SimpleHtmlCache; 13 import org.jahia.settings.SettingsBean; 14 15 public class TreeCacheFactory extends CacheFactory { 16 17 private static final Logger logger = Logger.getLogger(TreeCacheFactory.class); 18 19 private boolean propagated = false; 20 21 30 public void init(SettingsBean jSettings) throws JahiaInitializationException { 31 if (jSettings == null) 33 return; 34 35 JahiaTreeCache.activateService(); 37 38 propagated = true; 40 keyHierarchyEnabled = true; 41 } 43 44 public synchronized void shutdown() throws JahiaException { 46 super.shutdown(); 47 48 caches.clear(); 53 54 JahiaTreeCache.inactivateService(); 55 } 56 57 62 public static synchronized CacheFactory getInstance() { 63 if (instance == null) { 64 instance = new TreeCacheFactory(); 65 } 66 return instance; 67 } 68 69 86 public synchronized Cache createCacheInstance(String name) throws JahiaInitializationException { 87 if (name == null) 89 return null; 90 91 Cache cache = getCache(name); 93 if (cache != null) { 94 return cache; 95 } 96 97 TreeCacheFactory factory = this; 99 if (factory == null) { 100 logger.warn("Could not get the CacheFactory instance! "); 101 return null; 102 } 103 104 cache = new JahiaTreeCache(name, propagated); 106 107 logger.debug("Created cache instance [" + name + "]"); 108 109 if (registerCache(cache)) { 110 return cache; 111 } 112 113 cache = null; 114 return null; 115 } 116 117 private boolean registerCache(Cache cache) { 118 caches.put(cache.getName(), cache); 120 121 if (isJMXEnabled()) { 122 JahiaMBeanServer.getInstance().registerManagedInstance(cache, "Cache", cache.getName()); 124 } 125 return true; 126 } 127 128 public Cache getCache(String name) { 129 if (name == null) { 130 return null; 131 } 132 return (Cache) caches.get(name); 133 } 134 135 151 public HtmlCache createHtmlCacheInstance() throws JahiaInitializationException { 152 153 JahiaTreeHtmlCache cache = new JahiaTreeHtmlCache(this.propagated); 155 156 this.caches.put(cache.getName(), cache); 157 158 return cache; 159 } 160 161 172 public synchronized void flushAllCaches() { 173 174 Iterator cacheNames = getNames().iterator(); 175 while (cacheNames.hasNext()) { 176 String curCacheName = (String ) cacheNames.next(); 177 Cache cache = (Cache) caches.get(curCacheName); 178 179 synchronized (cache) { 180 cache.flush(); 181 } 182 } 183 184 logger.info("Flushed all caches."); 185 } 186 187 194 public boolean isJMSEnabled() { 195 return propagated; 196 } 197 198 209 public void enableJMSSynchronization() throws JahiaInitializationException { 210 propagated = true; 211 } 212 213 219 public void disableJMSSynchronization() { 220 propagated = false; 221 } 222 } 223 | Popular Tags |