Package org.confng.sources
Class TomlSource
- java.lang.Object
-
- org.confng.sources.AbstractFileConfigSource<org.tomlj.TomlTable>
-
- org.confng.sources.TomlSource
-
- All Implemented Interfaces:
ConfigSource
public class TomlSource extends AbstractFileConfigSource<org.tomlj.TomlTable>
Configuration source that reads from TOML files.This source loads configuration from TOML files using TomlJ for parsing. Supports both primitive values and nested objects.
Supports environment-specific configuration sections. When an environment is specified, only configuration from that section is loaded. For example:
[uat] database.host = "uat-db.example.com" api.url = "https://uat-api.example.com" [prod] database.host = "prod-db.example.com" api.url = "https://prod-api.example.com"
- Since:
- 1.0
- Author:
- Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
- See Also:
ConfigSource,AbstractFileConfigSource
-
-
Field Summary
-
Fields inherited from class org.confng.sources.AbstractFileConfigSource
environment, root, sourceName
-
-
Constructor Summary
Constructors Constructor Description TomlSource(String filePath)Creates a new TomlSource from the given file path (supports both filesystem and classpath).TomlSource(String filePath, String environment)Creates a new TomlSource from the given file path for a specific environment (supports both filesystem and classpath).TomlSource(Path file)Creates a new TomlSource from the given file.TomlSource(Path file, String environment)Creates a new TomlSource from the given file for a specific environment.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.tomlj.TomlTableextractEnvironmentSection(org.tomlj.TomlTable content, String environment, String filePath)Extracts the environment-specific section from the parsed configuration.protected StringgetSourceTypePrefix()Returns the source type prefix for the source name (e.g., "Json", "Yaml", "Toml").protected StringgetValueFromRoot(String key)Gets a value from the parsed configuration using the given key.protected org.tomlj.TomlTableparseContent(Reader reader)Parses the configuration content from a Reader.-
Methods inherited from class org.confng.sources.AbstractFileConfigSource
get, getName, getPriority
-
-
-
-
Constructor Detail
-
TomlSource
public TomlSource(String filePath)
Creates a new TomlSource from the given file path (supports both filesystem and classpath). Loads all top-level keys (not nested in environment sections).- Parameters:
filePath- path to the TOML file (filesystem or classpath)- Throws:
IllegalStateException- if the file cannot be loaded
-
TomlSource
public TomlSource(String filePath, String environment)
Creates a new TomlSource from the given file path for a specific environment (supports both filesystem and classpath). When an environment is specified, only configuration from that section is loaded.- Parameters:
filePath- path to the TOML file (filesystem or classpath)environment- the environment section to load (e.g., "uat", "prod"), or null for all top-level keys- Throws:
IllegalStateException- if the file cannot be loaded or the environment section doesn't exist
-
TomlSource
public TomlSource(Path file)
Creates a new TomlSource from the given file. Loads all top-level keys (not nested in environment sections).- Parameters:
file- path to the TOML file- Throws:
IllegalStateException- if the file cannot be loaded
-
TomlSource
public TomlSource(Path file, String environment)
Creates a new TomlSource from the given file for a specific environment. When an environment is specified, only configuration from that section is loaded.- Parameters:
file- path to the TOML fileenvironment- the environment section to load (e.g., "env1", "env2"), 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 String getSourceTypePrefix()
Description copied from class:AbstractFileConfigSourceReturns the source type prefix for the source name (e.g., "Json", "Yaml", "Toml").- Specified by:
getSourceTypePrefixin classAbstractFileConfigSource<org.tomlj.TomlTable>- Returns:
- the source type prefix
-
parseContent
protected org.tomlj.TomlTable parseContent(Reader reader) throws IOException
Description copied from class:AbstractFileConfigSourceParses the configuration content from a Reader.- Specified by:
parseContentin classAbstractFileConfigSource<org.tomlj.TomlTable>- Parameters:
reader- the reader to parse from- Returns:
- the parsed configuration object
- Throws:
IOException- if parsing fails
-
extractEnvironmentSection
protected org.tomlj.TomlTable extractEnvironmentSection(org.tomlj.TomlTable content, String environment, String filePath)Description copied from class:AbstractFileConfigSourceExtracts the environment-specific section from the parsed configuration.- Specified by:
extractEnvironmentSectionin classAbstractFileConfigSource<org.tomlj.TomlTable>- Parameters:
content- the full parsed configurationenvironment- the environment section to extractfilePath- the file path for error messages- Returns:
- the environment-specific configuration section
-
getValueFromRoot
protected String getValueFromRoot(String key)
Description copied from class:AbstractFileConfigSourceGets a value from the parsed configuration using the given key. Supports dot notation for nested keys (e.g., "app.name").- Specified by:
getValueFromRootin classAbstractFileConfigSource<org.tomlj.TomlTable>- Parameters:
key- the configuration key- Returns:
- the value as a string, or null if not found
-
-