From 6db594c79dec6532e68c06ac0a44af4edd055975 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 20 Aug 2011 03:02:31 +0000 Subject: [PATCH] Register JndiPropertySource by default in servlet environments Prior to this change, StandardServletEnvironment evaluated a "jndiPropertySourceEnabled" flag to determine whether or not to add a JndiPropertySource. Following the changes introduced in SPR-8490, there is now no reason not to enable a JNDI property source by default. This change eliminates the support for "jndiPropertySourceEnabled" and adds a JndiPropertySource automatically. Issue: SPR-8545, SPR-8490 --- .../jndi/JndiPropertySource.java | 34 +++------------- .../jndi/JndiPropertySourceTests.java | 6 +-- .../support/StandardServletEnvironment.java | 39 ++++++------------- .../StandardServletEnvironmentTests.java | 33 +--------------- 4 files changed, 21 insertions(+), 91 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java b/org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java index 071d728b3b3..adce69cc6f1 100644 --- a/org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java +++ b/org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java @@ -35,14 +35,12 @@ import org.springframework.core.env.PropertySource; * be specified using {@link JndiLocatorDelegate#setJndiEnvironment(java.util.Properties)} * prior to construction of the {@code JndiPropertySource}. * - *

{@link org.springframework.web.context.support.StandardServletEnvironment - * StandardServletEnvironment} allows for declaratively including a - * {@code JndiPropertySource} through its support for a {@link - * org.springframework.web.context.support.StandardServletEnvironment#JNDI_PROPERTY_SOURCE_ENABLED - * JNDI_PROPERTY_SOURCE_ENABLED} context-param, but any customization of the underlying - * {@link JndiLocatorDelegate} will typically be done within an {@link - * org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer} - * or {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer}. + *

Note that {@link org.springframework.web.context.support.StandardServletEnvironment + * StandardServletEnvironment} includes a {@code JndiPropertySource} by default, and any + * customization of the underlying {@link JndiLocatorDelegate} may be performed within an + * {@link org.springframework.context.ApplicationContextInitializer + * ApplicationContextInitializer} or {@link org.springframework.web.WebApplicationInitializer + * WebApplicationInitializer}. * * @author Chris Beams * @author Juergen Hoeller @@ -54,18 +52,6 @@ import org.springframework.core.env.PropertySource; */ public class JndiPropertySource extends PropertySource { - /** JNDI context property source name: {@value} */ - public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiPropertySource"; - - /** - * Create a new {@code JndiPropertySource} with the default name - * {@value #JNDI_PROPERTY_SOURCE_NAME} and a {@link JndiLocatorDelegate} configured - * to prefix any names with "java:comp/env/". - */ - public JndiPropertySource() { - this(JNDI_PROPERTY_SOURCE_NAME); - } - /** * Create a new {@code JndiPropertySource} with the given name * and a {@link JndiLocatorDelegate} configured to prefix any names with @@ -75,14 +61,6 @@ public class JndiPropertySource extends PropertySource { this(name, createDefaultJndiLocator()); } - /** - * Create a new {@code JndiPropertySource} with the default name - * {@value #JNDI_PROPERTY_SOURCE_NAME} and the given {@code JndiLocatorDelegate}. - */ - public JndiPropertySource(JndiLocatorDelegate jndiLocator) { - this(JNDI_PROPERTY_SOURCE_NAME, jndiLocator); - } - /** * Create a new {@code JndiPropertySource} with the given name and the given * {@code JndiLocatorDelegate}. diff --git a/org.springframework.context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java b/org.springframework.context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java index 7cb1cdcf530..5148742ca42 100644 --- a/org.springframework.context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java +++ b/org.springframework.context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java @@ -36,7 +36,7 @@ public class JndiPropertySourceTests { @Test public void nonExistentProperty() { - JndiPropertySource ps = new JndiPropertySource(); + JndiPropertySource ps = new JndiPropertySource("jndiProperties"); assertThat(ps.getProperty("bogus"), nullValue()); } @@ -55,7 +55,7 @@ public class JndiPropertySourceTests { jndiLocator.setResourceRef(true); jndiLocator.setJndiTemplate(jndiTemplate); - JndiPropertySource ps = new JndiPropertySource(jndiLocator); + JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator); assertThat((String)ps.getProperty("p1"), equalTo("v1")); } @@ -74,7 +74,7 @@ public class JndiPropertySourceTests { jndiLocator.setResourceRef(true); jndiLocator.setJndiTemplate(jndiTemplate); - JndiPropertySource ps = new JndiPropertySource(jndiLocator); + JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator); assertThat((String)ps.getProperty("p1"), equalTo("v1")); } diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java b/org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java index 56b864523ec..d2e7964e3ec 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java +++ b/org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java @@ -19,12 +19,11 @@ package org.springframework.web.context.support; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; -import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource.StubPropertySource; -import org.springframework.core.env.PropertySources; +import org.springframework.core.env.StandardEnvironment; import org.springframework.jndi.JndiPropertySource; /** @@ -32,20 +31,13 @@ import org.springframework.jndi.JndiPropertySource; * applications. All web-related (servlet-based) {@code ApplicationContext} classes * initialize an instance by default. * - *

Contributes {@code ServletConfig}- and {@code ServletContext}-based - * {@link PropertySource} instances. See the {@link #customizePropertySources} method - * for details. - * - *

After initial bootstrapping, property sources will be searched for the presence of a - * "jndiPropertySourceEnabled" property; if found, a {@link JndiPropertySource} will be - * added to this environment's {@link PropertySources}, with precedence higher than system - * properties and environment variables, but lower than that of ServletContext and - * ServletConfig init params. + *

Contributes {@code ServletConfig}, {@code ServletContext}, and JNDI-based + * {@link PropertySource} instances. See {@link #customizePropertySources} method + * documentation for details. * * @author Chris Beams * @since 3.1 * @see StandardEnvironment - * @see StandardPortletEnvironment */ public class StandardServletEnvironment extends StandardEnvironment { @@ -55,11 +47,8 @@ public class StandardServletEnvironment extends StandardEnvironment { /** Servlet config init parameters property source name: {@value} */ public static final String SERVLET_CONFIG_PROPERTY_SOURCE_NAME = "servletConfigInitParams"; - /** - * Name of property used to determine if a {@link JndiPropertySource} - * should be registered by default: {@value} - */ - public static final String JNDI_PROPERTY_SOURCE_ENABLED_FLAG = "jndiPropertySourceEnabled"; + /** JNDI property source name: {@value} */ + public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties"; /** @@ -68,21 +57,18 @@ public class StandardServletEnvironment extends StandardEnvironment { *

*

Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will - * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}. + * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and + * properties found in either of the above take precedence over those found in + * {@value #JNDI_PROPERTY_SOURCE_NAME}. *

Properties in any of the above will take precedence over system properties and * environment variables contributed by the {@link StandardEnvironment} superclass. *

The {@code Servlet}-related property sources are added as stubs for now, and * will be {@linkplain WebApplicationContextUtils#initServletPropertySources fully * initialized} once the actual {@link ServletConfig} and {@link ServletContext} * objects are available. - *

If the {@link JndiPropertySource#JNDI_PROPERTY_SOURCE_ENABLED_FLAG "jndiPropertySourceEnabled"} - * property is present in any of the default property sources, a - * {@link JndiPropertySource} will be added as well, with precedence lower than - * servlet property sources, but higher than system properties and environment - * variables. * @see StandardEnvironment#customizePropertySources * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources * @see ServletConfigPropertySource @@ -95,10 +81,7 @@ public class StandardServletEnvironment extends StandardEnvironment { protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); + propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME)); super.customizePropertySources(propertySources); - - if (this.getProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, boolean.class, false)) { - propertySources.addAfter(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, new JndiPropertySource()); - } } } diff --git a/org.springframework.web/src/test/java/org/springframework/web/context/support/StandardServletEnvironmentTests.java b/org.springframework.web/src/test/java/org/springframework/web/context/support/StandardServletEnvironmentTests.java index 068338a94e0..5c87bb7c057 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/context/support/StandardServletEnvironmentTests.java +++ b/org.springframework.web/src/test/java/org/springframework/web/context/support/StandardServletEnvironmentTests.java @@ -19,14 +19,12 @@ package org.springframework.web.context.support; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -import static org.springframework.web.context.support.StandardServletEnvironment.JNDI_PROPERTY_SOURCE_ENABLED_FLAG; import org.junit.Test; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; -import org.springframework.jndi.JndiPropertySource; /** * Unit tests for {@link StandardServletEnvironment}. @@ -40,42 +38,13 @@ public class StandardServletEnvironmentTests { public void propertySourceOrder() { ConfigurableEnvironment env = new StandardServletEnvironment(); MutablePropertySources sources = env.getPropertySources(); - assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0)); - assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1)); - assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2)); - assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3)); - assertThat(sources.size(), is(4)); - } - - @Test - public void propertySourceOrder_jndiPropertySourceEnabled() { - System.setProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "true"); - ConfigurableEnvironment env = new StandardServletEnvironment(); - MutablePropertySources sources = env.getPropertySources(); assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1)); - assertThat(sources.precedenceOf(PropertySource.named(JndiPropertySource.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2)); + assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4)); assertThat(sources.size(), is(5)); - - System.clearProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG); } - @Test - public void propertySourceOrder_jndiPropertySourceEnabledIsFalse() { - System.setProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "false"); - ConfigurableEnvironment env = new StandardServletEnvironment(); - MutablePropertySources sources = env.getPropertySources(); - - assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0)); - assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1)); - //assertThat(sources.precedenceOf(PropertySource.named(JndiPropertySource.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2)); - assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2)); - assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3)); - assertThat(sources.size(), is(4)); - - System.clearProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG); - } }