KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > context > PortletApplicationContextUtils


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.portlet.context;
18
19 import javax.portlet.PortletContext;
20
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.util.Assert;
23 import org.springframework.web.context.WebApplicationContext;
24
25 /**
26  * Convenience methods to retrieve the root WebApplicationContext for a given
27  * PortletContext. This is e.g. useful for accessing a Spring context from
28  * within custom Portlet implementations.
29  *
30  * @author Juergen Hoeller
31  * @author John A. Lewis
32  * @since 2.0
33  * @see org.springframework.web.context.ContextLoader
34  * @see org.springframework.web.context.support.WebApplicationContextUtils
35  * @see org.springframework.web.portlet.FrameworkPortlet
36  * @see org.springframework.web.portlet.DispatcherPortlet
37  */

38 public abstract class PortletApplicationContextUtils {
39     
40     /**
41      * Find the root WebApplicationContext for this portlet application, which is
42      * typically loaded via ContextLoaderListener or ContextLoaderServlet.
43      * <p>Will rethrow an exception that happened on root context startup,
44      * to differentiate between a failed context startup and no context at all.
45      * @param pc PortletContext to find the web application context for
46      * @return the root WebApplicationContext for this web app, or <code>null</code> if none
47      * (typed to ApplicationContext to avoid a Servlet API dependency; can usually
48      * be casted to WebApplicationContext, but there shouldn't be a need to)
49      * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
50      */

51     public static ApplicationContext getWebApplicationContext(PortletContext pc) {
52         Assert.notNull(pc, "PortletContext must not be null");
53         Object JavaDoc attr = pc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
54         if (attr == null) {
55             return null;
56         }
57         if (attr instanceof RuntimeException JavaDoc) {
58             throw (RuntimeException JavaDoc) attr;
59         }
60         if (attr instanceof Error JavaDoc) {
61             throw (Error JavaDoc) attr;
62         }
63         if (!(attr instanceof ApplicationContext)) {
64             throw new IllegalStateException JavaDoc("Root context attribute is not of type WebApplicationContext: " + attr);
65         }
66         return (ApplicationContext) attr;
67     }
68
69     /**
70      * Find the root WebApplicationContext for this portlet application, which is
71      * typically loaded via ContextLoaderListener or ContextLoaderServlet.
72      * <p>Will rethrow an exception that happened on root context startup,
73      * to differentiate between a failed context startup and no context at all.
74      * @param pc PortletContext to find the web application context for
75      * @return the root WebApplicationContext for this web app
76      * (typed to ApplicationContext to avoid a Servlet API dependency; can usually
77      * be casted to WebApplicationContext, but there shouldn't be a need to)
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 ApplicationContext getRequiredWebApplicationContext(PortletContext pc)
82         throws IllegalStateException JavaDoc {
83
84         ApplicationContext wac = getWebApplicationContext(pc);
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