From 5aaaf5c1e46498306fd9bb89b43168e8eae45ec2 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Wed, 11 Dec 2024 15:26:08 +0000 Subject: [PATCH] Source names as constants --- .../io/smallrye/config/DefaultValuesConfigSource.java | 6 ++++-- .../java/io/smallrye/config/PropertiesConfigSource.java | 7 ++++--- .../java/io/smallrye/config/SysPropConfigSource.java | 6 ++++-- .../config/source/file/FileSystemConfigSource.java | 9 ++++++--- .../smallrye/config/source/hocon/HoconConfigSource.java | 7 +++---- .../source/keystore/KeyStoreConfigSourceFactory.java | 8 ++++++-- .../io/smallrye/config/source/yaml/YamlConfigSource.java | 7 +++---- .../config/source/zookeeper/ZooKeeperConfigSource.java | 8 ++++---- 8 files changed, 34 insertions(+), 24 deletions(-) diff --git a/implementation/src/main/java/io/smallrye/config/DefaultValuesConfigSource.java b/implementation/src/main/java/io/smallrye/config/DefaultValuesConfigSource.java index c946105c7..fd2fe9a23 100644 --- a/implementation/src/main/java/io/smallrye/config/DefaultValuesConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/DefaultValuesConfigSource.java @@ -9,12 +9,14 @@ public final class DefaultValuesConfigSource extends AbstractConfigSource { private static final long serialVersionUID = -6386021034957868328L; - private final Map properties; + public static final String NAME = "DefaultValuesConfigSource"; + public static final int ORDINAL = Integer.MIN_VALUE; + private final Map properties; private final Map wildcards; public DefaultValuesConfigSource(final Map properties) { - this(properties, "DefaultValuesConfigSource", Integer.MIN_VALUE); + this(properties, NAME, ORDINAL); } public DefaultValuesConfigSource(final Map properties, final String name, final int ordinal) { diff --git a/implementation/src/main/java/io/smallrye/config/PropertiesConfigSource.java b/implementation/src/main/java/io/smallrye/config/PropertiesConfigSource.java index f5b2b5e6f..453f8a0e0 100644 --- a/implementation/src/main/java/io/smallrye/config/PropertiesConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/PropertiesConfigSource.java @@ -34,14 +34,15 @@ public class PropertiesConfigSource extends MapBackedConfigValueConfigSource { private static final long serialVersionUID = 1866835565147832432L; - private static final String NAME_PREFIX = "PropertiesConfigSource[source="; + public static final String NAME = "PropertiesConfigSource[source=%s]"; + public static final int ORDINAL = DEFAULT_ORDINAL; public PropertiesConfigSource(URL url) throws IOException { this(url, DEFAULT_ORDINAL); } public PropertiesConfigSource(URL url, int defaultOrdinal) throws IOException { - this(url, NAME_PREFIX + url.toString() + "]", defaultOrdinal); + this(url, String.format(NAME, url.toString()), defaultOrdinal); } private PropertiesConfigSource(URL url, String name, int defaultOrdinal) throws IOException { @@ -57,7 +58,7 @@ public PropertiesConfigSource(Properties properties, String name) { } public PropertiesConfigSource(Map properties, String name, int defaultOrdinal) { - this(NAME_PREFIX + name + "]", properties, defaultOrdinal); + this(String.format(NAME, name), properties, defaultOrdinal); } private PropertiesConfigSource(String name, Map properties, int defaultOrdinal) { diff --git a/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java b/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java index 504075cee..cf41a1902 100644 --- a/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java @@ -32,10 +32,12 @@ */ public class SysPropConfigSource extends AbstractConfigSource { private static final long serialVersionUID = 9167738611308785403L; - private static final int DEFAULT_ORDINAL = 400; + + public static final String NAME = "SysPropConfigSource"; + public static final int ORDINAL = 400; public SysPropConfigSource() { - super("SysPropConfigSource", ConfigSourceUtil.getOrdinalFromMap(getSystemProperties(), DEFAULT_ORDINAL)); + super(NAME, ConfigSourceUtil.getOrdinalFromMap(getSystemProperties(), ORDINAL)); } @Override diff --git a/sources/file-system/src/main/java/io/smallrye/config/source/file/FileSystemConfigSource.java b/sources/file-system/src/main/java/io/smallrye/config/source/file/FileSystemConfigSource.java index 294cd4839..0bcb8e031 100644 --- a/sources/file-system/src/main/java/io/smallrye/config/source/file/FileSystemConfigSource.java +++ b/sources/file-system/src/main/java/io/smallrye/config/source/file/FileSystemConfigSource.java @@ -58,14 +58,17 @@ public class FileSystemConfigSource extends MapBackedConfigSource { private static final long serialVersionUID = 654034634846856045L; + public static final String NAME = "FileSystemConfigSource[dir=%s]"; + public static final int ORDINAL = DEFAULT_ORDINAL; + private static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9_]"); public FileSystemConfigSource(File dir) { - this(dir, DEFAULT_ORDINAL); + this(dir, ORDINAL); } public FileSystemConfigSource(String dir) { - this(new File(dir), DEFAULT_ORDINAL); + this(new File(dir), ORDINAL); } /** @@ -75,7 +78,7 @@ public FileSystemConfigSource(String dir) { * @param ordinal the ordinal value */ public FileSystemConfigSource(File dir, int ordinal) { - super("FileSystemConfigSource[dir=" + dir.getAbsolutePath() + "]", scan(dir), ordinal); + super(String.format(NAME, dir.getAbsolutePath()), scan(dir), ordinal); } private static Map scan(File directory) { diff --git a/sources/hocon/src/main/java/io/smallrye/config/source/hocon/HoconConfigSource.java b/sources/hocon/src/main/java/io/smallrye/config/source/hocon/HoconConfigSource.java index 97b1fd0f1..eb66fadff 100644 --- a/sources/hocon/src/main/java/io/smallrye/config/source/hocon/HoconConfigSource.java +++ b/sources/hocon/src/main/java/io/smallrye/config/source/hocon/HoconConfigSource.java @@ -10,8 +10,6 @@ import java.util.Set; import java.util.TreeMap; -import org.eclipse.microprofile.config.spi.ConfigSource; - import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigList; @@ -24,7 +22,8 @@ public class HoconConfigSource extends MapBackedConfigSource { private static final long serialVersionUID = -458821383311704657L; - static final int ORDINAL = ConfigSource.DEFAULT_ORDINAL + 5; + public static final String NAME = "HoconConfigSource[source=%s]"; + public static final int ORDINAL = DEFAULT_ORDINAL + 5; public HoconConfigSource(String name, Map source, int ordinal) { super(name, source, ordinal, false); @@ -35,7 +34,7 @@ public HoconConfigSource(URL url) throws IOException { } public HoconConfigSource(URL url, int ordinal) throws IOException { - this("HoconConfigSource[source=" + url.toString() + "]", ClassPathUtils.readStream(url, inputStream -> { + this(String.format(NAME, url.toString()), ClassPathUtils.readStream(url, inputStream -> { try { return streamToMap(inputStream); } catch (IOException e) { diff --git a/sources/keystore/src/main/java/io/smallrye/config/source/keystore/KeyStoreConfigSourceFactory.java b/sources/keystore/src/main/java/io/smallrye/config/source/keystore/KeyStoreConfigSourceFactory.java index 6dbd65a58..e245b35ab 100644 --- a/sources/keystore/src/main/java/io/smallrye/config/source/keystore/KeyStoreConfigSourceFactory.java +++ b/sources/keystore/src/main/java/io/smallrye/config/source/keystore/KeyStoreConfigSourceFactory.java @@ -1,6 +1,7 @@ package io.smallrye.config.source.keystore; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.eclipse.microprofile.config.spi.ConfigSource.DEFAULT_ORDINAL; import java.io.IOException; import java.net.URL; @@ -33,6 +34,9 @@ import io.smallrye.config.source.keystore.KeyStoreConfig.KeyStore.Alias; public class KeyStoreConfigSourceFactory implements ConfigSourceFactory { + public static final String NAME = "KeyStoreConfigSource[source=%s]"; + public static final int ORDINAL = DEFAULT_ORDINAL; + @Override public Iterable getConfigSources(final ConfigSourceContext context) { KeyStoreConfig keyStoreConfig = getKeyStoreConfig(context); @@ -107,7 +111,7 @@ protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws @Override public Iterable getConfigSources(final ConfigSourceContext context) { - return loadConfigSources(keyStore.path(), 100); + return loadConfigSources(keyStore.path(), ORDINAL); } }.getConfigSources(context); @@ -195,7 +199,7 @@ public String getValue(final String propertyName) { @Override public String getName() { - return "KeyStoreConfigSource[source=" + url.toString() + "]"; + return String.format(NAME, url.toString()); } } } diff --git a/sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSource.java b/sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSource.java index da83142a7..da9890569 100644 --- a/sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSource.java +++ b/sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSource.java @@ -14,7 +14,6 @@ import java.util.function.Function; import java.util.stream.Collectors; -import org.eclipse.microprofile.config.spi.ConfigSource; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; @@ -33,8 +32,8 @@ public class YamlConfigSource extends MapBackedConfigSource { private static final long serialVersionUID = -418186029484956531L; - private static final String NAME_PREFIX = "YamlConfigSource[source="; - private static final int ORDINAL = ConfigSource.DEFAULT_ORDINAL + 10; + public static final String NAME = "YamlConfigSource[source=%s]"; + public static final int ORDINAL = DEFAULT_ORDINAL + 10; public YamlConfigSource(String name, Map source, int ordinal) { super(name, source, ordinal, false); @@ -45,7 +44,7 @@ public YamlConfigSource(URL url) throws IOException { } public YamlConfigSource(URL url, int ordinal) throws IOException { - this(NAME_PREFIX + url.toString() + "]", + this(String.format(NAME, url.toString()), ClassPathUtils.readStream(url, (Function>) inputStream -> { try { return streamToMap(inputStream); diff --git a/sources/zookeeper/src/main/java/io/smallrye/config/source/zookeeper/ZooKeeperConfigSource.java b/sources/zookeeper/src/main/java/io/smallrye/config/source/zookeeper/ZooKeeperConfigSource.java index 70668a3ad..9500a6d77 100644 --- a/sources/zookeeper/src/main/java/io/smallrye/config/source/zookeeper/ZooKeeperConfigSource.java +++ b/sources/zookeeper/src/main/java/io/smallrye/config/source/zookeeper/ZooKeeperConfigSource.java @@ -20,21 +20,21 @@ public class ZooKeeperConfigSource extends AbstractConfigSource { private static final long serialVersionUID = 3127679154588598693L; + public static final String NAME = "ZooKeeperConfigSource"; + public static final int ORDINAL = 150; + //Property the URL of the Zookeeper instance will be read from static final String ZOOKEEPER_URL_KEY = "io.smallrye.configsource.zookeeper.url"; //Property of the Application Id. This will be the root znode for an application's properties static final String APPLICATION_ID_KEY = "io.smallrye.configsource.zookeeper.applicationId"; - //Name of this ConfigSource - private static final String ZOOKEEPER_CONFIG_SOURCE_NAME = "io.smallrye.configsource.zookeeper"; - //Apache Curator framework used to access Zookeeper private final CuratorFramework curator; //Root node of an application's configuration private final String applicationId; public ZooKeeperConfigSource(final String zookeeperUrl, final String applicationId) { - super(ZOOKEEPER_CONFIG_SOURCE_NAME, 150); + super(NAME, ORDINAL); //Only create the ZK Client if the properties exist. if (zookeeperUrl != null && applicationId != null) {