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.context.support;
18:
19: import javax.servlet.ServletContext;
20:
21: import org.springframework.util.Assert;
22: import org.springframework.web.context.WebApplicationContext;
23:
24: /**
25: * Convenience methods to retrieve the root WebApplicationContext for a given
26: * ServletContext. This is e.g. useful for accessing a Spring context from
27: * within custom web views or Struts actions.
28: *
29: * <p>Note that there are more convenient ways of accessing the root context for
30: * many web frameworks, either part of Spring or available as external library.
31: * This helper class is just the most generic way to access the root context.
32: *
33: * @author Juergen Hoeller
34: * @see org.springframework.web.context.ContextLoader
35: * @see org.springframework.web.servlet.FrameworkServlet
36: * @see org.springframework.web.servlet.DispatcherServlet
37: * @see org.springframework.web.struts.ActionSupport
38: * @see org.springframework.web.struts.DelegatingActionProxy
39: * @see org.springframework.web.jsf.FacesContextUtils
40: * @see org.springframework.web.jsf.DelegatingVariableResolver
41: */
42: public abstract class WebApplicationContextUtils {
43:
44: /**
45: * Find the root WebApplicationContext for this web application, which is
46: * typically loaded via ContextLoaderListener or ContextLoaderServlet.
47: * <p>Will rethrow an exception that happened on root context startup,
48: * to differentiate between a failed context startup and no context at all.
49: * @param sc ServletContext to find the web application context for
50: * @return the root WebApplicationContext for this web app, or <code>null</code> if none
51: * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
52: */
53: public static WebApplicationContext getWebApplicationContext(
54: ServletContext sc) {
55: Assert.notNull(sc, "ServletContext must not be null");
56: Object attr = sc
57: .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
58: if (attr == null) {
59: return null;
60: }
61: if (attr instanceof RuntimeException) {
62: throw (RuntimeException) attr;
63: }
64: if (attr instanceof Error) {
65: throw (Error) attr;
66: }
67: if (!(attr instanceof WebApplicationContext)) {
68: throw new IllegalStateException(
69: "Root context attribute is not of type WebApplicationContext: "
70: + attr);
71: }
72: return (WebApplicationContext) attr;
73: }
74:
75: /**
76: * Find the root WebApplicationContext for this web application, which is
77: * typically loaded via ContextLoaderListener or ContextLoaderServlet.
78: * <p>Will rethrow an exception that happened on root context startup,
79: * to differentiate between a failed context startup and no context at all.
80: * @param sc ServletContext to find the web application context for
81: * @return the root WebApplicationContext for this web app
82: * @throws IllegalStateException if the root WebApplicationContext could not be found
83: * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
84: */
85: public static WebApplicationContext getRequiredWebApplicationContext(
86: ServletContext sc) throws IllegalStateException {
87:
88: WebApplicationContext wac = getWebApplicationContext(sc);
89: if (wac == null) {
90: throw new IllegalStateException(
91: "No WebApplicationContext found: no ContextLoaderListener registered?");
92: }
93: return wac;
94: }
95:
96: }
|