KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > cachius > spring > AbstractCacheableController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.cachius.spring;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.riotfamily.common.web.util.ServletUtils;
29 import org.springframework.beans.factory.BeanNameAware;
30
31
32 /**
33  * Abstract base class for cacheable controllers.
34  *
35  * @author Felix Gnass
36  */

37 public abstract class AbstractCacheableController
38         implements CacheableController, BeanNameAware {
39
40     private String JavaDoc beanName;
41     
42     public final void setBeanName(String JavaDoc beanName) {
43         this.beanName = beanName;
44     }
45         
46     protected String JavaDoc getBeanName() {
47         return beanName;
48     }
49     
50     /**
51      * Returns the cache-key for the request unless
52      * {@link #bypassCache(HttpServletRequest)} returns <code>true</code>.
53      * The method creates a StringBuffer and appends the controller's beanName.
54      * The buffer is passed to {@link #appendCacheKey(StringBuffer, HttpServletRequest)}
55      * allowing subclasses to add further values to the key.
56      */

57     public final String JavaDoc getCacheKey(HttpServletRequest JavaDoc request) {
58         if (bypassCache(request)) {
59             return null;
60         }
61         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
62         key.append(beanName).append(':');
63         appendCacheKey(key, request);
64         return key.toString();
65     }
66             
67     /**
68      * Returns whether the cache should be bypassed. The default implementation
69      * always returns <code>false</code>.
70      */

71     protected boolean bypassCache(HttpServletRequest JavaDoc request) {
72         return false;
73     }
74
75     /**
76      * Subclasses may overwrite this method to append values to the cache-key.
77      * The default implementation appends the {@link
78      * ServletUtils#getOriginatingRequestUri(HttpServletRequest) originating
79      * requestURI}.
80      */

81     protected void appendCacheKey(StringBuffer JavaDoc key, HttpServletRequest JavaDoc request) {
82         key.append(ServletUtils.getOriginatingRequestUri(request));
83     }
84
85     /**
86      * The default implementation returns <code>0</code> so that
87      * {@link #getLastModified(HttpServletRequest)} is invoked every time the
88      * controller is requested.
89      */

90     public long getTimeToLive() {
91         return 0;
92     }
93     
94     /**
95      * The default implementation returns
96      * <code>System.currentTimeMillis()</code> so that the item is
97      * refreshed as soon as it expires. Subclasses should override this
98      * method to return something reasonable.
99      */

100     public long getLastModified(HttpServletRequest JavaDoc request) {
101         return System.currentTimeMillis();
102     }
103     
104 }
105
Popular Tags