KickJava   Java API By Example, From Geeks To Geeks.

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


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.StaticApplicationContext;
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.ConfigurableWebApplicationContext;
30 import org.springframework.web.context.ServletConfigAware;
31 import org.springframework.web.context.ServletContextAware;
32 import org.springframework.web.context.request.RequestScope;
33 import org.springframework.web.context.request.SessionScope;
34
35 /**
36  * Static {@link org.springframework.web.context.WebApplicationContext}
37  * implementation for testing. Not intended for use in production applications.
38  *
39  * <p>Implements the {@link org.springframework.web.context.ConfigurableWebApplicationContext}
40  * interface to allow for direct replacement of an {@link XmlWebApplicationContext},
41  * despite not actually supporting external configuration files.
42  *
43  * <p>Interprets resource paths as servlet context resources, i.e. as paths beneath
44  * the web application root. Absolute paths, e.g. for files outside the web app root,
45  * can be accessed via "file:" URLs, as implemented by
46  * {@link org.springframework.core.io.DefaultResourceLoader}.
47  *
48  * <p>In addition to the special beans detected by
49  * {@link org.springframework.context.support.AbstractApplicationContext},
50  * this class detects a bean of type {@link org.springframework.ui.context.ThemeSource}
51  * in the context, under the special bean name "themeSource".
52  *
53  * @author Rod Johnson
54  * @author Juergen Hoeller
55  * @see org.springframework.ui.context.ThemeSource
56  */

57 public class StaticWebApplicationContext extends StaticApplicationContext
58         implements ConfigurableWebApplicationContext, ThemeSource {
59
60     private ServletContext JavaDoc servletContext;
61
62     private ServletConfig JavaDoc servletConfig;
63
64     private String JavaDoc namespace;
65
66     private ThemeSource themeSource;
67
68
69     public StaticWebApplicationContext() {
70         setDisplayName("Root WebApplicationContext");
71     }
72
73     /**
74      * Set the ServletContext that this WebApplicationContext runs in.
75      */

76     public void setServletContext(ServletContext JavaDoc servletContext) {
77         this.servletContext = servletContext;
78     }
79
80     public ServletContext JavaDoc getServletContext() {
81         return servletContext;
82     }
83
84     public void setServletConfig(ServletConfig JavaDoc servletConfig) {
85         this.servletConfig = servletConfig;
86         if (servletConfig != null && this.servletContext == null) {
87             this.servletContext = servletConfig.getServletContext();
88         }
89     }
90
91     public ServletConfig JavaDoc getServletConfig() {
92         return this.servletConfig;
93     }
94
95     public void setNamespace(String JavaDoc namespace) {
96         this.namespace = namespace;
97         if (namespace != null) {
98             setDisplayName("WebApplicationContext for namespace '" + namespace + "'");
99         }
100     }
101
102     public String JavaDoc getNamespace() {
103         return this.namespace;
104     }
105
106     /**
107      * The {@link StaticWebApplicationContext} class does not support this method.
108      * @throws UnsupportedOperationException <b>always</b>
109      */

110     public void setConfigLocations(String JavaDoc[] configLocations) {
111         throw new UnsupportedOperationException JavaDoc("StaticWebApplicationContext does not support config locations");
112     }
113
114     public String JavaDoc[] getConfigLocations() {
115         return null;
116     }
117
118
119     /**
120      * Register request/session scopes, a {@link ServletContextAwareProcessor}, etc.
121      */

122     protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
123         beanFactory.registerScope(SCOPE_REQUEST, new RequestScope());
124         beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false));
125         beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true));
126
127         beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
128         beanFactory.ignoreDependencyInterface(ServletContextAware.class);
129         beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
130     }
131
132     /**
133      * This implementation supports file paths beneath the root of the ServletContext.
134      * @see ServletContextResource
135      */

136     protected Resource getResourceByPath(String JavaDoc path) {
137         return new ServletContextResource(this.servletContext, path);
138     }
139
140     /**
141      * This implementation supports pattern matching in unexpanded WARs too.
142      * @see ServletContextResourcePatternResolver
143      */

144     protected ResourcePatternResolver getResourcePatternResolver() {
145         return new ServletContextResourcePatternResolver(this);
146     }
147
148     /**
149      * Initialize the theme capability.
150      */

151     protected void onRefresh() {
152         this.themeSource = UiApplicationContextUtils.initThemeSource(this);
153     }
154
155     public Theme getTheme(String JavaDoc themeName) {
156         return this.themeSource.getTheme(themeName);
157     }
158
159 }
160
Popular Tags