Class BaseConfigurationFactory<T>

  • Type Parameters:
    T - the type of the configuration objects to produce
    All Implemented Interfaces:
    ConfigurationFactory<T>
    Direct Known Subclasses:
    JsonConfigurationFactory, YamlConfigurationFactory

    public abstract class BaseConfigurationFactory<T>
    extends Object
    implements ConfigurationFactory<T>
    A generic factory class for loading configuration files, binding them to configuration objects, and validating their constraints. Allows for overriding configuration parameters from system properties.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected com.fasterxml.jackson.databind.ObjectMapper mapper
      The object mapper to use for mapping configuration files to objects.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected BaseConfigurationFactory​(com.fasterxml.jackson.core.JsonFactory parserFactory, String formatName, Class<T> klass, @Nullable javax.validation.Validator validator, com.fasterxml.jackson.databind.ObjectMapper objectMapper, String propertyPrefix)
      Creates a new configuration factory for the given class.
    • Field Detail

      • mapper

        protected final com.fasterxml.jackson.databind.ObjectMapper mapper
        The object mapper to use for mapping configuration files to objects.
    • Constructor Detail

      • BaseConfigurationFactory

        protected BaseConfigurationFactory​(com.fasterxml.jackson.core.JsonFactory parserFactory,
                                           String formatName,
                                           Class<T> klass,
                                           @Nullable javax.validation.Validator validator,
                                           com.fasterxml.jackson.databind.ObjectMapper objectMapper,
                                           String propertyPrefix)
        Creates a new configuration factory for the given class.
        Parameters:
        parserFactory - the factory that creates the parser used
        formatName - the name of the format parsed by this factory (used in exceptions)
        klass - the configuration class
        validator - the validator to use
        objectMapper - the object mapper to use
        propertyPrefix - the system property name prefix used by overrides
    • Method Detail

      • createParser

        protected com.fasterxml.jackson.core.JsonParser createParser​(InputStream input)
                                                              throws IOException
        Constructs a JsonParser to parse the contents of the provided InputStream.
        Parameters:
        input - the input to parse
        Returns:
        the JSON parser for the given input
        Throws:
        IOException - if the parser creation fails due to an I/O error
      • build

        protected T build​(com.fasterxml.jackson.databind.JsonNode node,
                          String path)
                   throws IOException,
                          ConfigurationException
        Loads, parses, binds, and validates a configuration object for a given JsonNode.
        Parameters:
        node - the json node to parse the configuration from
        path - the path of the configuration file
        Returns:
        a validated configuration object
        Throws:
        IOException - if there is an error reading the file
        ConfigurationException - if there is an error parsing or validating the file
      • addOverride

        protected void addOverride​(com.fasterxml.jackson.databind.JsonNode root,
                                   String name,
                                   String value)
        Applies an override to a given JsonNode.
        Parameters:
        root - the node to apply the override to
        name - the key of the override
        value - the new value