KickJava   Java API By Example, From Geeks To Geeks.

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


1 package info.magnolia.cms.cache;
2
3 import info.magnolia.cms.beans.config.ConfigurationException;
4 import info.magnolia.cms.beans.config.MIMEMapping;
5 import info.magnolia.cms.cache.noop.NoOpCache;
6 import info.magnolia.cms.util.ClassUtil;
7
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
11 import org.apache.commons.lang.StringUtils;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15
16 /**
17  * @author Andreas Brenk
18  * @author Fabrizio Giustina
19  * @since 3.0
20  */

21 public class DefaultCacheManager extends BaseCacheManager {
22
23     private static final Logger log = LoggerFactory.getLogger(DefaultCacheManager.class);
24
25     private static final Cache NOOP_CACHE = new NoOpCache();
26
27     private Cache cache = NOOP_CACHE;
28
29     /**
30      * @see info.magnolia.cms.cache.CacheManager#getCacheKey(javax.servlet.http.HttpServletRequest)
31      */

32     public CacheKey getCacheKey(HttpServletRequest JavaDoc request) {
33         return new CacheKey(request);
34     }
35
36     /**
37      * @see info.magnolia.cms.cache.CacheManager#isCacheable(javax.servlet.http.HttpServletRequest)
38      */

39     public boolean isCacheable(HttpServletRequest JavaDoc request) {
40
41         if (StringUtils.equalsIgnoreCase(request.getMethod(), "POST")) {
42             return false; // don't cache POSTs
43
}
44
45         if (!request.getParameterMap().isEmpty()) {
46             return false; // don't cache requests with parameters
47
}
48
49         if (StringUtils.isEmpty(MIMEMapping.getMIMEType(request))) {
50             return false; // check for MIMEMapping, extension must exist
51
}
52
53         return getConfig().isUriCacheable(request);
54     }
55
56     /**
57      * @see info.magnolia.cms.cache.CacheManager#canCompress(javax.servlet.http.HttpServletRequest)
58      */

59     public boolean canCompress(HttpServletRequest JavaDoc request) {
60
61         String JavaDoc extension = StringUtils.substringAfter(request.getRequestURI(), ".");
62         return getConfig().canCompress(extension);
63     }
64
65     public boolean cacheRequest(CacheKey key, CacheableEntry entry, boolean canCompress) {
66         cache.cacheRequest(key, entry, canCompress);
67         return true;
68     }
69
70     protected void doFlushAll() {
71         this.cache.flush();
72     }
73
74     protected long doGetCreationTime(CacheKey request) {
75         return this.cache.getCreationTime(request);
76     }
77
78     protected void doStart() {
79         try {
80             CacheConfig config = getConfig();
81
82             Cache cache = newCache(config.getCacheImplementation());
83             cache.start(config);
84
85             this.cache = cache;
86         }
87         catch (ConfigurationException e) {
88             // FIXME
89
}
90     }
91
92     protected void doStop() {
93         this.cache.stop();
94         this.cache = NOOP_CACHE;
95     }
96
97     protected boolean doStreamFromCache(CacheKey key, HttpServletResponse JavaDoc response, boolean canCompress) {
98         if (!isRunning()) {
99             return false;
100         }
101
102         boolean didUseCache = this.cache.streamFromCache(key, response, canCompress);
103
104         if (log.isDebugEnabled()) {
105             if (didUseCache) {
106                 log.debug("Used cache for: '{}'.", key);
107             }
108             else {
109                 log.debug("Not found in cache: '{}'.", key);
110             }
111         }
112
113         return didUseCache;
114     }
115
116     protected Cache getCache() {
117         return this.cache;
118     }
119
120     private boolean isCached(CacheKey request) {
121         return this.cache.isCached(request);
122     }
123
124     private Cache newCache(String JavaDoc cacheImplementation) throws ConfigurationException {
125         try {
126             return (Cache) ClassUtil.newInstance(cacheImplementation);
127         }
128         catch (ClassNotFoundException JavaDoc e) {
129             throw new ConfigurationException("Cache class not found!", e);
130         }
131         catch (InstantiationException JavaDoc e) {
132             throw new ConfigurationException("Could not instantiate Cache class!", e);
133         }
134         catch (IllegalAccessException JavaDoc e) {
135             throw new ConfigurationException("Could not access Cache class!", e);
136         }
137         catch (ClassCastException JavaDoc e) {
138             throw new ConfigurationException("Class does not implement Cache!", e);
139         }
140     }
141
142 }
143
Popular Tags