diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index 685c2602..69b59f14 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -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() { + @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 Config. - * + * *

* This works like {@link java.lang.ClassLoader#getResource}, not like * {@link java.lang.Class#getResource}, so the name never begins with a * slash. - * + * *

* 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 diff --git a/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala b/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala index 205c5a07..81008f94 100644 --- a/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala @@ -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 + } + } }