1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.cache.tagext;
17
18 import javax.servlet.*;
19 import javax.servlet.http.*;
20 import javax.servlet.jsp.*;
21 import javax.servlet.jsp.tagext.*;
22 import java.io.*;
23 import java.util.Hashtable;
24 import org.apache.commons.cache.*;
25 import org.apache.commons.jocl.JOCLContentHandler;
26
27
28
29
30
31
32 public class CacheTag extends BodyTagSupport {
33 private static final boolean DEBUG = false;
34 private static final boolean VERBOSE_DEBUG = DEBUG && false;
35 private static final boolean CHECKING = false;
36
37 protected Serializable _key = null;
38 protected Serializable _group = null;
39 protected Long _expiresAt = null;
40 protected static Cache _cache = null;
41 protected String _content = null;
42 protected boolean _mustRevalidate = false;
43 protected String _name = null;
44
45 public void release() {
46 super.release();
47 _key = null;
48 _group = null;
49 _name = null;
50 _expiresAt = null;
51 _content = null;
52 _mustRevalidate = false;
53 }
54
55 public void setMustRevalidate(String obj) {
56 _mustRevalidate = Boolean.valueOf(obj).booleanValue();
57 }
58
59
60
61
62
63
64
65
66
67
68
69 public void setMustRevalidate(boolean val) {
70 _mustRevalidate = val;
71 }
72
73 public void setName(String name) {
74 _name = name;
75 }
76
77 public void setGroup(String group) {
78 _group = group;
79 }
80
81
82
83
84
85
86
87
88 public void setKey(String key) {
89 _key = key;
90 }
91
92
93
94
95
96
97
98
99 public void setTtl(String ttl) {
100 if(ttl != null) {
101 if(ttl.endsWith("s") || ttl.endsWith("S")) {
102 setTtl(1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
103 } else if(ttl.endsWith("m") || ttl.endsWith("M")) {
104 setTtl(60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
105 } else if(ttl.endsWith("h") || ttl.endsWith("H")) {
106 setTtl(60L * 60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
107 } else if(ttl.endsWith("d") || ttl.endsWith("D")) {
108 setTtl(24L * 60L * 60L * 1000L * Long.parseLong(ttl.substring(0,ttl.length() - 1)));
109 } else {
110 setTtl(1000L * Long.parseLong(ttl));
111 }
112 }
113 }
114
115 private void setTtl(long ttl) {
116 _expiresAt = new Long(System.currentTimeMillis() + ttl);
117 }
118
119 private void setTtl(Long ttl) {
120 if(null == ttl) {
121 _expiresAt = null;
122 } else {
123 _expiresAt = new Long(System.currentTimeMillis() + ttl.longValue());
124 }
125 }
126
127 public void setLastModified(String lastmodstr) {
128 long lastmod = 0L;
129 try {
130 lastmod = Long.parseLong(lastmodstr);
131 _expiresAt = new Long( System.currentTimeMillis() + ((System.currentTimeMillis() - lastmod)/2) );
132 } catch(Exception e) {
133
134 _expiresAt = null;
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public void setExpiresAt(String expires) {
153 try {
154 _expiresAt = new Long(Long.parseLong(expires));
155 } catch(Exception e) {
156
157 _expiresAt = null;
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171 public int doStartTag() throws JspException {
172 if(!_mustRevalidate) {
173 try {
174 Object obj = getCache(_name).retrieve(_key);
175 if(null == obj) {
176 return EVAL_BODY_TAG;
177 } else {
178 _content = obj.toString();
179 return SKIP_BODY;
180 }
181 } catch(Exception e) {
182 e.printStackTrace();
183 throw new JspException(e.toString());
184 }
185 } else {
186 getCache(_name).clear(_key);
187 return EVAL_BODY_TAG;
188 }
189 }
190
191 public int doAfterBody() throws JspException {
192 _content = getBodyContent().getString();
193 getCache(_name).store(_key,_content,_expiresAt,null,_group);
194 return SKIP_BODY;
195 }
196
197 public int doEndTag() throws JspException {
198 try {
199 pageContext.getOut().write(_content);
200 } catch(Exception e) {
201 e.printStackTrace();
202 throw new JspException(e.toString());
203 }
204 return EVAL_PAGE;
205 }
206
207 private static final CacheTag _instance = new CacheTag();
208
209 public synchronized static Cache getCache(String name) throws JspException {
210 if(CacheSingleton.hasCache(name)) {
211 return CacheSingleton.getCache(name);
212 } else {
213 Cache cache = null;
214
215 String serfile = System.getProperty("org.apache.commons.cache.tagext.CacheTag.cache." + name + ".serialized-cache-file");
216 if(null != serfile) {
217 try {
218 cache = SimpleCache.readFromFile(serfile);
219 } catch(Exception e) {
220 cache = null;
221 }
222 }
223
224 if(null != cache) {
225 CacheSingleton.putCache(name,cache);
226 return cache;
227 } else {
228 String config = System.getProperty("org.apache.commons.cache.tagext.CacheTag.cache." + name + ".configuration");
229 if(null == config) {
230 throw new JspException("Cache \"" + name + "\" not found.");
231 } else {
232 try {
233 JOCLContentHandler jocl = JOCLContentHandler.parse(_instance.getClass().getClassLoader().getResourceAsStream(config));
234 cache = (Cache)(jocl.getValue(0));
235 CacheSingleton.putCache(name,cache);
236 return cache;
237 } catch(Exception e) {
238 e.printStackTrace();
239 throw new JspException(e.toString());
240 }
241 }
242 }
243 }
244 }
245 }