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.StaticApplicationContext;
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.web.context.ConfigurableWebApplicationContext;
030: import org.springframework.web.context.ServletConfigAware;
031: import org.springframework.web.context.ServletContextAware;
032: import org.springframework.web.context.request.RequestScope;
033: import org.springframework.web.context.request.SessionScope;
034:
035: /**
036: * Static {@link org.springframework.web.context.WebApplicationContext}
037: * implementation for testing. Not intended for use in production applications.
038: *
039: * <p>Implements the {@link org.springframework.web.context.ConfigurableWebApplicationContext}
040: * interface to allow for direct replacement of an {@link XmlWebApplicationContext},
041: * despite not actually supporting external configuration files.
042: *
043: * <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
044: * the web application root. Absolute paths, e.g. for files outside the web app root,
045: * can be accessed via "file:" URLs, as implemented by
046: * {@link org.springframework.core.io.DefaultResourceLoader}.
047: *
048: * <p>In addition to the special beans detected by
049: * {@link org.springframework.context.support.AbstractApplicationContext},
050: * this class detects a bean of type {@link org.springframework.ui.context.ThemeSource}
051: * in the context, under the special bean name "themeSource".
052: *
053: * @author Rod Johnson
054: * @author Juergen Hoeller
055: * @see org.springframework.ui.context.ThemeSource
056: */
057: public class StaticWebApplicationContext extends
058: StaticApplicationContext implements
059: ConfigurableWebApplicationContext, ThemeSource {
060:
061: private ServletContext servletContext;
062:
063: private ServletConfig servletConfig;
064:
065: private String namespace;
066:
067: private ThemeSource themeSource;
068:
069: public StaticWebApplicationContext() {
070: setDisplayName("Root WebApplicationContext");
071: }
072:
073: /**
074: * Set the ServletContext that this WebApplicationContext runs in.
075: */
076: public void setServletContext(ServletContext servletContext) {
077: this .servletContext = servletContext;
078: }
079:
080: public ServletContext getServletContext() {
081: return servletContext;
082: }
083:
084: public void setServletConfig(ServletConfig servletConfig) {
085: this .servletConfig = servletConfig;
086: if (servletConfig != null && this .servletContext == null) {
087: this .servletContext = servletConfig.getServletContext();
088: }
089: }
090:
091: public ServletConfig getServletConfig() {
092: return this .servletConfig;
093: }
094:
095: public void setNamespace(String namespace) {
096: this .namespace = namespace;
097: if (namespace != null) {
098: setDisplayName("WebApplicationContext for namespace '"
099: + namespace + "'");
100: }
101: }
102:
103: public String getNamespace() {
104: return this .namespace;
105: }
106:
107: /**
108: * The {@link StaticWebApplicationContext} class does not support this method.
109: * @throws UnsupportedOperationException <b>always</b>
110: */
111: public void setConfigLocations(String[] configLocations) {
112: throw new UnsupportedOperationException(
113: "StaticWebApplicationContext does not support config locations");
114: }
115:
116: public String[] getConfigLocations() {
117: return null;
118: }
119:
120: /**
121: * Register request/session scopes, a {@link ServletContextAwareProcessor}, etc.
122: */
123: protected void postProcessBeanFactory(
124: ConfigurableListableBeanFactory beanFactory) {
125: beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
126: beanFactory.registerScope(SCOPE_SESSION,
127: new SessionScope(false));
128: beanFactory.registerScope(SCOPE_GLOBAL_SESSION,
129: new SessionScope(true));
130:
131: beanFactory
132: .addBeanPostProcessor(new ServletContextAwareProcessor(
133: this .servletContext, this .servletConfig));
134: beanFactory
135: .ignoreDependencyInterface(ServletContextAware.class);
136: beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
137: }
138:
139: /**
140: * This implementation supports file paths beneath the root of the ServletContext.
141: * @see ServletContextResource
142: */
143: protected Resource getResourceByPath(String path) {
144: return new ServletContextResource(this .servletContext, path);
145: }
146:
147: /**
148: * This implementation supports pattern matching in unexpanded WARs too.
149: * @see ServletContextResourcePatternResolver
150: */
151: protected ResourcePatternResolver getResourcePatternResolver() {
152: return new ServletContextResourcePatternResolver(this );
153: }
154:
155: /**
156: * Initialize the theme capability.
157: */
158: protected void onRefresh() {
159: this .themeSource = UiApplicationContextUtils
160: .initThemeSource(this );
161: }
162:
163: public Theme getTheme(String themeName) {
164: return this.themeSource.getTheme(themeName);
165: }
166:
167: }
|