KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > servlet > internal > ServletRegistration


1 /*******************************************************************************
2  * Copyright (c) 2005-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.http.servlet.internal;
13
14 import java.io.IOException JavaDoc;
15 import java.util.Set JavaDoc;
16 import javax.servlet.*;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19 import org.osgi.framework.Bundle;
20 import org.osgi.service.http.HttpContext;
21
22 //This class wraps the servlet object registered in the HttpService.registerServlet call, to manage the context classloader when handleRequests are being asked.
23
//It is also responsible to ensure that a given servlet has only been registered once.
24
public class ServletRegistration extends Registration {
25
26     private Servlet servlet; //The actual servlet object registered against the http service. All requests will eventually be delegated to it.
27
private HttpContext httpContext; //The context used during the registration of the servlet
28
private Set JavaDoc servlets; //All the servlets registered against the instance of the proxy servlet that "ownes" self.
29
private ClassLoader JavaDoc registeredContextClassLoader;
30     private ProxyContext proxyContext;
31
32     public ServletRegistration(Servlet servlet, ProxyContext proxyContext, HttpContext context, Bundle bundle, Set JavaDoc 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 JavaDoc 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     //Delegate the init call to the actual servlet
57
public void init(ServletConfig servletConfig) throws ServletException {
58         boolean initialized = false;
59         proxyContext.createContextAttributes(httpContext);
60         try {
61             ClassLoader JavaDoc 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."); //$NON-NLS-1$
79
}
80     }
81
82     //Delegate the handling of the request to the actual servlet
83
public boolean handleRequest(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp, String JavaDoc alias) throws IOException JavaDoc, ServletException {
84         HttpServletRequest JavaDoc wrappedRequest = new HttpServletRequestAdaptor(req, alias, servlet);
85
86         if (httpContext.handleSecurity(wrappedRequest, resp)) {
87             ClassLoader JavaDoc 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