KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > support > WebApplicationContextUtils


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.web.context.support;
18
19 import javax.servlet.ServletContext JavaDoc;
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(ServletContext JavaDoc sc) {
54         Assert.notNull(sc, "ServletContext must not be null");
55         Object JavaDoc attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
56         if (attr == null) {
57             return null;
58         }
59         if (attr instanceof RuntimeException JavaDoc) {
60             throw (RuntimeException JavaDoc) attr;
61         }
62         if (attr instanceof Error JavaDoc) {
63             throw (Error JavaDoc) attr;
64         }
65         if (!(attr instanceof WebApplicationContext)) {
66             throw new IllegalStateException JavaDoc("Root context attribute is not of type WebApplicationContext: " + attr);
67         }
68         return (WebApplicationContext) attr;
69     }
70
71     /**
72      * Find the root WebApplicationContext for this web application, which is
73      * typically loaded via ContextLoaderListener or ContextLoaderServlet.
74      * <p>Will rethrow an exception that happened on root context startup,
75      * to differentiate between a failed context startup and no context at all.
76      * @param sc ServletContext to find the web application context for
77      * @return the root WebApplicationContext for this web app
78      * @throws IllegalStateException if the root WebApplicationContext could not be found
79      * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
80      */

81     public static WebApplicationContext getRequiredWebApplicationContext(ServletContext JavaDoc sc)
82         throws IllegalStateException JavaDoc {
83
84         WebApplicationContext wac = getWebApplicationContext(sc);
85         if (wac == null) {
86             throw new IllegalStateException JavaDoc("No WebApplicationContext found: no ContextLoaderListener registered?");
87         }
88         return wac;
89     }
90
91 }
92
Popular Tags