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.ServletContext;
020
021import org.springframework.beans.factory.FactoryBean;
022import org.springframework.lang.Nullable;
023import org.springframework.web.context.ServletContextAware;
024
025/**
026 * {@link FactoryBean} that fetches a specific, existing ServletContext attribute.
027 * Exposes that ServletContext attribute when used as bean reference,
028 * effectively making it available as named Spring bean instance.
029 *
030 * <p>Intended to link in ServletContext attributes that exist before
031 * the startup of the Spring application context. Typically, such
032 * attributes will have been put there by third-party web frameworks.
033 * In a purely Spring-based web application, no such linking in of
034 * ServletContext attributes will be necessary.
035 *
036 * <p><b>NOTE:</b> As of Spring 3.0, you may also use the "contextAttributes" default
037 * bean which is of type Map, and dereference it using an "#{contextAttributes.myKey}"
038 * expression to access a specific attribute by name.
039 *
040 * @author Juergen Hoeller
041 * @since 1.1.4
042 * @see org.springframework.web.context.WebApplicationContext#CONTEXT_ATTRIBUTES_BEAN_NAME
043 * @see ServletContextParameterFactoryBean
044 */
045public class ServletContextAttributeFactoryBean implements FactoryBean<Object>, ServletContextAware {
046
047        @Nullable
048        private String attributeName;
049
050        @Nullable
051        private Object attribute;
052
053
054        /**
055         * Set the name of the ServletContext attribute to expose.
056         */
057        public void setAttributeName(String attributeName) {
058                this.attributeName = attributeName;
059        }
060
061        @Override
062        public void setServletContext(ServletContext servletContext) {
063                if (this.attributeName == null) {
064                        throw new IllegalArgumentException("Property 'attributeName' is required");
065                }
066                this.attribute = servletContext.getAttribute(this.attributeName);
067                if (this.attribute == null) {
068                        throw new IllegalStateException("No ServletContext attribute '" + this.attributeName + "' found");
069                }
070        }
071
072
073        @Override
074        @Nullable
075        public Object getObject() throws Exception {
076                return this.attribute;
077        }
078
079        @Override
080        public Class<?> getObjectType() {
081                return (this.attribute != null ? this.attribute.getClass() : null);
082        }
083
084        @Override
085        public boolean isSingleton() {
086                return true;
087        }
088
089}