mirror of
https://github.com/lightbend/config.git
synced 2025-03-28 04:50:26 +08:00
Split out a helper method for parsing only application overrides
This commit is contained in:
parent
001e6c3b82
commit
09e2d57e18
@ -8,7 +8,7 @@ import com.typesafe.config.impl.Parseable;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.net.URL;
|
import java.net.*;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
@ -1090,6 +1090,63 @@ public final class ConfigFactory {
|
|||||||
return parseResourcesAnySyntax(resourceBasename, ConfigParseOptions.defaults());
|
return parseResourcesAnySyntax(resourceBasename, ConfigParseOptions.defaults());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse only any application overrides (those specified by config.{resource,file,url}), returning
|
||||||
|
* an empty Config if no overrides were set.
|
||||||
|
* @param parseOptions parse options
|
||||||
|
* @return the parsed configuration
|
||||||
|
*/
|
||||||
|
public static Config parseApplicationOverride(ConfigParseOptions parseOptions) {
|
||||||
|
ClassLoader loader = parseOptions.getClassLoader();
|
||||||
|
|
||||||
|
if (loader == null)
|
||||||
|
throw new ConfigException.BugOrBroken(
|
||||||
|
"ClassLoader should have been set here; bug in ConfigFactory. "
|
||||||
|
+ "(You can probably work around this bug by passing in a class loader or calling currentThread().setContextClassLoader() though.)");
|
||||||
|
|
||||||
|
int specified = 0;
|
||||||
|
|
||||||
|
// override application.conf with config.file, config.resource,
|
||||||
|
// config.url if requested.
|
||||||
|
String resource = System.getProperty("config.resource");
|
||||||
|
if (resource != null)
|
||||||
|
specified += 1;
|
||||||
|
String file = System.getProperty("config.file");
|
||||||
|
if (file != null)
|
||||||
|
specified += 1;
|
||||||
|
String url = System.getProperty("config.url");
|
||||||
|
if (url != null)
|
||||||
|
specified += 1;
|
||||||
|
|
||||||
|
if (specified == 0) {
|
||||||
|
return ConfigImpl.emptyConfig("TODO: what to put here? Should something else be returned?");
|
||||||
|
} else if (specified > 1) {
|
||||||
|
throw new ConfigException.Generic("You set more than one of config.file='" + file
|
||||||
|
+ "', config.url='" + url + "', config.resource='" + resource
|
||||||
|
+ "'; don't know which one to use!");
|
||||||
|
} else {
|
||||||
|
// the override file/url/resource MUST be present or it's an error
|
||||||
|
ConfigParseOptions overrideOptions = parseOptions.setAllowMissing(false);
|
||||||
|
if (resource != null) {
|
||||||
|
if (resource.startsWith("/"))
|
||||||
|
resource = resource.substring(1);
|
||||||
|
// this deliberately does not parseResourcesAnySyntax; if
|
||||||
|
// people want that they can use an include statement.
|
||||||
|
return ConfigFactory.parseResources(loader, resource, overrideOptions);
|
||||||
|
} else if (file != null) {
|
||||||
|
return ConfigFactory.parseFile(new File(file), overrideOptions);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
return ConfigFactory.parseURL(new URL(url), overrideOptions);
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
throw new ConfigException.Generic("Bad URL in config.url system property: '"
|
||||||
|
+ url + "': " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a string (which should be valid HOCON or JSON by default, or
|
* Parses a string (which should be valid HOCON or JSON by default, or
|
||||||
* the syntax specified in the options otherwise).
|
* the syntax specified in the options otherwise).
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.typesafe.config;
|
package com.typesafe.config;
|
||||||
|
|
||||||
|
import com.typesafe.config.impl.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -12,51 +14,11 @@ import java.net.URL;
|
|||||||
public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy {
|
public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy {
|
||||||
@Override
|
@Override
|
||||||
public Config parseApplicationConfig(ConfigParseOptions parseOptions) {
|
public Config parseApplicationConfig(ConfigParseOptions parseOptions) {
|
||||||
ClassLoader loader = parseOptions.getClassLoader();
|
Config overrideConfig = ConfigFactory.parseApplicationOverride(parseOptions);
|
||||||
if (loader == null)
|
if (overrideConfig.isEmpty()) {
|
||||||
throw new ConfigException.BugOrBroken(
|
|
||||||
"ClassLoader should have been set here; bug in ConfigFactory. "
|
|
||||||
+ "(You can probably work around this bug by passing in a class loader or calling currentThread().setContextClassLoader() though.)");
|
|
||||||
|
|
||||||
int specified = 0;
|
|
||||||
|
|
||||||
// override application.conf with config.file, config.resource,
|
|
||||||
// config.url if requested.
|
|
||||||
String resource = System.getProperty("config.resource");
|
|
||||||
if (resource != null)
|
|
||||||
specified += 1;
|
|
||||||
String file = System.getProperty("config.file");
|
|
||||||
if (file != null)
|
|
||||||
specified += 1;
|
|
||||||
String url = System.getProperty("config.url");
|
|
||||||
if (url != null)
|
|
||||||
specified += 1;
|
|
||||||
|
|
||||||
if (specified == 0) {
|
|
||||||
return ConfigFactory.parseResourcesAnySyntax("application", parseOptions);
|
return ConfigFactory.parseResourcesAnySyntax("application", parseOptions);
|
||||||
} else if (specified > 1) {
|
|
||||||
throw new ConfigException.Generic("You set more than one of config.file='" + file
|
|
||||||
+ "', config.url='" + url + "', config.resource='" + resource
|
|
||||||
+ "'; don't know which one to use!");
|
|
||||||
} else {
|
} else {
|
||||||
// the override file/url/resource MUST be present or it's an error
|
return overrideConfig;
|
||||||
ConfigParseOptions overrideOptions = parseOptions.setAllowMissing(false);
|
|
||||||
if (resource != null) {
|
|
||||||
if (resource.startsWith("/"))
|
|
||||||
resource = resource.substring(1);
|
|
||||||
// this deliberately does not parseResourcesAnySyntax; if
|
|
||||||
// people want that they can use an include statement.
|
|
||||||
return ConfigFactory.parseResources(loader, resource, overrideOptions);
|
|
||||||
} else if (file != null) {
|
|
||||||
return ConfigFactory.parseFile(new File(file), overrideOptions);
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
return ConfigFactory.parseURL(new URL(url), overrideOptions);
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
throw new ConfigException.Generic("Bad URL in config.url system property: '"
|
|
||||||
+ url + "': " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
config/src/main/main.iml
Normal file
11
config/src/main/main.iml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
13
config/src/test/test.iml
Normal file
13
config/src/test/test.iml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/scala" isTestSource="true" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module" module-name="main" />
|
||||||
|
</component>
|
||||||
|
</module>
|
Loading…
Reference in New Issue
Block a user