KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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.ServletContext JavaDoc;
20
21 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
22 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
23 import org.springframework.context.support.GenericApplicationContext;
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.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
34 /**
35  * Subclass of GenericApplicationContext, suitable for web environments.
36  *
37  * <p>Implements the WebApplicationContext interface, but not ConfigurableWebApplicationContext,
38  * as it is not intended for declarative setup in <code>web.xml</code>. Instead,
39  * it is designed for programmatic setup, for example for building nested contexts.
40  *
41  * <p><b>If you intend to implement a WebApplicationContext that reads bean definitions
42  * from configuration files, consider deriving from AbstractRefreshableWebApplicationContext,
43  * reading the bean definitions in an implementation of the <code>loadBeanDefinitions</code>
44  * method.</b>
45  *
46  * <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
47  * the web application root. Absolute paths, e.g. for files outside the web app root,
48  * can be accessed via "file:" URLs, as implemented by AbstractApplicationContext.
49  *
50  * <p>In addition to the special beans detected by AbstractApplicationContext,
51  * this class detects a ThemeSource bean in the context, with the name "themeSource".
52  *
53  * @author Juergen Hoeller
54  * @since 1.2
55  */

56 public class GenericWebApplicationContext extends GenericApplicationContext
57         implements WebApplicationContext, ThemeSource {
58
59     private ServletContext JavaDoc servletContext;
60
61     private ThemeSource themeSource;
62
63
64     /**
65      * Create a new GenericWebApplicationContext.
66      * @see #setServletContext
67      * @see #registerBeanDefinition
68      * @see #refresh
69      */

70     public GenericWebApplicationContext() {
71         super();
72     }
73
74     /**
75      * Create a new GenericWebApplicationContext with the given DefaultListableBeanFactory.
76      * @param beanFactory the DefaultListableBeanFactory instance to use for this context
77      * @see #setServletContext
78      * @see #registerBeanDefinition
79      * @see #refresh
80      */

81     public GenericWebApplicationContext(DefaultListableBeanFactory beanFactory) {
82         super(beanFactory);
83     }
84
85
86     /**
87      * Set the ServletContext that this WebApplicationContext runs in.
88      */

89     public void setServletContext(ServletContext JavaDoc servletContext) {
90         this.servletContext = servletContext;
91     }
92
93     public ServletContext JavaDoc getServletContext() {
94         return servletContext;
95     }
96
97
98     /**
99      * Register ServletContextAwareProcessor.
100      * @see ServletContextAwareProcessor
101      */

102     protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
103         beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
104         beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false));
105         beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true));
106
107         beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext));
108         beanFactory.ignoreDependencyInterface(ServletContextAware.class);
109     }
110
111     /**
112      * This implementation supports file paths beneath the root of the ServletContext.
113      * @see ServletContextResource
114      */

115     protected Resource getResourceByPath(String JavaDoc path) {
116         return new ServletContextResource(this.servletContext, path);
117     }
118
119     /**
120      * This implementation supports pattern matching in unexpanded WARs too.
121      * @see ServletContextResourcePatternResolver
122      */

123     protected ResourcePatternResolver getResourcePatternResolver() {
124         return new ServletContextResourcePatternResolver(this);
125     }
126
127     /**
128      * Initialize the theme capability.
129      */

130     protected void onRefresh() {
131         this.themeSource = UiApplicationContextUtils.initThemeSource(this);
132     }
133
134     public Theme getTheme(String JavaDoc themeName) {
135         return this.themeSource.getTheme(themeName);
136     }
137
138 }
139
Popular Tags