Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.config;
 
public class ConfigurationFactory<T> {
    private static final String PROPERTY_PREFIX = "dw.";
    public static <T> ConfigurationFactory<T> forClass(Class<T> klass,
                                                       Validator validator,
                                                       ObjectMapperFactory objectMapperFactory) {
        return new ConfigurationFactory<T>(klassvalidatorobjectMapperFactory);
    }
    public static <T> ConfigurationFactory<T> forClass(Class<T> klassValidator validator) {
        return new ConfigurationFactory<T>(klassvalidatornew ObjectMapperFactory());
    }
    private final Class<T> klass;
    private final ObjectMapper mapper;
    private final Validator validator;
    private ConfigurationFactory(Class<T> klassValidator validatorObjectMapperFactory objectMapperFactory) {
        this. = klass;
        objectMapperFactory.enable(.);
        this. = objectMapperFactory.build(new YAMLFactory());
        this. = validator;
    }
    
    public T build(File filethrows IOExceptionConfigurationException {
        final JsonNode node = .readTree(file);
        final String filename = file.toString();
        return build(nodefilename);
    }
    public T build() throws IOExceptionConfigurationException {
        return build(..objectNode(), "The default configuration");
    }
    private T build(JsonNode nodeString filenamethrows IOExceptionConfigurationException {
        for (Map.Entry<ObjectObjectpref : System.getProperties().entrySet()) {
            final String prefName = (Stringpref.getKey();
            if (prefName.startsWith()) {
                final String configName = prefName.substring(.length());
                addOverride(nodeconfigName, System.getProperty(prefName));
            }
        }
        final T config = .readValue(new TreeTraversingParser(node), );
        validate(filenameconfig);
        return config;
    }
    private void addOverride(JsonNode rootString nameString value) {
        JsonNode node = root;
        final Iterator<Stringkeys = Splitter.on('.').trimResults().split(name).iterator();
        while (keys.hasNext()) {
            final String key = keys.next();
            if (!(node instanceof ObjectNode)) {
                throw new IllegalArgumentException("Unable to override " + name + "; it's not a valid path.");
            }
            final ObjectNode obj = (ObjectNodenode;
            if (keys.hasNext()) {
                JsonNode child = obj.get(key);
                if (child == null) {
                    child = obj.objectNode();
                    obj.put(keychild);
                }
                node = child;
            } else {
                obj.put(keyvalue);
            }
        }
    }
    private void validate(String file, T configthrows ConfigurationException {
        final ImmutableList<Stringerrors = .validate(config);
        if (!errors.isEmpty()) {
            throw new ConfigurationException(fileerrors);
        }
    }
New to GrepCode? Check out our FAQ X