Add PropertiesUtil.Environment.containsKey
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/a010a2af Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a010a2af Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a010a2af Branch: refs/heads/LOG4J2-1431 Commit: a010a2af3dfeabab64db24d5575cb764127c01c5 Parents: 3efa9d4 Author: Matt Sicker <[email protected]> Authored: Sat Aug 26 14:27:18 2017 -0500 Committer: Matt Sicker <[email protected]> Committed: Sat Aug 26 15:50:56 2017 -0500 ---------------------------------------------------------------------- .../logging/log4j/util/PropertiesUtil.java | 29 ++++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a010a2af/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java index 6acf707..d8679ac 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java @@ -108,7 +108,7 @@ public final class PropertiesUtil { * @return {@code true} if the specified property is defined, regardless of its value */ public boolean hasProperty(final String name) { - return System.getProperties().containsKey(name) || props.containsKey(name); + return environment.containsKey(name); } /** @@ -282,6 +282,8 @@ public final class PropertiesUtil { * Legacy: the original property name as defined in the source pre-2.9. * * Tokenized: loose matching based on word boundaries. + * + * @since 2.9 */ private static class Environment { @@ -312,6 +314,14 @@ public final class PropertiesUtil { } } + private static boolean hasSystemProperty(final String key) { + try { + return System.getProperties().containsKey(key); + } catch (final SecurityException ignored) { + return false; + } + } + private String get(final String key) { if (normalized.containsKey(key)) { return normalized.get(key); @@ -319,17 +329,18 @@ public final class PropertiesUtil { if (literal.containsKey(key)) { return literal.get(key); } - String prop = null; - try { - prop = System.getProperty(key); - } catch (final SecurityException ignored) { - // Ignore - } - if (prop != null) { - return prop; + if (hasSystemProperty(key)) { + return System.getProperty(key); } return tokenized.get(PropertySource.Util.tokenize(key)); } + + private boolean containsKey(final String key) { + return normalized.containsKey(key) || + literal.containsKey(key) || + hasSystemProperty(key) || + tokenized.containsKey(PropertySource.Util.tokenize(key)); + } } /**
