KickJava   Java API By Example, From Geeks To Geeks.

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


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.ServletContextEvent JavaDoc;
28 import javax.servlet.ServletContextListener JavaDoc;
29
30 import com.sun.appserv.util.cache.Cache;
31 import com.sun.appserv.web.cache.CacheManager;
32
33 /**
34  * CacheContextListener implements the ServletContextListener interface
35  * in order to be notified when the context is created and destroyed.
36  * It is used to create the cache and add it as a context attribute.
37  */

38 public class CacheContextListener implements ServletContextListener JavaDoc
39 {
40     /**
41      * Public constructor taking no arguments according to servlet spec
42      */

43     public CacheContextListener() {}
44
45     /**
46      * This is called when the context is created.
47      */

48     public void contextInitialized(ServletContextEvent JavaDoc sce) {
49         ServletContext JavaDoc context = sce.getServletContext();
50
51         // see if a cache manager is already created and set in the context
52
CacheManager cm = (CacheManager)context.getAttribute(CacheManager.CACHE_MANAGER_ATTR_NAME);
53
54         // create a new cachemanager if one is not present and use it
55
// to create a new cache
56
if (cm == null)
57             cm = new CacheManager();
58
59         Cache cache = null;
60         try {
61             cache = cm.createCache();
62         } catch (Exception JavaDoc ex) {}
63
64         // set the cache as a context attribute
65
if (cache != null)
66             context.setAttribute(Constants.JSPTAG_CACHE_KEY, cache);
67     }
68
69     /**
70      * This is called when the context is shutdown.
71      */

72     public void contextDestroyed(ServletContextEvent JavaDoc sce) {
73         ServletContext JavaDoc context = sce.getServletContext();
74
75         // Remove the cache from context and clear the cache
76
Cache cache = (Cache)context.getAttribute(Constants.JSPTAG_CACHE_KEY);
77
78         if (cache != null) {
79             context.removeAttribute(Constants.JSPTAG_CACHE_KEY);
80             cache.clear();
81         }
82     }
83 }
84
Popular Tags