1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.talika.tarsis.servlets;
24
25 import java.io.IOException;
26
27 import javax.servlet.ServletConfig;
28 import javax.servlet.ServletException;
29 import javax.servlet.UnavailableException;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.talika.tarsis.Globals;
35 import org.talika.tarsis.context.Context;
36 import org.talika.tarsis.log.Logger;
37
38 /**
39 * Base implementation of <code>HttpServlet</code> for all web application servlets
40 * of Tarsis MV Framework architecture.
41 *
42 * @author Jose M. Palomar
43 * @version $Revision: 269 $
44 */
45 public abstract class AbstractServlet extends HttpServlet {
46
47
48 /**
49 * Tarsis context.
50 */
51 private Context context;
52
53 /**
54 * Tarsis logger.
55 */
56 private Logger logger;
57
58 /**
59 * Servlet name.
60 */
61 private String servletName;
62
63 /**
64 * Called by the servlet container to indicate to a servlet that the servlet is
65 * being placed into service.
66 *
67 * @param config ServletConfig the <code>ServletConfig</code> object that contains
68 * configutation information for this servlet.
69 * @throws ServletException if an exception occurs that interrupts the servlet's
70 * normal operation.
71 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
72 */
73 public void init(ServletConfig config) throws ServletException {
74 super.init(config);
75
76
77 this.context = (Context) config.getServletContext().getAttribute(Globals.CONTEXT_ATTR);
78 if (this.context == null) {
79 throw new UnavailableException("Null context!");
80 }
81
82 this.logger = this.context.getLogger();
83 this.servletName = config.getServletName();
84
85 if (this.logger.isInfoEnabled()) {
86 this.logger.logInfo(servletName + ": init");
87 }
88
89 }
90
91 /**
92 * Called by the servlet container to indicate to a servlet that the servlet is
93 * being taken out of service.
94 *
95 * @see javax.servlet.Servlet#destroy()
96 */
97 public void destroy() {
98
99 if (this.logger.isInfoEnabled()) {
100 this.logger.logInfo(servletName + ": destroy");
101 }
102
103 }
104
105 /**
106 * Called by the server to allow a servlet to handle a GET request.
107 *
108 * @param request servlet object that contains the request the client has made of
109 * the servlet.
110 * @param response servlet object that contains the response the servlet sends to
111 * the client.
112 * @throws ServletException if the request could not be handled.
113 * @throws IOException if an input or output error is detected when the servlet
114 * handles request.
115 */
116 protected void doGet(HttpServletRequest request, HttpServletResponse response)
117 throws ServletException, IOException {
118 if (this.logger.isDebugEnabled()) {
119 this.logger.logDebug(servletName + ": invoked (GET)");
120 }
121 process(request, response);
122 }
123
124 /**
125 * Called by the server to allow a servlet to handle a POST request.
126 *
127 * @param request servlet object that contains the request the client has made of
128 * the servlet.
129 * @param response servlet object that contains the response the servlet sends to
130 * the client.
131 * @throws ServletException if the request could not be handled.
132 * @throws IOException if an input or output error is detected when the servlet
133 * handles request.
134 */
135 protected void doPost(HttpServletRequest request, HttpServletResponse response)
136 throws ServletException, IOException {
137 if (this.logger.isDebugEnabled()) {
138 this.logger.logDebug(servletName + ": invoked (POST)");
139 }
140 process(request, response);
141 }
142
143 /**
144 * Processes requests for both HTTP GET and POST methods.
145 *
146 * @param request servlet object that contains the request the client has made of
147 * the servlet.
148 * @param response servlet object that contains the response the servlet sends to
149 * the client.
150 * @throws ServletException if the request could not be handled.
151 * @throws IOException if an input or output error is detected when the servlet
152 * handles request.
153 */
154 protected abstract void process(HttpServletRequest request, HttpServletResponse response)
155 throws ServletException, IOException;
156
157 /**
158 * Returns the Tarsis context.
159 *
160 * @return Context the Tarsis context.
161 */
162 protected final Context getContext() {
163 return context;
164 }
165
166 /**
167 * Returns the Tarsis logger.
168 *
169 * @return Logger the Tarsis logger.
170 */
171 protected final Logger getLogger() {
172 return logger;
173 }
174
175 /**
176 * Returns a short description of the servlet.
177 *
178 * @return String a short description of the servlet.
179 * @see javax.servlet.Servlet#getServletInfo()
180 */
181 public final String getServletInfo() {
182 return Globals.PROJECT_INFO + "/" + servletName;
183 }
184
185 }