1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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. package org.springframework.web.util;
  17. import javax.servlet.ServletContextEvent;
  18. import javax.servlet.ServletContextListener;
  19. /**
  20. * Listener that sets a system property to the web application root directory.
  21. * The key of the system property can be defined with the "webAppRootKey" init
  22. * parameter at the servlet context level (i.e. context-param in web.xml),
  23. * the default key is "webapp.root".
  24. *
  25. * <p>Can be used for toolkits that support substition with system properties
  26. * (i.e. System.getProperty values), like Log4J's "${key}" syntax within log
  27. * file locations.
  28. *
  29. * <p>Note: This listener should be placed before ContextLoaderListener in web.xml,
  30. * at least when used for Log4J. Log4jConfigListener sets the system property
  31. * implicitly, so there's no need for this listener in addition to it.
  32. *
  33. * <p><b>WARNING</b>: Some containers like Tomcat do NOT keep system properties separate
  34. * per web app. You have to use unique "webAppRootKey" context-params per web app
  35. * then, to avoid clashes. Other containers like Resin do isolate each web app's
  36. * system properties: Here you can use the default key (i.e. no "webAppRootKey"
  37. * context-param at all) without worrying.
  38. *
  39. * <p><b>WARNING</b>: The WAR file containing the web application needs to be expanded
  40. * to allow for setting the web app root system property. This is by default not
  41. * the case when a WAR file gets deployed to WebLogic, for example. Do not use
  42. * this listener in such an environment!
  43. *
  44. * @author Juergen Hoeller
  45. * @since 18.04.2003
  46. * @see WebUtils#setWebAppRootSystemProperty
  47. * @see Log4jConfigListener
  48. * @see java.lang.System#getProperty
  49. */
  50. public class WebAppRootListener implements ServletContextListener {
  51. public void contextInitialized(ServletContextEvent event) {
  52. WebUtils.setWebAppRootSystemProperty(event.getServletContext());
  53. }
  54. public void contextDestroyed(ServletContextEvent event) {
  55. }
  56. }