KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 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.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.springframework.beans.BeansException;
26 import org.springframework.beans.factory.config.BeanPostProcessor;
27 import org.springframework.web.context.ServletConfigAware;
28 import org.springframework.web.context.ServletContextAware;
29
30 /**
31  * BeanPostProcessor implementation that passes the ServletContext to
32  * beans that implement the ServletContextAware interface.
33  *
34  * <p>Web application contexts will automatically register this with their
35  * underlying bean factory. Applications do not use this directly.
36  *
37  * @author Juergen Hoeller
38  * @since 12.03.2004
39  * @see org.springframework.web.context.ServletContextAware
40  * @see org.springframework.web.context.support.XmlWebApplicationContext#postProcessBeanFactory
41  */

42 public class ServletContextAwareProcessor implements BeanPostProcessor {
43
44     protected final Log logger = LogFactory.getLog(getClass());
45
46     private ServletContext JavaDoc servletContext;
47
48     private ServletConfig JavaDoc servletConfig;
49
50
51     /**
52      * Create a new ServletContextAwareProcessor for the given context.
53      */

54     public ServletContextAwareProcessor(ServletContext JavaDoc servletContext) {
55         this(servletContext, null);
56     }
57
58     /**
59      * Create a new ServletContextAwareProcessor for the given config.
60      */

61     public ServletContextAwareProcessor(ServletConfig JavaDoc servletConfig) {
62         this(null, servletConfig);
63     }
64
65     /**
66      * Create a new ServletContextAwareProcessor for the given context and config.
67      */

68     public ServletContextAwareProcessor(ServletContext JavaDoc servletContext, ServletConfig JavaDoc servletConfig) {
69         this.servletContext = servletContext;
70         this.servletConfig = servletConfig;
71         if (servletContext == null && servletConfig != null) {
72             this.servletContext = servletConfig.getServletContext();
73         }
74     }
75
76
77     public Object JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
78         if (bean instanceof ServletContextAware) {
79             if (this.servletContext == null) {
80                 throw new IllegalStateException JavaDoc("Cannot satisfy ServletContextAware for bean '" +
81                         beanName + "' without ServletContext");
82             }
83             if (logger.isDebugEnabled()) {
84                 logger.debug("Invoking setServletContext on ServletContextAware bean '" + beanName + "'");
85             }
86             ((ServletContextAware) bean).setServletContext(this.servletContext);
87         }
88         if (bean instanceof ServletConfigAware) {
89             if (this.servletConfig == null) {
90                 throw new IllegalStateException JavaDoc("Cannot satisfy ServletConfigAware for bean '" +
91                         beanName + "' without ServletConfig");
92             }
93             if (logger.isDebugEnabled()) {
94                 logger.debug("Invoking setServletConfig on ServletConfigAware bean '" + beanName + "'");
95             }
96             ((ServletConfigAware) bean).setServletConfig(this.servletConfig);
97         }
98         return bean;
99     }
100
101     public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc beanName) {
102         return bean;
103     }
104
105 }
106
Popular Tags