KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jmxdebug > web > velocity > BasicVelocityActionServlet


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.jmxdebug.web.velocity;
19
20 import java.io.IOException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Properties JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServlet JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.velocity.Template;
31 import org.apache.velocity.VelocityContext;
32 import org.apache.velocity.app.VelocityEngine;
33 import org.apache.velocity.runtime.RuntimeConstants;
34
35 /**
36  * Simple servlet to dispatch based on 'action'. Also inits velocity in a
37  * simple way
38  *
39  * @version $Rev: 45929 $ $Date: 2004-09-11 17:10:38 -0700 (Sat, 11 Sep 2004) $
40  */

41 public abstract class BasicVelocityActionServlet extends HttpServlet JavaDoc {
42     public static final String JavaDoc DEFAULT_PROPS = "org/apache/geronimo/jmxdebug/web/velocity/velocity.defaults";
43
44     /**
45      * for dispatch purposes
46      */

47     private static final Class JavaDoc[] DISPATCH_ARGS = {HttpServletRequest JavaDoc.class, HttpServletResponse JavaDoc.class};
48
49     /**
50      * velocity engine for this servlet
51      */

52     private final VelocityEngine velocityEngine = new VelocityEngine();
53
54     /**
55      * for dispatching to the method specified...
56      */

57     public void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
58
59         // get the action
60
String JavaDoc action = req.getParameter(getActionVerb());
61         if (action == null || action.length() == 0) {
62             action = "defaultAction";
63         }
64
65         // look up and invoke the method with the action name
66
try {
67             Method JavaDoc method = this.getClass().getMethod(action, DISPATCH_ARGS);
68             method.invoke(this, new Object JavaDoc[]{req, res});
69         } catch (NoSuchMethodException JavaDoc nsme) {
70             unknownAction(req, res);
71         } catch (Exception JavaDoc e) {
72             log("BasicVelocityActionServlet.service() : exception", e);
73         }
74     }
75
76     public void init() throws ServletException JavaDoc {
77         Properties JavaDoc p = new Properties JavaDoc();
78
79         // load the default properties file using the classloader
80
try {
81             p.load(getClass().getClassLoader().getResourceAsStream(DEFAULT_PROPS));
82         } catch (Exception JavaDoc e) {
83             log("BasicVelocityActionServlet : default " + DEFAULT_PROPS + " not found.", e);
84             throw new ServletException JavaDoc(e);
85         }
86
87         // apply default propertis to velocitty engine
88
for (Iterator JavaDoc iterator = p.keySet().iterator(); iterator.hasNext();) {
89             String JavaDoc key = (String JavaDoc) iterator.next();
90             velocityEngine.setProperty(key, p.getProperty(key));
91         }
92
93         // hook velocity logger up to the servlet logger
94
ServletLogger sl = new ServletLogger(getServletContext());
95         velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, sl);
96
97         // set an app context for the webapploader if we are using it
98
ServletAppContext vssac = new ServletAppContext(getServletContext());
99         velocityEngine.setApplicationAttribute(WebappLoader.KEY, vssac);
100
101         // start the velocity engine
102
try {
103             velocityEngine.init();
104         } catch (Exception JavaDoc e) {
105             log("BasicVelocityActionServlet", e);
106             throw new ServletException JavaDoc(e);
107         }
108     }
109
110     /**
111      * Defines the 'action verb' for the app
112      */

113     protected abstract String JavaDoc getActionVerb();
114
115     /**
116      * Called when there is a request w/ no action verb
117      */

118     public abstract void defaultAction(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
119             throws ServletException JavaDoc, IOException JavaDoc;
120
121     /**
122      * Called when there is a request w/ invalid action verb
123      */

124     public abstract void unknownAction(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
125             throws ServletException JavaDoc, IOException JavaDoc;
126
127     protected VelocityEngine getVelocityEngine() {
128         return this.velocityEngine;
129     }
130
131
132     protected boolean renderTemplate(HttpServletRequest JavaDoc req,
133             HttpServletResponse JavaDoc res,
134             VelocityContext velocityContext,
135             String JavaDoc templateName) {
136
137         try {
138             Template template = getVelocityEngine().getTemplate(templateName);
139             template.merge(velocityContext, res.getWriter());
140             return true;
141         } catch (Exception JavaDoc e) {
142             log("Error rendering template: " + templateName, e);
143         }
144
145         return false;
146     }
147
148     /**
149      * little wrapper class to safely pass the ServletContext to the loader
150      */

151     public class ServletAppContext implements WebappLoader.WebappLoaderAppContext {
152         ServletContext JavaDoc servletContext = null;
153
154         ServletAppContext(ServletContext JavaDoc sc) {
155             servletContext = sc;
156         }
157
158         public ServletContext JavaDoc getServletContext() {
159             return servletContext;
160         }
161     }
162 }
163
Popular Tags