KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > management > http > SuperServlet


1 package org.jfox.ioc.management.http;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Properties JavaDoc;
6 import javax.servlet.ServletConfig JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9
10 import org.apache.velocity.Template;
11 import org.apache.velocity.app.Velocity;
12 import org.apache.velocity.context.Context;
13 import org.apache.velocity.servlet.VelocityServlet;
14 import org.jfox.ioc.Registry;
15
16 /**
17  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
18  */

19
20 public abstract class SuperServlet extends VelocityServlet {
21
22     /**
23      * Called by the VelocityServlet
24      * init(). We want to set a set of properties
25      * so that templates will be found in the webapp
26      * root. This makes this easier to work with as
27      * an example, so a new user doesn't have to worry
28      * about config issues when first figuring things
29      * out
30      */

31     protected Properties JavaDoc loadConfiguration(ServletConfig JavaDoc config)
32             throws IOException JavaDoc, FileNotFoundException JavaDoc {
33         Properties JavaDoc p = new Properties JavaDoc();
34
35         /*
36          * first, we set the template path for the
37          * FileResourceLoader to the root of the
38          * webapp. This probably won't work under
39          * in a WAR under WebLogic, but should
40          * under tomcat :)
41          */

42
43         String JavaDoc path = config.getServletContext().getRealPath("/");
44
45         if(path == null) {
46             path = "";
47         }
48
49         path += "/WEB-INF/template"; // template path
50

51         p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
52
53         /**
54          * and the same for the log file
55          */

56
57         p.setProperty("runtime.log", path + "velocity.log");
58
59         return p;
60     }
61
62
63     /**
64      * <p/>
65      * main routine to handle a request. Called by
66      * VelocityServlet, your responsibility as programmer
67      * is to simply return a valid Template
68      * </p>
69      *
70      * @param ctx a Velocity Context object to be filled with
71      * data. Will be used for rendering this
72      * template
73      * @return Template to be used for request
74      */

75     public Template handleRequest(HttpServletRequest JavaDoc request,
76                                   HttpServletResponse JavaDoc response,
77                                   Context ctx) throws Exception JavaDoc{
78
79         String JavaDoc template = getTemplate();
80
81         Template outty = getTemplate(template);
82         buildContext(request,ctx);
83
84         return outty;
85     }
86
87     public abstract String JavaDoc getTemplate();
88
89     public abstract void buildContext(HttpServletRequest JavaDoc request, Context ctx) throws Exception JavaDoc;
90
91     protected Registry getRegistry(){
92         return Registry.getInstance();
93     }
94 }
95
96
Popular Tags