use of org.openstreetmap.atlas.utilities.configuration.MergedConfiguration in project atlas-checks by osmlab.
the class ConfigurationResolver method loadConfiguration.
/**
* Resolves a {@link Configuration} for {@link Command}s given the {@link CommandMap} and
* {@link Command.Switch}s available
*
* @param commandMap
* the {@link Command}'s {@link CommandMap}
* @param keyFiles
* the {@link Command.Switch} containing a list of URIs for configuration
* @param keyJson
* the {@link Command.Switch} containing inline JSON configuration
* @return a new {@link Configuration}
*/
public static Configuration loadConfiguration(final CommandMap commandMap, final Command.Switch<StringList> keyFiles, final Command.Switch<String> keyJson) {
final List<InputStream> configurationSources = new ArrayList<>();
ConfigurationResolver.getResourceAsStream("application.json").ifPresent(configurationSources::add);
commandMap.getOption(keyFiles).ifPresent(files -> ((StringList) files).forEach(uri -> ConfigurationResolver.getResourceAsStream(URI.create(uri)).ifPresent(configurationSources::add)));
commandMap.getOption(keyJson).map(value -> new ByteArrayInputStream(value.toString().getBytes(StandardCharsets.UTF_8))).ifPresent(configurationSources::add);
final List<Resource> configurationResources = configurationSources.stream().map(InputStreamResource::new).collect(Collectors.toList());
final Configuration configuration;
Throwable thrown = null;
try {
configuration = new MergedConfiguration(Iterables.head(configurationResources), Iterables.tail(configurationResources));
} finally {
for (final InputStream source : configurationSources) {
try {
source.close();
} catch (final Throwable throwable) {
thrown = throwable;
}
}
}
if (thrown != null) {
throw new CoreException("Failed to load configuration", thrown);
}
return configuration;
}
Aggregations