01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import javax.servlet.ServletContext;
21:
22: import org.springframework.beans.BeansException;
23: import org.springframework.beans.factory.config.BeanPostProcessor;
24: import org.springframework.web.context.ServletConfigAware;
25: import org.springframework.web.context.ServletContextAware;
26:
27: /**
28: * {@link org.springframework.beans.factory.config.BeanPostProcessor}
29: * implementation that passes the ServletContext to beans that implement
30: * the {@link ServletContextAware} interface.
31: *
32: * <p>Web application contexts will automatically register this with their
33: * underlying bean factory. Applications do not use this directly.
34: *
35: * @author Juergen Hoeller
36: * @since 12.03.2004
37: * @see org.springframework.web.context.ServletContextAware
38: * @see org.springframework.web.context.support.XmlWebApplicationContext#postProcessBeanFactory
39: */
40: public class ServletContextAwareProcessor implements BeanPostProcessor {
41:
42: private ServletContext servletContext;
43:
44: private ServletConfig servletConfig;
45:
46: /**
47: * Create a new ServletContextAwareProcessor for the given context.
48: */
49: public ServletContextAwareProcessor(ServletContext servletContext) {
50: this (servletContext, null);
51: }
52:
53: /**
54: * Create a new ServletContextAwareProcessor for the given config.
55: */
56: public ServletContextAwareProcessor(ServletConfig servletConfig) {
57: this (null, servletConfig);
58: }
59:
60: /**
61: * Create a new ServletContextAwareProcessor for the given context and config.
62: */
63: public ServletContextAwareProcessor(ServletContext servletContext,
64: ServletConfig servletConfig) {
65: this .servletContext = servletContext;
66: this .servletConfig = servletConfig;
67: if (servletContext == null && servletConfig != null) {
68: this .servletContext = servletConfig.getServletContext();
69: }
70: }
71:
72: public Object postProcessBeforeInitialization(Object bean,
73: String beanName) throws BeansException {
74: if (this .servletContext != null
75: && bean instanceof ServletContextAware) {
76: ((ServletContextAware) bean)
77: .setServletContext(this .servletContext);
78: }
79: if (this .servletConfig != null
80: && bean instanceof ServletConfigAware) {
81: ((ServletConfigAware) bean)
82: .setServletConfig(this .servletConfig);
83: }
84: return bean;
85: }
86:
87: public Object postProcessAfterInitialization(Object bean,
88: String beanName) {
89: return bean;
90: }
91:
92: }
|