1 25 package org.ofbiz.webapp.view; 26 27 import java.io.IOException ; 28 import java.io.OutputStreamWriter ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Properties ; 32 import javax.servlet.ServletContext ; 33 import javax.servlet.ServletOutputStream ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import org.ofbiz.base.util.Debug; 38 import org.ofbiz.base.util.collections.FlexibleProperties; 39 40 import org.apache.velocity.Template; 41 import org.apache.velocity.VelocityContext; 42 import org.apache.velocity.app.VelocityEngine; 43 import org.apache.velocity.context.Context; 44 import org.apache.velocity.exception.ResourceNotFoundException; 45 import org.apache.velocity.io.VelocityWriter; 46 import org.apache.velocity.runtime.RuntimeConstants; 47 import org.apache.velocity.util.SimplePool; 48 49 56 public class VelocityViewHandler implements ViewHandler { 57 58 public static final String module = VelocityViewHandler.class.getName(); 59 60 public static final String REQUEST = "req"; 61 public static final String RESPONSE = "res"; 62 63 private static SimplePool writerPool = new SimplePool(40); 64 private VelocityEngine ve = null; 65 66 public void init(ServletContext context) throws ViewHandlerException { 67 try { 68 Debug.logInfo("[VelocityViewHandler.init] : Loading...", module); 69 ve = new VelocityEngine(); 70 ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, 74 "org.apache.velocity.runtime.log.Log4JLogSystem"); 75 ve.setProperty("runtime.log.logsystem.log4j.category", module); 76 77 Properties props = null; 78 URL propsURL = null; 79 80 try { 81 propsURL = context.getResource("/WEB-INF/velocity.properties"); 82 } catch (MalformedURLException e) { 83 Debug.logError(e, module); 84 } 85 86 if (propsURL != null) { 87 props = new FlexibleProperties(propsURL); 88 Debug.logWarning("[VelocityViewHandler.init] : Loaded /WEB-INF/velocity.properties", module); 89 } else { 90 props = new Properties (); 91 Debug.logWarning("[VelocityViewHandler.init] : Cannot load /WEB-INF/velocity.properties. " + 92 "Using default properties.", module); 93 } 94 95 if (context.getRealPath("/") != null) { 97 props.setProperty("file.resource.loader.path", context.getRealPath("/")); 98 Debug.logInfo("[VelocityViewHandler.init] : Got true webapp path, mounting as template path.", module); 99 } 100 101 ve.init(props); 102 } catch (Exception e) { 103 throw new ViewHandlerException(e.getMessage(), e); 104 } 105 } 106 107 public void render(String name, String page, String info, String contentType, String encoding, 108 HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { 109 if (ve == null) { 110 throw new ViewHandlerException("Velocity Template Engine has not been initialized"); 111 } 112 113 if (page == null || page.length() == 0) { 114 throw new ViewHandlerException("Invalid template source"); 115 } 116 117 Context context = new VelocityContext(); 118 119 context.put(REQUEST, request); 120 context.put(RESPONSE, response); 121 122 Template template = null; 123 124 try { 125 template = ve.getTemplate(page); 126 } catch (ResourceNotFoundException rne) { 127 throw new ViewHandlerException("Invalid template source", rne); 128 } catch (Exception e) { 129 throw new ViewHandlerException(e.getMessage(), e); 130 } 131 132 ServletOutputStream out = null; 133 VelocityWriter vw = null; 134 135 try { 136 out = response.getOutputStream(); 137 } catch (IOException e) { 138 throw new ViewHandlerException(e.getMessage(), e); 139 } 140 141 try { 142 vw = (VelocityWriter) writerPool.get(); 143 if (vw == null) 144 vw = new VelocityWriter(new OutputStreamWriter (out, encoding), 4 * 1024, true); 145 else 146 vw.recycle(new OutputStreamWriter (out, encoding)); 147 148 if (vw == null) 149 Debug.logWarning("[VelocityViewHandler.eval] : VelocityWriter is NULL", module); 150 151 template.merge(context, vw); 152 } catch (Exception e) { 153 throw new ViewHandlerException(e.getMessage(), e); 154 } finally { 155 try { 156 if (vw != null) { 157 vw.flush(); 158 writerPool.put(vw); 159 } 160 } catch (Exception e) { 161 throw new ViewHandlerException(e.getMessage(), e); 162 } 163 } 164 } 165 } 166 | Popular Tags |