Introduced SpringProperties class and optional "spring.properties" file
This in particular allows for specifying "spring.getenv.ignore" and "spring.beaninfo.ignore" in a local way within the application, in case that JVM-level system properties are locked. Issue: SPR-9014 Issue: SPR-11297
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Static holder for local Spring properties, i.e. defined at the Spring library level.
|
||||
*
|
||||
* <p>Reads a {@code spring.properties} file from the root of the Spring library classpath,
|
||||
* and also allows for programmatically setting properties through {@link #setProperty}.
|
||||
* When checking a property, local entries are being checked first, then falling back
|
||||
* to JVM-level system properties through a {@link System#getProperty} check.
|
||||
*
|
||||
* <p>This is an alternative way to set Spring-related system properties such as
|
||||
* "spring.getenv.ignore" and "spring.beaninfo.ignore", in particular for scenarios
|
||||
* where JVM system properties are locked on the target platform (e.g. WebSphere).
|
||||
* See {@link #setFlag} for a convenient way to locally set such flags to "true".
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.2.7
|
||||
* @see org.springframework.core.env.AbstractEnvironment#IGNORE_GETENV_PROPERTY_NAME
|
||||
* @see org.springframework.beans.CachedIntrospectionResults#IGNORE_BEANINFO_PROPERTY_NAME
|
||||
*/
|
||||
public abstract class SpringProperties {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(SpringProperties.class);
|
||||
|
||||
private static final Properties localProperties = new Properties();
|
||||
|
||||
|
||||
static {
|
||||
try {
|
||||
ClassLoader cl = SpringProperties.class.getClassLoader();
|
||||
URL url = cl.getResource("spring.properties");
|
||||
if (url != null) {
|
||||
logger.info("Found 'spring.properties' file in local classpath");
|
||||
InputStream is = url.openStream();
|
||||
try {
|
||||
localProperties.load(is);
|
||||
}
|
||||
finally {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Could not load 'spring.properties' file from local classpath: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Programmatically set a local property, overriding an entry in the
|
||||
* {@code spring.properties} file (if any).
|
||||
* @param key the property key
|
||||
* @param value the associated property value, or {@code null} to reset it
|
||||
*/
|
||||
public static void setProperty(String key, String value) {
|
||||
if (value != null) {
|
||||
localProperties.setProperty(key, value);
|
||||
}
|
||||
else {
|
||||
localProperties.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the property value for the given key, checking local Spring
|
||||
* properties first and falling back to JVM-level system properties.
|
||||
* @param key the property key
|
||||
* @return the associated property value, or {@code null} if none found
|
||||
*/
|
||||
public static String getProperty(String key) {
|
||||
String value = localProperties.getProperty(key);
|
||||
if (value == null) {
|
||||
try {
|
||||
value = System.getProperty(key);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not retrieve system property '" + key + "': " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Programmatically set a local flag to "true", overriding an
|
||||
* entry in the {@code spring.properties} file (if any).
|
||||
* @param key the property key
|
||||
*/
|
||||
public static void setFlag(String key) {
|
||||
localProperties.put(key, Boolean.TRUE.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the flag for the given property key.
|
||||
* @param key the property key
|
||||
* @return {@code true} if the property is set to "true",
|
||||
* {@code} false otherwise
|
||||
*/
|
||||
public static boolean getFlag(String key) {
|
||||
return Boolean.parseBoolean(getProperty(key));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import java.util.Set;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.core.SpringProperties;
|
||||
import org.springframework.core.convert.support.ConfigurableConversionService;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -377,15 +378,15 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
catch (AccessControlException ex) {
|
||||
return (Map) new ReadOnlySystemAttributesMap() {
|
||||
@Override
|
||||
protected String getSystemAttribute(String variableName) {
|
||||
protected String getSystemAttribute(String attributeName) {
|
||||
try {
|
||||
return System.getenv(variableName);
|
||||
return System.getenv(attributeName);
|
||||
}
|
||||
catch (AccessControlException ex) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info(format("Caught AccessControlException when accessing system " +
|
||||
"environment variable [%s]; its value will be returned [null]. Reason: %s",
|
||||
variableName, ex.getMessage()));
|
||||
attributeName, ex.getMessage()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -404,15 +405,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
* returning {@code true} if its value equals "true" in any case.
|
||||
*/
|
||||
protected boolean suppressGetenvAccess() {
|
||||
try {
|
||||
return "true".equalsIgnoreCase(System.getProperty(IGNORE_GETENV_PROPERTY_NAME));
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Could not obtain system property '" + IGNORE_GETENV_PROPERTY_NAME + "': " + ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return SpringProperties.getFlag(IGNORE_GETENV_PROPERTY_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -424,15 +417,15 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
catch (AccessControlException ex) {
|
||||
return (Map) new ReadOnlySystemAttributesMap() {
|
||||
@Override
|
||||
protected String getSystemAttribute(String propertyName) {
|
||||
protected String getSystemAttribute(String attributeName) {
|
||||
try {
|
||||
return System.getProperty(propertyName);
|
||||
return System.getProperty(attributeName);
|
||||
}
|
||||
catch (AccessControlException ex) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info(format("Caught AccessControlException when accessing system " +
|
||||
"property [%s]; its value will be returned [null]. Reason: %s",
|
||||
propertyName, ex.getMessage()));
|
||||
attributeName, ex.getMessage()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user