1 23 24 package com.sun.appserv.web.taglibs.cache; 25 26 import java.util.ResourceBundle ; 27 import java.util.logging.Logger ; 28 import java.util.logging.Level ; 29 30 import javax.servlet.jsp.*; 31 import javax.servlet.jsp.tagext.*; 32 33 import com.sun.enterprise.web.logging.pwc.LogDomains; 34 35 import com.sun.appserv.util.cache.Cache; 36 37 51 public class CacheTag extends BodyTagSupport 52 { 53 56 private static final int SECOND = 1; 57 private static final int MINUTE = 60 * SECOND; 58 private static final int HOUR = 60 * MINUTE; 59 private static final int DAY = 24 * HOUR; 60 61 64 private String _keyExpr; 65 66 70 private String _key; 71 72 75 private int _timeout = Constants.DEFAULT_JSP_CACHE_TIMEOUT; 76 77 81 private boolean _refreshCache = false; 82 83 87 private boolean _useCachedResponse = true; 88 89 92 private int _scope = PageContext.APPLICATION_SCOPE; 93 94 97 private Cache _cache; 98 99 102 private static Logger _logger = null; 103 104 107 private static boolean _debugLog; 108 109 112 private static ResourceBundle _rb = null; 113 114 117 121 public CacheTag() { 122 super(); 123 if (_logger == null) { 124 _logger = LogDomains.getLogger(LogDomains.PWC_LOGGER); 125 _rb = _logger.getResourceBundle(); 126 _debugLog = _logger.isLoggable(Level.FINE); 127 } 128 } 129 130 133 149 public int doStartTag() 150 throws JspException 151 { 152 int ret = EVAL_BODY_BUFFERED; 154 155 _key = CacheUtil.generateKey(_keyExpr, pageContext); 158 159 if (_debugLog) 160 _logger.fine("CacheTag["+ _key +"]: Timeout = "+ _timeout); 161 162 if (_useCachedResponse) { 165 166 _cache = CacheUtil.getCache(pageContext, _scope); 167 if (_cache == null) 168 throw new JspException(_rb.getString("taglibs.cache.nocache")); 169 170 if (_refreshCache == false) { 173 174 CacheEntry entry = (CacheEntry)_cache.get(_key); 177 178 if (entry != null && entry.isValid()) { 179 180 String content = entry.getContent(); 183 184 try { 185 pageContext.getOut().write(content); 186 } catch (java.io.IOException ex) { 187 throw new JspException(ex); 188 } 189 190 ret = SKIP_BODY; 194 } 195 } 196 } else { 197 ret = EVAL_BODY_INCLUDE; 203 } 204 205 return ret; 206 } 207 208 218 public int doAfterBody() 219 throws JspException 220 { 221 if (_useCachedResponse) { 225 if (bodyContent != null) { 226 227 String content = bodyContent.getString().trim(); 230 231 CacheEntry entry = new CacheEntry(content, _timeout); 232 _cache.put(_key, entry); 233 234 try { 236 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 237 } catch (java.io.IOException ex) { 238 throw new JspException(ex); 239 } 240 } 241 } 242 return SKIP_BODY; 243 } 244 245 251 public int doEndTag() 252 throws JspException 253 { 254 _key = null; 255 _keyExpr = null; 256 _timeout = Constants.DEFAULT_JSP_CACHE_TIMEOUT; 257 _refreshCache = false; 258 _useCachedResponse = true; 259 _scope = PageContext.APPLICATION_SCOPE; 260 _cache = null; 261 262 return EVAL_PAGE; 263 } 264 265 268 272 public void setKey(String key) { 273 if (key != null && key.length() > 0) 274 _keyExpr = key; 275 } 276 277 285 public void setTimeout(String timeout) { 286 if (timeout != null) { 287 try { 288 _timeout = Integer.parseInt(timeout); 289 } catch (NumberFormatException nfe) { 290 int i = 0; 293 while (i < timeout.length() && 294 Character.isDigit(timeout.charAt(i))) 295 i++; 296 297 if (i > 0) { 298 _timeout = Integer.parseInt(timeout.substring(0, i)); 299 300 char multiplier = timeout.charAt(i); 302 switch (multiplier) { 303 case 's' : _timeout *= SECOND; 304 break; 305 case 'm' : _timeout *= MINUTE; 306 break; 307 case 'h' : _timeout *= HOUR; 308 break; 309 case 'd' : _timeout *= DAY; 310 break; 311 default : break; 312 } 313 } 314 } 315 } 316 } 317 318 325 public void setNocache(boolean noCache) { 326 if (noCache) 327 _useCachedResponse = false; 328 } 329 330 337 public void setRefresh(boolean refresh) { 338 _refreshCache = refresh; 339 } 340 341 349 public void setScope(String scope) { 350 _scope = CacheUtil.convertScope(scope); 351 } 352 } 353 | Popular Tags |