1 package info.magnolia.cms.cache; 2 3 import info.magnolia.cms.beans.config.ConfigurationException; 4 import info.magnolia.cms.core.Content; 5 6 import javax.servlet.http.HttpServletResponse ; 7 8 import org.slf4j.Logger; 9 import org.slf4j.LoggerFactory; 10 11 12 20 public abstract class BaseCacheManager implements CacheManager { 21 22 private static final Logger log = LoggerFactory.getLogger(BaseCacheManager.class); 23 24 private CacheConfig config; 25 26 private boolean initialized; 27 28 private boolean paused; 29 30 private boolean started; 31 32 public final void flushAll() { 33 log.info("Flushing Cache..."); 34 35 try { 36 doFlushAll(); 37 } 38 catch (Exception e) { 39 handleNonFatalException(e); 40 } 41 42 log.info("Cache was flushed."); 43 } 44 45 48 public CacheConfig getConfig() { 49 return this.config; 50 } 51 52 public final long getCreationTime(CacheKey key) { 53 try { 54 return doGetCreationTime(key); 55 } 56 catch (Exception e) { 57 handleNonFatalException(e); 58 return Cache.UNKNOWN_CREATION_TIME; 59 } 60 } 61 62 69 public final void init(Content content) throws ConfigurationException { 70 if (isInitialized()) { 71 throw new IllegalStateException ("CacheManager is already initialized!"); 72 } 73 74 log.info("Initializing CacheManager..."); 75 76 createCacheConfig(content); 77 78 try { 79 doInit(); 80 } 81 catch (Exception e) { 82 handleFatalException(e); 83 } 84 85 this.initialized = true; 86 87 log.info("CacheManager is now initialized."); 88 89 if (isEnabled()) { 90 start(); 91 } 92 } 93 94 97 public boolean isEnabled() { 98 return this.initialized && this.config != null && this.config.isActive(); 99 } 100 101 public final boolean isInitialized() { 102 return this.initialized; 103 } 104 105 public final boolean isPaused() { 106 return this.paused; 107 } 108 109 public final boolean isRunning() { 110 return this.started && !this.paused; 111 } 112 113 public final boolean isStarted() { 114 return this.started; 115 } 116 117 120 public final void pause() { 121 if (!isInitialized()) { 122 throw new IllegalStateException ("CacheManager is not initialized!"); 123 } 124 125 if (!isRunning()) { 126 return; 127 } 128 129 log.info("Pausing CacheManager..."); 130 131 try { 132 doPause(); 133 } 134 catch (Exception e) { 135 handleFatalException(e); 136 } 137 138 this.paused = true; 139 140 log.info("CacheManager is now paused."); 141 } 142 143 149 public void restart() { 150 if (!isInitialized()) { 151 throw new IllegalStateException ("CacheManager is not initialized!"); 152 } 153 154 try { 155 stop(); 156 start(); 157 } 158 catch (Exception e) { 159 handleFatalException(e); 160 } 161 } 162 163 166 public final void resume() { 167 if (!isInitialized()) { 168 throw new IllegalStateException ("CacheManager is not initialized!"); 169 } 170 171 if (isRunning()) { 172 return; 173 } 174 175 log.info("Resuming CacheManager..."); 176 177 try { 178 doResume(); 179 } 180 catch (Exception e) { 181 handleFatalException(e); 182 } 183 184 this.paused = false; 185 186 log.info("CacheManager is now running."); 187 } 188 189 192 public final void start() { 193 if (!isInitialized()) { 194 throw new IllegalStateException ("CacheManager is not initialized!"); 195 } 196 197 if (isStarted()) { 198 return; 199 } 200 201 log.info("Starting CacheManager..."); 202 203 try { 204 doStart(); 205 } 206 catch (Exception e) { 207 handleFatalException(e); 208 } 209 210 this.paused = false; 211 this.started = true; 212 213 log.info("CacheManager is now running."); 214 } 215 216 219 public final void stop() { 220 if (!isInitialized()) { 221 throw new IllegalStateException ("CacheManager is not initialized!"); 222 } 223 224 if (!isStarted()) { 225 return; 226 } 227 228 log.info("Stopping CacheManager..."); 229 230 try { 231 doStop(); 232 } 233 catch (Exception e) { 234 handleFatalException(e); 235 } 236 237 this.paused = false; 238 this.started = false; 239 240 log.info("CacheManager is now stopped."); 241 } 242 243 public final boolean streamFromCache(CacheKey key, HttpServletResponse response, boolean canCompress) { 244 if (log.isDebugEnabled()) { 245 log.debug("Streaming from cache {}, {}", key, canCompress ? "compressed" : "not compressed"); 246 } 247 try { 248 return doStreamFromCache(key, response, canCompress); 249 } 250 catch (Exception e) { 251 handleNonFatalException(e); 252 return false; 253 } 254 } 255 256 261 protected abstract void doFlushAll(); 262 263 268 protected abstract long doGetCreationTime(CacheKey request); 269 270 275 protected abstract boolean doStreamFromCache(CacheKey request, HttpServletResponse response, boolean canCompress); 276 277 283 protected void doInit() { 284 } 286 287 293 protected void doPause() { 294 } 296 297 303 protected void doResume() { 304 } 306 307 313 protected void doStart() { 314 } 316 317 323 protected void doStop() { 324 } 326 327 private void createCacheConfig(Content content) throws ConfigurationException { 328 this.config = new CacheConfig(this, content); 329 } 330 331 334 private void handleFatalException(Exception e) { 335 this.paused = false; 336 this.started = false; 337 log.error("Unexpected exception! Stopping operation.", e); 338 } 339 340 343 private void handleNonFatalException(Exception e) { 344 log.error("Unexpected exception! Continueing operation.", e); 345 } 346 } 347 | Popular Tags |