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