FileDocCategorySizeDatePackage
CacheManager.javaAPI DocGlassfish v2 API13472Fri May 04 22:35:26 BST 2007com.sun.appserv.web.cache

CacheManager

public class CacheManager extends Object

Fields Summary
public static final String
CACHE_MANAGER_ATTR_NAME
public static final int
DEFAULT_CACHE_MAX_ENTRIES
public static final int
DEFAULT_CACHE_TIMEOUT
public static final String
DEFAULT_CACHE_CLASSNAME
private static Logger
_logger
private static ResourceBundle
_rb
The resource bundle containing the localized message strings.
int
maxEntries
int
defaultTimeout
String
cacheClassName
boolean
enabled
ServletContext
context
Properties
cacheProps
com.sun.appserv.util.cache.Cache
defaultCache
HashMap
cacheMappings
Map
defaultHelperProps
DefaultCacheHelper
defaultHelper
HashMap
helperDefs
HashMap
cacheHelpers
HashMap
cacheHelpersByFilterName
ArrayList
listeners
Constructors Summary
public CacheManager()
default constructor


           
       
Methods Summary
public voidaddCacheHelperDef(java.lang.String name, java.util.HashMap helperDef)
add a CacheHelper definition

param
name CacheHelper name
param
helperDef CacheHelper definition

        helperDefs.put(name, helperDef);
    
public voidaddCacheManagerListener(CacheManagerListener listener)
add CacheManagerListener object

param
listener CacheManagerListener object

        synchronized (listeners) {
            listeners.add(listener);
        }
    
public voidaddCacheMapping(java.lang.String name, com.sun.appserv.web.cache.mapping.CacheMapping mapping)
add cache mapping

param
name unique name of the mapping
param
mapping CacheMapping

        cacheMappings.put(name, mapping);
    
public voidaddProperty(java.lang.String name, java.lang.String value)
add generic property

param
name named property
param
value value

        if (name.equalsIgnoreCase("cacheClassName")) {
           cacheClassName = value; 
        } else {
            if (cacheProps == null) {
                cacheProps = new Properties();
            }
            cacheProps.setProperty(name, value);
        }
    
public com.sun.appserv.util.cache.CachecreateCache()
create the designated cache object

return
the Cache implementation
throws
Exception

        return createCache(maxEntries, DEFAULT_CACHE_CLASSNAME);
    
public com.sun.appserv.util.cache.CachecreateCache(int cacacity, java.lang.String className)
create the designated cache object

return
the Cache implementation
throws
Exception


        // use the context class loader to load class so that any
        // user-defined classes in WEB-INF can also be loaded.
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Class cacheClass = cl.loadClass(className);

        Cache cacheImpl = (Cache)cacheClass.newInstance();
        cacheImpl.init(maxEntries, cacheProps);

        return cacheImpl;
    
public voiddisable()
enable the cache manager (and all the listeners)

        for (int i = 0; i < listeners.size(); i++) {
            CacheManagerListener listener = (CacheManagerListener) 
                                    listeners.get(i);
            listener.cacheManagerDisabled();
        }
    
public voidenable()
enable the cache manager (and all the listeners)

        for (int i = 0; i < listeners.size(); i++) {
            CacheManagerListener listener = (CacheManagerListener) 
                                        listeners.get(i);
            listener.cacheManagerEnabled();
        }
    
public java.lang.StringgetCacheClassName()
get the underlying cache name

return
the cacheClassName

        return cacheClassName;
    
public CacheHelpergetCacheHelper(java.lang.String name)
get the helper by name

param
name name of the cache-helper
return
CacheHelper implementation

        return (CacheHelper)cacheHelpers.get(name);
    
public CacheHelpergetCacheHelperByFilterName(java.lang.String filterName)
get the helper by filter name

param
filterName filter name
return
CacheHelper implementation

        return (CacheHelper)cacheHelpersByFilterName.get(filterName);
    
public com.sun.appserv.web.cache.mapping.CacheMappinggetCacheMapping(java.lang.String name)
get cacheMapping given its name

param
name name identifying the mapping
return
CacheMapping

        return (CacheMapping)cacheMappings.get(name);
    
public com.sun.appserv.util.cache.CachegetDefaultCache()
get the default application-wide cache

return
cache object

        return defaultCache;
    
public intgetDefaultTimeout()
get the application wide default cache expiry timeout

return
timeout in seconds

        return defaultTimeout;
    
public booleanisEnabled()

return
whether this is enabled

        return enabled;
    
private CacheHelperloadCacheHelper(java.lang.String className)
load the helper class

param
className of the helper
return
CacheHelper instance


        // use the context class loader to load class so that any
        // user-defined classes in WEB-INF can also be loaded.
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Class helperClass = cl.loadClass(className);

        CacheHelper helper = (CacheHelper) helperClass.newInstance();

        return helper;
    
public voidremoveCacheManagerListener(CacheManagerListener listener)
remove CacheManagerListener object

param
listener CacheManagerListener object

        synchronized (listeners) {
            listeners.remove(listener);
        }
    
public voidsetDefaultHelperProps(java.util.Map map)
set the default-helper's properties

param
map a HashMap of properties

        this.defaultHelperProps = map;
    
public voidsetDefaultTimeout(int defaultTimeout)
set the defaultTimeout of this cache

param
defaultTimeout in seconds

        this.defaultTimeout = defaultTimeout;
    
public voidsetEnabled(boolean enabled)
set the whether this is enabled

param
enabled is this enabled?

        this.enabled = enabled;
    
public voidsetMaxEntries(int maxEntries)
set the maximum number of entries of this cache

param
maxEntries number of entries the cache should carry

        this.maxEntries = maxEntries;
    
public voidsetServletContext(javax.servlet.ServletContext context)
set the ServletContext of this application

param
context ServletContext

        this.context = context;
    
public voidstart()
Start this Context component.

exception
LifecycleException if a startup error occurs


        if (!enabled)
            return;

        // web container logger
        _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
        _rb = _logger.getResourceBundle();

        // create the default cache
        try {
            defaultCache = createCache(maxEntries, cacheClassName);
        } catch (Exception e) {
            _logger.log(Level.WARNING, "cache.manager.excep_createCache", e);

            String msg = _rb.getString("cache.manager.excep_createCache");
            throw new LifecycleException(msg, e);
        }

        // initialize the "default" helper
        defaultHelper = new DefaultCacheHelper();
        defaultHelper.setCacheManager(this);
        defaultHelper.init(context, defaultHelperProps);

        // initialize the custom cache-helpers
        Iterator helperNames = helperDefs.keySet().iterator();
        while(helperNames.hasNext()) {
            String name = (String) helperNames.next();
            HashMap map = (HashMap)helperDefs.get(name);

            try {
                String className = (String)map.get("class-name");
                CacheHelper helper = loadCacheHelper(className);
                helper.init(context, map);
                cacheHelpers.put(name, helper);

            } catch (Exception e) {
                String msg = _rb.getString("cache.manager.excep_initCacheHelper");
                Object[] params = { name };
                msg = MessageFormat.format(msg, params);

                throw new LifecycleException(msg, e);
            }
        }

        // cache-mappings are ordered by the associated filter name
        Iterator filterNames = cacheMappings.keySet().iterator();
        while(filterNames.hasNext()) {
            String name = (String) filterNames.next();
            CacheMapping mapping = (CacheMapping)cacheMappings.get(name);

            String helperNameRef = mapping.getHelperNameRef();
            CacheHelper helper;
            if (helperNameRef == null || helperNameRef.equals("default")) {
                helper = defaultHelper;
            } else {
                helper = (CacheHelper) cacheHelpers.get(helperNameRef);
            }
            cacheHelpersByFilterName.put(name, helper);
        }
    
public voidstop()
Stop this Context component. destroy all the caches created and flush/clear the cached content

exception
LifecycleException if a shutdown error occurs

        disable();

        try {
            defaultHelper.destroy();
        } catch (Exception e) {
            // XXX: ignore
        }

        // destroy the cache-helpers
        Enumeration helpers = Collections.enumeration(cacheHelpers.values());
        while(helpers.hasMoreElements()) {
            CacheHelper cacheHelper = (CacheHelper)helpers.nextElement();
            try {
                cacheHelper.destroy();
            } catch (Exception e) {
                // XXX: ignore
            }
        } 
        cacheHelpers.clear();
        cacheMappings.clear();
        cacheHelpersByFilterName.clear();
        listeners.clear();