1 12 package org.eclipse.equinox.jsp.jasper; 13 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.lang.reflect.Method ; 17 import java.net.MalformedURLException ; 18 import java.net.URL ; 19 import java.net.URLClassLoader ; 20 import java.util.Enumeration ; 21 import java.util.HashSet ; 22 import java.util.Set ; 23 24 import javax.servlet.*; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.eclipse.equinox.internal.jsp.jasper.JspClassLoader; 30 import org.osgi.framework.Bundle; 31 32 57 58 public class JspServlet extends HttpServlet { 59 private static final long serialVersionUID = -4110476909131707652L; 60 private Servlet jspServlet = new org.apache.jasper.servlet.JspServlet(); 61 Bundle bundle; 62 private URLClassLoader jspLoader; 63 String bundleResourcePath; 64 String alias; 65 66 public JspServlet(Bundle bundle, String bundleResourcePath, String alias) { 67 this.bundle = bundle; 68 this.bundleResourcePath = (bundleResourcePath == null || bundleResourcePath.equals("/")) ? "" : bundleResourcePath; this.alias = (alias == null || alias.equals("/")) ? null : alias; jspLoader = new JspClassLoader(bundle); 71 } 72 73 public JspServlet(Bundle bundle, String bundleResourcePath) { 74 this(bundle, bundleResourcePath, null); 75 } 76 77 public void init(ServletConfig config) throws ServletException { 78 ClassLoader 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 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 request, HttpServletResponse response) throws ServletException, IOException { 98 String pathInfo = request.getPathInfo(); 99 if (pathInfo != null && pathInfo.startsWith("/WEB-INF/")) { response.sendError(HttpServletResponse.SC_NOT_FOUND); 101 return; 102 } 103 104 ClassLoader 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 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 getInitParameter(String arg0) { 131 return config.getInitParameter(arg0); 132 } 133 134 public Enumeration getInitParameterNames() { 135 return config.getInitParameterNames(); 136 } 137 138 public ServletContext getServletContext() { 139 return context; 140 } 141 142 public String 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 getResource(String name) throws MalformedURLException { 155 if (alias != null && name.startsWith(alias)) 156 name = name.substring(alias.length()); 157 158 String resourceName = bundleResourcePath + name; 159 int lastSlash = resourceName.lastIndexOf('/'); 160 if (lastSlash == -1) 161 return null; 162 163 String path = resourceName.substring(0, lastSlash); 164 if (path.length() == 0) 165 path = "/"; String file = resourceName.substring(lastSlash + 1); 167 Enumeration entryPaths = bundle.findEntries(path, file, false); 168 if (entryPaths != null && entryPaths.hasMoreElements()) 169 return (URL ) entryPaths.nextElement(); 170 171 return delegate.getResource(name); 172 } 173 174 public InputStream getResourceAsStream(String name) { 175 try { 176 URL resourceURL = getResource(name); 177 if (resourceURL != null) 178 return resourceURL.openStream(); 179 } catch (IOException e) { 180 log("Error opening stream for resource '" + name + "'", e); } 182 return null; 183 } 184 185 public Set getResourcePaths(String name) { 186 Set result = delegate.getResourcePaths(name); 187 Enumeration e = bundle.findEntries(bundleResourcePath + name, null, false); 188 if (e != null) { 189 if (result == null) 190 result = new HashSet (); 191 while (e.hasMoreElements()) { 192 URL entryURL = (URL ) e.nextElement(); 193 result.add(entryURL.getFile().substring(bundleResourcePath.length())); 194 } 195 } 196 return result; 197 } 198 199 public RequestDispatcher getRequestDispatcher(String arg0) { 200 return delegate.getRequestDispatcher(arg0); 201 } 202 203 public Object getAttribute(String arg0) { 204 return delegate.getAttribute(arg0); 205 } 206 207 public Enumeration getAttributeNames() { 208 return delegate.getAttributeNames(); 209 } 210 211 public ServletContext getContext(String arg0) { 212 return delegate.getContext(arg0); 213 } 214 215 public String getInitParameter(String arg0) { 216 return delegate.getInitParameter(arg0); 217 } 218 219 public Enumeration getInitParameterNames() { 220 return delegate.getInitParameterNames(); 221 } 222 223 public int getMajorVersion() { 224 return delegate.getMajorVersion(); 225 } 226 227 public String getMimeType(String arg0) { 228 return delegate.getMimeType(arg0); 229 } 230 231 public int getMinorVersion() { 232 return delegate.getMinorVersion(); 233 } 234 235 public RequestDispatcher getNamedDispatcher(String arg0) { 236 return delegate.getNamedDispatcher(arg0); 237 } 238 239 public String getRealPath(String arg0) { 240 return delegate.getRealPath(arg0); 241 } 242 243 public String getServerInfo() { 244 return delegate.getServerInfo(); 245 } 246 247 248 public Servlet getServlet(String arg0) throws ServletException { 249 return delegate.getServlet(arg0); 250 } 251 252 public String getServletContextName() { 253 return delegate.getServletContextName(); 254 } 255 256 257 public Enumeration getServletNames() { 258 return delegate.getServletNames(); 259 } 260 261 262 public Enumeration getServlets() { 263 return delegate.getServlets(); 264 } 265 266 267 public void log(Exception arg0, String arg1) { 268 delegate.log(arg0, arg1); 269 } 270 271 public void log(String arg0, Throwable arg1) { 272 delegate.log(arg0, arg1); 273 } 274 275 public void log(String arg0) { 276 delegate.log(arg0); 277 } 278 279 public void removeAttribute(String arg0) { 280 delegate.removeAttribute(arg0); 281 } 282 283 public void setAttribute(String arg0, Object arg1) { 284 delegate.setAttribute(arg0, arg1); 285 } 286 287 public String getContextPath() { 289 try { 290 Method getContextPathMethod = delegate.getClass().getMethod("getContextPath", null); return (String ) getContextPathMethod.invoke(delegate, null); 292 } catch (Exception e) { 293 } 295 return null; 296 } 297 } 298 } 299 | Popular Tags |