KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > jsp > jasper > JspServlet


1 /*******************************************************************************
2  * Copyright (c) 2006-2007 Cognos Incorporated, IBM Corporation and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Cognos Incorporated - initial API and implementation
10  * IBM Corporation - bug fixes and enhancements
11  *******************************************************************************/

12 package org.eclipse.equinox.jsp.jasper;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.net.URLClassLoader JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.servlet.*;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.eclipse.equinox.internal.jsp.jasper.JspClassLoader;
30 import org.osgi.framework.Bundle;
31
32 /**
33  * <p>
34  * JSPServlet wraps the Apache Jasper Servlet making it appropriate for running in an OSGi environment under the Http Service.
35  * The Jasper JSPServlet makes use of the Thread Context Classloader to support compile and runtime of JSPs and to accommodate running
36  * in an OSGi environment, a Bundle is used to provide the similar context normally provided by the webapp.
37  * </p>
38  * <p>
39  * The Jasper Servlet will search the ServletContext to find JSPs, tag library descriptors, and additional information in the web.xml
40  * as per the JSP 2.0 specification. In addition to the ServletContext this implementation will search the bundle (but not attached
41  * fragments) for matching resources in a manner consistent with the Http Service's notion of a resource. By using alias and bundleResourcePath the JSP lookup should be in
42  * line with the resource mapping specified in {102.4} of the OSGi HttpService.
43  * </p>
44  * <p>
45  * TLD discovery is slightly different, to clarify it occurs in one of three ways:
46  * <ol>
47  * <li> declarations found in /WEB-INF/web.xml (found either on the bundleResourcePath in the bundle or in the ServletContext)</li>
48  * <li> tld files found under /WEB-INF (found either on the bundleResourcePath in the bundle or in the ServletContext)</li>
49  * <li> tld files found in jars on the Bundle-Classpath (see org.eclipse.equinox.internal.jsp.jasper.JSPClassLoader)</li>
50  * </ol>
51  * </p>
52  * <p>
53  * Other than the setting and resetting of the thread context classloader and additional resource lookups in the bundle the JSPServlet
54  * is behaviourally consistent with the JSP 2.0 specification and regular Jasper operation.
55  * </p>
56  */

57
58 public class JspServlet extends HttpServlet JavaDoc {
59     private static final long serialVersionUID = -4110476909131707652L;
60     private Servlet JavaDoc jspServlet = new org.apache.jasper.servlet.JspServlet();
61     Bundle bundle;
62     private URLClassLoader JavaDoc jspLoader;
63     String JavaDoc bundleResourcePath;
64     String JavaDoc alias;
65
66     public JspServlet(Bundle bundle, String JavaDoc bundleResourcePath, String JavaDoc alias) {
67         this.bundle = bundle;
68         this.bundleResourcePath = (bundleResourcePath == null || bundleResourcePath.equals("/")) ? "" : bundleResourcePath; //$NON-NLS-1$ //$NON-NLS-2$
69
this.alias = (alias == null || alias.equals("/")) ? null : alias; //$NON-NLS-1$
70
jspLoader = new JspClassLoader(bundle);
71     }
72
73     public JspServlet(Bundle bundle, String JavaDoc bundleResourcePath) {
74         this(bundle, bundleResourcePath, null);
75     }
76
77     public void init(ServletConfig config) throws ServletException {
78         ClassLoader JavaDoc original = Thread.currentThread().getContextClassLoader();
79         try {
80             Thread.currentThread().setContextClassLoader(jspLoader);
81             jspServlet.init(new ServletConfigAdaptor(config));
82         } finally {
83             Thread.currentThread().setContextClassLoader(original);
84         }
85     }
86
87     public void destroy() {
88         ClassLoader JavaDoc original = Thread.currentThread().getContextClassLoader();
89         try {
90             Thread.currentThread().setContextClassLoader(jspLoader);
91             jspServlet.destroy();
92         } finally {
93             Thread.currentThread().setContextClassLoader(original);
94         }
95     }
96
97     public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException, IOException JavaDoc {
98         String JavaDoc pathInfo = request.getPathInfo();
99         if (pathInfo != null && pathInfo.startsWith("/WEB-INF/")) { //$NON-NLS-1$
100
response.sendError(HttpServletResponse.SC_NOT_FOUND);
101             return;
102         }
103
104         ClassLoader JavaDoc original = Thread.currentThread().getContextClassLoader();
105         try {
106             Thread.currentThread().setContextClassLoader(jspLoader);
107             jspServlet.service(request, response);
108         } finally {
109             Thread.currentThread().setContextClassLoader(original);
110         }
111     }
112
113     public ServletConfig getServletConfig() {
114         return jspServlet.getServletConfig();
115     }
116
117     public String JavaDoc getServletInfo() {
118         return jspServlet.getServletInfo();
119     }
120
121     private class ServletConfigAdaptor implements ServletConfig {
122         private ServletConfig config;
123         private ServletContext context;
124
125         public ServletConfigAdaptor(ServletConfig config) {
126             this.config = config;
127             this.context = new ServletContextAdaptor(config.getServletContext());
128         }
129
130         public String JavaDoc getInitParameter(String JavaDoc arg0) {
131             return config.getInitParameter(arg0);
132         }
133
134         public Enumeration JavaDoc getInitParameterNames() {
135             return config.getInitParameterNames();
136         }
137
138         public ServletContext getServletContext() {
139             return context;
140         }
141
142         public String JavaDoc getServletName() {
143             return config.getServletName();
144         }
145     }
146
147     private class ServletContextAdaptor implements ServletContext {
148         private ServletContext delegate;
149
150         public ServletContextAdaptor(ServletContext delegate) {
151             this.delegate = delegate;
152         }
153
154         public URL JavaDoc getResource(String JavaDoc name) throws MalformedURLException JavaDoc {
155             if (alias != null && name.startsWith(alias))
156                 name = name.substring(alias.length());
157
158             String JavaDoc resourceName = bundleResourcePath + name;
159             int lastSlash = resourceName.lastIndexOf('/');
160             if (lastSlash == -1)
161                 return null;
162             
163             String JavaDoc path = resourceName.substring(0, lastSlash);
164             if (path.length() == 0)
165                 path = "/"; //$NON-NLS-1$
166
String JavaDoc file = resourceName.substring(lastSlash + 1);
167             Enumeration JavaDoc entryPaths = bundle.findEntries(path, file, false);
168             if (entryPaths != null && entryPaths.hasMoreElements())
169                 return (URL JavaDoc) entryPaths.nextElement();
170
171             return delegate.getResource(name);
172         }
173
174         public InputStream JavaDoc getResourceAsStream(String JavaDoc name) {
175             try {
176                 URL JavaDoc resourceURL = getResource(name);
177                 if (resourceURL != null)
178                     return resourceURL.openStream();
179             } catch (IOException JavaDoc e) {
180                 log("Error opening stream for resource '" + name + "'", e); //$NON-NLS-1$ //$NON-NLS-2$
181
}
182             return null;
183         }
184
185         public Set JavaDoc getResourcePaths(String JavaDoc name) {
186             Set JavaDoc result = delegate.getResourcePaths(name);
187             Enumeration JavaDoc e = bundle.findEntries(bundleResourcePath + name, null, false);
188             if (e != null) {
189                 if (result == null)
190                     result = new HashSet JavaDoc();
191                 while (e.hasMoreElements()) {
192                     URL JavaDoc entryURL = (URL JavaDoc) e.nextElement();
193                     result.add(entryURL.getFile().substring(bundleResourcePath.length()));
194                 }
195             }
196             return result;
197         }
198
199         public RequestDispatcher getRequestDispatcher(String JavaDoc arg0) {
200             return delegate.getRequestDispatcher(arg0);
201         }
202
203         public Object JavaDoc getAttribute(String JavaDoc arg0) {
204             return delegate.getAttribute(arg0);
205         }
206
207         public Enumeration JavaDoc getAttributeNames() {
208             return delegate.getAttributeNames();
209         }
210
211         public ServletContext getContext(String JavaDoc arg0) {
212             return delegate.getContext(arg0);
213         }
214
215         public String JavaDoc getInitParameter(String JavaDoc arg0) {
216             return delegate.getInitParameter(arg0);
217         }
218
219         public Enumeration JavaDoc getInitParameterNames() {
220             return delegate.getInitParameterNames();
221         }
222
223         public int getMajorVersion() {
224             return delegate.getMajorVersion();
225         }
226
227         public String JavaDoc getMimeType(String JavaDoc arg0) {
228             return delegate.getMimeType(arg0);
229         }
230
231         public int getMinorVersion() {
232             return delegate.getMinorVersion();
233         }
234
235         public RequestDispatcher getNamedDispatcher(String JavaDoc arg0) {
236             return delegate.getNamedDispatcher(arg0);
237         }
238
239         public String JavaDoc getRealPath(String JavaDoc arg0) {
240             return delegate.getRealPath(arg0);
241         }
242
243         public String JavaDoc getServerInfo() {
244             return delegate.getServerInfo();
245         }
246
247         /** @deprecated **/
248         public Servlet JavaDoc getServlet(String JavaDoc arg0) throws ServletException {
249             return delegate.getServlet(arg0);
250         }
251
252         public String JavaDoc getServletContextName() {
253             return delegate.getServletContextName();
254         }
255
256         /** @deprecated **/
257         public Enumeration JavaDoc getServletNames() {
258             return delegate.getServletNames();
259         }
260
261         /** @deprecated **/
262         public Enumeration JavaDoc getServlets() {
263             return delegate.getServlets();
264         }
265
266         /** @deprecated **/
267         public void log(Exception JavaDoc arg0, String JavaDoc arg1) {
268             delegate.log(arg0, arg1);
269         }
270
271         public void log(String JavaDoc arg0, Throwable JavaDoc arg1) {
272             delegate.log(arg0, arg1);
273         }
274
275         public void log(String JavaDoc arg0) {
276             delegate.log(arg0);
277         }
278
279         public void removeAttribute(String JavaDoc arg0) {
280             delegate.removeAttribute(arg0);
281         }
282
283         public void setAttribute(String JavaDoc arg0, Object JavaDoc arg1) {
284             delegate.setAttribute(arg0, arg1);
285         }
286         
287         // Added in Servlet 2.5
288
public String JavaDoc getContextPath() {
289             try {
290                 Method JavaDoc getContextPathMethod = delegate.getClass().getMethod("getContextPath", null); //$NON-NLS-1$
291
return (String JavaDoc) getContextPathMethod.invoke(delegate, null);
292             } catch (Exception JavaDoc e) {
293                 // ignore
294
}
295             return null;
296         }
297     }
298 }
299
Popular Tags