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

CacheTag

public class CacheTag extends BodyTagSupport
CacheTag is a JSP tag that allows server-side caching of JSP page fragments. It lets you specify a timeout for how long the cached data is valid. It also gives you programmatic control over key generation, refreshing of the cache and whether the cached content should be served or not. Usage Example: <%@ taglib prefix="ias" uri="Sun ONE Application Server Tags" %> ... expensive operation ...

Fields Summary
private static final int
SECOND
Constants used to calculate the timeout
private static final int
MINUTE
private static final int
HOUR
private static final int
DAY
private String
_keyExpr
User specified key
private String
_key
The key into the cache. This is generated by suffixing the servlet path with the key if one is specified or by a generated suffix.
private int
_timeout
Timeout for the cache entry.
private boolean
_refreshCache
This boolean specifies whether the cache should be forcibly refreshed after the current request or not.
private boolean
_useCachedResponse
This boolean specifies whether the cached response must be sent or the body should be evaluated. The cache is not refreshed.
private int
_scope
This specifies the scope of the cache.
private com.sun.appserv.util.cache.Cache
_cache
The actual cache itself.
private static Logger
_logger
The logger to use for logging ALL web container related messages.
private static boolean
_debugLog
This indicates whether debug logging is on or not
private static ResourceBundle
_rb
The resource bundle containing the localized message strings.
Constructors Summary
public CacheTag()
Default constructor that simply gets a handle to the web container subsystem's logger.


    // ---------------------------------------------------------------------
    // Constructor and initialization

                      
      
        super();
        if (_logger == null) {
            _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
            _rb = _logger.getResourceBundle();
            _debugLog = _logger.isLoggable(Level.FINE);
        }
    
Methods Summary
public intdoAfterBody()
doAfterBody is called only if the body was evaluated. This would happen if nocache is specified in which case this should do nothing if there was no cached response in which case the response data is obtained from the bodyContent and cached if the response has expired in which case the cache is refreshed

throws
JspException the standard exception thrown
return
always returns SKIP_BODY since we dont do any iteration

        // if useCachedResponse, update the cache with the new response
        // data. If it is false, the body has already been evaluated and
        // sent out, nothing more to be done.
        if (_useCachedResponse) {
            if (bodyContent != null) {

                // get the response as a string from bodyContent
                // and cache it for the specified timeout period
                String content = bodyContent.getString().trim();

                CacheEntry entry = new CacheEntry(content, _timeout);
                _cache.put(_key, entry);

                // write to body content to the enclosing writer as well
                try {
                    bodyContent.writeOut(bodyContent.getEnclosingWriter());
                } catch (java.io.IOException ex) {
                    throw new JspException(ex);
                }
            }
        }
        return SKIP_BODY;
    
public intdoEndTag()
doEndTag just resets all the valiables in case the tag is reused

throws
JspException the standard exception thrown
return
always returns EVAL_PAGE since we want the entire jsp evaluated

        _key = null;
        _keyExpr = null;
        _timeout = Constants.DEFAULT_JSP_CACHE_TIMEOUT;
        _refreshCache = false;
        _useCachedResponse = true;
        _scope = PageContext.APPLICATION_SCOPE;
        _cache = null;

        return EVAL_PAGE;
    
public intdoStartTag()
doStartTag is called every time the cache tag is encountered. By the time this is called, the tag attributes are already set, but the tag body has not been evaluated. The cache key is generated here and the cache is obtained as well

throws
JspException the standard exception thrown
return
EVAL_BODY_INCLUDE when nocache is specified so that the tag body is just evaluated into the output stream SKIP_BODY if the cached response is valid in which case it is just written to the output stream, hence there is nothing more to be done. EVAL_BODY_BUFFERED is the default return value which ensures that the BodyContent is created and the tag body is evaluated into it.

        // default is EVAL_BODY_BUFFERED to ensure that BodyContent is created
        int ret = EVAL_BODY_BUFFERED;

        // generate the cache key using the user specified key. If no
        // key is specified, a position specific key suffix is used
        _key = CacheUtil.generateKey(_keyExpr, pageContext);

        if (_debugLog)
            _logger.fine("CacheTag["+ _key +"]: Timeout = "+ _timeout);

        // if useCachedResponse is false, we do not check for any
        // cached response and just evaluate the tag body
        if (_useCachedResponse) {

            _cache = CacheUtil.getCache(pageContext, _scope);
            if (_cache == null)
                throw new JspException(_rb.getString("taglibs.cache.nocache"));

            // if refreshCache is true, we want to re-evaluate the
            // tag body and refresh the cached entry
            if (_refreshCache == false) {

                // check if an entry is present for the given key
                // if it is, check if it has expired or not
                CacheEntry entry = (CacheEntry)_cache.get(_key);

                if (entry != null && entry.isValid()) {

                    // valid cached entry, get cached response and
                    // write it to the output stream
                    String content = entry.getContent();

                    try {
                        pageContext.getOut().write(content);
                    } catch (java.io.IOException ex) {
                        throw new JspException(ex);
                    }
                         
                    // since cached response is already written, skip
                    // evaluation of the tag body. This also means that
                    // doAfterBody wont get called
                    ret = SKIP_BODY;
                }
            }
        } else {
            // since we dont want to use the cached response, just
            // return EVAL_BODY_INCLUDE which will evaluate the body
            // into the output stream. This will mean that this tag
            // will be treated as an IterationTag and BodyContent is
            // not created
            ret = EVAL_BODY_INCLUDE;
        }

        return ret;
    
public voidsetKey(java.lang.String key)
This is used to set a user-defined key to store the response in the cache.

        if (key != null && key.length() > 0)
            _keyExpr = key;
    
public voidsetNocache(boolean noCache)
This attribute is used to programmatically enable or disable the use of the cached response. If noCache is true, then the cached response is not sent, instead the tag body is evaluated and sent out, the cache is not refreshed either.

        if (noCache)
            _useCachedResponse = false;
    
public voidsetRefresh(boolean refresh)
This attribute is used to programmatically refresh the cached response. If refresh is true, the cached response is not sent, instead the tag body is evaluated and sent and the cache is refreshed with the new response.

        _refreshCache = refresh;
    
public voidsetScope(java.lang.String scope)
Sets the scope of the cache.

param
scope the scope of the cache
throws
IllegalArgumentException if the specified scope is different from request, session, and application

        _scope = CacheUtil.convertScope(scope);
    
public voidsetTimeout(java.lang.String timeout)
This sets the time for which the cached response is valid. The cached entry is invalid after this time is past. If no unit is specified, then the timeout is assumed to be in seconds. A different unit can be specified by postfixing the timeout value with the desired unit: s=seconds, m=minutes, h=hours, d=days

        if (timeout != null) {
            try {
                _timeout = Integer.parseInt(timeout);
            } catch (NumberFormatException nfe) {
                // nfe indicated that the timeout has non-integers in it
                // try to parse it as 1sec, 1min, 1 hour and 1day formats
                int i = 0;
                while (i < timeout.length() &&
                       Character.isDigit(timeout.charAt(i)))
                    i++;

                if (i > 0) {
                    _timeout = Integer.parseInt(timeout.substring(0, i));

                    // mutiply timeout by the specified unit of time
                    char multiplier = timeout.charAt(i);
                    switch (multiplier) {
                        case 's" : _timeout *= SECOND;
                                   break;
                        case 'm" : _timeout *= MINUTE;
                                   break;
                        case 'h" : _timeout *= HOUR;
                                   break;
                        case 'd" : _timeout *= DAY;
                                   break;
                        default  : break;
                    }
                }
            }
        }