Add ConfigFactory#load(ConfigParseOptions)

This commit is contained in:
jtournay 2012-09-05 03:02:16 -07:00
parent 796493cfcd
commit 9cb359e1bd
2 changed files with 59 additions and 9 deletions

View File

@ -166,6 +166,10 @@ public final class ConfigFactory {
}
private static Config loadDefaultConfig(ClassLoader loader) {
return loadDefaultConfig(loader, ConfigParseOptions.defaults().setClassLoader(loader));
}
private static Config loadDefaultConfig(ClassLoader loader, ConfigParseOptions parseOptions) {
int specified = 0;
// override application.conf with config.file, config.resource,
@ -181,7 +185,7 @@ public final class ConfigFactory {
specified += 1;
if (specified == 0) {
return load(loader, "application");
return load(loader, "application", parseOptions, ConfigResolveOptions.defaults());
} else if (specified > 1) {
throw new ConfigException.Generic("You set more than one of config.file='" + file
+ "', config.url='" + url + "', config.resource='" + resource
@ -192,18 +196,16 @@ public final class ConfigFactory {
resource = resource.substring(1);
// this deliberately does not parseResourcesAnySyntax; if
// people want that they can use an include statement.
return load(loader, parseResources(loader, resource));
return load(loader, parseResources(loader, resource, parseOptions));
} else if (file != null) {
return load(
loader,
parseFile(new File(file),
ConfigParseOptions.defaults().setClassLoader(loader)));
parseFile(new File(file), parseOptions));
} else {
try {
return load(
loader,
parseURL(new URL(url),
ConfigParseOptions.defaults().setClassLoader(loader)));
parseURL(new URL(url), parseOptions));
} catch (MalformedURLException e) {
throw new ConfigException.Generic("Bad URL in config.url system property: '"
+ url + "': " + e.getMessage(), e);
@ -243,6 +245,17 @@ public final class ConfigFactory {
return load(Thread.currentThread().getContextClassLoader());
}
/**
* Like {@link #load()} but allows specifying parse options
*
* @param parseOptions
* Options for parsing resources
* @return configuration for an application
*/
public static Config load(ConfigParseOptions parseOptions) {
return load(Thread.currentThread().getContextClassLoader(), parseOptions);
}
/**
* Like {@link #load()} but allows specifying a class loader other than the
* thread's current context class loader.
@ -260,6 +273,25 @@ public final class ConfigFactory {
});
}
/**
* Like {@link #load()} but allows specifying a class loader other than the
* thread's current context class loader, and parse options
*
* @param loader
* class loader for finding resources
* @param parseOptions
* Options for parsing resources
* @return configuration for an application
*/
public static Config load(final ClassLoader loader, final ConfigParseOptions parseOptions) {
return ConfigImpl.computeCachedConfig(loader, "load", new Callable<Config>() {
@Override
public Config call() {
return loadDefaultConfig(loader, parseOptions);
}
});
}
/**
* Obtains the default reference configuration, which is currently created
* by merging all resources "reference.conf" found on the classpath and
@ -575,16 +607,16 @@ public final class ConfigFactory {
/**
* Parses all resources on the classpath with the given name and merges them
* into a single <code>Config</code>.
*
*
* <p>
* This works like {@link java.lang.ClassLoader#getResource}, not like
* {@link java.lang.Class#getResource}, so the name never begins with a
* slash.
*
*
* <p>
* See {@link #parseResources(Class,String,ConfigParseOptions)} for full
* details.
*
*
* @param loader
* will be used to load resources by setting this loader on the
* provided options

View File

@ -826,4 +826,22 @@ class PublicApiTest extends TestUtils {
assertTrue("wrong exception: " + e.getMessage, e.getMessage.contains("include statements nested"))
}
@Test
def missingConfFails() {
val old = System.getProperty("config.resource")
System.setProperty("config.resource", "donotexists.conf")
intercept[ConfigException.IO] {
ConfigFactory.load(ConfigParseOptions.defaults().setAllowMissing(false))
}
// cleanup properties
Option(old).map{ v =>
System.setProperty("config.resource", v)
v
}.orElse{
System.clearProperty("config.resource")
None
}
}
}