001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.context.support;
018:
019: import javax.servlet.ServletConfig;
020: import javax.servlet.ServletContext;
021:
022: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
023: import org.springframework.context.support.AbstractRefreshableApplicationContext;
024: import org.springframework.core.io.Resource;
025: import org.springframework.core.io.support.ResourcePatternResolver;
026: import org.springframework.ui.context.Theme;
027: import org.springframework.ui.context.ThemeSource;
028: import org.springframework.ui.context.support.UiApplicationContextUtils;
029: import org.springframework.util.SystemPropertyUtils;
030: import org.springframework.web.context.ConfigurableWebApplicationContext;
031: import org.springframework.web.context.ServletConfigAware;
032: import org.springframework.web.context.ServletContextAware;
033: import org.springframework.web.context.request.RequestScope;
034: import org.springframework.web.context.request.SessionScope;
035:
036: /**
037: * {@link org.springframework.context.support.AbstractRefreshableApplicationContext}
038: * subclass which implements the
039: * {@link org.springframework.web.context.ConfigurableWebApplicationContext}
040: * interface for web environments. Provides a "configLocations" property,
041: * to be populated through the ConfigurableWebApplicationContext interface
042: * on web application startup.
043: *
044: * <p>This class is as easy to subclass as AbstractRefreshableApplicationContext:
045: * All you need to implements is the {@link #loadBeanDefinitions} method;
046: * see the superclass javadoc for details. Note that implementations are supposed
047: * to load bean definitions from the files specified by the locations returned
048: * by the {@link #getConfigLocations} method.
049: *
050: * <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
051: * the web application root. Absolute paths, e.g. for files outside the web app root,
052: * can be accessed via "file:" URLs, as implemented by
053: * {@link org.springframework.core.io.DefaultResourceLoader}.
054: *
055: * <p>In addition to the special beans detected by
056: * {@link org.springframework.context.support.AbstractApplicationContext},
057: * this class detects a bean of type {@link org.springframework.ui.context.ThemeSource}
058: * in the context, under the special bean name "themeSource".
059: *
060: * <p><b>This is the web context to be subclassed for a different bean definition format.</b>
061: * Such a context implementation can be specified as "contextClass" context-param
062: * for {@link org.springframework.web.context.ContextLoader} or as "contextClass"
063: * init-param for {@link org.springframework.web.servlet.FrameworkServlet},
064: * replacing the default {@link XmlWebApplicationContext}. It will then automatically
065: * receive the "contextConfigLocation" context-param or init-param, respectively.
066: *
067: * <p>Note that WebApplicationContext implementations are generally supposed
068: * to configure themselves based on the configuration received through the
069: * {@link ConfigurableWebApplicationContext} interface. In contrast, a standalone
070: * application context might allow for configuration in custom startup code
071: * (for example, {@link org.springframework.context.support.GenericApplicationContext}).
072: *
073: * @author Juergen Hoeller
074: * @since 1.1.3
075: * @see #loadBeanDefinitions
076: * @see org.springframework.web.context.ConfigurableWebApplicationContext#setConfigLocations
077: * @see org.springframework.ui.context.ThemeSource
078: * @see XmlWebApplicationContext
079: */
080: public abstract class AbstractRefreshableWebApplicationContext extends
081: AbstractRefreshableApplicationContext implements
082: ConfigurableWebApplicationContext, ThemeSource {
083:
084: /** Servlet context that this context runs in */
085: private ServletContext servletContext;
086:
087: /** Servlet config that this context runs in, if any */
088: private ServletConfig servletConfig;
089:
090: /** Namespace of this context, or <code>null</code> if root */
091: private String namespace;
092:
093: /** Paths to XML configuration files */
094: private String[] configLocations;
095:
096: /** the ThemeSource for this ApplicationContext */
097: private ThemeSource themeSource;
098:
099: public AbstractRefreshableWebApplicationContext() {
100: setDisplayName("Root WebApplicationContext");
101: }
102:
103: public void setServletContext(ServletContext servletContext) {
104: this .servletContext = servletContext;
105: }
106:
107: public ServletContext getServletContext() {
108: return this .servletContext;
109: }
110:
111: public void setServletConfig(ServletConfig servletConfig) {
112: this .servletConfig = servletConfig;
113: if (servletConfig != null && this .servletContext == null) {
114: this .servletContext = servletConfig.getServletContext();
115: }
116: }
117:
118: public ServletConfig getServletConfig() {
119: return this .servletConfig;
120: }
121:
122: public void setNamespace(String namespace) {
123: this .namespace = namespace;
124: if (namespace != null) {
125: setDisplayName("WebApplicationContext for namespace '"
126: + namespace + "'");
127: }
128: }
129:
130: public String getNamespace() {
131: return this .namespace;
132: }
133:
134: public void setConfigLocations(String[] locations) {
135: this .configLocations = new String[locations.length];
136: for (int i = 0; i < locations.length; i++) {
137: this .configLocations[i] = resolvePath(locations[i]);
138: }
139: }
140:
141: public String[] getConfigLocations() {
142: return (this .configLocations != null ? this .configLocations
143: : getDefaultConfigLocations());
144: }
145:
146: /**
147: * Return the default config locations to use, for the case where no
148: * explicit config locations have been specified.
149: * <p>The default implementation returns <code>null</code>,
150: * requiring explicit config locations.
151: * @see #setConfigLocations
152: */
153: protected String[] getDefaultConfigLocations() {
154: return null;
155: }
156:
157: /**
158: * Register request/session scopes, a {@link ServletContextAwareProcessor}, etc.
159: */
160: protected void postProcessBeanFactory(
161: ConfigurableListableBeanFactory beanFactory) {
162: beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
163: beanFactory.registerScope(SCOPE_SESSION,
164: new SessionScope(false));
165: beanFactory.registerScope(SCOPE_GLOBAL_SESSION,
166: new SessionScope(true));
167:
168: beanFactory
169: .addBeanPostProcessor(new ServletContextAwareProcessor(
170: this .servletContext, this .servletConfig));
171: beanFactory
172: .ignoreDependencyInterface(ServletContextAware.class);
173: beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
174: }
175:
176: /**
177: * Resolve the given path, replacing placeholders with corresponding
178: * system property values if necessary. Applied to config locations.
179: * @param path the original file path
180: * @return the resolved file path
181: * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
182: */
183: protected String resolvePath(String path) {
184: return SystemPropertyUtils.resolvePlaceholders(path);
185: }
186:
187: /**
188: * This implementation supports file paths beneath the root of the ServletContext.
189: * @see ServletContextResource
190: */
191: protected Resource getResourceByPath(String path) {
192: return new ServletContextResource(this .servletContext, path);
193: }
194:
195: /**
196: * This implementation supports pattern matching in unexpanded WARs too.
197: * @see ServletContextResourcePatternResolver
198: */
199: protected ResourcePatternResolver getResourcePatternResolver() {
200: return new ServletContextResourcePatternResolver(this );
201: }
202:
203: /**
204: * Initialize the theme capability.
205: */
206: protected void onRefresh() {
207: this .themeSource = UiApplicationContextUtils
208: .initThemeSource(this );
209: }
210:
211: public Theme getTheme(String themeName) {
212: return this.themeSource.getTheme(themeName);
213: }
214:
215: }
|