001: /*
002: * Copyright 2002-2006 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.ServletContext;
020:
021: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
022: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
023: import org.springframework.context.support.GenericApplicationContext;
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.ServletContextAware;
030: import org.springframework.web.context.WebApplicationContext;
031: import org.springframework.web.context.request.RequestScope;
032: import org.springframework.web.context.request.SessionScope;
033:
034: /**
035: * Subclass of GenericApplicationContext, suitable for web environments.
036: *
037: * <p>Implements the WebApplicationContext interface, but not ConfigurableWebApplicationContext,
038: * as it is not intended for declarative setup in <code>web.xml</code>. Instead,
039: * it is designed for programmatic setup, for example for building nested contexts.
040: *
041: * <p><b>If you intend to implement a WebApplicationContext that reads bean definitions
042: * from configuration files, consider deriving from AbstractRefreshableWebApplicationContext,
043: * reading the bean definitions in an implementation of the <code>loadBeanDefinitions</code>
044: * method.</b>
045: *
046: * <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
047: * the web application root. Absolute paths, e.g. for files outside the web app root,
048: * can be accessed via "file:" URLs, as implemented by AbstractApplicationContext.
049: *
050: * <p>In addition to the special beans detected by AbstractApplicationContext,
051: * this class detects a ThemeSource bean in the context, with the name "themeSource".
052: *
053: * @author Juergen Hoeller
054: * @since 1.2
055: */
056: public class GenericWebApplicationContext extends
057: GenericApplicationContext implements WebApplicationContext,
058: ThemeSource {
059:
060: private ServletContext servletContext;
061:
062: private ThemeSource themeSource;
063:
064: /**
065: * Create a new GenericWebApplicationContext.
066: * @see #setServletContext
067: * @see #registerBeanDefinition
068: * @see #refresh
069: */
070: public GenericWebApplicationContext() {
071: super ();
072: }
073:
074: /**
075: * Create a new GenericWebApplicationContext with the given DefaultListableBeanFactory.
076: * @param beanFactory the DefaultListableBeanFactory instance to use for this context
077: * @see #setServletContext
078: * @see #registerBeanDefinition
079: * @see #refresh
080: */
081: public GenericWebApplicationContext(
082: DefaultListableBeanFactory beanFactory) {
083: super (beanFactory);
084: }
085:
086: /**
087: * Set the ServletContext that this WebApplicationContext runs in.
088: */
089: public void setServletContext(ServletContext servletContext) {
090: this .servletContext = servletContext;
091: }
092:
093: public ServletContext getServletContext() {
094: return servletContext;
095: }
096:
097: /**
098: * Register ServletContextAwareProcessor.
099: * @see ServletContextAwareProcessor
100: */
101: protected void postProcessBeanFactory(
102: ConfigurableListableBeanFactory beanFactory) {
103: beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
104: beanFactory.registerScope(SCOPE_SESSION,
105: new SessionScope(false));
106: beanFactory.registerScope(SCOPE_GLOBAL_SESSION,
107: new SessionScope(true));
108:
109: beanFactory
110: .addBeanPostProcessor(new ServletContextAwareProcessor(
111: this .servletContext));
112: beanFactory
113: .ignoreDependencyInterface(ServletContextAware.class);
114: }
115:
116: /**
117: * This implementation supports file paths beneath the root of the ServletContext.
118: * @see ServletContextResource
119: */
120: protected Resource getResourceByPath(String path) {
121: return new ServletContextResource(this .servletContext, path);
122: }
123:
124: /**
125: * This implementation supports pattern matching in unexpanded WARs too.
126: * @see ServletContextResourcePatternResolver
127: */
128: protected ResourcePatternResolver getResourcePatternResolver() {
129: return new ServletContextResourcePatternResolver(this );
130: }
131:
132: /**
133: * Initialize the theme capability.
134: */
135: protected void onRefresh() {
136: this .themeSource = UiApplicationContextUtils
137: .initThemeSource(this );
138: }
139:
140: public Theme getTheme(String themeName) {
141: return this.themeSource.getTheme(themeName);
142: }
143:
144: }
|