|
8 | 8 |
|
9 | 9 | import java.io.File; |
10 | 10 | import java.io.Reader; |
| 11 | +import java.net.MalformedURLException; |
11 | 12 | import java.net.URL; |
12 | 13 | import java.util.Map; |
| 14 | +import java.util.Optional; |
13 | 15 | import java.util.Properties; |
14 | 16 | import java.util.concurrent.Callable; |
15 | 17 |
|
@@ -1090,6 +1092,89 @@ public static Config parseResourcesAnySyntax(String resourceBasename) { |
1090 | 1092 | return parseResourcesAnySyntax(resourceBasename, ConfigParseOptions.defaults()); |
1091 | 1093 | } |
1092 | 1094 |
|
| 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 | + |
1093 | 1178 | /** |
1094 | 1179 | * Parses a string (which should be valid HOCON or JSON by default, or |
1095 | 1180 | * the syntax specified in the options otherwise). |
|
0 commit comments