FileDocCategorySizeDatePackage
CacheContextListener.javaAPI DocGlassfish v2 API3880Fri May 04 22:35:28 BST 2007com.sun.appserv.web.taglibs.cache

CacheContextListener

public class CacheContextListener extends Object implements ServletContextListener
CacheContextListener implements the ServletContextListener interface in order to be notified when the context is created and destroyed. It is used to create the cache and add it as a context attribute.

Fields Summary
Constructors Summary
public CacheContextListener()
Public constructor taking no arguments according to servlet spec

Methods Summary
public voidcontextDestroyed(javax.servlet.ServletContextEvent sce)
This is called when the context is shutdown.

        ServletContext context = sce.getServletContext();

        // Remove the cache from context and clear the cache
        Cache cache = (Cache)context.getAttribute(Constants.JSPTAG_CACHE_KEY);

        if (cache != null) {
            context.removeAttribute(Constants.JSPTAG_CACHE_KEY);
            cache.clear();
        }
    
public voidcontextInitialized(javax.servlet.ServletContextEvent sce)
This is called when the context is created.

        ServletContext context = sce.getServletContext();

        // see if a cache manager is already created and set in the context
        CacheManager cm = (CacheManager)context.getAttribute(CacheManager.CACHE_MANAGER_ATTR_NAME);

        // create a new cachemanager if one is not present and use it
        // to create a new cache
        if (cm == null)
            cm = new CacheManager();

        Cache cache = null;
        try {
            cache = cm.createCache();
        } catch (Exception ex) {}

        // set the cache as a context attribute
        if (cache != null)
            context.setAttribute(Constants.JSPTAG_CACHE_KEY, cache);