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.context.support;
  17. import javax.servlet.ServletContext;
  18. import org.springframework.web.context.WebApplicationContext;
  19. /**
  20. * Convenience methods to retrieve the root WebApplicationContext for a given
  21. * ServletContext. This is e.g. useful for accessing a Spring context from
  22. * within custom web views or Struts actions.
  23. * @author Juergen Hoeller
  24. * @version $Id: WebApplicationContextUtils.java,v 1.10 2004/03/22 10:32:17 jhoeller Exp $
  25. * @see org.springframework.web.context.ContextLoader
  26. */
  27. public abstract class WebApplicationContextUtils {
  28. /**
  29. * Find the root WebApplicationContext for this web app, which is
  30. * typically loaded via ContextLoaderListener or ContextLoaderServlet.
  31. * @param sc ServletContext to find the web application context for
  32. * @return the root WebApplicationContext for this web app, or null if none
  33. * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
  34. */
  35. public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
  36. Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  37. if (attr == null) {
  38. return null;
  39. }
  40. if (attr instanceof RuntimeException) {
  41. throw (RuntimeException) attr;
  42. }
  43. if (attr instanceof Error) {
  44. throw (Error) attr;
  45. }
  46. if (!(attr instanceof WebApplicationContext)) {
  47. throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr);
  48. }
  49. return (WebApplicationContext) attr;
  50. }
  51. /**
  52. * Find the root WebApplicationContext for this web app, which is
  53. * typically loaded via ContextLoaderListener or ContextLoaderServlet.
  54. * @param sc ServletContext to find the web application context for
  55. * @return the root WebApplicationContext for this web app
  56. * @throws IllegalStateException if the root WebApplicationContext could not be found
  57. * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
  58. */
  59. public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc)
  60. throws IllegalStateException {
  61. WebApplicationContext wac = getWebApplicationContext(sc);
  62. if (wac == null) {
  63. throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
  64. }
  65. return wac;
  66. }
  67. }