KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > servlet > CoefficientServlet


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.servlet;
21
22 import za.org.coefficient.core.CoefficientInterceptor;
23 import za.org.coefficient.core.CoefficientWebContext;
24 import za.org.coefficient.core.Constants;
25 import za.org.coefficient.interfaces.CoefficientContext;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31
32 import javax.servlet.ServletConfig JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 /**
39  * The entry point for the Coefficient user calling
40  * http://www.xxx.net/index.html. This is a genuine servlet.
41  * When doGet(request, response) is called, everything is done from here.
42  *
43  *
44  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
45  * @version $Revision: 1.10 $
46  * @web.servlet
47  * name="CoefficientServlet"
48  * display-name="Coefficient Servlet"
49  * description="This servlet drives the whole coefficient system"
50  * @web.servlet-mapping
51  * url-pattern="/index.html"
52  */

53 public class CoefficientServlet extends HttpServlet JavaDoc {
54
55     //~ Instance fields ========================================================
56

57     private CoefficientInterceptor interceptor;
58
59     //~ Methods ================================================================
60

61     public void addInterceptor(CoefficientInterceptor val) {
62         if (val == null) {
63             throw new IllegalArgumentException JavaDoc("Cannot add null interceptor");
64         }
65         if (this.interceptor == null) {
66             this.interceptor = val;
67         } else {
68             this.interceptor.append(val);
69         }
70     }
71
72     /**
73      * Called by the servlet's container to indicate to the servlet that the
74      * servlet is being taken out of service. Note: super.destroy() is NOT
75      * called. This method does nothing.
76      */

77     public void destroy() {
78     }
79
80     /**
81      * Called by the servlet container to indicate to a servlet that
82      * the servlet is being placed into service. Note: this method
83      * calls super.init(cfg).
84      *
85      * @param cfg The ServletConfig instance used by the Servlet
86      * container to the current servlet.
87      *
88      * @throws ServletException
89      *
90      */

91     public void init(ServletConfig JavaDoc cfg) throws ServletException JavaDoc {
92         //Call super
93
super.init(cfg);
94     }
95
96     /**
97      * Called by the serlet container (via the service method)
98      * to allow a servlet to handle a GET request making this
99      * method the entry point for the Project Engine user.
100      *
101      * @param request The HttpServletRequest instance.
102      * @param response The HttpServletResponse instance.
103      *
104      * @see #execute(HttpServletRequest,HttpServletResponse) execute(request,response) execute(request,response).
105      *
106      */

107     protected void doGet(HttpServletRequest JavaDoc request,
108         HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
109         String JavaDoc pathInfo = request.getPathInfo();
110         String JavaDoc servletPath = request.getServletPath();
111         String JavaDoc contextPath = request.getContextPath();
112         String JavaDoc path =
113             (pathInfo == null) ? servletPath : (servletPath + pathInfo);
114         
115         if ("/index.html".equals(path) ||
116             "/".equals(path)) {
117             response.setContentType("text/html");
118             execute(request, response);
119             return;
120         }
121         response.sendError(HttpServletResponse.SC_NOT_FOUND);
122     }
123
124     /**
125      * Called by the servlet container to handle a POST request. This
126      * method calls doGet(request, response) to perform the job.
127      *
128      * @see #doGet(HttpServletRequest, HttpServletResponse)
129      */

130     protected void doPost(HttpServletRequest JavaDoc request,
131         HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
132         doGet(request, response);
133     }
134
135     /**
136      * Creates a Page instance and invokes the process method
137      * of the CoreMudule.
138      *
139      * @param request The HttpServletRequest instance.
140      * @param response The HttpServletResponse instance.
141      *
142      * @throws ServletException
143      * @throws IOException
144      */

145     private void execute(HttpServletRequest JavaDoc request,
146         HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
147         try {
148             if (interceptor == null) {
149                 initInterceptors();
150             }
151             CoefficientWebContext ctx = new CoefficientWebContext(request, response);
152             interceptor.invoke(ctx);
153             // synchronize with CoefficientContext (reset session variables,
154
// update cookies, forward if needed, etc...)
155
ctx.synchronize(request, response);
156
157             // Write out the page content
158
ctx.getPage().process(response.getWriter());
159         } catch (IOException JavaDoc e) {
160             throw e;
161         } catch (Exception JavaDoc e) {
162             throw new ServletException JavaDoc(e);
163         }
164     }
165
166     private synchronized void initInterceptors() {
167         if (interceptor == null) {
168             InputStream JavaDoc is =
169                 this.getServletContext()
170                     .getResourceAsStream("/WEB-INF/interceptors.properties");
171             try {
172                 BufferedReader JavaDoc r =
173                     new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
174                 String JavaDoc line = r.readLine();
175                 while (line != null) {
176                     // Only read non-empty and non-comment lines.
177
if (((line = line.trim()).length() > 0)
178                         && !line.startsWith("#")) {
179                         Class JavaDoc cls = Class.forName(line);
180                         addInterceptor((CoefficientInterceptor) cls.newInstance());
181                     }
182
183                     // Get the next line.
184
line = r.readLine();
185                 }
186             } catch (Exception JavaDoc e) {
187                 //log.error("Failed to create Engine", e);
188
} finally {
189                 try {
190                     if (is != null) {
191                         is.close();
192                     }
193                 } catch (IOException JavaDoc e) {
194                 }
195
196                 //Ignore
197
}
198         }
199     }
200 }
201
Popular Tags