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 retrieves a specific ServletContext init parameter
027 * (that is, a "context-param" defined in {@code web.xml}).
028 * Exposes that ServletContext init parameter when used as bean reference,
029 * effectively making it available as named Spring bean instance.
030 *
031 * <p><b>NOTE:</b> As of Spring 3.0, you may also use the "contextParameters" default
032 * bean which is of type Map, and dereference it using an "#{contextParameters.myKey}"
033 * expression to access a specific parameter by name.
034 *
035 * @author Juergen Hoeller
036 * @since 1.2.4
037 * @see org.springframework.web.context.WebApplicationContext#CONTEXT_PARAMETERS_BEAN_NAME
038 * @see ServletContextAttributeFactoryBean
039 */
040public class ServletContextParameterFactoryBean implements FactoryBean<String>, ServletContextAware {
041
042        @Nullable
043        private String initParamName;
044
045        @Nullable
046        private String paramValue;
047
048
049        /**
050         * Set the name of the ServletContext init parameter to expose.
051         */
052        public void setInitParamName(String initParamName) {
053                this.initParamName = initParamName;
054        }
055
056        @Override
057        public void setServletContext(ServletContext servletContext) {
058                if (this.initParamName == null) {
059                        throw new IllegalArgumentException("initParamName is required");
060                }
061                this.paramValue = servletContext.getInitParameter(this.initParamName);
062                if (this.paramValue == null) {
063                        throw new IllegalStateException("No ServletContext init parameter '" + this.initParamName + "' found");
064                }
065        }
066
067
068        @Override
069        @Nullable
070        public String getObject() {
071                return this.paramValue;
072        }
073
074        @Override
075        public Class<String> getObjectType() {
076                return String.class;
077        }
078
079        @Override
080        public boolean isSingleton() {
081                return true;
082        }
083
084}