Package org.confng.sources
Class AbstractFileConfigSource<T>
- java.lang.Object
-
- org.confng.sources.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 Summary
Fields Modifier and Type Field Description protected StringenvironmentThe environment section that was loaded, or null for all top-level keysprotected TrootThe parsed configuration root objectprotected StringsourceNameThe source name for display purposes
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractFileConfigSource(String filePath)Creates a new file-based config source from the given file path.protectedAbstractFileConfigSource(String filePath, String environment)Creates a new file-based config source from the given file path for a specific environment.protectedAbstractFileConfigSource(Path file)Creates a new file-based config source from the given Path.protectedAbstractFileConfigSource(Path file, String environment)Creates a new file-based config source from the given Path for a specific environment.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract TextractEnvironmentSection(T content, String environment, String filePath)Extracts the environment-specific section from the parsed configuration.Optional<String>get(String key)Retrieves a configuration value for the given key.StringgetName()Returns the name of this configuration source.intgetPriority()Returns the priority of this configuration source.protected abstract StringgetSourceTypePrefix()Returns the source type prefix for the source name (e.g., "Json", "Yaml", "Toml").protected abstract StringgetValueFromRoot(String key)Gets a value from the parsed configuration using the given key.protected abstract TparseContent(Reader reader)Parses the configuration content from a Reader.
-
-
-
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 fileenvironment- 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 configurationenvironment- the environment section to extractfilePath- 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:ConfigSourceReturns the name of this configuration source.- Specified by:
getNamein interfaceConfigSource- Returns:
- the source name, never null
-
get
public Optional<String> get(String key)
Description copied from interface:ConfigSourceRetrieves 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:
getin interfaceConfigSource- 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:ConfigSourceReturns 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:
getPriorityin interfaceConfigSource- Returns:
- the priority value, higher values have higher priority
-
-