View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.web.context;
18  
19  import java.io.IOException;
20  
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServlet;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.springframework.web.context.ContextLoader;
27  
28  /**
29   * Bootstrap servlet to start up the WebApplicationContext.
30   * Simply delegates to ModuleContextLoader.
31   *
32   * Note that this class has been deprecated for containers implementing
33   * Servlet API 2.4 or higher in favour of ModuleContextLoaderListener.
34   *
35   * @svnLink $Revision: 4091 $;$Date: 2010-01-15 12:21:07 +0100 (Fr, 15. Jan 2010) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/web/jar/src/main/java/ch/elca/el4j/web/context/ModuleContextLoaderServlet.java $
36   *
37   * @author Stefan Wismer (SWI)
38   * @author Jonas Hauenstein (JHN)
39   */
40  public class ModuleContextLoaderServlet extends HttpServlet {
41  
42  	/**
43  	 * The Context Loader to be used.
44  	 */
45  	private ContextLoader contextLoader;
46  
47  	/**
48  	 * Initialize the root web application context.
49  	 */
50  	public void init() throws ServletException {
51  		this.contextLoader = createContextLoader();
52  		this.contextLoader.initWebApplicationContext(getServletContext());
53  	}
54  
55  	/**
56  	 * Create the ContextLoader to use. Can be overridden in subclasses.
57  	 * Creates the ModuleContextLoader as default.
58  	 * @return the new ContextLoader
59  	 */
60  	protected ContextLoader createContextLoader() {
61  		return new ModuleContextLoader();
62  	}
63  
64  	/**
65  	 * Return the ContextLoader used by this servlet. 
66  	 * @return the current ContextLoader
67  	 */
68  	public ContextLoader getContextLoader() {
69  		return this.contextLoader;
70  	}
71  
72  
73  	/**
74  	 * Close the root web application context.
75  	 */
76  	public void destroy() {
77  		if (this.contextLoader != null) {
78  			this.contextLoader.closeWebApplicationContext(getServletContext());
79  		}
80  	}
81  
82  
83  	/**
84  	 * This should never even be called since no mapping to this servlet should
85  	 * ever be created in web.xml. That's why a correctly invoked Servlet 2.3
86  	 * listener is much more appropriate for initialization work ;-)
87  	 */
88  	public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
89  		getServletContext().log(
90  				"Attempt to call service method on ContextLoaderServlet as ["
91  				+ request.getRequestURI() + "] was ignored");
92  		response.sendError(HttpServletResponse.SC_BAD_REQUEST);
93  	}
94  
95  
96  	/** {@inheritDoc} */
97  	public String getServletInfo() {
98  		return "ContextLoaderServlet for Servlet API 2.3 "
99  			+ "(deprecated in favor of ContextLoaderListener for Servlet API 2.4)";
100 	}
101 }