001/*
002 * Copyright 2002-2013 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 *      https://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
017package org.springframework.web.context.support;
018
019import javax.servlet.ServletConfig;
020import javax.servlet.ServletContext;
021
022import org.springframework.beans.BeansException;
023import org.springframework.beans.factory.config.BeanPostProcessor;
024import org.springframework.web.context.ServletConfigAware;
025import org.springframework.web.context.ServletContextAware;
026
027/**
028 * {@link org.springframework.beans.factory.config.BeanPostProcessor}
029 * implementation that passes the ServletContext to beans that implement
030 * the {@link ServletContextAware} interface.
031 *
032 * <p>Web application contexts will automatically register this with their
033 * underlying bean factory. Applications do not use this directly.
034 *
035 * @author Juergen Hoeller
036 * @author Phillip Webb
037 * @since 12.03.2004
038 * @see org.springframework.web.context.ServletContextAware
039 * @see org.springframework.web.context.support.XmlWebApplicationContext#postProcessBeanFactory
040 */
041public class ServletContextAwareProcessor implements BeanPostProcessor {
042
043        private ServletContext servletContext;
044
045        private ServletConfig servletConfig;
046
047
048        /**
049         * Create a new ServletContextAwareProcessor without an initial context or config.
050         * When this constructor is used the {@link #getServletContext()} and/or
051         * {@link #getServletConfig()} methods should be overridden.
052         */
053        protected ServletContextAwareProcessor() {
054        }
055
056        /**
057         * Create a new ServletContextAwareProcessor for the given context.
058         */
059        public ServletContextAwareProcessor(ServletContext servletContext) {
060                this(servletContext, null);
061        }
062
063        /**
064         * Create a new ServletContextAwareProcessor for the given config.
065         */
066        public ServletContextAwareProcessor(ServletConfig servletConfig) {
067                this(null, servletConfig);
068        }
069
070        /**
071         * Create a new ServletContextAwareProcessor for the given context and config.
072         */
073        public ServletContextAwareProcessor(ServletContext servletContext, ServletConfig servletConfig) {
074                this.servletContext = servletContext;
075                this.servletConfig = servletConfig;
076        }
077
078
079        /**
080         * Returns the {@link ServletContext} to be injected or {@code null}. This method
081         * can be overridden by subclasses when a context is obtained after the post-processor
082         * has been registered.
083         */
084        protected ServletContext getServletContext() {
085                if (this.servletContext == null && getServletConfig() != null) {
086                        return getServletConfig().getServletContext();
087                }
088                return this.servletContext;
089        }
090
091        /**
092         * Returns the {@link ServletContext} to be injected or {@code null}. This method
093         * can be overridden by subclasses when a context is obtained after the post-processor
094         * has been registered.
095         */
096        protected ServletConfig getServletConfig() {
097                return this.servletConfig;
098        }
099
100        @Override
101        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
102                if (getServletContext() != null && bean instanceof ServletContextAware) {
103                        ((ServletContextAware) bean).setServletContext(getServletContext());
104                }
105                if (getServletConfig() != null && bean instanceof ServletConfigAware) {
106                        ((ServletConfigAware) bean).setServletConfig(getServletConfig());
107                }
108                return bean;
109        }
110
111        @Override
112        public Object postProcessAfterInitialization(Object bean, String beanName) {
113                return bean;
114        }
115
116}