KickJava   Java API By Example, From Geeks To Geeks.

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


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.context.support;
18
19 import javax.servlet.ServletConfig JavaDoc;
20 import javax.servlet.ServletContext JavaDoc;
21
22 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
23 import org.springframework.context.support.AbstractRefreshableApplicationContext;
24 import org.springframework.core.io.Resource;
25 import org.springframework.core.io.support.ResourcePatternResolver;
26 import org.springframework.ui.context.Theme;
27 import org.springframework.ui.context.ThemeSource;
28 import org.springframework.ui.context.support.UiApplicationContextUtils;
29 import org.springframework.util.SystemPropertyUtils;
30 import org.springframework.web.context.ConfigurableWebApplicationContext;
31 import org.springframework.web.context.ServletConfigAware;
32 import org.springframework.web.context.ServletContextAware;
33 import org.springframework.web.context.request.RequestScope;
34 import org.springframework.web.context.request.SessionScope;
35
36 /**
37  * {@link org.springframework.context.support.AbstractRefreshableApplicationContext}
38  * subclass which implements the
39  * {@link org.springframework.web.context.ConfigurableWebApplicationContext}
40  * interface for web environments. Provides a "configLocations" property,
41  * to be populated through the ConfigurableWebApplicationContext interface
42  * on web application startup.
43  *
44  * <p>This class is as easy to subclass as AbstractRefreshableApplicationContext:
45  * All you need to implements is the {@link #loadBeanDefinitions} method;
46  * see the superclass javadoc for details. Note that implementations are supposed
47  * to load bean definitions from the files specified by the locations returned
48  * by the {@link #getConfigLocations} method.
49  *
50  * <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
51  * the web application root. Absolute paths, e.g. for files outside the web app root,
52  * can be accessed via "file:" URLs, as implemented by
53  * {@link org.springframework.core.io.DefaultResourceLoader}.
54  *
55  * <p>In addition to the special beans detected by
56  * {@link org.springframework.context.support.AbstractApplicationContext},
57  * this class detects a bean of type {@link org.springframework.ui.context.ThemeSource}
58  * in the context, under the special bean name "themeSource".
59  *
60  * <p><b>This is the web context to be subclassed for a different bean definition format.</b>
61  * Such a context implementation can be specified as "contextClass" context-param
62  * for {@link org.springframework.web.context.ContextLoader} or as "contextClass"
63  * init-param for {@link org.springframework.web.servlet.FrameworkServlet},
64  * replacing the default {@link XmlWebApplicationContext}. It will then automatically
65  * receive the "contextConfigLocation" context-param or init-param, respectively.
66  *
67  * <p>Note that WebApplicationContext implementations are generally supposed
68  * to configure themselves based on the configuration received through the
69  * {@link ConfigurableWebApplicationContext} interface. In contrast, a standalone
70  * application context might allow for configuration in custom startup code
71  * (for example, {@link org.springframework.context.support.GenericApplicationContext}).
72  *
73  * @author Juergen Hoeller
74  * @since 1.1.3
75  * @see #loadBeanDefinitions
76  * @see org.springframework.web.context.ConfigurableWebApplicationContext#setConfigLocations
77  * @see org.springframework.ui.context.ThemeSource
78  * @see XmlWebApplicationContext
79  */

80 public abstract class AbstractRefreshableWebApplicationContext extends AbstractRefreshableApplicationContext
81         implements ConfigurableWebApplicationContext, ThemeSource {
82
83     /** Servlet context that this context runs in */
84     private ServletContext JavaDoc servletContext;
85
86     /** Servlet config that this context runs in, if any */
87     private ServletConfig JavaDoc servletConfig;
88
89     /** Namespace of this context, or <code>null</code> if root */
90     private String JavaDoc namespace;
91
92     /** Paths to XML configuration files */
93     private String JavaDoc[] configLocations;
94
95     /** the ThemeSource for this ApplicationContext */
96     private ThemeSource themeSource;
97
98
99     public AbstractRefreshableWebApplicationContext() {
100         setDisplayName("Root WebApplicationContext");
101     }
102
103
104     public void setServletContext(ServletContext JavaDoc servletContext) {
105         this.servletContext = servletContext;
106     }
107
108     public ServletContext JavaDoc getServletContext() {
109         return this.servletContext;
110     }
111
112     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
113         this.servletConfig = servletConfig;
114         if (servletConfig != null && this.servletContext == null) {
115             this.servletContext = servletConfig.getServletContext();
116         }
117     }
118
119     public ServletConfig JavaDoc getServletConfig() {
120         return this.servletConfig;
121     }
122
123     public void setNamespace(String JavaDoc namespace) {
124         this.namespace = namespace;
125         if (namespace != null) {
126             setDisplayName("WebApplicationContext for namespace '" + namespace + "'");
127         }
128     }
129
130     public String JavaDoc getNamespace() {
131         return this.namespace;
132     }
133
134     public void setConfigLocations(String JavaDoc[] locations) {
135         this.configLocations = new String JavaDoc[locations.length];
136         for (int i = 0; i < locations.length; i++) {
137             this.configLocations[i] = resolvePath(locations[i]);
138         }
139     }
140
141     public String JavaDoc[] getConfigLocations() {
142         return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
143     }
144
145     /**
146      * Return the default config locations to use, for the case where no
147      * explicit config locations have been specified.
148      * <p>The default implementation returns <code>null</code>,
149      * requiring explicit config locations.
150      * @see #setConfigLocations
151      */

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

160     protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
161         beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
162         beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false));
163         beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true));
164
165         beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
166         beanFactory.ignoreDependencyInterface(ServletContextAware.class);
167         beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
168     }
169
170     /**
171      * Resolve the given path, replacing placeholders with corresponding
172      * system property values if necessary. Applied to config locations.
173      * @param path the original file path
174      * @return the resolved file path
175      * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
176      */

177     protected String JavaDoc resolvePath(String JavaDoc path) {
178         return SystemPropertyUtils.resolvePlaceholders(path);
179     }
180
181     /**
182      * This implementation supports file paths beneath the root of the ServletContext.
183      * @see ServletContextResource
184      */

185     protected Resource getResourceByPath(String JavaDoc path) {
186         return new ServletContextResource(this.servletContext, path);
187     }
188
189     /**
190      * This implementation supports pattern matching in unexpanded WARs too.
191      * @see ServletContextResourcePatternResolver
192      */

193     protected ResourcePatternResolver getResourcePatternResolver() {
194         return new ServletContextResourcePatternResolver(this);
195     }
196
197     /**
198      * Initialize the theme capability.
199      */

200     protected void onRefresh() {
201         this.themeSource = UiApplicationContextUtils.initThemeSource(this);
202     }
203
204     public Theme getTheme(String JavaDoc themeName) {
205         return this.themeSource.getTheme(themeName);
206     }
207
208 }
209
Popular Tags