001: /*
002: * $Id: VelocityViewHandler.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
003: *
004: * Copyright (c) 2002-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.view;
026:
027: import java.io.IOException;
028: import java.io.OutputStreamWriter;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.util.Properties;
032:
033: import javax.servlet.ServletContext;
034: import javax.servlet.ServletOutputStream;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: import org.apache.velocity.Template;
039: import org.apache.velocity.VelocityContext;
040: import org.apache.velocity.app.VelocityEngine;
041: import org.apache.velocity.context.Context;
042: import org.apache.velocity.exception.ResourceNotFoundException;
043: import org.apache.velocity.io.VelocityWriter;
044: import org.apache.velocity.runtime.RuntimeConstants;
045: import org.apache.velocity.util.SimplePool;
046: import org.ofbiz.base.util.Debug;
047: import org.ofbiz.base.util.FlexibleProperties;
048:
049: /**
050: * VelocityViewHandler - Velocity Template Engine View Handler
051: *
052: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
053: * @version $Revision: 1.2 $
054: * @since 2.0
055: */
056: public class VelocityViewHandler implements ViewHandler {
057:
058: public static final String module = VelocityViewHandler.class
059: .getName();
060:
061: public static final String REQUEST = "req";
062: public static final String RESPONSE = "res";
063:
064: private static SimplePool writerPool = new SimplePool(40);
065: private VelocityEngine ve = null;
066:
067: public void init(ServletContext context)
068: throws ViewHandlerException {
069: try {
070: Debug.logInfo("[VelocityViewHandler.init] : Loading...",
071: module);
072: ve = new VelocityEngine();
073: // set the properties
074: // use log4j for logging
075: // use classpath template loading (file loading will not work in WAR)
076: ve.setProperty(
077: RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
078: "org.apache.velocity.runtime.log.Log4JLogSystem");
079: ve.setProperty("runtime.log.logsystem.log4j.category",
080: module);
081:
082: Properties props = null;
083: URL propsURL = null;
084:
085: try {
086: propsURL = context
087: .getResource("/WEB-INF/velocity.properties");
088: } catch (MalformedURLException e) {
089: Debug.logError(e, module);
090: }
091:
092: if (propsURL != null) {
093: props = new FlexibleProperties(propsURL);
094: Debug
095: .logWarning(
096: "[VelocityViewHandler.init] : Loaded /WEB-INF/velocity.properties",
097: module);
098: } else {
099: props = new Properties();
100: Debug.logWarning(
101: "[VelocityViewHandler.init] : Cannot load /WEB-INF/velocity.properties. "
102: + "Using default properties.", module);
103: }
104:
105: // set the file loader path -- used to mount the webapp
106: if (context.getRealPath("/") != null) {
107: props.setProperty("file.resource.loader.path", context
108: .getRealPath("/"));
109: Debug
110: .logInfo(
111: "[VelocityViewHandler.init] : Got true webapp path, mounting as template path.",
112: module);
113: }
114:
115: ve.init(props);
116: } catch (Exception e) {
117: throw new ViewHandlerException(e.getMessage(), e);
118: }
119: }
120:
121: public void render(String name, String page, String info,
122: String contentType, String encoding,
123: HttpServletRequest request, HttpServletResponse response)
124: throws ViewHandlerException {
125: if (ve == null) {
126: throw new ViewHandlerException(
127: "Velocity Template Engine has not been initialized");
128: }
129:
130: if (page == null || page.length() == 0) {
131: throw new ViewHandlerException("Invalid template source");
132: }
133:
134: Context context = new VelocityContext();
135:
136: context.put(REQUEST, request);
137: context.put(RESPONSE, response);
138:
139: Template template = null;
140:
141: try {
142: template = ve.getTemplate(page);
143: } catch (ResourceNotFoundException rne) {
144: throw new ViewHandlerException("Invalid template source",
145: rne);
146: } catch (Exception e) {
147: throw new ViewHandlerException(e.getMessage(), e);
148: }
149:
150: ServletOutputStream out = null;
151: VelocityWriter vw = null;
152:
153: try {
154: out = response.getOutputStream();
155: } catch (IOException e) {
156: throw new ViewHandlerException(e.getMessage(), e);
157: }
158:
159: try {
160: vw = (VelocityWriter) writerPool.get();
161: if (vw == null)
162: vw = new VelocityWriter(new OutputStreamWriter(out,
163: encoding), 4 * 1024, true);
164: else
165: vw.recycle(new OutputStreamWriter(out, encoding));
166:
167: if (vw == null)
168: Debug
169: .logWarning(
170: "[VelocityViewHandler.eval] : VelocityWriter is NULL",
171: module);
172:
173: template.merge(context, vw);
174: } catch (Exception e) {
175: throw new ViewHandlerException(e.getMessage(), e);
176: } finally {
177: try {
178: if (vw != null) {
179: vw.flush();
180: writerPool.put(vw);
181: }
182: } catch (Exception e) {
183: throw new ViewHandlerException(e.getMessage(), e);
184: }
185: }
186: }
187: }
|