Package io.confound.config.file
Class FileSystemConfigurationManager
java.lang.Object
io.confound.config.AbstractConfigurationManager
io.confound.config.file.AbstractFileConfigurationManager
io.confound.config.file.FileSystemConfigurationManager
- All Implemented Interfaces:
io.clogr.Clogged
,io.confound.config.ConfigurationManager
public class FileSystemConfigurationManager
extends AbstractFileConfigurationManager
implements io.clogr.Clogged
A configuration manager that can load and parse a configuration from the file system.
- Author:
- Garret Wilson
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builder for the manager. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The default base filename to use for determining a file system resource. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
FileSystemConfigurationManager
(Iterable<ConfigurationFileFormat> fileFormats, Supplier<Stream<Path>> configurationFileCandidatePathsSupplier, boolean required) File formats, candidate paths supplier, and optional required constructor.FileSystemConfigurationManager
(Supplier<Stream<Path>> configurationFileCandidatePathsSupplier) Constructor for an optional configuration.FileSystemConfigurationManager
(Supplier<Stream<Path>> configurationFileCandidatePathsSupplier, boolean required) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionforBaseFilename
(Path directory, String baseFilename) Creates a configuration manager that discovers an optional configuration file using a base filename.forCandidatePaths
(Path... configurationCandidatePaths) Creates a configuration manager that discovers an optional configuration file from one of the given paths.forDirectory
(Path directory) Creates a configuration manager that discovers an optional configuration file using the default base filename "config".forFilenameGlob
(Path directory, String filenameGlob) Creates a configuration manager that discovers an optional configuration file using a filename glob.forFilenamePattern
(Path directory, Pattern filenamePattern) Creates a configuration manager that discovers an optional configuration file using a filename pattern.Creates a configuration manager that loads an optional configuration from a given path as necessary.getFileFormat
(Path path) Determines the file format to use for the given path based on the registered formats and the path filename extension(s).void
This version additionally removes any cached configuration path.boolean
isStale
(io.confound.config.Configuration configuration) This version additionally checks to see if whether there is a cached configuration path.Optional<io.confound.config.Configuration>
static Optional<io.confound.config.Configuration>
loadConfigurationForBaseFilename
(Path directory, String baseFilename) Discovers and loads an optional configuration file using a base filename.static Optional<io.confound.config.Configuration>
loadConfigurationForCandidatePaths
(Path... configurationCandidatePaths) Discovers and loads an optional configuration file from one of the given paths.static Optional<io.confound.config.Configuration>
loadConfigurationForDirectory
(Path directory) Discovers and loads an optional configuration file using the default base filename "config".static Optional<io.confound.config.Configuration>
loadConfigurationForFilenameGlob
(Path directory, String filenameGlob) Discovers and loads an optional configuration file using a filename glob.static Optional<io.confound.config.Configuration>
loadConfigurationForFilenamePattern
(Path directory, Pattern filenamePattern) Discovers and loads an optional configuration file using a filename pattern.static Optional<io.confound.config.Configuration>
loadConfigurationForPath
(Path configurationPath) Loads an optional configuration from a given path as necessary.void
saveConfiguration
(io.confound.config.Configuration configuration) This implementation does not yet support saving configurations, and will throw an exception.Methods inherited from class io.confound.config.file.AbstractFileConfigurationManager
createConfigurationNotFoundException, defaultFileFormats, getFileFormatForExtension, getFileFormatForFilename, getFileFormatsByExtension
Methods inherited from class io.confound.config.AbstractConfigurationManager
isRequired
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.clogr.Clogged
getLogger
-
Field Details
-
DEFAULT_BASE_FILENAME
The default base filename to use for determining a file system resource.- See Also:
-
-
Constructor Details
-
FileSystemConfigurationManager
public FileSystemConfigurationManager(@Nonnull Supplier<Stream<Path>> configurationFileCandidatePathsSupplier) Constructor for an optional configuration.The path supplier is allowed to throw
UncheckedIOException
when the stream is returned and during stream iteration.- Parameters:
configurationFileCandidatePathsSupplier
- The strategy for determining candidate paths for finding a configuration.
-
FileSystemConfigurationManager
public FileSystemConfigurationManager(@Nonnull Supplier<Stream<Path>> configurationFileCandidatePathsSupplier, boolean required) Constructor.The path supplier is allowed to throw
UncheckedIOException
when the stream is returned and during stream iteration.- Parameters:
configurationFileCandidatePathsSupplier
- The strategy for determining candidate paths for finding a configuration.required
- Whether the manager requires a configuration to be determined when loading.
-
FileSystemConfigurationManager
protected FileSystemConfigurationManager(@Nonnull Iterable<ConfigurationFileFormat> fileFormats, @Nonnull Supplier<Stream<Path>> configurationFileCandidatePathsSupplier, boolean required) File formats, candidate paths supplier, and optional required constructor.The path supplier is allowed to throw
UncheckedIOException
when the stream is returned and during stream iteration.- Parameters:
fileFormats
- The file formats to support.configurationFileCandidatePathsSupplier
- The strategy for determining candidate paths for finding a configuration.required
- Whether the manager requires a configuration to be determined when loading.
-
-
Method Details
-
getFileFormat
Determines the file format to use for the given path based on the registered formats and the path filename extension(s).- Parameters:
path
- The path for which the file format should be determined.- Returns:
- The configured file format for the path, if any.
- Throws:
IllegalArgumentException
- if the given path has no filename.
-
loadConfiguration
public Optional<io.confound.config.Configuration> loadConfiguration() throws IOException, io.confound.config.ConfigurationException- Specified by:
loadConfiguration
in interfaceio.confound.config.ConfigurationManager
- Implementation Specification:
- This implementation uses the existing configuration path if it has been determined. If the configuration path has not been determined, such as if
this manager has been invalidated using
invalidate()
, it determines a new configuration path and updates the record. - Throws:
IOException
io.confound.config.ConfigurationException
-
saveConfiguration
This implementation does not yet support saving configurations, and will throw an exception.- Specified by:
saveConfiguration
in interfaceio.confound.config.ConfigurationManager
- Throws:
IOException
-
isStale
This version additionally checks to see if whether there is a cached configuration path.- Specified by:
isStale
in interfaceio.confound.config.ConfigurationManager
- Throws:
IOException
-
invalidate
public void invalidate()This version additionally removes any cached configuration path.- Specified by:
invalidate
in interfaceio.confound.config.ConfigurationManager
-
forPath
Creates a configuration manager that loads an optional configuration from a given path as necessary.- Parameters:
configurationPath
- The path at which to find the configuration file.- Returns:
- A configuration manager for the file at the given path.
-
forCandidatePaths
public static FileSystemConfigurationManager forCandidatePaths(@Nonnull Path... configurationCandidatePaths) Creates a configuration manager that discovers an optional configuration file from one of the given paths.The first configuration file with a supported format is used.
- Parameters:
configurationCandidatePaths
- The potential paths at which to find the configuration file.- Returns:
- A configuration manager for one of the files at the given paths.
- Throws:
NullPointerException
- if one of the paths isnull
.IllegalArgumentException
- if no paths are given, or if one of the given paths has no filename.
-
forDirectory
Creates a configuration manager that discovers an optional configuration file using the default base filename "config".- Parameters:
directory
- The source directory for configuration file discovery.- Returns:
- A configuration manager to use a configuration file with the given base name.
- Throws:
NullPointerException
- if the directory isnull
.- See Also:
-
forBaseFilename
public static FileSystemConfigurationManager forBaseFilename(@Nonnull Path directory, @Nonnull String baseFilename) Creates a configuration manager that discovers an optional configuration file using a base filename.- Parameters:
directory
- The source directory for configuration file discovery.baseFilename
- The base filename, such asbase
, to return files with any extension, such asbase.foo
andbase.foo.bar
, or no extension at all.- Returns:
- A configuration manager to use a configuration file with the given base name.
- Throws:
NullPointerException
- if the directory and/or base filename isnull
.
-
forFilenameGlob
public static FileSystemConfigurationManager forFilenameGlob(@Nonnull Path directory, @Nonnull String filenameGlob) Creates a configuration manager that discovers an optional configuration file using a filename glob.Only a single directory level is searched, regardless of the glob.
- Parameters:
directory
- The source directory for configuration file discovery.filenameGlob
- The glob for matching files in the directory, such asfoo?.{properties,xml}
which would match bothfoot.xml
andfood.properties
.- Returns:
- A configuration manager to use a configuration file matched by the given filename glob.
- Throws:
NullPointerException
- if the directory and/or filename glob isnull
.
-
forFilenamePattern
public static FileSystemConfigurationManager forFilenamePattern(@Nonnull Path directory, @Nonnull Pattern filenamePattern) Creates a configuration manager that discovers an optional configuration file using a filename pattern.- Parameters:
directory
- The source directory for configuration file discovery.filenamePattern
- The regular expression pattern for matching files in the directory.- Returns:
- A configuration manager to use a configuration file matched by the given filename pattern.
- Throws:
NullPointerException
- if the directory and/or filename pattern isnull
.
-
loadConfigurationForPath
public static Optional<io.confound.config.Configuration> loadConfigurationForPath(@Nonnull Path configurationPath) throws IOException, io.confound.config.ConfigurationException Loads an optional configuration from a given path as necessary.- Parameters:
configurationPath
- The path at which to find the configuration file.- Returns:
- The loaded configuration, which will not be present if no appropriate configuration was found.
- Throws:
IOException
- if an I/O error occurs loading the configuration.io.confound.config.ConfigurationException
- If there is invalid data or invalid state preventing the configuration from being loaded.
-
loadConfigurationForCandidatePaths
public static Optional<io.confound.config.Configuration> loadConfigurationForCandidatePaths(@Nonnull Path... configurationCandidatePaths) throws IOException, io.confound.config.ConfigurationException Discovers and loads an optional configuration file from one of the given paths.The first configuration file with a supported format is used.
- Parameters:
configurationCandidatePaths
- The potential paths at which to find the configuration file.- Returns:
- The loaded configuration, which will not be present if no appropriate configuration was found.
- Throws:
NullPointerException
- if one of the paths isnull
.IllegalArgumentException
- if no paths are given, or if one of the given paths has no filename.IOException
- if an I/O error occurs loading the configuration.io.confound.config.ConfigurationException
- If there is invalid data or invalid state preventing the configuration from being loaded.
-
loadConfigurationForDirectory
public static Optional<io.confound.config.Configuration> loadConfigurationForDirectory(@Nonnull Path directory) throws IOException, io.confound.config.ConfigurationException Discovers and loads an optional configuration file using the default base filename "config".- Parameters:
directory
- The source directory for configuration file discovery.- Returns:
- The loaded configuration, which will not be present if no appropriate configuration was found.
- Throws:
NullPointerException
- if the directory isnull
.IOException
- if an I/O error occurs loading the configuration.io.confound.config.ConfigurationException
- If there is invalid data or invalid state preventing the configuration from being loaded.- See Also:
-
loadConfigurationForBaseFilename
public static Optional<io.confound.config.Configuration> loadConfigurationForBaseFilename(@Nonnull Path directory, @Nonnull String baseFilename) throws IOException, io.confound.config.ConfigurationException Discovers and loads an optional configuration file using a base filename.- Parameters:
directory
- The source directory for configuration file discovery.baseFilename
- The base filename, such asbase
, to return files with any extension, such asbase.foo
andbase.foo.bar
, or no extension at all.- Returns:
- The loaded configuration, which will not be present if no appropriate configuration was found.
- Throws:
NullPointerException
- if the directory and/or base filename isnull
.IOException
- if an I/O error occurs loading the configuration.io.confound.config.ConfigurationException
- If there is invalid data or invalid state preventing the configuration from being loaded.
-
loadConfigurationForFilenameGlob
public static Optional<io.confound.config.Configuration> loadConfigurationForFilenameGlob(@Nonnull Path directory, @Nonnull String filenameGlob) throws IOException, io.confound.config.ConfigurationException Discovers and loads an optional configuration file using a filename glob.Only a single directory level is searched, regardless of the glob.
- Parameters:
directory
- The source directory for configuration file discovery.filenameGlob
- The glob for matching files in the directory, such asfoo?.{properties,xml}
which would match bothfoot.xml
andfood.properties
.- Returns:
- The loaded configuration, which will not be present if no appropriate configuration was found.
- Throws:
NullPointerException
- if the directory and/or filename glob isnull
.IOException
- if an I/O error occurs loading the configuration.io.confound.config.ConfigurationException
- If there is invalid data or invalid state preventing the configuration from being loaded.
-
loadConfigurationForFilenamePattern
public static Optional<io.confound.config.Configuration> loadConfigurationForFilenamePattern(@Nonnull Path directory, @Nonnull Pattern filenamePattern) throws IOException, io.confound.config.ConfigurationException Discovers and loads an optional configuration file using a filename pattern.- Parameters:
directory
- The source directory for configuration file discovery.filenamePattern
- The regular expression pattern for matching files in the directory.- Returns:
- The loaded configuration, which will not be present if no appropriate configuration was found.
- Throws:
NullPointerException
- if the directory and/or filename pattern isnull
.IOException
- if an I/O error occurs loading the configuration.io.confound.config.ConfigurationException
- If there is invalid data or invalid state preventing the configuration from being loaded.
-