1 package info.magnolia.cms.cache; 2 3 import info.magnolia.cms.beans.config.ConfigurationException; 4 import info.magnolia.cms.core.Content; 5 import info.magnolia.cms.core.Path; 6 7 import java.util.ArrayList ; 8 9 import javax.management.MBeanServer ; 10 import javax.management.MBeanServerFactory ; 11 import javax.management.ObjectName ; 12 import javax.servlet.ServletResponse ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 16 import org.slf4j.Logger; 17 import org.slf4j.LoggerFactory; 18 19 20 26 public class ManageableCacheManager implements CacheManager, ManageableCacheManagerMBean { 27 28 private static final Logger log = LoggerFactory.getLogger(ManageableCacheManager.class); 29 30 private int cacheHits; 31 32 private final CacheManager cacheManager; 33 34 private int cacheMisses; 35 36 private int cachePuts; 37 38 public ManageableCacheManager(CacheManager cacheManager) { 39 this.cacheManager = cacheManager; 40 } 41 42 public boolean cacheRequest(CacheKey key, CacheableEntry entry, boolean canCompress) { 43 boolean didCache = this.cacheManager.cacheRequest(key, entry, canCompress); 44 45 if (didCache) { 46 this.cachePuts++; 47 } 48 49 return didCache; 50 } 51 52 public void flushAll() { 53 this.cacheManager.flushAll(); 54 } 55 56 public int getCacheHits() { 57 return this.cacheHits; 58 } 59 60 public int getCacheMisses() { 61 return this.cacheMisses; 62 } 63 64 public int getCachePuts() { 65 return this.cachePuts; 66 } 67 68 public long getCreationTime(CacheKey request) { 69 return this.cacheManager.getCreationTime(request); 70 } 71 72 public void init(Content content) throws ConfigurationException { 73 this.cacheManager.init(content); 74 registerMBean(); 75 } 76 77 public boolean isEnabled() { 78 return this.cacheManager.isEnabled(); 79 } 80 81 public boolean isPaused() { 82 return this.cacheManager.isPaused(); 83 } 84 85 public boolean isRunning() { 86 return this.cacheManager.isRunning(); 87 } 88 89 public boolean isStarted() { 90 return this.cacheManager.isStarted(); 91 } 92 93 public void pause() { 94 this.cacheManager.pause(); 95 } 96 97 public void resetStatistics() { 98 this.cacheHits = 0; 99 this.cacheMisses = 0; 100 this.cachePuts = 0; 101 } 102 103 public void restart() { 104 this.cacheManager.restart(); 105 } 106 107 public void resume() { 108 this.cacheManager.resume(); 109 } 110 111 public void start() { 112 this.cacheManager.start(); 113 } 114 115 public void stop() { 116 this.cacheManager.stop(); 117 } 118 119 public boolean streamFromCache(HttpServletRequest request, ServletResponse response) { 120 return false; 121 } 122 123 126 public boolean isCacheable(HttpServletRequest request) { 127 return this.cacheManager.isCacheable(request); 128 } 129 130 133 public boolean canCompress(HttpServletRequest request) { 134 return this.cacheManager.canCompress(request); 135 } 136 137 public CacheKey getCacheKey(HttpServletRequest request) { 138 return this.cacheManager.getCacheKey(request); 139 } 140 141 public boolean streamFromCache(CacheKey key, HttpServletResponse response, boolean canCompress) { 142 boolean didUseCache = this.cacheManager.streamFromCache(key, response, canCompress); 143 144 if (didUseCache) { 145 this.cacheHits++; 146 } 147 else if (isRunning()) { 148 this.cacheMisses++; 149 } 150 151 return didUseCache; 152 } 153 154 private void registerMBean() { 155 String appName = Path.getAppRootDir().getName(); 156 String objectName = "Magnolia:type=CacheManager,domain=" + appName; 158 try { 159 ArrayList list = MBeanServerFactory.findMBeanServer(null); 160 MBeanServer mbeanServer; 161 if (list != null && list.size() > 0) { 162 mbeanServer = (MBeanServer ) list.get(0); 163 } 164 else { 165 mbeanServer = MBeanServerFactory.createMBeanServer(); 166 } 167 mbeanServer.registerMBean(this, new ObjectName (objectName)); 168 } 169 catch (Exception e) { 170 log.error("Could not register JMX MBean '" + objectName + "'.", e); 171 } 172 } 173 } 174 | Popular Tags |