KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > cache > BaseCacheManager


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 JavaDoc;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11
12 /**
13  * An abstract base implementation of <code>CacheManager</code> implementing its lifecycle and enforcing a no
14  * exception policy after initialization. Also does INFO level logging.
15  * @author Andreas Brenk
16  * @author Fabrizio Giustina
17  * @since 3.0
18  * $Id: BaseCacheManager.java 6341 2006-09-12 09:18:27Z philipp $
19  */

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 JavaDoc e) {
39             handleNonFatalException(e);
40         }
41
42         log.info("Cache was flushed.");
43     }
44
45     /**
46      * @return the config created by a previous call to init(Content) or null if not yet initialized.
47      */

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 JavaDoc e) {
57             handleNonFatalException(e);
58             return Cache.UNKNOWN_CREATION_TIME;
59         }
60     }
61
62     /**
63      * This method must be called once and only once. It loads the configuration from the repository and starts the
64      * cache if it is enabled. Normally called by ConfigLoader.
65      * @see info.magnolia.cms.beans.config.ConfigLoader
66      * @throws ConfigurationException if the configuration is invalid
67      * @throws IllegalStateException if called more than once
68      */

69     public final void init(Content content) throws ConfigurationException {
70         if (isInitialized()) {
71             throw new IllegalStateException JavaDoc("CacheManager is already initialized!");
72         }
73
74         log.info("Initializing CacheManager...");
75
76         createCacheConfig(content);
77
78         try {
79             doInit();
80         }
81         catch (Exception JavaDoc 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     /**
95      * Return true only if it has been inited AND the config is active
96      */

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     /**
118      * @throws IllegalStateException if called before init(Content)
119      */

120     public final void pause() {
121         if (!isInitialized()) {
122             throw new IllegalStateException JavaDoc("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 JavaDoc e) {
135             handleFatalException(e);
136         }
137
138         this.paused = true;
139
140         log.info("CacheManager is now paused.");
141     }
142
143     /**
144      * Do a complete stop / start cycle.
145      * @see #stop()
146      * @see #start()
147      * @throws IllegalStateException if called before init(Content)
148      */

149     public void restart() {
150         if (!isInitialized()) {
151             throw new IllegalStateException JavaDoc("CacheManager is not initialized!");
152         }
153
154         try {
155             stop();
156             start();
157         }
158         catch (Exception JavaDoc e) {
159             handleFatalException(e);
160         }
161     }
162
163     /**
164      * @throws IllegalStateException if called before init(Content)
165      */

166     public final void resume() {
167         if (!isInitialized()) {
168             throw new IllegalStateException JavaDoc("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 JavaDoc e) {
181             handleFatalException(e);
182         }
183
184         this.paused = false;
185
186         log.info("CacheManager is now running.");
187     }
188
189     /**
190      * @throws IllegalStateException if called before init(Content)
191      */

192     public final void start() {
193         if (!isInitialized()) {
194             throw new IllegalStateException JavaDoc("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 JavaDoc 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     /**
217      * @throws IllegalStateException if called before init(Content)
218      */

219     public final void stop() {
220         if (!isInitialized()) {
221             throw new IllegalStateException JavaDoc("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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
251             handleNonFatalException(e);
252             return false;
253         }
254     }
255
256     /**
257      * Template method to be implemented by subclasses. Should not throw any exceptions. All exceptions thrown by this
258      * method are logged as an error but are not propagated.
259      * @see BaseCacheManager#handleNonFatalException(Exception)
260      */

261     protected abstract void doFlushAll();
262
263     /**
264      * Template method to be implemented by subclasses. Should not throw any exceptions. All exceptions thrown by this
265      * method are logged as an error but are not propagated.
266      * @see BaseCacheManager#handleNonFatalException(Exception)
267      */

268     protected abstract long doGetCreationTime(CacheKey request);
269
270     /**
271      * Template method to be implemented by subclasses. Should not throw any exceptions. All exceptions thrown by this
272      * method are logged as an error but are not propagated.
273      * @see BaseCacheManager#handleNonFatalException(Exception)
274      */

275     protected abstract boolean doStreamFromCache(CacheKey request, HttpServletResponse JavaDoc response, boolean canCompress);
276
277     /**
278      * Template method to be implemented by subclasses. Should not throw any exceptions. All exceptions thrown by this
279      * method are logged as a fatal error but are not propagated. The state is reset to "stopped". This implementation
280      * is empty.
281      * @see BaseCacheManager#handleFatalException(Exception)
282      */

283     protected void doInit() {
284         // empty template method
285
}
286
287     /**
288      * Template method to be implemented by subclasses. Should not throw any exceptions. All exceptions thrown by this
289      * method are logged as a fatal error but are not propagated. The state is reset to "stopped". This implementation
290      * is empty.
291      * @see BaseCacheManager#handleFatalException(Exception)
292      */

293     protected void doPause() {
294         // empty template method
295
}
296
297     /**
298      * Template method to be implemented by subclasses. Should not throw any exceptions. All exceptions thrown by this
299      * method are logged as a fatal error but are not propagated. The state is reset to "stopped". This implementation
300      * is empty.
301      * @see BaseCacheManager#handleFatalException(Exception)
302      */

303     protected void doResume() {
304         // empty template method
305
}
306
307     /**
308      * Template method to be implemented by subclasses. Should not throw any exceptions. All exceptions thrown by this
309      * method are logged as a fatal error but are not propagated. The state is reset to "stopped". This implementation
310      * is empty.
311      * @see BaseCacheManager#handleFatalException(Exception)
312      */

313     protected void doStart() {
314         // empty template method
315
}
316
317     /**
318      * Template method to be implemented by subclasses. Should not throw any exceptions. All exceptions thrown by this
319      * method are logged as a fatal error but are not propagated. The state is reset to "stopped". This implementation
320      * is empty.
321      * @see BaseCacheManager#handleFatalException(Exception)
322      */

323     protected void doStop() {
324         // empty template method
325
}
326
327     private void createCacheConfig(Content content) throws ConfigurationException {
328         this.config = new CacheConfig(this, content);
329     }
330
331     /**
332      * Log the <code>Exception</code> as a fatal error and stop operation.
333      */

334     private void handleFatalException(Exception JavaDoc e) {
335         this.paused = false;
336         this.started = false;
337         log.error("Unexpected exception! Stopping operation.", e);
338     }
339
340     /**
341      * Log the <code>Exception</code> as an error and continue operation.
342      */

343     private void handleNonFatalException(Exception JavaDoc e) {
344         log.error("Unexpected exception! Continueing operation.", e);
345     }
346 }
347
Popular Tags