mirror of
https://github.com/lightbend/config.git
synced 2025-01-29 05:30:08 +08:00
Add ConfigFactory#load(ConfigParseOptions)
This commit is contained in:
parent
796493cfcd
commit
9cb359e1bd
@ -166,6 +166,10 @@ 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) {
|
||||||
int specified = 0;
|
int specified = 0;
|
||||||
|
|
||||||
// override application.conf with config.file, config.resource,
|
// override application.conf with config.file, config.resource,
|
||||||
@ -181,7 +185,7 @@ public final class ConfigFactory {
|
|||||||
specified += 1;
|
specified += 1;
|
||||||
|
|
||||||
if (specified == 0) {
|
if (specified == 0) {
|
||||||
return load(loader, "application");
|
return load(loader, "application", parseOptions, ConfigResolveOptions.defaults());
|
||||||
} 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 +196,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));
|
||||||
} else if (file != null) {
|
} else if (file != null) {
|
||||||
return load(
|
return load(
|
||||||
loader,
|
loader,
|
||||||
parseFile(new File(file),
|
parseFile(new File(file), parseOptions));
|
||||||
ConfigParseOptions.defaults().setClassLoader(loader)));
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
return load(
|
return load(
|
||||||
loader,
|
loader,
|
||||||
parseURL(new URL(url),
|
parseURL(new URL(url), parseOptions));
|
||||||
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 +245,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 +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
|
* 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
|
||||||
@ -575,16 +607,16 @@ public final class ConfigFactory {
|
|||||||
/**
|
/**
|
||||||
* Parses all resources on the classpath with the given name and merges them
|
* Parses all resources on the classpath with the given name and merges them
|
||||||
* into a single <code>Config</code>.
|
* into a single <code>Config</code>.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* This works like {@link java.lang.ClassLoader#getResource}, not like
|
* This works like {@link java.lang.ClassLoader#getResource}, not like
|
||||||
* {@link java.lang.Class#getResource}, so the name never begins with a
|
* {@link java.lang.Class#getResource}, so the name never begins with a
|
||||||
* slash.
|
* slash.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* See {@link #parseResources(Class,String,ConfigParseOptions)} for full
|
* See {@link #parseResources(Class,String,ConfigParseOptions)} for full
|
||||||
* details.
|
* details.
|
||||||
*
|
*
|
||||||
* @param loader
|
* @param loader
|
||||||
* will be used to load resources by setting this loader on the
|
* will be used to load resources by setting this loader on the
|
||||||
* provided options
|
* provided options
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user