1 16 17 package org.apache.taglibs.cache; 18 19 import javax.servlet.jsp.*; 20 import javax.servlet.jsp.tagext.*; 21 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; 22 23 28 29 public class CacheTag extends BodyTagSupport { 30 31 34 private String nameExpr; private String keyExpr; 37 private int scope; private String name, key; 40 private LRUCache cache; private String cached; 43 46 public int doStartTag() throws JspException { 47 evaluateExpressions(); 48 cache = CacheUtil.getCache(scope, name, pageContext); 49 cached = cache.get(key); 50 if (cached != null) 51 return SKIP_BODY; 52 else 53 return EVAL_BODY_BUFFERED; 54 } 55 56 public int doEndTag() throws JspException { 57 try { 58 String s = cached; 59 if (s == null) { 60 if (bodyContent == null || bodyContent.getString() == null) 61 s = ""; 62 else 63 s = bodyContent.getString().trim(); 64 cache.put(key, s); 65 } 66 pageContext.getOut().write(s); 67 } catch (java.io.IOException ex) { 68 throw new JspException(ex); 69 } 70 return EVAL_PAGE; 71 } 72 73 76 public void setScope(String scope) { 77 if (scope.equalsIgnoreCase("page")) 78 this.scope = PageContext.PAGE_SCOPE; 79 else if (scope.equalsIgnoreCase("request")) 80 this.scope = PageContext.REQUEST_SCOPE; 81 else if (scope.equalsIgnoreCase("session")) 82 this.scope = PageContext.SESSION_SCOPE; 83 else if (scope.equalsIgnoreCase("application")) 84 this.scope = PageContext.APPLICATION_SCOPE; 85 else 86 throw new IllegalArgumentException ("invalid scope"); 87 } 88 89 public void setName(String nameExpr) { 90 this.nameExpr = nameExpr; 91 } 92 93 public void setKey(String keyExpr) { 94 this.keyExpr = keyExpr; 95 } 96 97 98 101 public CacheTag() { 102 super(); 103 init(); 104 } 105 106 private void init() { 107 scope = PageContext.APPLICATION_SCOPE; 108 name = nameExpr = ""; 109 key = keyExpr = ""; 110 } 111 112 113 116 private void evaluateExpressions() throws JspException { 117 name = (String ) ExpressionEvaluatorManager.evaluate( 118 "name", 119 nameExpr, 120 String .class, 121 this, 122 pageContext); 123 key = (String ) ExpressionEvaluatorManager.evaluate( 124 "key", 125 keyExpr, 126 String .class, 127 this, 128 pageContext); 129 } 130 131 } 132 | Popular Tags |