001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.context;
018:
019: import java.io.IOException;
020:
021: import javax.servlet.ServletException;
022: import javax.servlet.http.HttpServlet;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: /**
027: * Bootstrap servlet to start up Spring's root {@link WebApplicationContext}.
028: * Simply delegates to {@link ContextLoader}.
029: *
030: * <p>This servlet should have a lower <code>load-on-startup</code> value
031: * in <code>web.xml</code> than any servlets that access the root web
032: * application context.
033: *
034: * <p><i>Note that this class has been deprecated for containers implementing
035: * Servlet API 2.4 or higher, in favor of {@link ContextLoaderListener}.</i><br>
036: * According to Servlet 2.4, listeners must be initialized before load-on-startup
037: * servlets. Many Servlet 2.3 containers already enforce this behavior. If you
038: * use such a container, this servlet can be replaced with ContextLoaderListener.
039: * Else or if working with a Servlet 2.2 container, stick with this servlet.
040: *
041: * <p>Servlet 2.3 containers known to work with bootstrap listeners are:
042: * <ul>
043: * <li>Apache Tomcat 4.x+
044: * <li>Jetty 4.x+
045: * <li>Resin 2.1.8+
046: * <li>Orion 2.0.2+
047: * <li>BEA WebLogic 8.1 SP3
048: * </ul>
049: * For working with any of them, ContextLoaderListener is recommended.
050: *
051: * <p>Servlet 2.3 containers known <i>not</i> to work with bootstrap listeners are:
052: * <ul>
053: * <li>BEA WebLogic up to 8.1 SP2
054: * <li>IBM WebSphere 5.x
055: * <li>Oracle OC4J 9.0.3
056: * </ul>
057: * If you happen to work with such a server, this servlet has to be used.
058: *
059: * <p>So unfortunately, the only context initialization option that is compatible
060: * with <i>all</i> Servlet 2.3 containers is this servlet.
061: *
062: * <p>Note that a startup failure of this servlet will not stop the rest of the
063: * web application from starting, in contrast to a listener failure. This can
064: * lead to peculiar side effects if other servlets get started that depend on
065: * initialization of the root web application context.
066: *
067: * @author Juergen Hoeller
068: * @author Darren Davison
069: * @see ContextLoaderListener
070: * @see org.springframework.web.util.Log4jConfigServlet
071: */
072: public class ContextLoaderServlet extends HttpServlet {
073:
074: private ContextLoader contextLoader;
075:
076: /**
077: * Initialize the root web application context.
078: */
079: public void init() throws ServletException {
080: this .contextLoader = createContextLoader();
081: this .contextLoader
082: .initWebApplicationContext(getServletContext());
083: }
084:
085: /**
086: * Create the ContextLoader to use. Can be overridden in subclasses.
087: * @return the new ContextLoader
088: */
089: protected ContextLoader createContextLoader() {
090: return new ContextLoader();
091: }
092:
093: /**
094: * Return the ContextLoader used by this servlet.
095: * @return the current ContextLoader
096: */
097: public ContextLoader getContextLoader() {
098: return this .contextLoader;
099: }
100:
101: /**
102: * Close the root web application context.
103: */
104: public void destroy() {
105: if (this .contextLoader != null) {
106: this .contextLoader
107: .closeWebApplicationContext(getServletContext());
108: }
109: }
110:
111: /**
112: * This should never even be called since no mapping to this servlet should
113: * ever be created in web.xml. That's why a correctly invoked Servlet 2.3
114: * listener is much more appropriate for initialization work ;-)
115: */
116: public void service(HttpServletRequest request,
117: HttpServletResponse response) throws IOException {
118: getServletContext().log(
119: "Attempt to call service method on ContextLoaderServlet as ["
120: + request.getRequestURI() + "] was ignored");
121: response.sendError(HttpServletResponse.SC_BAD_REQUEST);
122: }
123:
124: public String getServletInfo() {
125: return "ContextLoaderServlet for Servlet API 2.2/2.3 "
126: + "(deprecated in favor of ContextLoaderListener for Servlet API 2.4)";
127: }
128:
129: }
|