org.openstreetmap.atlas.utilities.configuration.StandardConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atlas Show documentation
Show all versions of atlas Show documentation
"Library to load OSM data into an Atlas format"
package org.openstreetmap.atlas.utilities.configuration;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.openstreetmap.atlas.exception.CoreException;
import org.openstreetmap.atlas.streaming.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
/**
* Standard implementation of the Configuration interface supporting dot-notation key-value lookup.
*
* @author cstaylor
* @author brian_l_davis
* @author jklamer
*/
public class StandardConfiguration implements Configuration
{
/**
* Enum for the supported configuration file formats
*/
public enum ConfigurationFormat
{
JSON,
YAML,
UNKNOWN
}
/**
* Configurable implementation that pulls from the outer class's data table
*
* @param
* configured type
* @param
* transformed type
* @author cstaylor
* @author brian_l_davis
* @author cameron_frenette
*/
private final class StandardConfigurable implements Configurable
{
private final T defaultValue;
private final String key;
private final Function transform;
private StandardConfigurable(final String key, final R defaultValue,
final Function transform)
{
this.key = key;
this.transform = transform;
this.defaultValue = Optional.ofNullable(defaultValue).map(transform).orElse(null);
}
@SuppressWarnings("unchecked")
@Override
public V value()
{
try
{
final R found = (R) resolve(this.key, StandardConfiguration.this.configurationData);
return (V) Optional.ofNullable(found).map(this.transform).orElse(this.defaultValue);
}
catch (final ClassCastException e)
{
logger.error(String.format("Invalid configuration type for %s", this.key), e);
}
return null;
}
@Override
public Optional valueOption()
{
return Optional.ofNullable(value());
}
}
// "override" is no longer available to use as a configuration key
private static final String OVERRIDE_STRING = "override";
private static final String DOT = ".";
private static final Logger logger = LoggerFactory.getLogger(StandardConfiguration.class);
private Map configurationData;
private final String name;
public StandardConfiguration(final Resource resource)
{
this(resource, ConfigurationFormat.UNKNOWN);
}
public StandardConfiguration(final Resource resource, final ConfigurationFormat configFormat)
{
this.name = resource.getName();
final byte[] configBytes = resource.readBytesAndClose();
switch (configFormat)
{
case JSON:
this.configurationData = this.readConfigurationMapFromJSON(configBytes)
.orElseThrow(() -> new CoreException("Unable to load JSON configuration."));
return;
case YAML:
this.configurationData = this.readConfigurationMapFromYAML(configBytes)
.orElseThrow(() -> new CoreException("Unable to load YAML configuration."));
return;
case UNKNOWN:
default:
// If the config format is unknown, attempt to load the config with each format
// until one finds some data
final Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy