001/*
002 * Copyright 2002-2018 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 java.util.Map;
020
021import javax.servlet.ServletContext;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025
026import org.springframework.lang.Nullable;
027import org.springframework.web.context.ServletContextAware;
028
029/**
030 * Exporter that takes Spring-defined objects and exposes them as
031 * ServletContext attributes. Usually, bean references will be used
032 * to export Spring-defined beans as ServletContext attributes.
033 *
034 * <p>Useful to make Spring-defined beans available to code that is
035 * not aware of Spring at all, but rather just of the Servlet API.
036 * Client code can then use plain ServletContext attribute lookups
037 * to access those objects, despite them being defined in a Spring
038 * application context.
039 *
040 * <p>Alternatively, consider using the WebApplicationContextUtils
041 * class to access Spring-defined beans via the WebApplicationContext
042 * interface. This makes client code aware of Spring API, of course.
043 *
044 * @author Juergen Hoeller
045 * @since 1.1.4
046 * @see javax.servlet.ServletContext#getAttribute
047 * @see WebApplicationContextUtils#getWebApplicationContext
048 */
049public class ServletContextAttributeExporter implements ServletContextAware {
050
051        protected final Log logger = LogFactory.getLog(getClass());
052
053        @Nullable
054        private Map<String, Object> attributes;
055
056
057        /**
058         * Set the ServletContext attributes to expose as key-value pairs.
059         * Each key will be considered a ServletContext attributes key,
060         * and each value will be used as corresponding attribute value.
061         * <p>Usually, you will use bean references for the values,
062         * to export Spring-defined beans as ServletContext attributes.
063         * Of course, it is also possible to define plain values to export.
064         */
065        public void setAttributes(Map<String, Object> attributes) {
066                this.attributes = attributes;
067        }
068
069        @Override
070        public void setServletContext(ServletContext servletContext) {
071                if (this.attributes != null) {
072                        for (Map.Entry<String, Object> entry : this.attributes.entrySet()) {
073                                String attributeName = entry.getKey();
074                                if (logger.isDebugEnabled()) {
075                                        if (servletContext.getAttribute(attributeName) != null) {
076                                                logger.debug("Replacing existing ServletContext attribute with name '" + attributeName + "'");
077                                        }
078                                }
079                                servletContext.setAttribute(attributeName, entry.getValue());
080                                if (logger.isTraceEnabled()) {
081                                        logger.trace("Exported ServletContext attribute with name '" + attributeName + "'");
082                                }
083                        }
084                }
085        }
086
087}