1 25 package org.ofbiz.webapp.ftl; 26 27 import java.io.IOException ; 28 import java.io.Writer ; 29 import java.util.Map ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.http.HttpServletRequest ; 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 50 public class JpCacheIncludeTransform implements TemplateTransformModel { 51 52 public static final String module = JpCacheIncludeTransform.class.getName(); 53 protected static UtilCache pageCache = new UtilCache("webapp.JpInclude", 0, 0, 0, false, false); 54 55 public Writer getWriter(final Writer writer, Map args) throws TemplateModelException, IOException { 56 Environment env = Environment.getCurrentEnvironment(); 57 BeanModel req = (BeanModel) env.getVariable("request"); 58 BeanModel jpr = (BeanModel) env.getVariable("pages"); 59 60 final HttpServletRequest request = (HttpServletRequest ) req.getWrappedObject(); 61 final ServletContext ctx = (ServletContext ) request.getAttribute("servletContext"); 62 final RepositoryWrapper wrapper = (RepositoryWrapper) jpr.getWrappedObject(); 63 final String contextName = ctx.getServletContextName(); 64 final long expireTime = this.getExpireTime(args); 65 final String include = this.getInclude(args); 66 67 return new Writer (writer) { 68 public void write(char cbuf[], int off, int len) throws IOException { 69 writer.write(cbuf, off, len); 70 } 71 72 public void flush() throws IOException { 73 writer.flush(); 74 } 75 76 public void close() throws IOException { 77 Debug.log("Checking for cached content (" + contextName + "." + include + ")", module); 78 String content = (String ) 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 args) { 92 Object 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 ets = s.getAsString(); 99 Debug.log("ExpireTime String - " + ets, module); 100 try { 101 expireTime = Long.parseLong(ets); 102 } catch (Exception e) { 103 Debug.logError(e, module); 104 } 105 } 106 } 107 return expireTime; 108 } 109 110 public String getInclude(Map args) { 111 Object o = args.get("include"); 112 Debug.log("Include Object - " + o, module); 113 String 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 |