|
|
|
@ -52,6 +52,18 @@ import static org.springframework.util.StringUtils.*; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* System property that instructs Spring to ignore system environment variables, |
|
|
|
|
|
|
|
* i.e. to never attempt to retrieve such a variable via {@link System#getenv()}. |
|
|
|
|
|
|
|
* <p>The default is "false", falling back to system environment variable checks if a |
|
|
|
|
|
|
|
* Spring environment property (e.g. a placeholder in a configuration String) isn't |
|
|
|
|
|
|
|
* resolvable otherwise. Consider switching this flag to "true" if you experience |
|
|
|
|
|
|
|
* log warnings from {@code getenv} calls coming from Spring, e.g. on WebSphere |
|
|
|
|
|
|
|
* with strict SecurityManager settings and AccessControlExceptions warnings. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static final String IGNORE_GETENV_PROPERTY_NAME = "spring.getenv.ignore"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Name of property to set to specify active profiles: {@value}. Value may be comma |
|
|
|
* Name of property to set to specify active profiles: {@value}. Value may be comma |
|
|
|
* delimited. |
|
|
|
* delimited. |
|
|
|
@ -168,8 +180,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
* {@code remove}, or {@code replace} methods exposed by {@link MutablePropertySources} |
|
|
|
* {@code remove}, or {@code replace} methods exposed by {@link MutablePropertySources} |
|
|
|
* in order to create the exact arrangement of property sources desired. |
|
|
|
* in order to create the exact arrangement of property sources desired. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>The base implementation in {@link AbstractEnvironment#customizePropertySources} |
|
|
|
* <p>The base implementation registers no property sources. |
|
|
|
* registers no property sources. |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Note that clients of any {@link ConfigurableEnvironment} may further customize |
|
|
|
* <p>Note that clients of any {@link ConfigurableEnvironment} may further customize |
|
|
|
* property sources via the {@link #getPropertySources()} accessor, typically within |
|
|
|
* property sources via the {@link #getPropertySources()} accessor, typically within |
|
|
|
@ -229,7 +240,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected Set<String> doGetActiveProfiles() { |
|
|
|
protected Set<String> doGetActiveProfiles() { |
|
|
|
if (this.activeProfiles.isEmpty()) { |
|
|
|
if (this.activeProfiles.isEmpty()) { |
|
|
|
String profiles = this.getProperty(ACTIVE_PROFILES_PROPERTY_NAME); |
|
|
|
String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME); |
|
|
|
if (StringUtils.hasText(profiles)) { |
|
|
|
if (StringUtils.hasText(profiles)) { |
|
|
|
setActiveProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles))); |
|
|
|
setActiveProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles))); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -277,7 +288,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected Set<String> doGetDefaultProfiles() { |
|
|
|
protected Set<String> doGetDefaultProfiles() { |
|
|
|
if (this.defaultProfiles.equals(getReservedDefaultProfiles())) { |
|
|
|
if (this.defaultProfiles.equals(getReservedDefaultProfiles())) { |
|
|
|
String profiles = this.getProperty(DEFAULT_PROFILES_PROPERTY_NAME); |
|
|
|
String profiles = getProperty(DEFAULT_PROFILES_PROPERTY_NAME); |
|
|
|
if (StringUtils.hasText(profiles)) { |
|
|
|
if (StringUtils.hasText(profiles)) { |
|
|
|
setDefaultProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles))); |
|
|
|
setDefaultProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles))); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -356,12 +367,22 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public Map<String, Object> getSystemEnvironment() { |
|
|
|
public Map<String, Object> getSystemEnvironment() { |
|
|
|
Map<String, ?> systemEnvironment; |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
systemEnvironment = System.getenv(); |
|
|
|
if ("true".equalsIgnoreCase(System.getProperty(IGNORE_GETENV_PROPERTY_NAME))) { |
|
|
|
|
|
|
|
return Collections.emptyMap(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Throwable ex) { |
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
|
|
|
|
logger.debug("Could not obtain system property '" + IGNORE_GETENV_PROPERTY_NAME + "': " + ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return (Map) System.getenv(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (AccessControlException ex) { |
|
|
|
catch (AccessControlException ex) { |
|
|
|
systemEnvironment = new ReadOnlySystemAttributesMap() { |
|
|
|
return (Map) new ReadOnlySystemAttributesMap() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected String getSystemAttribute(String variableName) { |
|
|
|
protected String getSystemAttribute(String variableName) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
@ -369,9 +390,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
} |
|
|
|
} |
|
|
|
catch (AccessControlException ex) { |
|
|
|
catch (AccessControlException ex) { |
|
|
|
if (logger.isInfoEnabled()) { |
|
|
|
if (logger.isInfoEnabled()) { |
|
|
|
logger.info(format("Caught AccessControlException when " + |
|
|
|
logger.info(format("Caught AccessControlException when accessing system " + |
|
|
|
"accessing system environment variable [%s]; its " + |
|
|
|
"environment variable [%s]; its value will be returned [null]. Reason: %s", |
|
|
|
"value will be returned [null]. Reason: %s", |
|
|
|
|
|
|
|
variableName, ex.getMessage())); |
|
|
|
variableName, ex.getMessage())); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
@ -379,18 +399,16 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
return (Map<String, Object>) systemEnvironment; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public Map<String, Object> getSystemProperties() { |
|
|
|
public Map<String, Object> getSystemProperties() { |
|
|
|
Map systemProperties; |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
systemProperties = System.getProperties(); |
|
|
|
return (Map) System.getProperties(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (AccessControlException ex) { |
|
|
|
catch (AccessControlException ex) { |
|
|
|
systemProperties = new ReadOnlySystemAttributesMap() { |
|
|
|
return (Map) new ReadOnlySystemAttributesMap() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected String getSystemAttribute(String propertyName) { |
|
|
|
protected String getSystemAttribute(String propertyName) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
@ -398,9 +416,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
} |
|
|
|
} |
|
|
|
catch (AccessControlException ex) { |
|
|
|
catch (AccessControlException ex) { |
|
|
|
if (logger.isInfoEnabled()) { |
|
|
|
if (logger.isInfoEnabled()) { |
|
|
|
logger.info(format("Caught AccessControlException when " + |
|
|
|
logger.info(format("Caught AccessControlException when accessing system " + |
|
|
|
"accessing system property [%s]; its value will be " + |
|
|
|
"property [%s]; its value will be returned [null]. Reason: %s", |
|
|
|
"returned [null]. Reason: %s", |
|
|
|
|
|
|
|
propertyName, ex.getMessage())); |
|
|
|
propertyName, ex.getMessage())); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
@ -408,7 +425,6 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
return systemProperties; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|