KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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.PortletConfig;
20 import javax.portlet.PortletContext;
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.context.support.AbstractRefreshableApplicationContext;
26 import org.springframework.core.io.Resource;
27 import org.springframework.core.io.support.ResourcePatternResolver;
28 import org.springframework.util.ObjectUtils;
29 import org.springframework.web.context.ServletContextAware;
30 import org.springframework.web.context.WebApplicationContext;
31 import org.springframework.web.context.request.RequestScope;
32 import org.springframework.web.context.request.SessionScope;
33 import org.springframework.web.context.support.ServletContextAwareProcessor;
34
35 /**
36  * {@link org.springframework.context.support.AbstractRefreshableApplicationContext}
37  * subclass which implements the {@link ConfigurablePortletApplicationContext}
38  * interface for portlet environments. Provides a "configLocations" property,
39  * to be populated through the ConfigurablePortletApplicationContext interface
40  * on portlet application startup.
41  *
42  * <p>This class is as easy to subclass as AbstractRefreshableApplicationContext:
43  * All you need to implements is the {@link #loadBeanDefinitions} method;
44  * see the superclass javadoc for details. Note that implementations are supposed
45  * to load bean definitions from the files specified by the locations returned
46  * by the {@link #getConfigLocations} method.
47  *
48  * <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
49  * the web application root. Absolute paths, e.g. for files outside the web app root,
50  * can be accessed via "file:" URLs, as implemented by
51  * {@link org.springframework.core.io.DefaultResourceLoader}.
52  *
53  * <p><b>This is the portlet context to be subclassed for a different bean definition format.</b>
54  * Such a context implementation can be specified as "contextClass" init-param
55  * for FrameworkPortlet, replacing the default {@link XmlPortletApplicationContext}.
56  * It will then automatically receive the "contextConfigLocation" init-param.
57  *
58  * <p>Note that Portlet-based context implementations are generally supposed
59  * to configure themselves based on the configuration received through the
60  * {@link ConfigurablePortletApplicationContext} interface. In contrast, a standalone
61  * application context might allow for configuration in custom startup code
62  * (for example, {@link org.springframework.context.support.GenericApplicationContext}).
63  *
64  * @author Juergen Hoeller
65  * @author John A. Lewis
66  * @since 2.0
67  * @see #loadBeanDefinitions
68  * @see org.springframework.web.portlet.context.ConfigurablePortletApplicationContext#setConfigLocations
69  * @see XmlPortletApplicationContext
70  */

71 public abstract class AbstractRefreshablePortletApplicationContext extends AbstractRefreshableApplicationContext
72         implements WebApplicationContext, ConfigurablePortletApplicationContext {
73
74     /** Servlet context that this context runs in */
75     private ServletContext JavaDoc servletContext;
76
77     /** Portlet context that this context runs in */
78     private PortletContext portletContext;
79
80     /** Portlet config that this context runs in */
81     private PortletConfig portletConfig;
82
83     /** Namespace of this context, or null if root */
84     private String JavaDoc namespace;
85
86     /** Paths to XML configuration files */
87     private String JavaDoc[] configLocations;
88
89
90     public AbstractRefreshablePortletApplicationContext() {
91         setDisplayName("Root PortletApplicationContext");
92     }
93
94     public void setParent(ApplicationContext parent) {
95         super.setParent(parent);
96         if (parent instanceof WebApplicationContext) {
97             this.servletContext = ((WebApplicationContext) parent).getServletContext();
98         }
99     }
100
101     public ServletContext JavaDoc getServletContext() {
102         return this.servletContext;
103     }
104
105     public void setPortletContext(PortletContext portletContext) {
106         this.portletContext = portletContext;
107     }
108
109     public PortletContext getPortletContext() {
110         return this.portletContext;
111     }
112
113     public void setPortletConfig(PortletConfig portletConfig) {
114         this.portletConfig = portletConfig;
115         if (portletConfig != null && this.portletContext == null) {
116             this.portletContext = portletConfig.getPortletContext();
117         }
118     }
119
120     public PortletConfig getPortletConfig() {
121         return this.portletConfig;
122     }
123
124     public void setNamespace(String JavaDoc namespace) {
125         this.namespace = namespace;
126         if (namespace != null) {
127             setDisplayName("PortletApplicationContext for namespace '" + namespace + "'");
128         }
129     }
130
131     public String JavaDoc getNamespace() {
132         return this.namespace;
133     }
134
135     public void setConfigLocations(String JavaDoc[] configLocations) {
136         this.configLocations = configLocations;
137     }
138
139     public String JavaDoc[] getConfigLocations() {
140         return (!ObjectUtils.isEmpty(this.configLocations) ? this.configLocations : getDefaultConfigLocations());
141     }
142
143     /**
144      * Return the default config locations to use, for the case where no
145      * explicit config locations have been specified.
146      * <p>The default implementation returns <code>null</code>,
147      * requiring explicit config locations.
148      * @see #setConfigLocations
149      */

150     protected String JavaDoc[] getDefaultConfigLocations() {
151         return null;
152     }
153
154
155     /**
156      * Register request/session scopes, a {@link PortletContextAwareProcessor}, etc.
157      */

158     protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
159         beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
160         beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false));
161         beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true));
162
163         beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext));
164         beanFactory.addBeanPostProcessor(new PortletContextAwareProcessor(this.portletContext, this.portletConfig));
165         beanFactory.ignoreDependencyInterface(ServletContextAware.class);
166         beanFactory.ignoreDependencyInterface(PortletContextAware.class);
167         beanFactory.ignoreDependencyInterface(PortletConfigAware.class);
168     }
169
170     /**
171      * This implementation supports file paths beneath the root of the PortletContext.
172      * @see PortletContextResource
173      */

174     protected Resource getResourceByPath(String JavaDoc path) {
175         return new PortletContextResource(this.portletContext, path);
176     }
177
178     /**
179      * This implementation supports pattern matching in unexpanded WARs too.
180      * @see PortletContextResourcePatternResolver
181      */

182     protected ResourcePatternResolver getResourcePatternResolver() {
183         return new PortletContextResourcePatternResolver(this);
184     }
185
186 }
187
Popular Tags