001/* 002 * Copyright 2002-2014 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.portlet.context; 018 019import javax.portlet.PortletConfig; 020import javax.portlet.PortletContext; 021import javax.servlet.ServletContext; 022 023import org.springframework.core.env.Environment; 024import org.springframework.core.env.MutablePropertySources; 025import org.springframework.core.env.PropertySource; 026import org.springframework.core.env.PropertySource.StubPropertySource; 027import org.springframework.core.env.StandardEnvironment; 028import org.springframework.jndi.JndiLocatorDelegate; 029import org.springframework.jndi.JndiPropertySource; 030import org.springframework.web.context.support.StandardServletEnvironment; 031 032/** 033 * {@link Environment} implementation to be used by {@code Servlet}-based web 034 * applications. All Portlet-related {@code ApplicationContext} classes 035 * initialize an instance by default. 036 * 037 * <p>Contributes {@code ServletContext}, {@code PortletContext}, 038 * {@code PortletConfig} and JNDI-based {@link PropertySource} instances. 039 * See the {@link #customizePropertySources} method for details. 040 * 041 * @author Chris Beams 042 * @author Juergen Hoeller 043 * @since 3.1 044 * @see StandardEnvironment 045 * @see StandardServletEnvironment 046 */ 047public class StandardPortletEnvironment extends StandardEnvironment { 048 049 /** Portlet context init parameters property source name: {@value} */ 050 public static final String PORTLET_CONTEXT_PROPERTY_SOURCE_NAME = "portletContextInitParams"; 051 052 /** Portlet config init parameters property source name: {@value} */ 053 public static final String PORTLET_CONFIG_PROPERTY_SOURCE_NAME = "portletConfigInitParams"; 054 055 056 /** 057 * Customize the set of property sources with those contributed by superclasses as 058 * well as those appropriate for standard portlet-based environments: 059 * <ul> 060 * <li>{@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME} 061 * <li>{@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME} 062 * <li>{@linkplain StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"} 063 * <li>{@linkplain StandardServletEnvironment#JNDI_PROPERTY_SOURCE_NAME "jndiProperties"} 064 * </ul> 065 * <p>Properties present in {@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME} will 066 * take precedence over those in {@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME}, 067 * which takes precedence over those in {@linkplain 068 * StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"} 069 * and so on. 070 * <p>Properties in any of the above will take precedence over system properties and 071 * environment variables contributed by the {@link StandardEnvironment} superclass. 072 * <p>The property sources are added as stubs for now, and will be 073 * {@linkplain PortletApplicationContextUtils#initPortletPropertySources fully 074 * initialized} once the actual {@link PortletConfig}, {@link PortletContext}, and 075 * {@link ServletContext} objects are available. 076 * @see StandardEnvironment#customizePropertySources 077 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources 078 * @see PortletConfigPropertySource 079 * @see PortletContextPropertySource 080 * @see PortletApplicationContextUtils#initPortletPropertySources 081 */ 082 @Override 083 protected void customizePropertySources(MutablePropertySources propertySources) { 084 propertySources.addLast(new StubPropertySource(PORTLET_CONFIG_PROPERTY_SOURCE_NAME)); 085 propertySources.addLast(new StubPropertySource(PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)); 086 propertySources.addLast(new StubPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); 087 if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { 088 propertySources.addLast(new JndiPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)); 089 } 090 super.customizePropertySources(propertySources); 091 } 092 093 /** 094 * Replace any {@linkplain 095 * org.springframework.core.env.PropertySource.StubPropertySource stub property source} 096 * instances acting as placeholders with real portlet context/config property sources 097 * using the given parameters. 098 * @param servletContext the {@link ServletContext} (may be {@code null}) 099 * @param portletContext the {@link PortletContext} (may not be {@code null}) 100 * @param portletConfig the {@link PortletConfig} ({@code null} if not available) 101 * @see org.springframework.web.portlet.context.PortletApplicationContextUtils#initPortletPropertySources( 102 * org.springframework.core.env.MutablePropertySources, ServletContext, PortletContext, PortletConfig) 103 */ 104 public void initPropertySources(ServletContext servletContext, PortletContext portletContext, PortletConfig portletConfig) { 105 PortletApplicationContextUtils.initPortletPropertySources(getPropertySources(), servletContext, portletContext, portletConfig); 106 } 107 108}