diff options
author | Gary Gregory <garydgregory@gmail.com> | 2018-04-20 08:55:32 -0600 |
---|---|---|
committer | Gary Gregory <garydgregory@gmail.com> | 2018-04-20 08:55:32 -0600 |
commit | efba54d35fa094de5e580b200a8607bfc7bd5a7a (patch) | |
tree | f398ef7640ef58d51e0e382ad1fade88cd09cf71 /src/main/java/org/apache/commons/lang3/SystemUtils.java | |
parent | 8e3ec1722bc5c70ea932b13ec1b564950c623e77 (diff) | |
download | apache-commons-lang-efba54d35fa094de5e580b200a8607bfc7bd5a7a.tar.gz |
[LANG-1393] Add API SystemUtils.String getEnvironmentVariable(final
String name, final String defaultValue).
Diffstat (limited to 'src/main/java/org/apache/commons/lang3/SystemUtils.java')
-rw-r--r-- | src/main/java/org/apache/commons/lang3/SystemUtils.java | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/lang3/SystemUtils.java b/src/main/java/org/apache/commons/lang3/SystemUtils.java index f91628a4b..2289d5d2d 100644 --- a/src/main/java/org/apache/commons/lang3/SystemUtils.java +++ b/src/main/java/org/apache/commons/lang3/SystemUtils.java @@ -1607,6 +1607,33 @@ public class SystemUtils { /** * <p> + * Gets an environment variable, defaulting to {@code defaultValue} if the variable cannot be read. + * </p> + * <p> + * If a {@code SecurityException} is caught, the return value is {@code defaultValue} and a message is written to + * {@code System.err}. + * </p> + * + * @param name + * the environment variable name + * @param defaultValue + * the default value + * @return the environment variable value or {@code defaultValue} if a security problem occurs + * @since 3.7 + */ + public static String getEnvironmentVariable(final String name, final String defaultValue) { + try { + final String value = System.getenv(name); + return value == null ? defaultValue : value; + } catch (final SecurityException ex) { + // we are not allowed to look at this property + System.err.println("Caught a SecurityException reading the environment variable '" + name + "'."); + return defaultValue; + } + } + + /** + * <p> * Gets the user directory as a {@code File}. * </p> * |