Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DH-18588: Allow shortened Name.rootFIle config file specifier #6621

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,19 @@ private static class Named extends Configuration {

@Override
protected String determinePropertyFile() {
final String propFile = System.getProperty("Configuration." + name + ".rootFile");
if (propFile == null) {
throw new ConfigurationException("No property file defined for named configuration " + name);
String propFile = System.getProperty("Configuration." + name + ".rootFile");
if (propFile != null) {
return propFile;
}

return propFile;
// If Configuration.name.rootFile doesn't exist allow a fallback onto `name.rootFile`
propFile = System.getProperty(name + ".rootFile");
if (propFile != null) {
return propFile;
}

throw new ConfigurationException("No property file defined for named configuration " + name +
": Make sure the property `Configuration." + name + ".rootFile` or `" + name + ".rootFile` is set");
}
}

Expand All @@ -98,6 +105,7 @@ public static Configuration getInstance() {
* @throws ConfigurationException if the named configuration could not be loaded.
*/
public static Configuration getNamed(@NotNull final String name) throws ConfigurationException {
validateConfigName(name);
return NAMED_CONFIGURATIONS.computeIfAbsent(name, (k) -> {
try {
return new Named(name, DefaultConfigurationContext::new);
Expand Down Expand Up @@ -151,6 +159,7 @@ public static Configuration newStandaloneConfiguration(
* @return a new Configuration instance, guaranteed to not be cached.
*/
public static Configuration newStandaloneConfiguration(@NotNull final String name) {
validateConfigName(name);
return newStandaloneConfiguration(name, DefaultConfigurationContext::new);
}

Expand Down Expand Up @@ -433,4 +442,19 @@ private static void writeLine(StringBuilder out, String sKey, String sLeftValue,
static boolean isQuiet() {
return Bootstrap.isQuiet() || System.getProperty(QUIET_PROPERTY) != null;
}

/**
* Check that the passed in config name is not null and is allowed.
*
* @param name the Configuration name.
*/
static void validateConfigName(final String name) {
if (name == null) {
throw new ConfigurationException("Configuration name must not be null");
}

if ("Configuration".equals(name)) {
throw new ConfigurationException("The name `Configuration` may not be used as a Configuration name");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,46 @@ public void testNamedConfiguration() {
assertNull(c2.getStringWithDefault("test2", null));
assertEquals("true", c3.getProperty("test2"));
}

public void testNamedConfigurationSimple() {
System.setProperty(FILENAME_PROPERTY, "resources/lib-tests.prop");
System.clearProperty("Configuration.Test1.rootFile");
System.setProperty("Test1.rootFile", "resources/test1.prop");

Configuration.reset();
Configuration dc = Configuration.getInstance();
Configuration c1 = Configuration.getNamed("Test1");

assertEquals("cache", dc.getProperty("cacheDir"));
assertNull(c1.getStringWithDefault("cacheDir", null));

assertNull(dc.getStringWithDefault("test1", null));
assertEquals("true", c1.getProperty("test1"));

assertNull(dc.getStringWithDefault("test3", null));
// test1 imports test3, so expected.
assertEquals("true", c1.getProperty("test3"));

// Make sure we can't try to load a Configuration with a null name, or `Configuration`
try {
final Configuration c = Configuration.getNamed(null);
fail("Should have rejected a null name");
} catch (ConfigurationException ignored) {

}

try {
final Configuration c = Configuration.getNamed("Configuration");
fail("Should have rejected the name `Configuration`");
} catch (ConfigurationException ignored) {

}

try {
final Configuration c = Configuration.getNamed("NonExistent");
fail("Should have rejected a named config that doesnt exist");
} catch (ConfigurationException ignored) {

}
}
}
Loading