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