1 12 package org.eclipse.equinox.http.servlet.internal; 13 14 import java.io.IOException ; 15 import java.util.Set ; 16 import javax.servlet.*; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 import org.osgi.framework.Bundle; 20 import org.osgi.service.http.HttpContext; 21 22 public class ServletRegistration extends Registration { 25 26 private Servlet servlet; private HttpContext httpContext; private Set servlets; private ClassLoader registeredContextClassLoader; 30 private ProxyContext proxyContext; 31 32 public ServletRegistration(Servlet servlet, ProxyContext proxyContext, HttpContext context, Bundle bundle, Set servlets) { 33 this.servlet = servlet; 34 this.servlets = servlets; 35 this.httpContext = context; 36 this.proxyContext = proxyContext; 37 registeredContextClassLoader = Thread.currentThread().getContextClassLoader(); 38 } 39 40 public void destroy() { 41 ClassLoader original = Thread.currentThread().getContextClassLoader(); 42 try { 43 Thread.currentThread().setContextClassLoader(registeredContextClassLoader); 44 super.destroy(); 45 servlet.destroy(); 46 } finally { 47 Thread.currentThread().setContextClassLoader(original); 48 } 49 } 50 51 public void close() { 52 servlets.remove(servlet); 53 proxyContext.destroyContextAttributes(httpContext); 54 } 55 56 public void init(ServletConfig servletConfig) throws ServletException { 58 boolean initialized = false; 59 proxyContext.createContextAttributes(httpContext); 60 try { 61 ClassLoader original = Thread.currentThread().getContextClassLoader(); 62 try { 63 Thread.currentThread().setContextClassLoader(registeredContextClassLoader); 64 servlet.init(servletConfig); 65 } finally { 66 Thread.currentThread().setContextClassLoader(original); 67 } 68 servlets.add(servlet); 69 initialized = true; 70 } finally { 71 if (! initialized) 72 proxyContext.destroyContextAttributes(httpContext); 73 } 74 } 75 76 public void checkServletRegistration() throws ServletException { 77 if (servlets.contains(servlet)) { 78 throw new ServletException("This servlet has already been registered at a different alias."); } 80 } 81 82 public boolean handleRequest(HttpServletRequest req, HttpServletResponse resp, String alias) throws IOException , ServletException { 84 HttpServletRequest wrappedRequest = new HttpServletRequestAdaptor(req, alias, servlet); 85 86 if (httpContext.handleSecurity(wrappedRequest, resp)) { 87 ClassLoader original = Thread.currentThread().getContextClassLoader(); 88 try { 89 Thread.currentThread().setContextClassLoader(registeredContextClassLoader); 90 servlet.service(wrappedRequest, resp); 91 } finally { 92 Thread.currentThread().setContextClassLoader(original); 93 } 94 } 95 return true; 96 } 97 } 98 | Popular Tags |