From c825a4d270838f9583b7bc31a3b20f8747ac2269 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Sat, 1 Feb 2025 21:56:45 +0000 Subject: [PATCH] Avoid creating a copy of SystemProperties just to look for the `config_ordinal` --- .../smallrye/config/SysPropConfigSource.java | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java b/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java index cf41a1902..f50c60273 100644 --- a/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java @@ -15,6 +15,7 @@ */ package io.smallrye.config; +import static io.smallrye.config.common.utils.ConfigSourceUtil.CONFIG_ORDINAL_KEY; import static io.smallrye.config.common.utils.ConfigSourceUtil.propertiesToMap; import static java.security.AccessController.doPrivileged; import static java.util.Collections.unmodifiableMap; @@ -25,7 +26,6 @@ import java.util.Set; import io.smallrye.config.common.AbstractConfigSource; -import io.smallrye.config.common.utils.ConfigSourceUtil; /** * @author Jeff Mesnil (c) 2017 Red Hat inc. @@ -37,21 +37,51 @@ public class SysPropConfigSource extends AbstractConfigSource { public static final int ORDINAL = 400; public SysPropConfigSource() { - super(NAME, ConfigSourceUtil.getOrdinalFromMap(getSystemProperties(), ORDINAL)); + super(NAME, getSystemOrdinal()); } @Override public Map getProperties() { - return getSystemProperties(); + if (System.getSecurityManager() == null) { + return unmodifiableMap(propertiesToMap(System.getProperties())); + } else { + return doPrivileged(new PrivilegedAction>() { + @Override + public Map run() { + return unmodifiableMap(propertiesToMap(doPrivileged((PrivilegedAction) System::getProperties))); + } + }); + } } @Override public Set getPropertyNames() { - return getProperties().keySet(); + if (System.getSecurityManager() == null) { + return System.getProperties().stringPropertyNames(); + } else { + return doPrivileged(new PrivilegedAction>() { + @Override + public Set run() { + return System.getProperties().stringPropertyNames(); + } + }); + } } @Override public String getValue(String propertyName) { + return getSystemProperty(propertyName); + } + + private static int getSystemOrdinal() { + String value = getSystemProperty(CONFIG_ORDINAL_KEY); + if (value != null) { + return Converters.INTEGER_CONVERTER.convert(value); + } + return ORDINAL; + } + + private static String getSystemProperty(final String propertyName) { if (System.getSecurityManager() == null) { return System.getProperty(propertyName); } else { @@ -63,8 +93,4 @@ public String run() { }); } } - - private static Map getSystemProperties() { - return unmodifiableMap(propertiesToMap(doPrivileged((PrivilegedAction) System::getProperties))); - } }