KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > cache > DefaultCacheHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.appserv.web.cache;
25
26 import java.util.Map JavaDoc;
27
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34
35 import com.sun.enterprise.web.logging.pwc.LogDomains;
36
37 import com.sun.appserv.web.cache.mapping.Constants;
38 import com.sun.appserv.web.cache.mapping.CacheMapping;
39 import com.sun.appserv.web.cache.mapping.ConstraintField;
40 import com.sun.appserv.web.cache.mapping.Field;
41
42 /** DefaultCacheHelper interface is the built-in implementation of the
43  * <code>CacheHelper</code> interface to aide in:
44  * a) the key generation b) whether to cache the response.
45  * There is one CacheHelper instance per web application.
46  */

47 public class DefaultCacheHelper implements CacheHelper {
48
49     public static final String JavaDoc ATTR_CACHING_FILTER_NAME =
50                                 "com.sun.ias.web.cachingFilterName";
51     public static final String JavaDoc PROP_KEY_GENERATOR_ATTR_NAME =
52                                 "cacheKeyGeneratorAttrName";
53     // PWC_LOGGER
54
private static Logger JavaDoc _logger;
55     private static boolean _isTraceEnabled = false;
56
57     /**
58      * The resource bundle containing the localized message strings.
59      */

60     private static ResourceBundle JavaDoc _rb = null;
61
62     ServletContext JavaDoc context;
63
64     // cache manager
65
CacheManager manager;
66
67     String JavaDoc attrKeyGenerator = null;
68     boolean isKeyGeneratorChecked = false;
69     CacheKeyGenerator keyGenerator;
70
71     /**
72      * set the CacheManager for this application
73      * @param manager associated with this application
74      */

75     public void setCacheManager(CacheManager manager) {
76         this.manager = manager;
77     }
78
79     /*** CacheHelper methods **/
80
81     /**
82      * initialize this helper
83      * @param context the web application context this helper belongs to
84      * @param props helper properties
85      */

86     public void init(ServletContext JavaDoc context, Map JavaDoc props) {
87         this.context = context;
88         attrKeyGenerator = (String JavaDoc)props.get(PROP_KEY_GENERATOR_ATTR_NAME);
89
90         // web container logger
91
_logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
92         _isTraceEnabled = _logger.isLoggable(Level.FINE);
93         _rb = _logger.getResourceBundle();
94     }
95
96     /**
97      * cache-mapping for this servlet-name or the URLpattern
98      * @param request incoming request
99      * @return cache-mapping object; uses servlet name or the URL pattern
100      * to lookup in the CacheManager for the mapping.
101      */

102     private CacheMapping lookupCacheMapping(HttpServletRequest JavaDoc request) {
103         String JavaDoc name = (String JavaDoc)request.getAttribute(ATTR_CACHING_FILTER_NAME);
104         return manager.getCacheMapping(name);
105     }
106
107     /** getCacheKey: generate the key to be used to cache this request
108      * @param request incoming <code>HttpServletRequest</code>
109      * @return key string used to access the cache entry.
110      * Key is composed of: servletPath + a concatenation of the field values in
111      * the request; all key field names must be found in the appropriate scope.
112      */

113     public String JavaDoc getCacheKey(HttpServletRequest JavaDoc request) {
114
115         // cache mapping associated with the request
116
CacheMapping mapping = lookupCacheMapping(request);
117
118         if (isKeyGeneratorChecked == false && attrKeyGenerator != null) {
119             try {
120                 keyGenerator = (CacheKeyGenerator)
121                                 context.getAttribute(attrKeyGenerator);
122             } catch (ClassCastException JavaDoc cce){
123                 _logger.log(Level.WARNING, "cache.defaultHelp.illegalKeyGenerator", cce);
124             }
125
126             isKeyGeneratorChecked = true;
127         }
128
129         if (keyGenerator != null) {
130             String JavaDoc key = keyGenerator.getCacheKey(context, request);
131             if (key != null)
132                 return key;
133         }
134
135         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(128);
136
137         /** XXX: the StringBuffer append is a (uncontended) synchronized method.
138          * performance hit?
139          */

140         sb.append(request.getServletPath());
141
142         // append the key fields
143
Field[] keys = mapping.getKeyFields();
144         for (int i = 0; i < keys.length; i++) {
145             Object JavaDoc value = keys[i].getValue(context, request);
146
147             // all defined key field must be present
148
if (value == null) {
149                 if (_isTraceEnabled) {
150                     _logger.fine("DefaultCacheHelper: cannot find all the required key fields in the request " + request.getServletPath());
151                 }
152                 return null;
153             }
154
155             sb.append(";");
156             sb.append(Constants.KEY_PREFIXES[keys[i].getScope()]);
157             sb.append(keys[i].getName());
158             sb.append("=");
159             sb.append(value);
160         }
161
162         return sb.toString();
163     }
164
165     /** isCacheable: is the response to given request cachebale?
166      * @param request incoming <code>HttpServletRequest</code> object
167      * @return <code>true</code> if the response could be cached.
168      * or return <code>false</code> if the results of this request
169      * must not be cached.
170      *
171      * Applies pre-configured cacheability constraints in the cache-mapping;
172      * all constraints must pass for this to be cacheable.
173      */

174     public boolean isCacheable(HttpServletRequest JavaDoc request) {
175         boolean result = false;
176
177         // cache mapping associated with the request
178
CacheMapping mapping = lookupCacheMapping(request);
179
180         // check if the method is in the allowed methods list
181
if (mapping.findMethod(request.getMethod())) {
182             result = true;
183
184             ConstraintField fields[] = mapping.getConstraintFields();
185             // apply all the constraints
186
for (int i = 0; i < fields.length; i++) {
187                 if (!fields[i].applyConstraints(context, request)) {
188                     result = false;
189                     break;
190                 }
191             }
192         }
193         return result;
194     }
195
196     /** isRefreshNeeded: is the response to given request be refreshed?
197      * @param request incoming <code>HttpServletRequest</code> object
198      * @return <code>true</code> if the response needs to be refreshed.
199      * or return <code>false</code> if the results of this request
200      * don't need to be refreshed.
201      *
202      * XXX: 04/16/02 right now there is no configurability for this in
203      * ias-web.xml; should add a refresh-field element there:
204      * <refresh-field name="refresh" scope="request.parameter" />
205      */

206     public boolean isRefreshNeeded(HttpServletRequest JavaDoc request) {
207         boolean result = false;
208
209         // cache mapping associated with the request
210
CacheMapping mapping = lookupCacheMapping(request);
211         Field field = mapping.getRefreshField();
212         if (field != null) {
213             Object JavaDoc value = field.getValue(context, request);
214             // the field's string representation must be "true" or "false"
215
if (value != null && "true".equals(value.toString())) {
216                 result = true;
217             }
218         }
219         return result;
220     }
221
222     /** get timeout for the cacheable data in this request
223      * @param request incoming <code>HttpServletRequest</code> object
224      * @return either the statically specified value or from the request
225      * fields. If not specified, get the timeout defined for the
226      * cache element.
227      */

228     public int getTimeout(HttpServletRequest JavaDoc request) {
229         // cache mapping associated with the request
230
CacheMapping mapping = lookupCacheMapping(request);
231
232         // get the statically configured value, if any
233
int result = mapping.getTimeout();
234
235         // if the field is not defined, return the configured value
236
Field field = mapping.getTimeoutField();
237         if (field != null) {
238             Object JavaDoc value = field.getValue(context, request);
239             if (value != null) {
240                 try {
241                     // Integer type timeout object
242
Integer JavaDoc timeoutAttr = new Integer JavaDoc(value.toString());
243                     result = timeoutAttr.intValue();
244                 } catch (NumberFormatException JavaDoc cce) { }
245             }
246         }
247
248         // Note: this could be CacheHelper.TIMEOUT_NOT_SET
249
return result;
250     }
251
252     /**
253      * Stop this Context component.
254      * @exception Exception if a shutdown error occurs
255      */

256     public void destroy() throws Exception JavaDoc {
257     }
258 }
259
Popular Tags