webwork/src/main/webwork/view/velocity VelocityHelper.java,NONE,1.1 WebWorkVelocityServlet.java,1.5,1.6
[email protected] Sat, 15 Nov 2003 12:08:14 -0800
| Newsgroups | gmane.comp.java.open-symphony.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/opensymphony/webwork/src/main/webwork/view/velocity In directory sc8-pr-cvs1:/tmp/cvs-serv28503/src/main/webwork/view/velocity Modified Files: WebWorkVelocityServlet.java Added Files: VelocityHelper.java Log Message: Abstracted velocity stuff into a separate class so it can be used more easily elsewhere --- NEW FILE: VelocityHelper.java --- package webwork.view.velocity; import java.util.Properties; import java.util.Enumeration; import java.util.Iterator; import java.io.Writer; import javax.servlet.ServletContext; import javax.servlet.ServletResponse; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.velocity.app.Velocity; import org.apache.velocity.context.Context; import org.apache.velocity.VelocityContext; import org.apache.velocity.Template; import org.apache.velocity.runtime.RuntimeSingleton; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import webwork.config.Configuration; import webwork.util.ServletValueStack; /** * @author Hani Suleiman ([email protected]) * Date: Nov 15 * @author 2003 * Time: 11:21:14 AM */ public class VelocityHelper { private static final Log log = LogFactory.getLog(VelocityHelper.class); /** * The HTTP request object context key. */ public static final String REQUEST = "req"; /** * The HTTP response object context key. */ public static final String RESPONSE = "res"; static final String WEBWORK_UTIL = "webwork"; private static boolean initialized = false; public static final String VELO_CONTEXT = "__webwork__velocity__context"; /** * Hook up Velocity with the WebWork configuration. */ public static synchronized void initVelocity(ServletContext context) throws Exception { // WebWork configuration provides main config final Properties conf = new Properties() { public Object get(Object key) { return Configuration.get(key.toString()); } public String getProperty(String key) { return Configuration.getString(key.toString()); } public Enumeration keys() { final Iterator list = Configuration.list(); return new Enumeration() { public Object nextElement() { return list.next(); } public boolean hasMoreElements() { return list.hasNext(); } }; } }; // Set dynamic properties here // The properties not set here are taken from the WebWork configuration Properties p = new Properties(conf) { public Enumeration keys() { return conf.keys(); } }; /* * first, normalize our velocity log file to be in the * webapp */ String log = p.getProperty(Velocity.RUNTIME_LOG); if(log != null) { log = context.getRealPath(log); if(log != null) { p.setProperty(Velocity.RUNTIME_LOG, log); } } /* * If there is a file loader resource path, treat it the * same way, but only if it doesn't start with /. In that case * we use it as-is to allow the templates to be taken from some * repository (!very useful during development!). */ String path = p.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH); if(path != null && (path.equals("/") || !path.startsWith("/"))) { path = context.getRealPath(path); if(path != null) { p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path); } } Velocity.init(p); initialized = true; } public static void merge(Context context, String templateName, Writer writer) { context.put(WEBWORK_UTIL, new WebWorkUtil(context)); try { Template t = RuntimeSingleton.getTemplate(templateName); t.merge(context, writer); //java.io.PrintWriter out = new java.io.PrintWriter(System.out); //t.merge(context, out); //out.close(); } catch(Exception e) { log.error(e.getMessage(), e); } } public static Context getContext(ServletContext context, ServletRequest request, ServletResponse response) { if(!initialized) { try { initVelocity(context); } catch(Exception e) { log.error(e.getMessage(), e); return null; } } WebWorkVelocityContext ctx = (WebWorkVelocityContext)request.getAttribute(VELO_CONTEXT); if(ctx==null) { ctx = new WebWorkVelocityContext(ServletValueStack.getStack(request)); ctx.put(REQUEST, request); ctx.put(RESPONSE, response); request.setAttribute(VELO_CONTEXT, ctx); } return ctx; } /** * WebWork specific Velocity context implementation. */ static class WebWorkVelocityContext extends VelocityContext { ServletValueStack stack; WebWorkVelocityContext(ServletValueStack aStack) { stack = aStack; } public boolean internalContainsKey(java.lang.Object key) { boolean contains = super.internalContainsKey(key); return contains ? true : stack.test(key.toString()); } public Object internalGet(String key) { return super.internalContainsKey(key) ? super.internalGet(key) : stack.findValue(key); } } } Index: WebWorkVelocityServlet.java =================================================================== RCS file: /cvsroot/opensymphony/webwork/src/main/webwork/view/velocity/WebWorkVelocityServlet.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- WebWorkVelocityServlet.java 12 Feb 2003 05:25:19 -0000 1.5 +++ WebWorkVelocityServlet.java 15 Nov 2003 20:08:11 -0000 1.6 @@ -7,26 +7,16 @@ package webwork.view.velocity; import org.apache.velocity.Template; -import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.context.Context; import org.apache.velocity.servlet.VelocityServlet; import org.apache.commons.logging.*; -import webwork.action.factory.ActionFactory; -import webwork.action.ActionContext; import webwork.action.ServletActionContext; -import webwork.config.Configuration; -import webwork.util.ServletValueStack; -import javax.servlet.ServletConfig; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.ServletException; -import java.io.FileNotFoundException; import java.io.IOException; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.Properties; /** * Velocity integration servlet. Transfer action properties to Velocity @@ -42,11 +32,20 @@ static final String CONTEXT = "velocity_context"; static final String WEBWORK_UTIL = "webwork"; - ThreadLocal request = new ThreadLocal(); - ThreadLocal response = new ThreadLocal(); - protected Log log = LogFactory.getLog(getClass()); + public void init() throws ServletException + { + try + { + VelocityHelper.initVelocity(getServletContext()); + } + catch(Exception e) + { + throw new ServletException(e); + } + } + public void service(ServletRequest aRequest, ServletResponse aResponse) throws ServletException, IOException { long start = 0; @@ -62,103 +61,13 @@ } /** - * Hook up Velocity with the WebWork configuration. - */ - protected Properties loadConfiguration(ServletConfig config ) - throws IOException, FileNotFoundException - { - log.debug(">>> loadConfiguration <<<<<"); - - // WebWork configuration provides main config - final Properties conf = new Properties() - { - public Object get(Object key) - { - return Configuration.get(key.toString()); - } - - public String getProperty(String key) - { - return Configuration.getString(key.toString()); - } - - public Enumeration keys() - { - final Iterator list = Configuration.list(); - return new Enumeration() - { - public Object nextElement() - { - return list.next(); - } - - public boolean hasMoreElements() - { - return list.hasNext(); - } - }; - } - }; - - // Set dynamic properties here - // The properties not set here are taken from the WebWork configuration - Properties p = new Properties(conf) - { - public Enumeration keys() - { - return conf.keys(); - } - }; - - /* - * first, normalize our velocity log file to be in the - * webapp - */ - - String log = p.getProperty( Velocity.RUNTIME_LOG); - - if (log != null ) - { - log = getServletContext().getRealPath( log ); - - if (log != null) - { - p.setProperty( Velocity.RUNTIME_LOG, log ); - } - } - - - /* - * If there is a file loader resource path, treat it the - * same way, but only if it doesn't start with /. In that case - * we use it as-is to allow the templates to be taken from some - * repository (!very useful during development!). - */ - String path = p.getProperty( Velocity.FILE_RESOURCE_LOADER_PATH ); - - if ( path != null && (path.equals("/") || !path.startsWith("/"))) - { - path = getServletContext().getRealPath( path ); - if ( path != null) - { - p.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH, path ); - } - } - - return p; - } - - /** * Create a context that delegates to the standard context and * also allows Velocity to access properties from the ValueStack. */ protected Context createContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) { - Context ctx = new WebWorkVelocityContext(ServletValueStack.getStack(request)); - ctx.put(REQUEST, request); - ctx.put(RESPONSE, response); - return ctx; + return VelocityHelper.getContext(getServletContext(), request, response); } /** @@ -178,32 +87,5 @@ if (servletPath == null) servletPath = aRequest.getServletPath(); return getTemplate(servletPath); - } - - /** - * WebWork specific Velocity context implementation. - */ - static class WebWorkVelocityContext - extends VelocityContext - { - ServletValueStack stack; - - WebWorkVelocityContext(ServletValueStack aStack) - { - stack = aStack; - } - - public boolean internalContainsKey(java.lang.Object key) - { - boolean contains = super.internalContainsKey(key); - return contains ? true : stack.test(key.toString()); - } - - public Object internalGet(String key) - { - return super.internalContainsKey(key) ? - super.internalGet(key) : - stack.findValue(key); - } } } ------------------------------------------------------- This SF. Net email is sponsored by: GoToMyPC GoToMyPC is the fast, easy and secure way to access your computer from any Web browser or wireless device. Click here to Try it Free! https://www.gotomypc.com/tr/OSDN/AW/Q4_2003/t/g22lp?Target=mm/g22lp.tmpl