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