Skip to content

Commit 2d43727

Browse files
authored
Merge pull request lightbend#708 from bbaldino/parse_app_overrides
Split out a helper method for parsing only application overrides
2 parents 001e6c3 + 5029d93 commit 2d43727

File tree

2 files changed

+87
-50
lines changed

2 files changed

+87
-50
lines changed

config/src/main/java/com/typesafe/config/ConfigFactory.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
import java.io.File;
1010
import java.io.Reader;
11+
import java.net.MalformedURLException;
1112
import java.net.URL;
1213
import java.util.Map;
14+
import java.util.Optional;
1315
import java.util.Properties;
1416
import java.util.concurrent.Callable;
1517

@@ -1090,6 +1092,89 @@ public static Config parseResourcesAnySyntax(String resourceBasename) {
10901092
return parseResourcesAnySyntax(resourceBasename, ConfigParseOptions.defaults());
10911093
}
10921094

1095+
/**
1096+
* Parse only any application replacement (specified by one of config.{resource,file,url}), returning
1097+
* an empty Config if no overrides were set.
1098+
*
1099+
* @since 1.4.1
1100+
*
1101+
* @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()}
1102+
* if none was specified.
1103+
*/
1104+
public static java.util.Optional<Config> parseApplicationReplacement() {
1105+
return parseApplicationReplacement(ConfigParseOptions.defaults());
1106+
}
1107+
1108+
/**
1109+
* Like {@link #parseApplicationReplacement()} but allows you to specify a class loader
1110+
* ti yse rather than the current context class loader.
1111+
*
1112+
* @since 1.4.1
1113+
*
1114+
* @param loader the class loader
1115+
* @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()}
1116+
* if none was specified.
1117+
*/
1118+
public static java.util.Optional<Config> parseApplicationReplacement(ClassLoader loader) {
1119+
return parseApplicationReplacement(ConfigParseOptions.defaults().setClassLoader(loader));
1120+
}
1121+
1122+
/**
1123+
* Like {@link #parseApplicationReplacement()} but allows you to specify parse options.
1124+
*
1125+
* @since 1.4.1
1126+
*
1127+
* @param parseOptions parse options
1128+
* @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()}
1129+
* if none was specified.
1130+
*/
1131+
public static java.util.Optional<Config> parseApplicationReplacement(ConfigParseOptions parseOptions) {
1132+
ensureClassLoader(parseOptions, "parseApplicationReplacement");
1133+
ClassLoader loader = parseOptions.getClassLoader();
1134+
1135+
int specified = 0;
1136+
1137+
// override application.conf with config.file, config.resource,
1138+
// config.url if requested.
1139+
String resource = System.getProperty("config.resource");
1140+
if (resource != null)
1141+
specified += 1;
1142+
String file = System.getProperty("config.file");
1143+
if (file != null)
1144+
specified += 1;
1145+
String url = System.getProperty("config.url");
1146+
if (url != null)
1147+
specified += 1;
1148+
1149+
if (specified == 0) {
1150+
return java.util.Optional.empty();
1151+
} else if (specified > 1) {
1152+
throw new ConfigException.Generic("You set more than one of config.file='" + file
1153+
+ "', config.url='" + url + "', config.resource='" + resource
1154+
+ "'; don't know which one to use!");
1155+
} else {
1156+
// the override file/url/resource MUST be present or it's an error
1157+
ConfigParseOptions overrideOptions = parseOptions.setAllowMissing(false);
1158+
if (resource != null) {
1159+
if (resource.startsWith("/"))
1160+
resource = resource.substring(1);
1161+
// this deliberately does not parseResourcesAnySyntax; if
1162+
// people want that they can use an include statement.
1163+
return java.util.Optional.of(ConfigFactory.parseResources(loader, resource, overrideOptions));
1164+
} else if (file != null) {
1165+
return java.util.Optional.of(ConfigFactory.parseFile(new File(file), overrideOptions));
1166+
} else {
1167+
try {
1168+
return java.util.Optional.of(ConfigFactory.parseURL(new URL(url), overrideOptions));
1169+
} catch (MalformedURLException e) {
1170+
throw new ConfigException.Generic("Bad URL in config.url system property: '"
1171+
+ url + "': " + e.getMessage(), e);
1172+
}
1173+
}
1174+
}
1175+
1176+
}
1177+
10931178
/**
10941179
* Parses a string (which should be valid HOCON or JSON by default, or
10951180
* the syntax specified in the options otherwise).
Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.typesafe.config;
22

3-
import java.io.File;
4-
import java.net.MalformedURLException;
5-
import java.net.URL;
6-
73
/**
84
* Default config loading strategy. Able to load resource, file or URL.
95
* Behavior may be altered by defining one of VM properties
@@ -12,51 +8,7 @@
128
public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy {
139
@Override
1410
public Config parseApplicationConfig(ConfigParseOptions parseOptions) {
15-
ClassLoader loader = parseOptions.getClassLoader();
16-
if (loader == null)
17-
throw new ConfigException.BugOrBroken(
18-
"ClassLoader should have been set here; bug in ConfigFactory. "
19-
+ "(You can probably work around this bug by passing in a class loader or calling currentThread().setContextClassLoader() though.)");
20-
21-
int specified = 0;
22-
23-
// override application.conf with config.file, config.resource,
24-
// config.url if requested.
25-
String resource = System.getProperty("config.resource");
26-
if (resource != null)
27-
specified += 1;
28-
String file = System.getProperty("config.file");
29-
if (file != null)
30-
specified += 1;
31-
String url = System.getProperty("config.url");
32-
if (url != null)
33-
specified += 1;
34-
35-
if (specified == 0) {
36-
return ConfigFactory.parseResourcesAnySyntax("application", parseOptions);
37-
} else if (specified > 1) {
38-
throw new ConfigException.Generic("You set more than one of config.file='" + file
39-
+ "', config.url='" + url + "', config.resource='" + resource
40-
+ "'; don't know which one to use!");
41-
} else {
42-
// the override file/url/resource MUST be present or it's an error
43-
ConfigParseOptions overrideOptions = parseOptions.setAllowMissing(false);
44-
if (resource != null) {
45-
if (resource.startsWith("/"))
46-
resource = resource.substring(1);
47-
// this deliberately does not parseResourcesAnySyntax; if
48-
// people want that they can use an include statement.
49-
return ConfigFactory.parseResources(loader, resource, overrideOptions);
50-
} else if (file != null) {
51-
return ConfigFactory.parseFile(new File(file), overrideOptions);
52-
} else {
53-
try {
54-
return ConfigFactory.parseURL(new URL(url), overrideOptions);
55-
} catch (MalformedURLException e) {
56-
throw new ConfigException.Generic("Bad URL in config.url system property: '"
57-
+ url + "': " + e.getMessage(), e);
58-
}
59-
}
60-
}
11+
return ConfigFactory.parseApplicationReplacement(parseOptions)
12+
.orElseGet(() -> ConfigFactory.parseResourcesAnySyntax("application", parseOptions));
6113
}
6214
}

0 commit comments

Comments
 (0)