KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > web > taglibs > cache > CacheRequestListener


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.taglibs.cache;
25
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.ServletRequestEvent JavaDoc;
29 import javax.servlet.ServletRequestListener JavaDoc;
30
31 import com.sun.appserv.util.cache.Cache;
32 import com.sun.appserv.web.cache.CacheManager;
33
34 /**
35  * ServletRequestListener which creates a cache for JSP tag body invocations
36  * and adds it as a request attribute in response to requestInitialized
37  * events, and clears the cache in response to requestDestroyed events.
38  */

39 public class CacheRequestListener implements ServletRequestListener JavaDoc {
40
41     /**
42      * No-arg constructor
43      */

44     public CacheRequestListener() {}
45
46
47     /**
48      * Receives notification that the request is about to enter the scope
49      * of the web application, and adds newly created cache for JSP tag
50      * body invocations as a request attribute.
51      *
52      * @param sre the notification event
53      */

54     public void requestInitialized(ServletRequestEvent JavaDoc sre) {
55
56         ServletContext JavaDoc context = sre.getServletContext();
57
58         // Check if a cache manager has already been created and set in the
59
// context
60
CacheManager cm = (CacheManager)
61             context.getAttribute(CacheManager.CACHE_MANAGER_ATTR_NAME);
62
63         // Create a new cache manager if one is not present and use it
64
// to create a new cache
65
if (cm == null) {
66             cm = new CacheManager();
67         }
68
69         Cache cache = null;
70         try {
71             cache = cm.createCache();
72         } catch (Exception JavaDoc ex) {}
73
74         // Set the cache as a request attribute
75
if (cache != null) {
76             ServletRequest JavaDoc req = sre.getServletRequest();
77             req.setAttribute(Constants.JSPTAG_CACHE_KEY, cache);
78         }
79     }
80
81
82     /**
83      * Receives notification that the request is about to go out of scope
84      * of the web application, and clears the request's cache of JSP tag
85      * body invocations (if present).
86      *
87      * @param sre the notification event
88      */

89     public void requestDestroyed(ServletRequestEvent JavaDoc sre) {
90
91         // Clear the cache
92
ServletRequest JavaDoc req = sre.getServletRequest();
93         Cache cache = (Cache) req.getAttribute(Constants.JSPTAG_CACHE_KEY);
94         if (cache != null) {
95             cache.clear();
96         }
97     }
98 }
99
Popular Tags