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
  • Field Details

    • DEFAULT_BASE_FILENAME

      public static final String 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

      public Optional<ConfigurationFileFormat> getFileFormat(@Nonnull Path path)
      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 interface io.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

      public void saveConfiguration(io.confound.config.Configuration configuration) throws IOException
      This implementation does not yet support saving configurations, and will throw an exception.
      Specified by:
      saveConfiguration in interface io.confound.config.ConfigurationManager
      Throws:
      IOException
    • isStale

      public boolean isStale(io.confound.config.Configuration configuration) throws IOException
      This version additionally checks to see if whether there is a cached configuration path.
      Specified by:
      isStale in interface io.confound.config.ConfigurationManager
      Throws:
      IOException
    • invalidate

      public void invalidate()
      This version additionally removes any cached configuration path.
      Specified by:
      invalidate in interface io.confound.config.ConfigurationManager
    • forPath

      public static FileSystemConfigurationManager forPath(@Nonnull Path configurationPath)
      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 is null.
      IllegalArgumentException - if no paths are given, or if one of the given paths has no filename.
    • forDirectory

      public static FileSystemConfigurationManager forDirectory(@Nonnull Path directory)
      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 is null.
      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 as base, to return files with any extension, such as base.foo and base.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 is null.
    • 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 as foo?.{properties,xml} which would match both foot.xml and food.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 is null.
    • 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 is null.
    • 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 is null.
      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 is null.
      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 as base, to return files with any extension, such as base.foo and base.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 is null.
      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 as foo?.{properties,xml} which would match both foot.xml and food.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 is null.
      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 is null.
      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.