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