KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > ftl > JpCacheIncludeTransform


1 /*
2  * $Id: JpCacheIncludeTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.ftl;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32
33 import freemarker.core.Environment;
34 import freemarker.ext.beans.BeanModel;
35 import freemarker.template.SimpleScalar;
36 import freemarker.template.TemplateModelException;
37 import freemarker.template.TemplateTransformModel;
38
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.cache.UtilCache;
41
42 import org.jpublish.RepositoryWrapper;
43
44 /**
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 3.2
49  */

50 public class JpCacheIncludeTransform implements TemplateTransformModel {
51
52     public static final String JavaDoc module = JpCacheIncludeTransform.class.getName();
53     protected static UtilCache pageCache = new UtilCache("webapp.JpInclude", 0, 0, 0, false, false);
54
55     public Writer JavaDoc getWriter(final Writer JavaDoc writer, Map JavaDoc args) throws TemplateModelException, IOException JavaDoc {
56         Environment env = Environment.getCurrentEnvironment();
57         BeanModel req = (BeanModel) env.getVariable("request");
58         BeanModel jpr = (BeanModel) env.getVariable("pages");
59
60         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req.getWrappedObject();
61         final ServletContext JavaDoc ctx = (ServletContext JavaDoc) request.getAttribute("servletContext");
62         final RepositoryWrapper wrapper = (RepositoryWrapper) jpr.getWrappedObject();
63         final String JavaDoc contextName = ctx.getServletContextName();
64         final long expireTime = this.getExpireTime(args);
65         final String JavaDoc include = this.getInclude(args);
66
67         return new Writer JavaDoc(writer) {
68             public void write(char cbuf[], int off, int len) throws IOException JavaDoc {
69                 writer.write(cbuf, off, len);
70             }
71
72             public void flush() throws IOException JavaDoc {
73                 writer.flush();
74             }
75
76             public void close() throws IOException JavaDoc {
77                 Debug.log("Checking for cached content (" + contextName + "." + include + ")", module);
78                 String JavaDoc content = (String JavaDoc) pageCache.get(contextName + "." + include);
79                 if (content == null) {
80                     content = wrapper.get(include);
81                     pageCache.put(contextName + "." + include, content, expireTime);
82                     Debug.log("No content found; cached result for - " + expireTime, module);
83                 }
84                 if (content != null) {
85                     writer.write(content);
86                 }
87             }
88         };
89     }
90
91     public long getExpireTime(Map JavaDoc args) {
92         Object JavaDoc o = args.get("expireTime");
93         Debug.log("ExpireTime Object - " + o, module);
94         long expireTime = 0;
95         if (o != null) {
96             if (o instanceof SimpleScalar) {
97                 SimpleScalar s = (SimpleScalar) o;
98                 String JavaDoc ets = s.getAsString();
99                 Debug.log("ExpireTime String - " + ets, module);
100                 try {
101                     expireTime = Long.parseLong(ets);
102                 } catch (Exception JavaDoc e) {
103                     Debug.logError(e, module);
104                 }
105             }
106         }
107         return expireTime;
108     }
109
110     public String JavaDoc getInclude(Map JavaDoc args) {
111         Object JavaDoc o = args.get("include");
112         Debug.log("Include Object - " + o, module);
113         String JavaDoc include = null;
114         if (o != null) {
115             if (o instanceof SimpleScalar) {
116                 SimpleScalar s = (SimpleScalar) o;
117                 include = s.getAsString();
118                 Debug.log("Include String - " + include, module);
119             }
120         }
121         return include;
122     }
123 }
124
Popular Tags