KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > espada > bugtracker > servlets > BTServlet


1 /*
2 ====================================================================
3 Project Name: bugtracker
4 File Name: /src/com/espada/bugtracker/servlets/BTServlet.java
5 Author: Manik Surtani <manik@espadanet.com>
6 Description: The parent servlet class
7 CVS Repository: goliath:/projects/repository/cvsroot/
8 CVS Module: bugtracker
9 Version: CVS $Id: BTServlet.java,v 1.5 2001/04/17 12:43:02 manik Exp $
10 ====================================================================
11
12 */

13
14 package com.espada.bugtracker.servlets;
15
16 // webmacro resources
17
import org.webmacro.*;
18 import org.webmacro.broker.*;
19 import org.webmacro.resource.*;
20 import org.webmacro.servlet.WebContext;
21
22 // servlet libraries
23
import javax.servlet.http.*;
24 import javax.servlet.*;
25
26 // bugtracker java apps
27
import com.espada.bugtracker.app.*;
28
29 // log4j
30

31 import org.apache.log4j.Category;
32
33
34 public abstract class BTServlet extends HttpServlet
35 {
36
37    // initializing the log4j logger for this class.
38
static Category logger = Category.getInstance(BTServlet.class.getName());
39    
40    String JavaDoc defaultTemplate = "errorMesg.wm";
41    protected String JavaDoc getInitialTemplate(){
42      return("errorMesg.wm");
43    }
44    
45    /**
46      * This is the core WebMacro interface which we use to create Contexts,
47      * load Templates, and begin other WebMacro operations.
48      */

49    /** the WebMacro Object **/
50    protected static WebMacro _wm = null;
51
52    public void init()
53    {
54       defaultTemplate=getInitialTemplate();
55       // prepares a webmacro instance, which stays persistent.
56
try
57       {
58          if (_wm == null)
59          {
60            _wm = new WM(getServletContext().getRealPath(getServletContext().getInitParameter("btpropfile")));
61          }
62       }
63         catch (InitException e)
64         {
65         logger.fatal("Problem creating the static webmacro instance.");
66         }
67
68    } //end of method
69

70    /**
71      * It's not strictly necessary to destroy the WebMacro object when
72      * you're done with it--garbage collection will eventually get it--but
73      * it makes sure the resources get freed. You really ought to do this,
74      * since it might be a lot of resources.
75      */

76    public void destroy()
77    {
78
79       if (_wm != null)
80       {
81          _wm.destroy();
82          _wm = null;
83       }
84    } //end of method
85

86    /**
87      */

88    public void doPost(HttpServletRequest req, HttpServletResponse resp){
89     doGet(req, resp);
90    } //end of method
91

92    /**
93      */

94    public void doGet(HttpServletRequest req, HttpServletResponse resp)
95    {
96
97        try
98        {
99           try
100           {
101
102              // create a context for the current request
103
WebContext c = _wm.getWebContext(req,resp);
104
105              /******************************************************************/
106
107
108              String JavaDoc s = "http://" + req.getServerName();
109              c.put("serverName", s);
110              HttpSession session = req.getSession();
111              doAction(req,resp,c);
112              c.put("loggedIn",(String JavaDoc)session.getAttribute("loggedIn"));
113              if (logger.isDebugEnabled())
114          logger.debug("isLoggedIn = " + session.getAttribute("loggedIn"));
115          
116             
117
118               /*****************************************************************/
119
120              // get the template we intend to execute
121
Template t = _wm.getTemplate(defaultTemplate);
122          if (logger.isDebugEnabled())
123          logger.debug("Created template object from " + defaultTemplate + " and got " + t + " in return.");
124          
125       
126
127              // Create FastWriter for fast output encoding
128
FastWriter fw = new FastWriter(resp.getOutputStream(),resp.getCharacterEncoding());
129
130              // write the template to the output, using our context
131
t.write(fw, c);
132              fw.close();
133
134           }
135             catch (org.webmacro.NotFoundException e)
136             {
137
138                FastWriter out = new FastWriter(resp.getOutputStream(),resp.getCharacterEncoding());
139
140                out.write("ERROR! Could not locate template " + defaultTemplate + ", check that your template path is set properly in WebMacro.properties");
141                logger.error("Could not locate template " + defaultTemplate + ", check that your template path is set properly in WebMacro.properties", e);
142                
143
144                out.close();
145
146            }
147
148            catch (org.webmacro.ContextException e)
149            {
150
151                FastWriter out = new FastWriter(resp.getOutputStream(),resp.getCharacterEncoding());
152
153                out.write("ERROR! Could not locate required data in the Context.");
154                logger.error("Could not locate required data in the Context.", e);
155
156                out.close();
157
158            }
159
160        }
161          catch (java.io.IOException JavaDoc e)
162          {
163
164              // what else can we do?
165
System.out.println("ERROR: IOException while writing to servlet output stream.");
166              logger.fatal("IOException while writing to servlet output stream.", e);
167              
168
169          }
170          
171
172    } //end of method
173

174    protected void doAction(HttpServletRequest request, HttpServletResponse response, WebContext c){
175    };
176 } //end of class
Popular Tags