Class AbstractFileConfigSource<T>

  • Type Parameters:
    T - the type of the parsed configuration object (e.g., JsonObject, Map, TomlTable)
    All Implemented Interfaces:
    ConfigSource
    Direct Known Subclasses:
    JsonSource, TomlSource, YamlSource

    public abstract class AbstractFileConfigSource<T>
    extends Object
    implements ConfigSource
    Abstract base class for file-based configuration sources.

    This class provides common functionality for loading configuration from files, including support for both filesystem and classpath resources, and environment-specific configuration sections.

    Subclasses must implement the parsing logic for their specific file format.

    Since:
    1.0.3
    Author:
    Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
    See Also:
    ConfigSource
    • Field Detail

      • root

        protected final T root
        The parsed configuration root object
      • sourceName

        protected final String sourceName
        The source name for display purposes
      • environment

        protected final String environment
        The environment section that was loaded, or null for all top-level keys
    • Constructor Detail

      • AbstractFileConfigSource

        protected AbstractFileConfigSource​(String filePath)
        Creates a new file-based config source from the given file path. Loads all top-level keys (not nested in environment sections).
        Parameters:
        filePath - path to the configuration file (filesystem or classpath)
        Throws:
        IllegalStateException - if the file cannot be loaded
      • AbstractFileConfigSource

        protected AbstractFileConfigSource​(String filePath,
                                           String environment)
        Creates a new file-based config source from the given file path for a specific environment. When an environment is specified, only configuration from that section is loaded.
        Parameters:
        filePath - path to the configuration file (filesystem or classpath)
        environment - the environment section to load, or null for all top-level keys
        Throws:
        IllegalStateException - if the file cannot be loaded or the environment section doesn't exist
      • AbstractFileConfigSource

        protected AbstractFileConfigSource​(Path file)
        Creates a new file-based config source from the given Path. Loads all top-level keys (not nested in environment sections).
        Parameters:
        file - path to the configuration file
        Throws:
        IllegalStateException - if the file cannot be loaded
      • AbstractFileConfigSource

        protected AbstractFileConfigSource​(Path file,
                                           String environment)
        Creates a new file-based config source from the given Path for a specific environment. When an environment is specified, only configuration from that section is loaded.
        Parameters:
        file - path to the configuration file
        environment - the environment section to load, or null for all top-level keys
        Throws:
        IllegalStateException - if the file cannot be loaded or the environment section doesn't exist
    • Method Detail

      • getSourceTypePrefix

        protected abstract String getSourceTypePrefix()
        Returns the source type prefix for the source name (e.g., "Json", "Yaml", "Toml").
        Returns:
        the source type prefix
      • parseContent

        protected abstract T parseContent​(Reader reader)
                                   throws IOException
        Parses the configuration content from a Reader.
        Parameters:
        reader - the reader to parse from
        Returns:
        the parsed configuration object
        Throws:
        IOException - if parsing fails
      • extractEnvironmentSection

        protected abstract T extractEnvironmentSection​(T content,
                                                       String environment,
                                                       String filePath)
        Extracts the environment-specific section from the parsed configuration.
        Parameters:
        content - the full parsed configuration
        environment - the environment section to extract
        filePath - the file path for error messages
        Returns:
        the environment-specific configuration section
        Throws:
        IllegalStateException - if the environment section doesn't exist
      • getValueFromRoot

        protected abstract String getValueFromRoot​(String key)
        Gets a value from the parsed configuration using the given key. Supports dot notation for nested keys (e.g., "app.name").
        Parameters:
        key - the configuration key
        Returns:
        the value as a string, or null if not found
      • getName

        public String getName()
        Description copied from interface: ConfigSource
        Returns the name of this configuration source.
        Specified by:
        getName in interface ConfigSource
        Returns:
        the source name, never null
      • get

        public Optional<String> get​(String key)
        Description copied from interface: ConfigSource
        Retrieves a configuration value for the given key.

        If the key is not found in this source, an empty Optional should be returned. This allows the ConfNG system to try other sources in the resolution chain.

        Specified by:
        get in interface ConfigSource
        Parameters:
        key - the configuration key to look up
        Returns:
        an Optional containing the value if found, empty otherwise
      • getPriority

        public int getPriority()
        Description copied from interface: ConfigSource
        Returns the priority of this configuration source.

        Sources with higher priority values are consulted first in the resolution chain. Default priorities:

        • 100+ - Secret managers and secure sources
        • 80-99 - TestNG parameters and test-specific sources
        • 60-79 - Environment variables
        • 40-59 - System properties
        • 20-39 - Configuration files (JSON, Properties)
        • 0-19 - Default and fallback sources
        Specified by:
        getPriority in interface ConfigSource
        Returns:
        the priority value, higher values have higher priority