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: *
040: * <p>Servlet 2.3 containers known to work with bootstrap listeners are:
041: * <ul>
042: * <li>Apache Tomcat 4.x+
043: * <li>Jetty 4.x+
044: * <li>Resin 2.1.8+
045: * <li>Orion 2.0.2+
046: * <li>BEA WebLogic 8.1 SP3
047: * </ul>
048: * For working with any of them, ContextLoaderListener is recommended.
049: *
050: * <p>Servlet 2.3 containers known <i>not</i> to work with bootstrap listeners are:
051: * <ul>
052: * <li>BEA WebLogic up to 8.1 SP2
053: * <li>IBM WebSphere 5.x
054: * <li>Oracle OC4J 9.0.3
055: * </ul>
056: * If you happen to work with such a server, this servlet has to be used.
057: *
058: * <p>So unfortunately, the only context initialization option that is compatible
059: * with <i>all</i> Servlet 2.3 containers is this servlet.
060: *
061: * <p>Note that a startup failure of this servlet will not stop the rest of the
062: * web application from starting, in contrast to a listener failure. This can
063: * lead to peculiar side effects if other servlets get started that depend on
064: * initialization of the root web application context.
065: *
066: * @author Juergen Hoeller
067: * @author Darren Davison
068: * @see ContextLoaderListener
069: * @see org.springframework.web.util.Log4jConfigServlet
070: */
071: public class ContextLoaderServlet extends HttpServlet {
072:
073: private ContextLoader contextLoader;
074:
075: /**
076: * Initialize the root web application context.
077: */
078: public void init() throws ServletException {
079: this .contextLoader = createContextLoader();
080: this .contextLoader
081: .initWebApplicationContext(getServletContext());
082: }
083:
084: /**
085: * Create the ContextLoader to use. Can be overridden in subclasses.
086: * @return the new ContextLoader
087: */
088: protected ContextLoader createContextLoader() {
089: return new ContextLoader();
090: }
091:
092: /**
093: * Return the ContextLoader used by this servlet.
094: * @return the current ContextLoader
095: */
096: public ContextLoader getContextLoader() {
097: return this .contextLoader;
098: }
099:
100: /**
101: * Close the root web application context.
102: */
103: public void destroy() {
104: if (this .contextLoader != null) {
105: this .contextLoader
106: .closeWebApplicationContext(getServletContext());
107: }
108: }
109:
110: /**
111: * This should never even be called since no mapping to this servlet should
112: * ever be created in web.xml. That's why a correctly invoked Servlet 2.3
113: * listener is much more appropriate for initialization work ;-)
114: */
115: public void service(HttpServletRequest request,
116: HttpServletResponse response) throws IOException {
117: getServletContext().log(
118: "Attempt to call service method on ContextLoaderServlet as ["
119: + request.getRequestURI() + "] was ignored");
120: response.sendError(HttpServletResponse.SC_BAD_REQUEST);
121: }
122:
123: public String getServletInfo() {
124: return "ContextLoaderServlet for Servlet API 2.3 "
125: + "(deprecated in favor of ContextLoaderListener for Servlet API 2.4)";
126: }
127:
128: }
|