Browse Source

Polishing

(cherry picked from commit 16325c2)
pull/642/head
Juergen Hoeller 11 years ago
parent
commit
50e50d0c18
  1. 16
      spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java
  2. 29
      spring-context/src/test/java/org/springframework/context/support/EnvironmentSecurityManagerIntegrationTests.java
  3. 73
      spring-context/src/test/java/org/springframework/jndi/SimpleNamingContextTests.java
  4. 17
      spring-context/src/test/java/org/springframework/tests/mock/jndi/SimpleNamingContextBuilder.java
  5. 80
      spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java
  6. 97
      spring-core/src/main/java/org/springframework/core/env/AbstractPropertyResolver.java
  7. 6
      spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java
  8. 27
      spring-core/src/main/java/org/springframework/core/env/ConfigurablePropertyResolver.java
  9. 13
      spring-core/src/main/java/org/springframework/core/env/EnumerablePropertySource.java
  10. 1
      spring-core/src/main/java/org/springframework/core/env/MapPropertySource.java
  11. 15
      spring-core/src/main/java/org/springframework/core/env/SystemEnvironmentPropertySource.java
  12. 10
      spring-test/src/main/java/org/springframework/mock/jndi/SimpleNamingContextBuilder.java

16
spring-context/src/main/java/org/springframework/jndi/JndiPropertySource.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -69,9 +69,9 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
super(name, jndiLocator); super(name, jndiLocator);
} }
/** /**
* {@inheritDoc} * This implementation looks up and returns the value associated with the given
* <p>This implementation looks up and returns the value associated with the given
* name from the underlying {@link JndiLocatorDelegate}. If a {@link NamingException} * name from the underlying {@link JndiLocatorDelegate}. If a {@link NamingException}
* is thrown during the call to {@link JndiLocatorDelegate#lookup(String)}, returns * is thrown during the call to {@link JndiLocatorDelegate#lookup(String)}, returns
* {@code null} and issues a DEBUG-level log statement with the exception message. * {@code null} and issues a DEBUG-level log statement with the exception message.
@ -80,12 +80,16 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
public Object getProperty(String name) { public Object getProperty(String name) {
try { try {
Object value = this.source.lookup(name); Object value = this.source.lookup(name);
logger.debug("JNDI lookup for name [" + name + "] returned: [" + value + "]"); if (logger.isDebugEnabled()) {
logger.debug("JNDI lookup for name [" + name + "] returned: [" + value + "]");
}
return value; return value;
} }
catch (NamingException ex) { catch (NamingException ex) {
logger.debug("JNDI lookup for name [" + name + "] threw NamingException " + if (logger.isDebugEnabled()) {
"with message: " + ex.getMessage() + ". Returning null."); logger.debug("JNDI lookup for name [" + name + "] threw NamingException " +
"with message: " + ex.getMessage() + ". Returning null.");
}
return null; return null;
} }
} }

29
spring-context/src/test/java/org/springframework/context/support/EnvironmentSecurityManagerIntegrationTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,10 +16,6 @@
package org.springframework.context.support; package org.springframework.context.support;
import static java.lang.String.format;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.security.AccessControlException; import java.security.AccessControlException;
import java.security.Permission; import java.security.Permission;
import java.util.Map; import java.util.Map;
@ -35,6 +31,10 @@ import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.StandardEnvironmentTests; import org.springframework.core.env.StandardEnvironmentTests;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import static java.lang.String.format;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/** /**
* Tests integration between Environment and SecurityManagers. See SPR-9970. * Tests integration between Environment and SecurityManagers. See SPR-9970.
* *
@ -43,8 +43,10 @@ import org.springframework.stereotype.Component;
public class EnvironmentSecurityManagerIntegrationTests { public class EnvironmentSecurityManagerIntegrationTests {
private SecurityManager originalSecurityManager; private SecurityManager originalSecurityManager;
private Map<String, String> env; private Map<String, String> env;
@Before @Before
public void setUp() { public void setUp() {
originalSecurityManager = System.getSecurityManager(); originalSecurityManager = System.getSecurityManager();
@ -58,16 +60,16 @@ public class EnvironmentSecurityManagerIntegrationTests {
System.setSecurityManager(originalSecurityManager); System.setSecurityManager(originalSecurityManager);
} }
@Test @Test
public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() { public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() {
SecurityManager securityManager = new SecurityManager() { SecurityManager securityManager = new SecurityManager() {
@Override @Override
public void checkPermission(Permission perm) { public void checkPermission(Permission perm) {
// disallowing access to System#getenv means that our // Disallowing access to System#getenv means that our
// ReadOnlySystemAttributesMap will come into play. // ReadOnlySystemAttributesMap will come into play.
if ("getenv.*".equals(perm.getName())) { if ("getenv.*".equals(perm.getName())) {
throw new AccessControlException( throw new AccessControlException("Accessing the system environment is disallowed");
"Accessing the system environment is disallowed");
} }
} }
}; };
@ -84,18 +86,17 @@ public class EnvironmentSecurityManagerIntegrationTests {
SecurityManager securityManager = new SecurityManager() { SecurityManager securityManager = new SecurityManager() {
@Override @Override
public void checkPermission(Permission perm) { public void checkPermission(Permission perm) {
// disallowing access to System#getenv means that our // Disallowing access to System#getenv means that our
// ReadOnlySystemAttributesMap will come into play. // ReadOnlySystemAttributesMap will come into play.
if ("getenv.*".equals(perm.getName())) { if ("getenv.*".equals(perm.getName())) {
throw new AccessControlException( throw new AccessControlException("Accessing the system environment is disallowed");
"Accessing the system environment is disallowed");
} }
// disallowing access to the spring.profiles.active property means that // Disallowing access to the spring.profiles.active property means that
// the BeanDefinitionReader won't be able to determine which profiles are // the BeanDefinitionReader won't be able to determine which profiles are
// active. We should see an INFO-level message in the console about this // active. We should see an INFO-level message in the console about this
// and as a result, any components marked with a non-default profile will // and as a result, any components marked with a non-default profile will
// be ignored. // be ignored.
if (("getenv."+AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) { if (("getenv." + AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) {
throw new AccessControlException( throw new AccessControlException(
format("Accessing system environment variable [%s] is disallowed", format("Accessing system environment variable [%s] is disallowed",
AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME)); AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME));
@ -110,8 +111,10 @@ public class EnvironmentSecurityManagerIntegrationTests {
assertThat(bf.containsBean("c1"), is(false)); assertThat(bf.containsBean("c1"), is(false));
} }
@Component("c1") @Component("c1")
@Profile("p1") @Profile("p1")
static class C1 { static class C1 {
} }
} }

73
spring-context/src/test/java/org/springframework/jndi/SimpleNamingContextTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -214,52 +214,53 @@ public class SimpleNamingContextTests {
assertEquals(ctx.lookup(name), o2); assertEquals(ctx.lookup(name), o2);
} }
}
class StubDataSource implements DataSource { static class StubDataSource implements DataSource {
@Override @Override
public Connection getConnection() throws SQLException { public Connection getConnection() throws SQLException {
return null; return null;
} }
@Override @Override
public Connection getConnection(String username, String password) throws SQLException { public Connection getConnection(String username, String password) throws SQLException {
return null; return null;
} }
@Override @Override
public PrintWriter getLogWriter() throws SQLException { public PrintWriter getLogWriter() throws SQLException {
return null; return null;
} }
@Override @Override
public int getLoginTimeout() throws SQLException { public int getLoginTimeout() throws SQLException {
return 0; return 0;
} }
@Override @Override
public void setLogWriter(PrintWriter arg0) throws SQLException { public void setLogWriter(PrintWriter arg0) throws SQLException {
} }
@Override @Override
public void setLoginTimeout(int arg0) throws SQLException { public void setLoginTimeout(int arg0) throws SQLException {
} }
@Override @Override
public boolean isWrapperFor(Class<?> arg0) throws SQLException { public boolean isWrapperFor(Class<?> arg0) throws SQLException {
return false; return false;
} }
@Override @Override
public <T> T unwrap(Class<T> arg0) throws SQLException { public <T> T unwrap(Class<T> arg0) throws SQLException {
return null; return null;
} }
@Override @Override
public Logger getParentLogger() { public Logger getParentLogger() {
return null; return null;
}
} }
} }

17
spring-context/src/test/java/org/springframework/tests/mock/jndi/SimpleNamingContextBuilder.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.tests.mock.jndi; package org.springframework.tests.mock.jndi;
import java.util.Hashtable; import java.util.Hashtable;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory; import javax.naming.spi.InitialContextFactory;
@ -40,9 +39,9 @@ import org.springframework.util.ClassUtils;
* *
* <p>There are various choices for DataSource implementations: * <p>There are various choices for DataSource implementations:
* <ul> * <ul>
* <li>SingleConnectionDataSource (using the same Connection for all getConnection calls); * <li>{@code SingleConnectionDataSource} (using the same Connection for all getConnection calls)
* <li>DriverManagerDataSource (creating a new Connection on each getConnection call); * <li>{@code DriverManagerDataSource} (creating a new Connection on each getConnection call)
* <li>Apache's Jakarta Commons DBCP offers BasicDataSource (a real pool). * <li>Apache's Jakarta Commons DBCP offers {@code org.apache.commons.dbcp.BasicDataSource} (a real pool)
* </ul> * </ul>
* *
* <p>Typical usage in bootstrap code: * <p>Typical usage in bootstrap code:
@ -77,7 +76,6 @@ import org.springframework.util.ClassUtils;
* @see SimpleNamingContext * @see SimpleNamingContext
* @see org.springframework.jdbc.datasource.SingleConnectionDataSource * @see org.springframework.jdbc.datasource.SingleConnectionDataSource
* @see org.springframework.jdbc.datasource.DriverManagerDataSource * @see org.springframework.jdbc.datasource.DriverManagerDataSource
* @see org.apache.commons.dbcp.BasicDataSource
*/ */
public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder { public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder {
@ -197,7 +195,7 @@ public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder
if (activated == null && environment != null) { if (activated == null && environment != null) {
Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY); Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
if (icf != null) { if (icf != null) {
Class<?> icfClass = null; Class<?> icfClass;
if (icf instanceof Class) { if (icf instanceof Class) {
icfClass = (Class<?>) icf; icfClass = (Class<?>) icf;
} }
@ -216,10 +214,7 @@ public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder
return (InitialContextFactory) icfClass.newInstance(); return (InitialContextFactory) icfClass.newInstance();
} }
catch (Throwable ex) { catch (Throwable ex) {
IllegalStateException ise = throw new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf, ex);
new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf);
ise.initCause(ex);
throw ise;
} }
} }
} }

80
spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java vendored

@ -295,7 +295,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
} }
/** /**
* {@inheritDoc} * Specify the set of profiles to be made active by default if no other profiles
* are explicitly made active through {@link #setActiveProfiles}.
* <p>Calling this method removes overrides any reserved default profiles * <p>Calling this method removes overrides any reserved default profiles
* that may have been added during construction of the environment. * that may have been added during construction of the environment.
* @see #AbstractEnvironment() * @see #AbstractEnvironment()
@ -456,93 +457,98 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
//--------------------------------------------------------------------- //---------------------------------------------------------------------
@Override @Override
public boolean containsProperty(String key) { public ConfigurableConversionService getConversionService() {
return this.propertyResolver.containsProperty(key); return this.propertyResolver.getConversionService();
} }
@Override @Override
public String getProperty(String key) { public void setConversionService(ConfigurableConversionService conversionService) {
return this.propertyResolver.getProperty(key); this.propertyResolver.setConversionService(conversionService);
} }
@Override @Override
public String getProperty(String key, String defaultValue) { public void setPlaceholderPrefix(String placeholderPrefix) {
return this.propertyResolver.getProperty(key, defaultValue); this.propertyResolver.setPlaceholderPrefix(placeholderPrefix);
} }
@Override @Override
public <T> T getProperty(String key, Class<T> targetType) { public void setPlaceholderSuffix(String placeholderSuffix) {
return this.propertyResolver.getProperty(key, targetType); this.propertyResolver.setPlaceholderSuffix(placeholderSuffix);
} }
@Override @Override
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) { public void setValueSeparator(String valueSeparator) {
return this.propertyResolver.getProperty(key, targetType, defaultValue); this.propertyResolver.setValueSeparator(valueSeparator);
} }
@Override @Override
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) { public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) {
return this.propertyResolver.getPropertyAsClass(key, targetType); this.propertyResolver.setIgnoreUnresolvableNestedPlaceholders(ignoreUnresolvableNestedPlaceholders);
} }
@Override @Override
public String getRequiredProperty(String key) throws IllegalStateException { public void setRequiredProperties(String... requiredProperties) {
return this.propertyResolver.getRequiredProperty(key); this.propertyResolver.setRequiredProperties(requiredProperties);
} }
@Override @Override
public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException { public void validateRequiredProperties() throws MissingRequiredPropertiesException {
return this.propertyResolver.getRequiredProperty(key, targetType); this.propertyResolver.validateRequiredProperties();
} }
//---------------------------------------------------------------------
// Implementation of PropertyResolver interface
//---------------------------------------------------------------------
@Override @Override
public void setRequiredProperties(String... requiredProperties) { public boolean containsProperty(String key) {
this.propertyResolver.setRequiredProperties(requiredProperties); return this.propertyResolver.containsProperty(key);
} }
@Override @Override
public void validateRequiredProperties() throws MissingRequiredPropertiesException { public String getProperty(String key) {
this.propertyResolver.validateRequiredProperties(); return this.propertyResolver.getProperty(key);
} }
@Override @Override
public String resolvePlaceholders(String text) { public String getProperty(String key, String defaultValue) {
return this.propertyResolver.resolvePlaceholders(text); return this.propertyResolver.getProperty(key, defaultValue);
} }
@Override @Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException { public <T> T getProperty(String key, Class<T> targetType) {
return this.propertyResolver.resolveRequiredPlaceholders(text); return this.propertyResolver.getProperty(key, targetType);
} }
@Override @Override
public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) { public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
this.propertyResolver.setIgnoreUnresolvableNestedPlaceholders(ignoreUnresolvableNestedPlaceholders); return this.propertyResolver.getProperty(key, targetType, defaultValue);
} }
@Override @Override
public void setConversionService(ConfigurableConversionService conversionService) { public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
this.propertyResolver.setConversionService(conversionService); return this.propertyResolver.getPropertyAsClass(key, targetType);
} }
@Override @Override
public ConfigurableConversionService getConversionService() { public String getRequiredProperty(String key) throws IllegalStateException {
return this.propertyResolver.getConversionService(); return this.propertyResolver.getRequiredProperty(key);
} }
@Override @Override
public void setPlaceholderPrefix(String placeholderPrefix) { public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException {
this.propertyResolver.setPlaceholderPrefix(placeholderPrefix); return this.propertyResolver.getRequiredProperty(key, targetType);
} }
@Override @Override
public void setPlaceholderSuffix(String placeholderSuffix) { public String resolvePlaceholders(String text) {
this.propertyResolver.setPlaceholderSuffix(placeholderSuffix); return this.propertyResolver.resolvePlaceholders(text);
} }
@Override @Override
public void setValueSeparator(String valueSeparator) { public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
this.propertyResolver.setValueSeparator(valueSeparator); return this.propertyResolver.resolveRequiredPlaceholders(text);
} }

97
spring-core/src/main/java/org/springframework/core/env/AbstractPropertyResolver.java vendored

@ -65,16 +65,50 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
this.conversionService = conversionService; this.conversionService = conversionService;
} }
/**
* Set the prefix that placeholders replaced by this resolver must begin with.
* <p>The default is "${".
* @see org.springframework.util.SystemPropertyUtils#PLACEHOLDER_PREFIX
*/
@Override @Override
public String getProperty(String key, String defaultValue) { public void setPlaceholderPrefix(String placeholderPrefix) {
String value = getProperty(key); this.placeholderPrefix = placeholderPrefix;
return (value != null ? value : defaultValue);
} }
/**
* Set the suffix that placeholders replaced by this resolver must end with.
* <p>The default is "}".
* @see org.springframework.util.SystemPropertyUtils#PLACEHOLDER_SUFFIX
*/
@Override @Override
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) { public void setPlaceholderSuffix(String placeholderSuffix) {
T value = getProperty(key, targetType); this.placeholderSuffix = placeholderSuffix;
return (value != null ? value : defaultValue); }
/**
* Specify the separating character between the placeholders replaced by this
* resolver and their associated default value, or {@code null} if no such
* special character should be processed as a value separator.
* <p>The default is ":".
* @see org.springframework.util.SystemPropertyUtils#VALUE_SEPARATOR
*/
@Override
public void setValueSeparator(String valueSeparator) {
this.valueSeparator = valueSeparator;
}
/**
* Set whether to throw an exception when encountering an unresolvable placeholder
* nested within the value of a given property. A {@code false} value indicates strict
* resolution, i.e. that an exception will be thrown. A {@code true} value indicates
* that unresolvable nested placeholders should be passed through in their unresolved
* ${...} form.
* <p>The default is {@code false}.
* @since 3.2
*/
@Override
public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) {
this.ignoreUnresolvableNestedPlaceholders = ignoreUnresolvableNestedPlaceholders;
} }
@Override @Override
@ -97,6 +131,19 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
} }
} }
@Override
public String getProperty(String key, String defaultValue) {
String value = getProperty(key);
return (value != null ? value : defaultValue);
}
@Override
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
T value = getProperty(key, targetType);
return (value != null ? value : defaultValue);
}
@Override @Override
public String getRequiredProperty(String key) throws IllegalStateException { public String getRequiredProperty(String key) throws IllegalStateException {
String value = getProperty(key); String value = getProperty(key);
@ -115,33 +162,6 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
return value; return value;
} }
/**
* {@inheritDoc} The default is "${".
* @see org.springframework.util.SystemPropertyUtils#PLACEHOLDER_PREFIX
*/
@Override
public void setPlaceholderPrefix(String placeholderPrefix) {
this.placeholderPrefix = placeholderPrefix;
}
/**
* {@inheritDoc} The default is "}".
* @see org.springframework.util.SystemPropertyUtils#PLACEHOLDER_SUFFIX
*/
@Override
public void setPlaceholderSuffix(String placeholderSuffix) {
this.placeholderSuffix = placeholderSuffix;
}
/**
* {@inheritDoc} The default is ":".
* @see org.springframework.util.SystemPropertyUtils#VALUE_SEPARATOR
*/
@Override
public void setValueSeparator(String valueSeparator) {
this.valueSeparator = valueSeparator;
}
@Override @Override
public String resolvePlaceholders(String text) { public String resolvePlaceholders(String text) {
if (this.nonStrictHelper == null) { if (this.nonStrictHelper == null) {
@ -158,16 +178,6 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
return doResolvePlaceholders(text, this.strictHelper); return doResolvePlaceholders(text, this.strictHelper);
} }
/**
* {@inheritDoc}
* <p>The default value for this implementation is {@code false}.
* @since 3.2
*/
@Override
public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) {
this.ignoreUnresolvableNestedPlaceholders = ignoreUnresolvableNestedPlaceholders;
}
/** /**
* Resolve placeholders within the given string, deferring to the value of * Resolve placeholders within the given string, deferring to the value of
* {@link #setIgnoreUnresolvableNestedPlaceholders} to determine whether any * {@link #setIgnoreUnresolvableNestedPlaceholders} to determine whether any
@ -199,6 +209,7 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
}); });
} }
/** /**
* Retrieve the specified property as a raw String, * Retrieve the specified property as a raw String,
* i.e. without resolution of nested placeholders. * i.e. without resolution of nested placeholders.

6
spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java vendored

@ -223,8 +223,7 @@ public abstract class CommandLinePropertySource<T> extends EnumerablePropertySou
} }
/** /**
* Return whether this {@code PropertySource} contains a property with the given name. * This implementation first checks to see if the name specified is the special
* <p>This implementation first checks to see if the name specified is the special
* {@linkplain #setNonOptionArgsPropertyName(String) "non-option arguments" property}, * {@linkplain #setNonOptionArgsPropertyName(String) "non-option arguments" property},
* and if so delegates to the abstract {@link #getNonOptionArgs()} method * and if so delegates to the abstract {@link #getNonOptionArgs()} method
* checking to see whether it returns an empty collection. Otherwise delegates to and * checking to see whether it returns an empty collection. Otherwise delegates to and
@ -239,8 +238,7 @@ public abstract class CommandLinePropertySource<T> extends EnumerablePropertySou
} }
/** /**
* {@inheritDoc} * This implementation first checks to see if the name specified is the special
* <p>This implementation first checks to see if the name specified is the special
* {@linkplain #setNonOptionArgsPropertyName(String) "non-option arguments" property}, * {@linkplain #setNonOptionArgsPropertyName(String) "non-option arguments" property},
* and if so delegates to the abstract {@link #getNonOptionArgs()} method. If so * and if so delegates to the abstract {@link #getNonOptionArgs()} method. If so
* and the collection of non-option arguments is empty, this method returns {@code * and the collection of non-option arguments is empty, this method returns {@code

27
spring-core/src/main/java/org/springframework/core/env/ConfigurablePropertyResolver.java vendored

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -73,6 +73,19 @@ public interface ConfigurablePropertyResolver extends PropertyResolver {
*/ */
void setValueSeparator(String valueSeparator); void setValueSeparator(String valueSeparator);
/**
* Set whether to throw an exception when encountering an unresolvable placeholder
* nested within the value of a given property. A {@code false} value indicates strict
* resolution, i.e. that an exception will be thrown. A {@code true} value indicates
* that unresolvable nested placeholders should be passed through in their unresolved
* ${...} form.
* <p>Implementations of {@link #getProperty(String)} and its variants must inspect
* the value set here to determine correct behavior when property values contain
* unresolvable placeholders.
* @since 3.2
*/
void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders);
/** /**
* Specify which properties must be present, to be verified by * Specify which properties must be present, to be verified by
* {@link #validateRequiredProperties()}. * {@link #validateRequiredProperties()}.
@ -88,16 +101,4 @@ public interface ConfigurablePropertyResolver extends PropertyResolver {
*/ */
void validateRequiredProperties() throws MissingRequiredPropertiesException; void validateRequiredProperties() throws MissingRequiredPropertiesException;
/**
* Set whether to throw an exception when encountering an unresolvable placeholder
* nested within the value of a given property. A {@code false} value indicates strict
* resolution, i.e. that an exception will be thrown. A {@code true} value indicates
* that unresolvable nested placeholders should be passed through in their unresolved
* ${...} form.
* <p>Implementations of {@link #getProperty(String)} and its variants must inspect
* the value set here to determine correct behavior when property values contain
* unresolvable placeholders.
* @since 3.2
*/
void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders);
} }

13
spring-core/src/main/java/org/springframework/core/env/EnumerablePropertySource.java vendored

@ -41,6 +41,7 @@ import org.springframework.util.Assert;
* or not. * or not.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
* @since 3.1 * @since 3.1
*/ */
public abstract class EnumerablePropertySource<T> extends PropertySource<T> { public abstract class EnumerablePropertySource<T> extends PropertySource<T> {
@ -56,12 +57,6 @@ public abstract class EnumerablePropertySource<T> extends PropertySource<T> {
} }
/**
* Return the names of all properties contained by the
* {@linkplain #getSource() source} object (never {@code null}).
*/
public abstract String[] getPropertyNames();
/** /**
* Return whether this {@code PropertySource} contains a property with the given name. * Return whether this {@code PropertySource} contains a property with the given name.
* <p>This implementation checks for the presence of the given name within the * <p>This implementation checks for the presence of the given name within the
@ -85,4 +80,10 @@ public abstract class EnumerablePropertySource<T> extends PropertySource<T> {
return false; return false;
} }
/**
* Return the names of all properties contained by the
* {@linkplain #getSource() source} object (never {@code null}).
*/
public abstract String[] getPropertyNames();
} }

1
spring-core/src/main/java/org/springframework/core/env/MapPropertySource.java vendored

@ -33,6 +33,7 @@ public class MapPropertySource extends EnumerablePropertySource<Map<String, Obje
super(name, source); super(name, source);
} }
@Override @Override
public Object getProperty(String name) { public Object getProperty(String name) {
return this.source.get(name); return this.source.get(name);

15
spring-core/src/main/java/org/springframework/core/env/SystemEnvironmentPropertySource.java vendored

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,11 +40,11 @@ import org.springframework.util.Assert;
* {@code true} if any of the above properties are present, otherwise {@code false}. * {@code true} if any of the above properties are present, otherwise {@code false}.
* *
* <p>This feature is particularly useful when specifying active or default profiles as * <p>This feature is particularly useful when specifying active or default profiles as
* environment variables. The following is not allowable under Bash * environment variables. The following is not allowable under Bash:
* *
* <pre class="code">spring.profiles.active=p1 java -classpath ... MyApp</pre> * <pre class="code">spring.profiles.active=p1 java -classpath ... MyApp</pre>
* *
* However, the following syntax is permitted and is also more conventional. * However, the following syntax is permitted and is also more conventional:
* *
* <pre class="code">SPRING_PROFILES_ACTIVE=p1 java -classpath ... MyApp</pre> * <pre class="code">SPRING_PROFILES_ACTIVE=p1 java -classpath ... MyApp</pre>
* *
@ -70,8 +70,9 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
super(name, source); super(name, source);
} }
/** /**
* Return true if a property with the given name or any underscore/uppercase variant * Return {@code true} if a property with the given name or any underscore/uppercase variant
* thereof exists in this property source. * thereof exists in this property source.
*/ */
@Override @Override
@ -80,13 +81,11 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
} }
/** /**
* {@inheritDoc} * This implementation returns {@code true} if a property with the given name or
* <p>This implementation returns {@code true} if a property with the given name or
* any underscore/uppercase variant thereof exists in this property source. * any underscore/uppercase variant thereof exists in this property source.
*/ */
@Override @Override
public Object getProperty(String name) { public Object getProperty(String name) {
Assert.notNull(name, "property name must not be null");
String actualName = resolvePropertyName(name); String actualName = resolvePropertyName(name);
if (logger.isDebugEnabled() && !name.equals(actualName)) { if (logger.isDebugEnabled() && !name.equals(actualName)) {
logger.debug(String.format("PropertySource [%s] does not contain '%s', but found equivalent '%s'", logger.debug(String.format("PropertySource [%s] does not contain '%s', but found equivalent '%s'",
@ -101,6 +100,7 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
* found or otherwise the original name. Never returns {@code null}. * found or otherwise the original name. Never returns {@code null}.
*/ */
private String resolvePropertyName(String name) { private String resolvePropertyName(String name) {
Assert.notNull(name, "Property name must not be null");
if (super.containsProperty(name)) { if (super.containsProperty(name)) {
return name; return name;
} }
@ -125,4 +125,5 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
return name; return name;
} }
} }

10
spring-test/src/main/java/org/springframework/mock/jndi/SimpleNamingContextBuilder.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.mock.jndi; package org.springframework.mock.jndi;
import java.util.Hashtable; import java.util.Hashtable;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.NamingException; import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory; import javax.naming.spi.InitialContextFactory;
@ -196,7 +195,7 @@ public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder
if (activated == null && environment != null) { if (activated == null && environment != null) {
Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY); Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
if (icf != null) { if (icf != null) {
Class<?> icfClass = null; Class<?> icfClass;
if (icf instanceof Class) { if (icf instanceof Class) {
icfClass = (Class<?>) icf; icfClass = (Class<?>) icf;
} }
@ -215,10 +214,7 @@ public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder
return (InitialContextFactory) icfClass.newInstance(); return (InitialContextFactory) icfClass.newInstance();
} }
catch (Throwable ex) { catch (Throwable ex) {
IllegalStateException ise = throw new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf, ex);
new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf);
ise.initCause(ex);
throw ise;
} }
} }
} }

Loading…
Cancel
Save