KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > PageServlet


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 JavaDoc;
13
14 import javax.servlet.ServletConfig JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19
20 /**
21  * Extend RollerServlet to load proper resource loader for page execution.
22  *
23  * @web.servlet name="PageServlet" load-on-startup="0"
24  * @web.servlet-init-param name="org.apache.velocity.properties"
25  * value="/WEB-INF/velocity.properties"
26  *
27  * @web.servlet-mapping url-pattern="/page/*"
28  */

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     /** We are overriding the default Runtime Velocity
37      * singleton to gain control over the initialization
38      * and so that the PreviewResourceLoader is not set
39      * for the PageServlet.
40      */

41     transient VelocityEngine ve = null;
42     
43     public Template handleRequest( HttpServletRequest JavaDoc request,
44                                     HttpServletResponse JavaDoc response,
45                                     Context ctx ) throws Exception JavaDoc
46     {
47         return super.handleRequest(request, response, ctx);
48     }
49     
50     /**
51      * Override initVelocity() so we can man-handle the list of
52      * resource loaders and remove "preview" if it is present.
53      * @see org.apache.velocity.servlet.VelocityServlet#initVelocity(ServletConfig)
54      */

55     protected void initVelocity( ServletConfig JavaDoc config )
56          throws ServletException JavaDoc
57     {
58         try
59         {
60             /*
61              * call the overridable method to allow the
62              * derived classes a shot at altering the configuration
63              * before initializing Runtime
64              */

65             Properties JavaDoc props = loadConfiguration( config );
66             
67             // remove "preview," from the beginning of the
68
// resource.loader list
69
String JavaDoc resourceLoaders = (String JavaDoc)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 JavaDoc tempStr = "";
76                 if (begin > 0)
77                 {
78                     tempStr = resourceLoaders.substring(0,begin);
79                 }
80                 resourceLoaders = tempStr + resourceLoaders.substring(begin+length);
81
82                 //System.out.println("PageServlet RESOURCELOADERS: " + resourceLoaders);
83
props.put("resource.loader", resourceLoaders);
84             }
85             
86             // remove all trace of the PreviewResourceLoader
87
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             /** set custom logging file */
94             props.setProperty( "runtime.log", "page_servlet.log" );
95             
96             // make user WebappResourceLoader has what it needs
97
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 JavaDoc e )
112         {
113             String JavaDoc msg = "Error initializing Velocity: " + e.toString();
114             mLogger.error(msg, e);
115             throw new ServletException JavaDoc(msg, e);
116         }
117     }
118
119     /**
120      * Override the parent getTemplate( String name ) method.
121      * @see org.apache.velocity.servlet.VelocityServlet#getTemplate(String, String)
122      */

123     public Template getTemplate( String JavaDoc name )
124         throws ResourceNotFoundException, ParseErrorException, Exception JavaDoc
125     {
126         return ve.getTemplate( name );
127     }
128
129     /**
130      * Override the parent getTemplate(String name, String encoding) method.
131      * @see org.apache.velocity.servlet.VelocityServlet#getTemplate(String, String)
132      */

133     public Template getTemplate( String JavaDoc name, String JavaDoc encoding )
134         throws ResourceNotFoundException, ParseErrorException, Exception JavaDoc
135     {
136         return ve.getTemplate( name, encoding );
137     }
138 }
139
140
Popular Tags