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