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

DefaultCacheHelper

public class DefaultCacheHelper extends Object implements CacheHelper
DefaultCacheHelper interface is the built-in implementation of the CacheHelper interface to aide in: a) the key generation b) whether to cache the response. There is one CacheHelper instance per web application.

Fields Summary
private static final String[]
KEY_PREFIXES
public static final String
ATTR_CACHING_FILTER_NAME
public static final String
PROP_KEY_GENERATOR_ATTR_NAME
private static Logger
_logger
private static boolean
_isTraceEnabled
private static ResourceBundle
_rb
The resource bundle containing the localized message strings.
ServletContext
context
CacheManager
manager
String
attrKeyGenerator
boolean
isKeyGeneratorChecked
CacheKeyGenerator
keyGenerator
Constructors Summary
Methods Summary
public voiddestroy()
Stop this Context component.

exception
Exception if a shutdown error occurs

    
public java.lang.StringgetCacheKey(javax.servlet.http.HttpServletRequest request)
getCacheKey: generate the key to be used to cache this request

param
request incoming HttpServletRequest
return
key string used to access the cache entry. Key is composed of: servletPath + a concatenation of the field values in the request; all key field names must be found in the appropriate scope.


        // cache mapping associated with the request
        CacheMapping mapping = lookupCacheMapping(request);

        if (isKeyGeneratorChecked == false && attrKeyGenerator != null) {
            try {
                keyGenerator = (CacheKeyGenerator) 
                                context.getAttribute(attrKeyGenerator);
            } catch (ClassCastException cce){
                _logger.log(Level.WARNING, "cache.defaultHelp.illegalKeyGenerator", cce);
            }

            isKeyGeneratorChecked = true;
        }

        if (keyGenerator != null) {
            String key = keyGenerator.getCacheKey(context, request); 
            if (key != null)
                return key;
        }

        StringBuffer sb = new StringBuffer(128);

        /** XXX: the StringBuffer append is a (uncontended) synchronized method. 
         *  performance hit?
         */
        sb.append(request.getServletPath());

        // append the key fields
        Field[] keys = mapping.getKeyFields();
        for (int i = 0; i < keys.length; i++) {
            Object value = keys[i].getValue(context, request);

            // all defined key field must be present
            if (value == null) {
                if (_isTraceEnabled) {
                    _logger.fine("DefaultCacheHelper: cannot find all the required key fields in the request " + request.getServletPath());
                }
                return null;
            }

            sb.append(";");
            sb.append(KEY_PREFIXES[keys[i].getScope()]);
            sb.append(keys[i].getName());
            sb.append("=");
            sb.append(value);
        }

        return sb.toString();
    
public intgetTimeout(javax.servlet.http.HttpServletRequest request)
get timeout for the cacheable data in this request

param
request incoming HttpServletRequest object
return
either the statically specified value or from the request fields. If not specified, get the timeout defined for the cache element.

        // cache mapping associated with the request
        CacheMapping mapping = lookupCacheMapping(request);

        // get the statically configured value, if any
        int result = mapping.getTimeout();

        // if the field is not defined, return the configured value
        Field field = mapping.getTimeoutField();
        if (field != null) {
            Object value = field.getValue(context, request);
            if (value != null) {
                try {
                    // Integer type timeout object
                    Integer timeoutAttr = Integer.valueOf(value.toString());
                    result = timeoutAttr.intValue();
                } catch (NumberFormatException cce) { }
            }
        }

        // Note: this could be CacheHelper.TIMEOUT_NOT_SET
        return result;
    
public voidinit(javax.servlet.ServletContext context, java.util.Map props)
initialize this helper

param
context the web application context this helper belongs to
param
props helper properties

        this.context = context;
        attrKeyGenerator = (String)props.get(PROP_KEY_GENERATOR_ATTR_NAME);

        // web container logger
        _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
        _isTraceEnabled = _logger.isLoggable(Level.FINE);
        _rb = _logger.getResourceBundle();
    
public booleanisCacheable(javax.servlet.http.HttpServletRequest request)
isCacheable: is the response to given request cachebale?

param
request incoming HttpServletRequest object
return
true if the response could be cached. or return false if the results of this request must not be cached. Applies pre-configured cacheability constraints in the cache-mapping; all constraints must pass for this to be cacheable.

        boolean result = false;

        // cache mapping associated with the request
        CacheMapping mapping = lookupCacheMapping(request);

        // check if the method is in the allowed methods list
        if (mapping.findMethod(request.getMethod())) {
            result = true;

            ConstraintField fields[] = mapping.getConstraintFields();
            // apply all the constraints
            for (int i = 0; i < fields.length; i++) {
                if (!fields[i].applyConstraints(context, request)) {
                    result = false;
                    break;
                }
            }
        }
        return result;
    
public booleanisRefreshNeeded(javax.servlet.http.HttpServletRequest request)
isRefreshNeeded: is the response to given request be refreshed?

param
request incoming HttpServletRequest object
return
true if the response needs to be refreshed. or return false if the results of this request don't need to be refreshed. XXX: 04/16/02 right now there is no configurability for this in ias-web.xml; should add a refresh-field element there:

        boolean result = false;

        // cache mapping associated with the request
        CacheMapping mapping = lookupCacheMapping(request);
        Field field = mapping.getRefreshField();
        if (field != null) {
            Object value = field.getValue(context, request);
            // the field's string representation must be "true" or "false"
            if (value != null && "true".equals(value.toString())) {
                result = true;
            }
        }
        return result;
    
private com.sun.appserv.web.cache.mapping.CacheMappinglookupCacheMapping(javax.servlet.http.HttpServletRequest request)
cache-mapping for this servlet-name or the URLpattern

param
request incoming request
return
cache-mapping object; uses servlet name or the URL pattern to lookup in the CacheManager for the mapping.

        String name = (String)request.getAttribute(ATTR_CACHING_FILTER_NAME);
        return manager.getCacheMapping(name);
    
public voidsetCacheManager(CacheManager manager)
set the CacheManager for this application

param
manager associated with this application


                     
        
        this.manager = manager;