KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > servlet > BootstrapServlet


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

16 package org.apache.cocoon.servlet;
17
18 import java.io.File JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.servlet.RequestDispatcher JavaDoc;
26 import javax.servlet.Servlet JavaDoc;
27 import javax.servlet.ServletConfig JavaDoc;
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30
31 /**
32  * A bootstrap servlet to allow Cocoon to run in servlet engines that aren't fully
33  * compliant with the servlet 2.2 spec.
34  * <p>
35  * This servlet adds a mandatory "context-dir" parameter to those accepted by {@link CocoonServlet},
36  * which should point to Cocoon's context directory (e.g. "<code>/path-to-webapp/cocoon</code>").
37  * This directory is used to :
38  * <ul>
39  * <li>build a classloader with the correct class path with the contents of
40  * <code>WEB-INF/classes</code> and <code>WEB-INF/lib</code> (see
41  * {@link ParanoidClassLoader}),</li>
42  * <li>resolve paths for context resources.
43  * </ul>
44  *
45  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
46  * @version CVS $Id: BootstrapServlet.java 30932 2004-07-29 17:35:38Z vgritsenko $
47  */

48
49 public class BootstrapServlet extends ParanoidCocoonServlet {
50     
51     protected File JavaDoc contextDir;
52     
53     protected File JavaDoc getContextDir() throws ServletException JavaDoc {
54         
55         ServletContext JavaDoc context = getServletContext();
56         ServletConfig JavaDoc config = getServletConfig();
57         
58         log("getRealPath(\"/\") = " + context.getRealPath("/"));
59
60         String JavaDoc contextDirParam = config.getInitParameter("context-directory");
61         
62         if (contextDirParam == null) {
63                 throw new ServletException JavaDoc("The 'context-directory' parameter must be set to the root of the servlet context");
64         }
65         
66         // Ensure context dir doesn't end with a "/" (servlet spec says that paths for
67
// getResource() should start by a "/")
68
if (contextDirParam.endsWith("/")) {
69             contextDirParam = contextDirParam.substring(0, contextDirParam.length() - 1);
70         }
71         
72         // Ensure context dir exists and is a directory
73
this.contextDir = new File JavaDoc(contextDirParam);
74         if (!this.contextDir.exists()) {
75             String JavaDoc msg = "Context dir '" + this.contextDir + "' doesn't exist";
76             log(msg);
77             throw new ServletException JavaDoc(msg);
78         }
79
80         if (!this.contextDir.isDirectory()) {
81             String JavaDoc msg = "Context dir '" + this.contextDir + "' should be a directory";
82             log(msg);
83             throw new ServletException JavaDoc(msg);
84         }
85         
86         context.log("Context dir set to " + this.contextDir);
87         
88         return this.contextDir;
89     }
90
91
92     protected void initServlet() throws ServletException JavaDoc {
93         
94         ServletContext JavaDoc newContext = new ContextWrapper(getServletContext(), this.contextDir);
95         ServletConfig JavaDoc newConfig = new ConfigWrapper(getServletConfig(), newContext);
96         
97         this.servlet.init(newConfig);
98     }
99
100     //-------------------------------------------------------------------------
101
/**
102      * Implementation of <code>ServletConfig</code> passed to the actual servlet.
103      * It wraps the original config object and returns the new context.
104      */

105     public static class ConfigWrapper implements ServletConfig JavaDoc {
106         ServletConfig JavaDoc config;
107         ServletContext JavaDoc context;
108         
109         /**
110          * Builds a <code>ServletConfig</code> using a servlet name and
111          * a <code>ServletContext</code>.
112          */

113         public ConfigWrapper(ServletConfig JavaDoc config, ServletContext JavaDoc context) {
114             this.config = config;
115             this.context = context;
116         }
117         public String JavaDoc getServletName() {
118             return config.getServletName();
119         }
120         
121         public Enumeration JavaDoc getInitParameterNames() {
122             return this.config.getInitParameterNames();
123         }
124         
125         public ServletContext JavaDoc getServletContext() {
126             return this.context;
127         }
128         
129         public String JavaDoc getInitParameter(String JavaDoc name) {
130             return config.getInitParameter(name);
131         }
132     }
133
134     //-------------------------------------------------------------------------
135
/**
136      * Wrapper for the <code>ServletContext</code> passed to the actual servlet.
137      * It implements all resource-related methods using the provided context
138      * root directory. Other calls are delegated to the wrapped context.
139      */

140     public static class ContextWrapper implements ServletContext JavaDoc {
141         ServletContext JavaDoc context;
142         File JavaDoc contextRoot;
143         
144         /**
145          * Builds a wrapper around an existing context, and handle all
146          * resource resolution relatively to <code>contextRoot</code>
147          */

148         public ContextWrapper(ServletContext JavaDoc context, File JavaDoc contextRoot) {
149             this.context = context;
150             this.contextRoot = contextRoot;
151         }
152         
153         public ServletContext JavaDoc getContext(String JavaDoc param) {
154             return this.context.getContext(param);
155         }
156     
157         public int getMajorVersion() {
158             return this.context.getMajorVersion();
159         }
160     
161         public int getMinorVersion() {
162             return this.context.getMinorVersion();
163         }
164     
165         public String JavaDoc getMimeType(String JavaDoc param) {
166             return this.context.getMimeType(param);
167         }
168
169         /**
170          * Returns the resource URL by appending <code>path</code> to the context
171          * root. If this doesn't point to an existing file, <code>null</code> is
172          * returned.
173          */

174         public URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc {
175             File JavaDoc file = new File JavaDoc(this.contextRoot, path);
176             if (file.exists()) {
177                 URL JavaDoc result = file.toURL();
178                 //this.context.log("getResource(" + path + ") = " + result);
179
return result;
180             } else {
181                 //this.context.log("getResource(" + path + ") = null");
182
return null;
183             }
184         }
185     
186         /**
187          * Returns the stream for the result of <code>getResource()</code>, or
188          * <code>null</code> if the resource doesn't exist.
189          */

190         public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
191             try {
192                 URL JavaDoc url = getResource(path);
193                 return (url == null) ? null : url.openStream();
194             } catch(Exception JavaDoc e) {
195                 this.context.log("getResourceAsStream(" + path + ") failed", e);
196                 return null;
197             }
198         }
199     
200         public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc param) {
201             return this.context.getRequestDispatcher(param);
202         }
203     
204         public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc param) {
205             return this.context.getNamedDispatcher(param);
206         }
207
208         /**
209          * @deprecated The method BootstrapServlet.ContextWrapper.getServlet(String)
210          * overrides a deprecated method from ServletContext.
211          * @see <a HREF="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getServlet(java.lang.String)">ServletContext#getServlet(java.lang.String)</a>
212          */

213         public Servlet JavaDoc getServlet(String JavaDoc param) throws ServletException JavaDoc {
214             return this.context.getServlet(param);
215         }
216
217         /**
218          * @deprecated The method BootstrapServlet.ContextWrapper.getServlets()
219          * overrides a deprecated method from ServletContext.
220          * @see <a HREF="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getServlets()">ServletContext#getServlets()</a>
221          */

222         public Enumeration JavaDoc getServlets() {
223             return this.context.getServlets();
224         }
225
226         /**
227          * @deprecated The method BootstrapServlet.ContextWrapper.getServletNames()
228          * overrides a deprecated method from ServletContext.
229          * @see <a HREF="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getServletNames()">ServletContext#getServletNames()</a>
230          */

231         public Enumeration JavaDoc getServletNames() {
232             return this.context.getServletNames();
233         }
234     
235         public void log(String JavaDoc msg) {
236             this.context.log(msg);
237         }
238
239         /** @deprecated use {@link #log(String message, Throwable throwable)} instead. */
240         public void log(Exception JavaDoc ex, String JavaDoc msg) {
241             this.context.log(ex, msg);
242         }
243     
244         public void log(String JavaDoc msg, Throwable JavaDoc thr) {
245             this.context.log(msg, thr);
246         }
247
248         /**
249          * Appends <code>path</code> to the context root.
250          */

251         public String JavaDoc getRealPath(String JavaDoc path) {
252             String JavaDoc result = this.contextRoot + path;
253             //this.context.log("getRealPath(" + path + ") = " + result);
254
return result;
255         }
256     
257         public String JavaDoc getServerInfo() {
258             return this.context.getServerInfo();
259         }
260     
261         public String JavaDoc getInitParameter(String JavaDoc param) {
262             return this.context.getInitParameter(param);
263         }
264     
265         public Enumeration JavaDoc getInitParameterNames() {
266             return this.context.getInitParameterNames();
267         }
268     
269         public Object JavaDoc getAttribute(String JavaDoc param) {
270             Object JavaDoc result = this.context.getAttribute(param);
271             //this.context.log("getAttribute(" + param + ") = " + result);
272
return result;
273         }
274     
275         public Enumeration JavaDoc getAttributeNames() {
276             return this.context.getAttributeNames();
277         }
278     
279         public void setAttribute(String JavaDoc name, Object JavaDoc value) {
280             this.context.setAttribute(name, value);
281         }
282     
283         public void removeAttribute(String JavaDoc name) {
284             this.context.removeAttribute(name);
285         }
286         
287         // Implementation of Servlet 2.3 methods. This is not absolutely required
288
// for real usage since this servlet is targeted at 2.2, but is needed
289
// for successful compilation
290
public Set JavaDoc getResourcePaths(String JavaDoc param) {
291             return null;
292         }
293         
294         public String JavaDoc getServletContextName() {
295             return "Cocoon context";
296         }
297     }
298 }
299
300
Popular Tags