KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > CacheModule


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 package com.sun.enterprise.web;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28
29 import javax.servlet.ServletContext JavaDoc;
30
31 import org.apache.catalina.Context;
32 import org.apache.catalina.LifecycleException;
33 import org.apache.catalina.deploy.FilterDef;
34 import org.apache.catalina.deploy.FilterMap;
35
36 import com.sun.logging.LogDomains;
37
38 import com.sun.enterprise.config.ConfigBean;
39 import com.sun.enterprise.deployment.runtime.web.WebProperty;
40 import com.sun.enterprise.deployment.runtime.web.SunWebApp;
41 import com.sun.enterprise.deployment.runtime.web.Cache;
42 import com.sun.enterprise.deployment.runtime.web.CacheHelper;
43 import com.sun.enterprise.deployment.runtime.web.DefaultHelper;
44 // import com.sun.enterprise.tools.common.dd.webapp.CacheMapping;
45
// import com.sun.enterprise.tools.common.dd.webapp.ConstraintField;
46

47 import com.sun.appserv.web.cache.mapping.CacheMapping;
48 import com.sun.appserv.web.cache.mapping.ConstraintField;
49 import com.sun.appserv.web.cache.CacheManager;
50 import com.sun.appserv.web.cache.DefaultCacheHelper;
51 import com.sun.appserv.web.cache.mapping.Field;
52 import com.sun.appserv.web.cache.mapping.ValueConstraint;
53
54 /**
55  * configures the cache for the application
56  */

57 public final class CacheModule {
58     public final static String JavaDoc CACHING_FILTER_CLASSNAME =
59                             "com.sun.appserv.web.cache.filter.CachingFilter";
60     public final static String JavaDoc DEFAULT_CACHE_HELPER_CLASSNAME =
61                             "com.sun.appserv.web.cache.DefaultCacheHelper";
62
63     private static String JavaDoc trim(String JavaDoc str) {
64         if (str != null)
65             return str.trim();
66         return str;
67     }
68
69     /**
70      * configure ias-web response cache
71      * @param app WebModule containing the cache
72      * @param bean ias-web app config bean
73      * @throws Exception
74      *
75      * read the configuration and setup the runtime support for caching in a
76      * application.
77      */

78     public static CacheManager configureResponseCache(WebModule app,
79                                 SunWebApp bean) throws Exception JavaDoc {
80         Logger JavaDoc logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
81
82         Cache cacheConfig = bean.getCache();
83
84         // is cache configured?
85
if (cacheConfig == null) {
86             return null;
87         }
88
89         if (logger.isLoggable(Level.FINE)) {
90             logger.fine("configuring cache for web application " + app.getPath());
91         }
92
93         // create the CacheManager object for this app
94
CacheManager manager = new CacheManager();
95
96         String JavaDoc name, value;
97         value = cacheConfig.getAttributeValue(Cache.ENABLED);
98         if (value != null) {
99             boolean enabled = ConfigBean.toBoolean(value);
100             manager.setEnabled(enabled);
101         }
102
103         // set cache element's attributes and properties
104
value = cacheConfig.getAttributeValue(Cache.MAX_ENTRIES);
105         if (value != null) {
106             try {
107                 int maxEntries = Integer.parseInt(value.trim());
108                 manager.setMaxEntries(maxEntries);
109             } catch (NumberFormatException JavaDoc e) {
110                 // XXX need error message
111
throw new Exception JavaDoc("invalid max-entries", e);
112             }
113         }
114
115         value = cacheConfig.getAttributeValue(Cache.TIMEOUT_IN_SECONDS);
116         if (value != null) {
117             try {
118                 int defaultTimeout = Integer.parseInt(value.trim());
119                 manager.setDefaultTimeout(defaultTimeout);
120             } catch (NumberFormatException JavaDoc e) {
121                 // XXX need error message
122
throw new Exception JavaDoc("invalid timeout", e);
123             }
124         }
125
126         WebProperty[] props = cacheConfig.getWebProperty();
127         for (int i = 0; i < props.length; i++) {
128             name = props[i].getAttributeValue(WebProperty.NAME);
129             value = props[i].getAttributeValue(WebProperty.VALUE);
130
131             manager.addProperty(name, value);
132         }
133         
134         // configure the default cache-helper
135
DefaultHelper defHelperConfig = cacheConfig.getDefaultHelper();
136
137         HashMap JavaDoc map = new HashMap JavaDoc();
138         if (defHelperConfig != null) {
139             props = defHelperConfig.getWebProperty();
140             for (int i = 0; i < props.length; i++) {
141                 name = props[i].getAttributeValue(WebProperty.NAME);
142                 value = props[i].getAttributeValue(WebProperty.VALUE);
143
144                 map.put(name, value);
145             }
146         }
147         manager.setDefaultHelperProps(map);
148
149         // configure custom cache-helper classes
150
for (int i = 0; i < cacheConfig.sizeCacheHelper(); i++) {
151             CacheHelper helperConfig = cacheConfig.getCacheHelper(i);
152
153             String JavaDoc helperName = helperConfig.getAttributeValue(
154                 CacheHelper.NAME);
155             HashMap JavaDoc helperProps = new HashMap JavaDoc();
156             props = helperConfig.getWebProperty();
157             for (int j = 0; j < props.length; j++) {
158                 name = props[i].getAttributeValue(WebProperty.NAME);
159                 value = props[i].getAttributeValue(WebProperty.VALUE);
160
161                 helperProps.put(name, value);
162             }
163             helperProps.put("class-name",
164                             helperConfig.getAttributeValue(
165                             CacheHelper.CLASS_NAME));
166
167             manager.addCacheHelperDef(helperName, helperProps);
168         }
169
170         // for each cache-mapping, create CacheMapping, setup the filter
171
for (int i = 0; i < cacheConfig.sizeCacheMapping(); i++) {
172             com.sun.enterprise.deployment.runtime.web.CacheMapping
173                             mapConfig = cacheConfig.getCacheMapping(i);
174             
175             CacheMapping mapping = new CacheMapping();
176             configureCacheMapping(mapConfig, mapping, logger);
177
178             // use filter's name to refer to setup the filter
179
String JavaDoc filterName = CACHING_FILTER_CLASSNAME + i;
180
181             /**
182              * all cache-mapings are indexed by the unique filter-name;
183              * DefaultCacheHelper uses this name to access the mapping.
184              */

185             manager.addCacheMapping(filterName, mapping);
186
187             // setup the ias CachingFilter definition with the context
188
FilterDef filterDef = new FilterDef();
189             filterDef.setFilterName(filterName);
190             filterDef.setFilterClass(CACHING_FILTER_CLASSNAME);
191
192             filterDef.addInitParameter("servletName", mapping.getServletName());
193             filterDef.addInitParameter("URLPattern", mapping.getURLPattern());
194             app.addFilterDef(filterDef);
195
196             // setup the mapping for the specified servlet-name or url-pattern
197
FilterMap filterMap = new FilterMap();
198             filterMap.setServletName(mapping.getServletName());
199             filterMap.setURLPattern(mapping.getURLPattern());
200             String JavaDoc[] dispatchers = mapConfig.getDispatcher();
201             for (int j=0; dispatchers!=null && j<dispatchers.length; j++) {
202                 // calls to FilterMap.setDispatcher are cumulative
203
filterMap.setDispatcher(dispatchers[j]);
204             }
205             filterMap.setFilterName(filterName);
206             app.addFilterMap(filterMap);
207
208             if (logger.isLoggable(Level.FINE)) {
209                 logger.fine("added a caching filter for servlet-name = " + mapping.getServletName() + " url-pattern = " + mapping.getURLPattern());
210             }
211         }
212         
213         manager.setServletContext(app.getServletContext());
214         return manager;
215     }
216
217     /**
218      * configure ias-web cache-mapping
219      * @param Catalina context
220      * @param bean ias-web app cache-mapping config bean
221      * @throws Exception
222      *
223      */

224     private static void configureCacheMapping(
225             com.sun.enterprise.deployment.runtime.web.CacheMapping mapConfig,
226             CacheMapping mapping,
227             Logger JavaDoc logger) throws Exception JavaDoc {
228         String JavaDoc name, scope, value, expr;
229
230         /**
231          * <cache-mapping ((servlet-name|url-pattern)..)
232          */

233         mapping.setServletName(trim(mapConfig.getServletName()));
234         mapping.setURLPattern(trim(mapConfig.getUrlPattern()));
235
236         // resolve the helper for this mapping
237
String JavaDoc helperRef = mapConfig.getCacheHelperRef();
238         if (helperRef == null) {
239             helperRef = "default";
240         }
241         mapping.setHelperNameRef(helperRef);
242
243         /**
244          * <timeout>600</timeout>
245          * <timeout name="cacheTimeout" scope="request.attribute" />
246          */

247         value = mapConfig.getTimeout();
248         if (value != null) {
249             try {
250                 mapping.setTimeout(Integer.parseInt(value.trim()));
251             } catch (NumberFormatException JavaDoc e) {
252                 throw new Exception JavaDoc("invalid timeout", e);
253             }
254         } else {
255             // XXX: get the timeout as a field?
256
name = mapConfig.getAttributeValue(
257                 com.sun.enterprise.deployment.runtime.web.CacheMapping.TIMEOUT,
258                 com.sun.enterprise.deployment.runtime.web.CacheMapping.NAME);
259             scope = mapConfig.getAttributeValue(
260                 com.sun.enterprise.deployment.runtime.web.CacheMapping.TIMEOUT,
261                 com.sun.enterprise.deployment.runtime.web.CacheMapping.SCOPE);
262             if (name != null && scope != null)
263                 mapping.setTimeoutField(new Field(name, scope));
264         }
265
266         /**
267          * <refresh-field name="refreshNow" scope="request.attribute" />
268          */

269
270         name = mapConfig.getAttributeValue(
271             com.sun.enterprise.deployment.runtime.web.CacheMapping.REFRESH_FIELD,
272             com.sun.enterprise.deployment.runtime.web.CacheMapping.NAME);
273         scope = mapConfig.getAttributeValue(
274             com.sun.enterprise.deployment.runtime.web.CacheMapping.REFRESH_FIELD,
275             com.sun.enterprise.deployment.runtime.web.CacheMapping.SCOPE);
276         if (name != null && scope != null) {
277             Field refreshField = new Field(name, scope);
278             mapping.setRefreshField(refreshField);
279         }
280
281         /** <http-method> GET </http-method>
282          * <http-method> POST </http-method>
283          */

284         if (mapConfig.sizeHttpMethod() > 0) {
285             mapping.setMethods(mapConfig.getHttpMethod());
286         }
287
288         /**
289          * <key-field name="foo" scope="request.parameter"/>
290          */

291         for (int i = 0; i < mapConfig.sizeKeyField(); i++) {
292             name = mapConfig.getAttributeValue(
293                 com.sun.enterprise.deployment.runtime.web.CacheMapping.KEY_FIELD,
294                 i, com.sun.enterprise.deployment.runtime.web.CacheMapping.NAME);
295             scope = mapConfig.getAttributeValue(
296                 com.sun.enterprise.deployment.runtime.web.CacheMapping.KEY_FIELD,
297                 i, com.sun.enterprise.deployment.runtime.web.CacheMapping.SCOPE);
298             if (name != null && scope != null) {
299                 mapping.addKeyField(new Field(name, scope));
300
301                 if (logger.isLoggable(Level.FINE)) {
302                     logger.fine("added a key-field : name = " + name
303                                 + " scope = " + scope);
304                 }
305             }
306         }
307
308         /**
309          * <constraint-field name="foo" scope="request.parameter">
310          * <value match-expr="equals"> 200 </value>
311          */

312         for (int i = 0; i < mapConfig.sizeConstraintField(); i++) {
313             com.sun.enterprise.deployment.runtime.web.ConstraintField
314                                 fieldConfig = mapConfig.getConstraintField(i);
315
316             name = fieldConfig.getAttributeValue(
317                 com.sun.enterprise.deployment.runtime.web.ConstraintField.NAME);
318             scope = fieldConfig.getAttributeValue(
319                 com.sun.enterprise.deployment.runtime.web.ConstraintField.SCOPE);
320             ConstraintField constraintField =
321                                         new ConstraintField(name, scope);
322
323             value = fieldConfig.getAttributeValue(com.sun.enterprise.deployment.runtime.web.ConstraintField.CACHE_ON_MATCH);
324             if (value != null)
325                 constraintField.setCacheOnMatch(ConfigBean.toBoolean(value));
326
327             value = fieldConfig.getAttributeValue(com.sun.enterprise.deployment.runtime.web.ConstraintField.CACHE_ON_MATCH_FAILURE);
328             if (value != null)
329                 constraintField.setCacheOnMatchFailure(
330                                     ConfigBean.toBoolean(value));
331
332
333             // now set the value's and the match expressions
334
for (int j = 0; j < fieldConfig.sizeValue(); j++) {
335                 value = fieldConfig.getValue(j).trim();
336                 expr = fieldConfig.getAttributeValue(
337                     com.sun.enterprise.deployment.runtime.web.ConstraintField.VALUE, j, com.sun.enterprise.deployment.runtime.web.ConstraintField.MATCH_EXPR);
338                 
339                 ValueConstraint constraint = new ValueConstraint(value, expr);
340                 value = fieldConfig.getAttributeValue(com.sun.enterprise.deployment.runtime.web.ConstraintField.VALUE, j, com.sun.enterprise.deployment.runtime.web.ConstraintField.CACHE_ON_MATCH);
341                 if (value != null) {
342                     constraint.setCacheOnMatch(ConfigBean.toBoolean(value));
343                 }
344                 value = fieldConfig.getAttributeValue(com.sun.enterprise.deployment.runtime.web.ConstraintField.VALUE, j, com.sun.enterprise.deployment.runtime.web.ConstraintField.CACHE_ON_MATCH_FAILURE);
345                 if (value != null) {
346                     constraint.setCacheOnMatchFailure(
347                                     ConfigBean.toBoolean(value));
348                 }
349                 constraintField.addConstraint(constraint);
350
351                 if (logger.isLoggable(Level.FINE)) {
352                     logger.fine("added a constraint: " + constraint.toString());
353                 }
354             }
355
356             mapping.addConstraintField(constraintField);
357
358             if (logger.isLoggable(Level.FINE)) {
359                 logger.fine("added a constraint-field name = " + name + " scope = " + scope + " cache-on-match = " + constraintField.getCacheOnMatch() + " cache-on-match-failure = " + constraintField.getCacheOnMatchFailure());
360             }
361         }
362     }
363 }
364
Popular Tags