Search in sources :

Example 1 with Command

use of org.openstreetmap.atlas.utilities.runtime.Command 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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) StandardConfiguration(org.openstreetmap.atlas.utilities.configuration.StandardConfiguration) LoggerFactory(org.slf4j.LoggerFactory) HttpStatus(org.apache.http.HttpStatus) ArrayList(java.util.ArrayList) Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) URI(java.net.URI) Command(org.openstreetmap.atlas.utilities.runtime.Command) File(org.openstreetmap.atlas.streaming.resource.File) MergedConfiguration(org.openstreetmap.atlas.utilities.configuration.MergedConfiguration) Logger(org.slf4j.Logger) Resource(org.openstreetmap.atlas.streaming.resource.Resource) CoreException(org.openstreetmap.atlas.exception.CoreException) CommandMap(org.openstreetmap.atlas.utilities.runtime.CommandMap) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) GetResource(org.openstreetmap.atlas.streaming.resource.http.GetResource) List(java.util.List) Iterables(org.openstreetmap.atlas.utilities.collections.Iterables) InputStreamResource(org.openstreetmap.atlas.streaming.resource.InputStreamResource) Optional(java.util.Optional) StringResource(org.openstreetmap.atlas.streaming.resource.StringResource) StringList(org.openstreetmap.atlas.utilities.collections.StringList) Assert(org.junit.Assert) InputStream(java.io.InputStream) MergedConfiguration(org.openstreetmap.atlas.utilities.configuration.MergedConfiguration) StandardConfiguration(org.openstreetmap.atlas.utilities.configuration.StandardConfiguration) Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration) MergedConfiguration(org.openstreetmap.atlas.utilities.configuration.MergedConfiguration) CoreException(org.openstreetmap.atlas.exception.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(org.openstreetmap.atlas.streaming.resource.Resource) GetResource(org.openstreetmap.atlas.streaming.resource.http.GetResource) InputStreamResource(org.openstreetmap.atlas.streaming.resource.InputStreamResource) StringResource(org.openstreetmap.atlas.streaming.resource.StringResource)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 HttpStatus (org.apache.http.HttpStatus)1 Assert (org.junit.Assert)1 CoreException (org.openstreetmap.atlas.exception.CoreException)1 File (org.openstreetmap.atlas.streaming.resource.File)1 InputStreamResource (org.openstreetmap.atlas.streaming.resource.InputStreamResource)1 Resource (org.openstreetmap.atlas.streaming.resource.Resource)1 StringResource (org.openstreetmap.atlas.streaming.resource.StringResource)1 GetResource (org.openstreetmap.atlas.streaming.resource.http.GetResource)1 Iterables (org.openstreetmap.atlas.utilities.collections.Iterables)1 StringList (org.openstreetmap.atlas.utilities.collections.StringList)1