001/*
002 * Copyright 2002-2013 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.ServletConfig;
020import javax.servlet.ServletContext;
021
022import org.springframework.core.env.Environment;
023import org.springframework.core.env.MutablePropertySources;
024import org.springframework.core.env.PropertySource;
025import org.springframework.core.env.PropertySource.StubPropertySource;
026import org.springframework.core.env.StandardEnvironment;
027import org.springframework.jndi.JndiLocatorDelegate;
028import org.springframework.jndi.JndiPropertySource;
029import org.springframework.web.context.ConfigurableWebEnvironment;
030
031/**
032 * {@link Environment} implementation to be used by {@code Servlet}-based web
033 * applications. All web-related (servlet-based) {@code ApplicationContext} classes
034 * initialize an instance by default.
035 *
036 * <p>Contributes {@code ServletConfig}, {@code ServletContext}, and JNDI-based
037 * {@link PropertySource} instances. See {@link #customizePropertySources} method
038 * documentation for details.
039 *
040 * @author Chris Beams
041 * @since 3.1
042 * @see StandardEnvironment
043 */
044public class StandardServletEnvironment extends StandardEnvironment implements ConfigurableWebEnvironment {
045
046        /** Servlet context init parameters property source name: {@value} */
047        public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams";
048
049        /** Servlet config init parameters property source name: {@value} */
050        public static final String SERVLET_CONFIG_PROPERTY_SOURCE_NAME = "servletConfigInitParams";
051
052        /** JNDI property source name: {@value} */
053        public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";
054
055
056        /**
057         * Customize the set of property sources with those contributed by superclasses as
058         * well as those appropriate for standard servlet-based environments:
059         * <ul>
060         * <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
061         * <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
062         * <li>{@value #JNDI_PROPERTY_SOURCE_NAME}
063         * </ul>
064         * <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
065         * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and
066         * properties found in either of the above take precedence over those found in
067         * {@value #JNDI_PROPERTY_SOURCE_NAME}.
068         * <p>Properties in any of the above will take precedence over system properties and
069         * environment variables contributed by the {@link StandardEnvironment} superclass.
070         * <p>The {@code Servlet}-related property sources are added as
071         * {@link StubPropertySource stubs} at this stage, and will be
072         * {@linkplain #initPropertySources(ServletContext, ServletConfig) fully initialized}
073         * once the actual {@link ServletContext} object becomes available.
074         * @see StandardEnvironment#customizePropertySources
075         * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
076         * @see ServletConfigPropertySource
077         * @see ServletContextPropertySource
078         * @see org.springframework.jndi.JndiPropertySource
079         * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources
080         * @see #initPropertySources(ServletContext, ServletConfig)
081         */
082        @Override
083        protected void customizePropertySources(MutablePropertySources propertySources) {
084                propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
085                propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
086                if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
087                        propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
088                }
089                super.customizePropertySources(propertySources);
090        }
091
092        @Override
093        public void initPropertySources(ServletContext servletContext, ServletConfig servletConfig) {
094                WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
095        }
096
097}