Class PropertiesFileConfiguration

All Implemented Interfaces:
Transactional, Cloneable, Configuration, EventSource, FileBasedConfiguration, ImmutableConfiguration, FileBased, FileLocatorAware, SynchronizerSupport

public class PropertiesFileConfiguration extends PropertiesConfiguration implements Transactional
This class extends a PropertiesConfiguration by the means for an underlying file as well as optionally automatically saving the configuration in case of changes. When automatically saving, each change will immediately lead to a save try. Saving will be suspended while the configuration file is being loaded or already saved. A failed save will be logged, and the change which caused it will then throw a ConfigurationRuntimeException; the change remains in memory.
The default for the list delimiter is ,. The PropertiesIOFactory writes the file, keeping the characters outside the Basic Multilingual Plane.

Within a writable transaction, the changes are not saved one by one but once when the transaction is finished; if that save fails, finishing throws the ConfigurationRuntimeException. Closing a transaction without finishing it does not restore the values changed in memory, the next save stores them. Other threads see the changes of a transaction at once unless a lock keeps them out, as the one of a TransactionalCompositeConfiguration does.

  • Field Details

    • fileHandler

      protected final FileHandler fileHandler
      The handler for the configuration file.
    • streamUrl

      protected final URL streamUrl
      The URL of a stream; the file handler does not have one in case of an input stream.
  • Constructor Details

    • PropertiesFileConfiguration

      public PropertiesFileConfiguration(String file, Logger autosaveLogger) throws ConfigurationException
      Creates a new properties configuration based on the designated file and loads the configuration if the file exists. If a logger is provided, changes of the configuration will be automatically saved to the underlying file, the file will be created if required. A failed save will be logged, and the change which caused it will throw a ConfigurationRuntimeException.
      Parameters:
      file - The file to base the properties configuration on.
      autosaveLogger - Whether to automatically save changes to the underlying file. If so, a failed save will be logged to this logger, and the change which caused it will throw a ConfigurationRuntimeException. Use null to not automatically save changes.
      Throws:
      ConfigurationException - If loading the configuration from the designated file fails, a ConfigurationException will be thrown.
    • PropertiesFileConfiguration

      public PropertiesFileConfiguration(File file, Logger autosaveLogger) throws ConfigurationException
      Creates a new properties configuration based on the designated file and loads the configuration if the file exists. If a logger is provided, changes of the configuration will be automatically saved to the underlying file, the file will be created if required. A failed save will be logged, and the change which caused it will throw a ConfigurationRuntimeException.
      Parameters:
      file - The file to base the properties configuration on.
      autosaveLogger - Whether to automatically save changes to the underlying file. If so, a failed save will be logged to this logger, and the change which caused it will throw a ConfigurationRuntimeException. Use null to not automatically save changes.
      Throws:
      ConfigurationException - If loading the configuration from the designated file fails, the file cannot be read or the configuration should be automatically saved but the file cannot be written, a ConfigurationException will be thrown.
    • PropertiesFileConfiguration

      public PropertiesFileConfiguration(URL file, Logger autosaveLogger) throws ConfigurationException
      Creates a new properties configuration based on the designated file and loads the configuration. The file has to exist. If a logger is provided, changes of the configuration will be automatically saved to the underlying file. A failed save will be logged, and the change which caused it will throw a ConfigurationRuntimeException.
      Parameters:
      file - The file to base the properties configuration on.
      autosaveLogger - Whether to automatically save changes to the underlying file. If so, a failed save will be logged to this logger, and the change which caused it will throw a ConfigurationRuntimeException. Use null to not automatically save changes.
      Throws:
      ConfigurationException - If loading the configuration from the designated file fails, a ConfigurationException will be thrown. If the file cannot be read or the configuration should be automatically saved but the file cannot be written, the ConfigurationException will wrap a FileNotFoundException.
    • PropertiesFileConfiguration

      public PropertiesFileConfiguration(URL url, InputStream stream) throws ConfigurationException
      Creates a new properties configuration based on the designated URL and loads the configuration from the designated stream. Autosaving is not possible since the stream is not an output stream.
      Parameters:
      url - The URL specifying the properties file. This is merely for information purpose. If you do not have an URL use PropertiesConfiguration directly.
      stream - The input stream to read the configuration from.
      Throws:
      ConfigurationException - If loading the configuration from the designated file fails, a ConfigurationException will be thrown.
  • Method Details

    • addPropertyInternal

      protected void addPropertyInternal(String key, Object value)

      Unlike the library, this flattens each element of an iterable or an iterator on its own, as the library does for an array: the library remembers the elements it has visited by identity, so it adds repeated elements which are the same object, such as small numbers, only once.

      Overrides:
      addPropertyInternal in class AbstractConfiguration
    • getFileUrl

      public URL getFileUrl()
      Gets the URL of the underlying properties file.
      Returns:
      The URL of the underlying properties file.
    • load

      public void load() throws ConfigurationException
      Loads the configuration from the underlying properties file.
      Throws:
      ConfigurationException - If there are problems loading the configuration from the underlying properties file, a ConfigurationException will be thrown.
    • save

      public void save() throws ConfigurationException
      Saves the configuration to the underlying properties file.
      Throws:
      ConfigurationException - If there are problems saving the configuration to the underlying properties file, a ConfigurationException will be thrown.
    • startTransaction

      public void startTransaction(boolean readOnly)
      Description copied from interface: Transactional
      Starts a transaction of the calling thread on this object. If the calling thread has started one already, this joins it and opens another level. A failed start leaves no level behind.
      Specified by:
      startTransaction in interface Transactional
      Parameters:
      readOnly - Whether the transaction only reads values. This is set by the outermost start; a nested read-only level joins a writable transaction.
    • finishTransaction

      public void finishTransaction()
      Description copied from interface: Transactional
      Finishes the current level of the transaction of the calling thread. Finishing the outermost level commits the transaction, finishing a nested level only ends that level. The level still has to be closed. If committing fails, the outermost level remains unfinished, so that closing it rolls the transaction back.
      Specified by:
      finishTransaction in interface Transactional
    • closeTransaction

      public void closeTransaction()
      Description copied from interface: Transactional
      Closes the current level of the transaction of the calling thread. Closing the outermost level rolls the transaction back unless it has been finished, and releases what the transaction holds. This belongs in a finally block. So as not to hide the exception which skipped finishing, a failure when rolling back or releasing is logged instead of thrown.
      Specified by:
      closeTransaction in interface Transactional
    • clone

      public Object clone()
      Refuses to clone this configuration: a copy would share the transactions of the threads and the file handler with it, so that saving the copy would save this configuration.
      Overrides:
      clone in class PropertiesConfiguration
      Returns:
      Nothing, this always throws.
      Throws:
      ConfigurationRuntimeException - Since this configuration cannot be cloned, a ConfigurationRuntimeException wrapping a CloneNotSupportedException will always be thrown.
    • beginWrite

      protected void beginWrite(boolean optimize)
      Overrides:
      beginWrite in class AbstractConfiguration