1 2 package org.roller.presentation.velocity; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.apache.velocity.Template; 7 import org.apache.velocity.app.VelocityEngine; 8 import org.apache.velocity.context.Context; 9 import org.apache.velocity.exception.ParseErrorException; 10 import org.apache.velocity.exception.ResourceNotFoundException; 11 12 import java.util.Properties ; 13 14 import javax.servlet.ServletConfig ; 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 20 29 public class PageServlet extends BasePageServlet 30 { 31 static final long serialVersionUID = 5083624357559616805L; 32 33 private static Log mLogger = 34 LogFactory.getFactory().getInstance(PageServlet.class); 35 36 41 transient VelocityEngine ve = null; 42 43 public Template handleRequest( HttpServletRequest request, 44 HttpServletResponse response, 45 Context ctx ) throws Exception 46 { 47 return super.handleRequest(request, response, ctx); 48 } 49 50 55 protected void initVelocity( ServletConfig config ) 56 throws ServletException 57 { 58 try 59 { 60 65 Properties props = loadConfiguration( config ); 66 67 String resourceLoaders = (String )props.get("resource.loader"); 70 if (resourceLoaders != null && 71 resourceLoaders.indexOf("preview") > -1) 72 { 73 int begin = resourceLoaders.indexOf("preview"); 74 int length = "preview".length() + 1; 75 String tempStr = ""; 76 if (begin > 0) 77 { 78 tempStr = resourceLoaders.substring(0,begin); 79 } 80 resourceLoaders = tempStr + resourceLoaders.substring(begin+length); 81 82 props.put("resource.loader", resourceLoaders); 84 } 85 86 props.remove("preview.resource.loader.public.name"); 88 props.remove("preview.resource.loader.description"); 89 props.remove("preview.resource.loader.class"); 90 props.remove("preview.resource.loader.cache"); 91 props.remove("preview.resource.loader.modificationCheckInterval"); 92 93 94 props.setProperty( "runtime.log", "page_servlet.log" ); 95 96 WebappResourceLoader.setServletContext( getServletContext() ); 98 99 if (mLogger.isDebugEnabled()) 100 { 101 mLogger.debug("VelocityEngine props: " + props.toString()); 102 } 103 104 ve = new VelocityEngine(); 105 ve.info("*******************************************"); 106 ve.info("Initializing VelocityEngine for PageServlet"); 107 ve.init( props ); 108 ve.info("Done initializing VelocityEngine for PageServlet"); 109 ve.info("************************************************"); 110 } 111 catch( Exception e ) 112 { 113 String msg = "Error initializing Velocity: " + e.toString(); 114 mLogger.error(msg, e); 115 throw new ServletException (msg, e); 116 } 117 } 118 119 123 public Template getTemplate( String name ) 124 throws ResourceNotFoundException, ParseErrorException, Exception 125 { 126 return ve.getTemplate( name ); 127 } 128 129 133 public Template getTemplate( String name, String encoding ) 134 throws ResourceNotFoundException, ParseErrorException, Exception 135 { 136 return ve.getTemplate( name, encoding ); 137 } 138 } 139 140 | Popular Tags |