KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > view > VelocityViewHandler


1 /*
2  * $Id: VelocityViewHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2003 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.view;
26
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Properties JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.ServletOutputStream JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
50  * VelocityViewHandler - Velocity Template Engine View Handler
51  *
52  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
53  * @version $Rev: 5462 $
54  * @since 2.0
55  */

56 public class VelocityViewHandler implements ViewHandler {
57
58     public static final String JavaDoc module = VelocityViewHandler.class.getName();
59
60     public static final String JavaDoc REQUEST = "req";
61     public static final String JavaDoc RESPONSE = "res";
62
63     private static SimplePool writerPool = new SimplePool(40);
64     private VelocityEngine ve = null;
65
66     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
67         try {
68             Debug.logInfo("[VelocityViewHandler.init] : Loading...", module);
69             ve = new VelocityEngine();
70             // set the properties
71
// use log4j for logging
72
// use classpath template loading (file loading will not work in WAR)
73
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 JavaDoc props = null;
78             URL JavaDoc propsURL = null;
79
80             try {
81                 propsURL = context.getResource("/WEB-INF/velocity.properties");
82             } catch (MalformedURLException JavaDoc 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 JavaDoc();
91                 Debug.logWarning("[VelocityViewHandler.init] : Cannot load /WEB-INF/velocity.properties. " +
92                     "Using default properties.", module);
93             }
94
95             // set the file loader path -- used to mount the webapp
96
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 JavaDoc e) {
103             throw new ViewHandlerException(e.getMessage(), e);
104         }
105     }
106
107     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding,
108             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc 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 JavaDoc 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 JavaDoc e) {
129             throw new ViewHandlerException(e.getMessage(), e);
130         }
131
132         ServletOutputStream JavaDoc out = null;
133         VelocityWriter vw = null;
134
135         try {
136             out = response.getOutputStream();
137         } catch (IOException JavaDoc 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 JavaDoc(out, encoding), 4 * 1024, true);
145             else
146                 vw.recycle(new OutputStreamWriter JavaDoc(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 JavaDoc 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 JavaDoc e) {
161                 throw new ViewHandlerException(e.getMessage(), e);
162             }
163         }
164     }
165 }
166
Popular Tags