01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.web.util;
18:
19: import javax.servlet.ServletContextEvent;
20: import javax.servlet.ServletContextListener;
21:
22: /**
23: * Listener that sets a system property to the web application root directory.
24: * The key of the system property can be defined with the "webAppRootKey" init
25: * parameter at the servlet context level (i.e. context-param in web.xml),
26: * the default key is "webapp.root".
27: *
28: * <p>Can be used for toolkits that support substition with system properties
29: * (i.e. System.getProperty values), like Log4J's "${key}" syntax within log
30: * file locations.
31: *
32: * <p>Note: This listener should be placed before ContextLoaderListener in web.xml,
33: * at least when used for Log4J. Log4jConfigListener sets the system property
34: * implicitly, so there's no need for this listener in addition to it.
35: *
36: * <p><b>WARNING</b>: Some containers like Tomcat do NOT keep system properties separate
37: * per web app. You have to use unique "webAppRootKey" context-params per web app
38: * then, to avoid clashes. Other containers like Resin do isolate each web app's
39: * system properties: Here you can use the default key (i.e. no "webAppRootKey"
40: * context-param at all) without worrying.
41: *
42: * <p><b>WARNING</b>: The WAR file containing the web application needs to be expanded
43: * to allow for setting the web app root system property. This is by default not
44: * the case when a WAR file gets deployed to WebLogic, for example. Do not use
45: * this listener in such an environment!
46: *
47: * @author Juergen Hoeller
48: * @since 18.04.2003
49: * @see WebUtils#setWebAppRootSystemProperty
50: * @see Log4jConfigListener
51: * @see java.lang.System#getProperty
52: */
53: public class WebAppRootListener implements ServletContextListener {
54:
55: public void contextInitialized(ServletContextEvent event) {
56: WebUtils.setWebAppRootSystemProperty(event.getServletContext());
57: }
58:
59: public void contextDestroyed(ServletContextEvent event) {
60: WebUtils.removeWebAppRootSystemProperty(event
61: .getServletContext());
62: }
63:
64: }
|