View Javadoc

1   /**
2    * This file Copyright (c) 2008-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.cache.filter;
35  
36  import info.magnolia.cms.core.AggregationState;
37  import info.magnolia.cms.filters.OncePerRequestAbstractMgnlFilter;
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.module.ModuleRegistry;
40  import info.magnolia.module.cache.Cache;
41  import info.magnolia.module.cache.CacheConfiguration;
42  import info.magnolia.module.cache.CacheModuleLifecycleListener;
43  import info.magnolia.module.cache.CacheModule;
44  import info.magnolia.module.cache.CachePolicyExecutor;
45  import info.magnolia.module.cache.CachePolicyResult;
46  import info.magnolia.module.cache.mbean.CacheMonitor;
47  
48  import javax.servlet.FilterChain;
49  import javax.servlet.FilterConfig;
50  import javax.servlet.ServletException;
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  
54  import java.io.IOException;
55  
56  /**
57   * Uses the CachePolicy to determine the cache behavior. Uses then the
58   * CacheConfiguration to get the executors to be executed.
59   *
60   * @author gjoseph
61   * @version $Revision: $ ($Author: $)
62   */
63  public class CacheFilter extends OncePerRequestAbstractMgnlFilter implements CacheModuleLifecycleListener {
64      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CacheFilter.class);
65  
66      private static final String DEFAULT_CACHE_CONFIG = "default";
67  
68      private CacheMonitor monitor;
69      private String cacheConfigurationName;
70      private CacheConfiguration cacheConfig;
71      private Cache cache;
72  
73      public String getCacheConfigurationName() {
74          return cacheConfigurationName;
75      }
76  
77      public void setCacheConfigurationName(String cacheConfigurationName) {
78          this.cacheConfigurationName = cacheConfigurationName;
79      }
80  
81      public void init(FilterConfig filterConfig) throws ServletException {
82          super.init(filterConfig);
83          CacheModule.getInstance().register(this);
84          // modules are started *after* filters - so we have to force a call onCacheModuleStart() here
85          onCacheModuleStart();
86      }
87  
88      public void onCacheModuleStart() {
89          if (cacheConfigurationName == null) {
90              log.warn("The cacheConfigurationName property is not set for the {} CacheFilter, falling back to {}.", getName(), DEFAULT_CACHE_CONFIG);
91              this.cacheConfigurationName = DEFAULT_CACHE_CONFIG;
92          }
93  
94          final CacheModule cacheModule = getModule();
95          this.cacheConfig = cacheModule.getConfiguration(cacheConfigurationName);
96          this.cache = cacheModule.getCacheFactory().getCache(cacheConfigurationName);
97  
98          if (cacheConfig == null || cache == null) {
99              log.error("The " + getName() + " CacheFilter is not properly configured, either cacheConfig(" + cacheConfig + ") or cache(" + cache + ") is null. Check if " + cacheConfigurationName + " is a valid cache configuration name. Will disable temporarily.");
100             setEnabled(false);
101         }
102 
103         monitor = CacheMonitor.getInstance();
104     }
105 
106     protected CacheModule getModule() {
107         return CacheModule.getInstance();
108     }
109 
110     public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
111         final AggregationState aggregationState = MgnlContext.getAggregationState();
112         final CachePolicyResult cachePolicyResult = cacheConfig.getCachePolicy().shouldCache(cache, aggregationState, cacheConfig.getFlushPolicy());
113 
114         log.debug("Cache policy result: {}", cachePolicyResult);
115 
116         final CachePolicyResult.CachePolicyBehaviour behaviour = cachePolicyResult.getBehaviour();
117         monitor.logBehavior(behaviour.getName());
118         monitor.logAccess(cachePolicyResult.getCacheKey());
119         final CachePolicyExecutor executor = cacheConfig.getExecutor(behaviour);
120         if (executor == null) {
121             throw new IllegalStateException("Unexpected cache policy result: " + cachePolicyResult);
122         }
123         executor.processCacheRequest(request, response, chain, cache, cachePolicyResult);
124 
125         // TODO if the cache blocks we will have to add this again.
126         /*
127         finally{
128             Object key = cachePolicyResult.getCacheKey();
129             if (!cachePolicyResult.getBehaviour().equals(CachePolicyResult.bypass) && (
130             ((EhCacheWrapper)cache).getWrappedEhcache().getQuiet(key) == null)) {
131                 log.warn("Cache nearly blocked for key: {}, removed entry", key);
132                 cache.put(key, null);
133                 cache.remove(key);
134             }
135         }
136         */
137     }
138 
139 }