Merge pull request #37 from jto/new_loader

Add ConfigFactory#load(ConfigParseOptions)
This commit is contained in:
Havoc Pennington 2012-09-06 05:52:43 -07:00
commit 5b71bb9904
2 changed files with 93 additions and 9 deletions

View File

@ -166,6 +166,18 @@ public final class ConfigFactory {
} }
private static Config loadDefaultConfig(ClassLoader loader) { private static Config loadDefaultConfig(ClassLoader loader) {
return loadDefaultConfig(loader, ConfigParseOptions.defaults().setClassLoader(loader));
}
private static Config loadDefaultConfig(ClassLoader loader, ConfigParseOptions parseOptions) {
return loadDefaultConfig(loader, parseOptions, ConfigResolveOptions.defaults());
}
private static Config loadDefaultConfig(ClassLoader loader, ConfigResolveOptions resolveOptions) {
return loadDefaultConfig(loader, ConfigParseOptions.defaults(), resolveOptions);
}
private static Config loadDefaultConfig(ClassLoader loader, ConfigParseOptions parseOptions, ConfigResolveOptions resolveOptions) {
int specified = 0; int specified = 0;
// override application.conf with config.file, config.resource, // override application.conf with config.file, config.resource,
@ -181,7 +193,7 @@ public final class ConfigFactory {
specified += 1; specified += 1;
if (specified == 0) { if (specified == 0) {
return load(loader, "application"); return load(loader, "application", parseOptions, resolveOptions);
} else if (specified > 1) { } else if (specified > 1) {
throw new ConfigException.Generic("You set more than one of config.file='" + file throw new ConfigException.Generic("You set more than one of config.file='" + file
+ "', config.url='" + url + "', config.resource='" + resource + "', config.url='" + url + "', config.resource='" + resource
@ -192,18 +204,16 @@ public final class ConfigFactory {
resource = resource.substring(1); resource = resource.substring(1);
// this deliberately does not parseResourcesAnySyntax; if // this deliberately does not parseResourcesAnySyntax; if
// people want that they can use an include statement. // people want that they can use an include statement.
return load(loader, parseResources(loader, resource)); return load(loader, parseResources(loader, resource, parseOptions), resolveOptions);
} else if (file != null) { } else if (file != null) {
return load( return load(
loader, loader,
parseFile(new File(file), parseFile(new File(file), parseOptions), resolveOptions);
ConfigParseOptions.defaults().setClassLoader(loader)));
} else { } else {
try { try {
return load( return load(
loader, loader,
parseURL(new URL(url), parseURL(new URL(url), parseOptions), resolveOptions);
ConfigParseOptions.defaults().setClassLoader(loader)));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new ConfigException.Generic("Bad URL in config.url system property: '" throw new ConfigException.Generic("Bad URL in config.url system property: '"
+ url + "': " + e.getMessage(), e); + url + "': " + e.getMessage(), e);
@ -243,6 +253,17 @@ public final class ConfigFactory {
return load(Thread.currentThread().getContextClassLoader()); 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 * Like {@link #load()} but allows specifying a class loader other than the
* thread's current context class loader. * thread's current context class loader.
@ -260,6 +281,51 @@ 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(ClassLoader loader, ConfigParseOptions parseOptions) {
return loadDefaultConfig(loader, parseOptions);
}
/**
* Like {@link #load()} but allows specifying a class loader other than the
* thread's current context class loader, and resolve options
*
* @param loader
* class loader for finding resources
* @param resolveOptions
* options for resolving the assembled config stack
* @return configuration for an application
*/
public static Config load(ClassLoader loader, ConfigResolveOptions resolveOptions) {
return loadDefaultConfig(loader, resolveOptions);
}
/**
* Like {@link #load()} but allows specifying a class loader other than the
* thread's current context class loader, parse options, and resolve options
*
* @param loader
* class loader for finding resources
* @param parseOptions
* Options for parsing resources
* @param resolveOptions
* options for resolving the assembled config stack
* @return configuration for an application
*/
public static Config load(ClassLoader loader, ConfigParseOptions parseOptions, ConfigResolveOptions resolveOptions) {
return loadDefaultConfig(loader, parseOptions, resolveOptions);
}
/** /**
* Obtains the default reference configuration, which is currently created * Obtains the default reference configuration, which is currently created
* by merging all resources "reference.conf" found on the classpath and * by merging all resources "reference.conf" found on the classpath and

View File

@ -826,4 +826,22 @@ class PublicApiTest extends TestUtils {
assertTrue("wrong exception: " + e.getMessage, e.getMessage.contains("include statements nested")) 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
}
}
} }