Class AbstractFileConfiguration

All Implemented Interfaces:
Cloneable, Configuration, FileConfiguration, FileSystemBased
Direct Known Subclasses:
AbstractHierarchicalFileConfiguration.FileConfigurationDelegate, INIConfiguration, PropertiesConfiguration

Partial implementation of the FileConfiguration interface. Developers of file based configuration may want to extend this class, the two methods left to implement are FileConfiguration.load(Reader) and FileConfiguration.save(Writer).

This base class already implements a couple of ways to specify the location of the file this configuration is based on. The following possibilities exist:

  • URLs: With the method setURL() a full URL to the configuration source can be specified. This is the most flexible way. Note that the save() methods support only file: URLs.
  • Files: The setFile() method allows to specify the configuration source as a file. This can be either a relative or an absolute file. In the former case the file is resolved based on the current directory.
  • As file paths in string form: With the setPath() method a full path to a configuration file can be provided as a string.
  • Separated as base path and file name: This is the native form in which the location is stored. The base path is a string defining either a local directory or a URL. It can be set using the setBasePath() method. The file name, non surprisingly, defines the name of the configuration file.

The configuration source to be loaded can be specified using one of the methods described above. Then the parameterless load() method can be called. Alternatively, one of the load() methods can be used which is passed the source directly. These methods typically do not change the internally stored file; however, if the configuration is not yet associated with a configuration source, the first call to one of the load() methods sets the base path and the source URL. This fact has to be taken into account when calling load() multiple times with different file paths.

Note that the load() methods do not wipe out the configuration's content before the new configuration file is loaded. Thus it is very easy to construct a union configuration by simply loading multiple configuration files, e.g.

 config.load(configFile1);
 config.load(configFile2);
 

After executing this code fragment, the resulting configuration will contain both the properties of configFile1 and configFile2. On the other hand, if the current configuration file is to be reloaded, clear() should be called first. Otherwise the properties are doubled. This behavior is analogous to the behavior of the load(InputStream) method in java.util.Properties.

Since:
1.0-rc2
Version:
$Id: AbstractFileConfiguration.java 1234118 2012-01-20 20:36:04Z oheger $
Author:
Emmanuel Bourg
  • Field Details

  • Constructor Details

  • Method Details

    • setFileSystem

      public void setFileSystem(FileSystem fileSystem)
      Specified by:
      setFileSystem in interface FileSystemBased
    • resetFileSystem

      public void resetFileSystem()
      Specified by:
      resetFileSystem in interface FileSystemBased
    • getFileSystem

      Specified by:
      getFileSystem in interface FileSystemBased
    • getReloadLock

    • load

      public void load() throws ConfigurationException
      Load the configuration from the underlying location.
      Specified by:
      load in interface FileConfiguration
      Throws:
      ConfigurationException - if loading of the configuration fails
    • load

      public void load(String fileName) throws ConfigurationException
      Locate the specified file and load the configuration. If the configuration is already associated with a source, the current source is not changed. Otherwise (i.e. this is the first load operation), the source URL and the base path are set now based on the source to be loaded.
      Specified by:
      load in interface FileConfiguration
      Parameters:
      fileName - the name of the file to be loaded
      Throws:
      ConfigurationException - if an error occurs
    • load

      public void load(File file) throws ConfigurationException
      Load the configuration from the specified file. If the configuration is already associated with a source, the current source is not changed. Otherwise (i.e. this is the first load operation), the source URL and the base path are set now based on the source to be loaded.
      Specified by:
      load in interface FileConfiguration
      Parameters:
      file - the file to load
      Throws:
      ConfigurationException - if an error occurs
    • load

      public void load(URL url) throws ConfigurationException
      Load the configuration from the specified URL. If the configuration is already associated with a source, the current source is not changed. Otherwise (i.e. this is the first load operation), the source URL and the base path are set now based on the source to be loaded.
      Specified by:
      load in interface FileConfiguration
      Parameters:
      url - the URL of the file to be loaded
      Throws:
      ConfigurationException - if an error occurs
    • load

      public void load(InputStream in) throws ConfigurationException
      Load the configuration from the specified stream, using the encoding returned by getEncoding().
      Specified by:
      load in interface FileConfiguration
      Parameters:
      in - the input stream
      Throws:
      ConfigurationException - if an error occurs during the load operation
    • load

      public void load(InputStream in, String encoding) throws ConfigurationException
      Load the configuration from the specified stream, using the specified encoding. If the encoding is null the default encoding is used.
      Specified by:
      load in interface FileConfiguration
      Parameters:
      in - the input stream
      encoding - the encoding used. null to use the default encoding
      Throws:
      ConfigurationException - if an error occurs during the load operation
    • save

      public void save() throws ConfigurationException
      Save the configuration. Before this method can be called a valid file name must have been set.
      Specified by:
      save in interface FileConfiguration
      Throws:
      ConfigurationException - if an error occurs or no file name has been set yet
    • save

      public void save(String fileName) throws ConfigurationException
      Save the configuration to the specified file. This doesn't change the source of the configuration, use setFileName() if you need it.
      Specified by:
      save in interface FileConfiguration
      Parameters:
      fileName - the file name
      Throws:
      ConfigurationException - if an error occurs during the save operation
    • save

      public void save(URL url) throws ConfigurationException
      Save the configuration to the specified URL. This doesn't change the source of the configuration, use setURL() if you need it.
      Specified by:
      save in interface FileConfiguration
      Parameters:
      url - the URL
      Throws:
      ConfigurationException - if an error occurs during the save operation
    • save

      public void save(File file) throws ConfigurationException
      Save the configuration to the specified file. The file is created automatically if it doesn't exist. This doesn't change the source of the configuration, use setFile(java.io.File) if you need it.
      Specified by:
      save in interface FileConfiguration
      Parameters:
      file - the target file
      Throws:
      ConfigurationException - if an error occurs during the save operation
    • save

      public void save(OutputStream out) throws ConfigurationException
      Save the configuration to the specified stream, using the encoding returned by getEncoding().
      Specified by:
      save in interface FileConfiguration
      Parameters:
      out - the output stream
      Throws:
      ConfigurationException - if an error occurs during the save operation
    • save

      public void save(OutputStream out, String encoding) throws ConfigurationException
      Save the configuration to the specified stream, using the specified encoding. If the encoding is null the default encoding is used.
      Specified by:
      save in interface FileConfiguration
      Parameters:
      out - the output stream
      encoding - the encoding to use
      Throws:
      ConfigurationException - if an error occurs during the save operation
    • getFileName

      public String getFileName()
      Return the name of the file.
      Specified by:
      getFileName in interface FileConfiguration
      Returns:
      the file name
    • setFileName

      public void setFileName(String fileName)
      Set the name of the file. The passed in file name can contain a relative path. It must be used when referring files with relative paths from classpath. Use setPath() to set a full qualified file name.
      Specified by:
      setFileName in interface FileConfiguration
      Parameters:
      fileName - the name of the file
    • getBasePath

      public String getBasePath()
      Return the base path.
      Specified by:
      getBasePath in interface FileConfiguration
      Returns:
      the base path
      See Also:
    • setBasePath

      public void setBasePath(String basePath)
      Sets the base path. The base path is typically either a path to a directory or a URL. Together with the value passed to the setFileName() method it defines the location of the configuration file to be loaded. The strategies for locating the file are quite tolerant. For instance if the file name is already an absolute path or a fully defined URL, the base path will be ignored. The base path can also be a URL, in which case the file name is interpreted in this URL's context. Because the base path is used by some of the derived classes for resolving relative file names it should contain a meaningful value. If other methods are used for determining the location of the configuration file (e.g. setFile() or setURL()), the base path is automatically set.
      Specified by:
      setBasePath in interface FileConfiguration
      Parameters:
      basePath - the base path.
    • getFile

      public File getFile()
      Return the file where the configuration is stored. If the base path is a URL with a protocol different than "file", or the configuration file is within a compressed archive, the return value will not point to a valid file object.
      Specified by:
      getFile in interface FileConfiguration
      Returns:
      the file where the configuration is stored; this can be null
    • setFile

      public void setFile(File file)
      Set the file where the configuration is stored. The passed in file is made absolute if it is not yet. Then the file's path component becomes the base path and its name component becomes the file name.
      Specified by:
      setFile in interface FileConfiguration
      Parameters:
      file - the file where the configuration is stored
    • getPath

      public String getPath()
      Returns the full path to the file this configuration is based on. The return value is a valid File path only if this configuration is based on a file on the local disk. If the configuration was loaded from a packed archive the returned value is the string form of the URL from which the configuration was loaded.
      Returns:
      the full path to the configuration file
    • setPath

      public void setPath(String path)
      Sets the location of this configuration as a full or relative path name. The passed in path should represent a valid file name on the file system. It must not be used to specify relative paths for files that exist in classpath, either plain file system or compressed archive, because this method expands any relative path to an absolute one which may end in an invalid absolute path for classpath references.
      Parameters:
      path - the full path name of the configuration file
    • getURL

      public URL getURL()
      Return the URL where the configuration is stored.
      Specified by:
      getURL in interface FileConfiguration
      Returns:
      the configuration's location as URL
    • setURL

      public void setURL(URL url)
      Set the location of this configuration as a URL. For loading this can be an arbitrary URL with a supported protocol. If the configuration is to be saved, too, a URL with the "file" protocol should be provided.
      Specified by:
      setURL in interface FileConfiguration
      Parameters:
      url - the location of this configuration as URL
    • setAutoSave

      public void setAutoSave(boolean autoSave)
      Description copied from interface: FileConfiguration
      Enable or disable the automatically saving of modified properties to the disk.
      Specified by:
      setAutoSave in interface FileConfiguration
      Parameters:
      autoSave - true to enable, false to disable
    • isAutoSave

      public boolean isAutoSave()
      Description copied from interface: FileConfiguration
      Tells if properties are automatically saved to the disk.
      Specified by:
      isAutoSave in interface FileConfiguration
      Returns:
      true if auto-saving is enabled, false otherwise
    • possiblySave

      protected void possiblySave()
      Save the configuration if the automatic persistence is enabled and if a file is specified.
    • addProperty

      public void addProperty(String key, Object value)
      Adds a new property to this configuration. This implementation checks if the auto save mode is enabled and saves the configuration if necessary.
      Specified by:
      addProperty in interface Configuration
      Overrides:
      addProperty in class AbstractConfiguration
      Parameters:
      key - the key of the new property
      value - the value
    • setProperty

      public void setProperty(String key, Object value)
      Sets a new value for the specified property. This implementation checks if the auto save mode is enabled and saves the configuration if necessary.
      Specified by:
      setProperty in interface Configuration
      Overrides:
      setProperty in class AbstractConfiguration
      Parameters:
      key - the key of the affected property
      value - the value
    • clearProperty

      public void clearProperty(String key)
      Description copied from class: AbstractConfiguration
      Removes the specified property from this configuration. This implementation performs some preparations and then delegates to clearPropertyDirect(), which will do the real work.
      Specified by:
      clearProperty in interface Configuration
      Overrides:
      clearProperty in class AbstractConfiguration
      Parameters:
      key - the key to be removed
    • getReloadingStrategy

      Description copied from interface: FileConfiguration
      Return the reloading strategy.
      Specified by:
      getReloadingStrategy in interface FileConfiguration
      Returns:
      the reloading strategy currently used
    • setReloadingStrategy

      public void setReloadingStrategy(ReloadingStrategy strategy)
      Description copied from interface: FileConfiguration
      Set the reloading strategy.
      Specified by:
      setReloadingStrategy in interface FileConfiguration
      Parameters:
      strategy - the reloading strategy to use
    • reload

      public void reload()
      Performs a reload operation if necessary. This method is called on each access of this configuration. It asks the associated reloading strategy whether a reload should be performed. If this is the case, the configuration is cleared and loaded again from its source. If this operation causes an exception, the registered error listeners will be notified. The error event passed to the listeners is of type EVENT_RELOAD and contains the exception that caused the event.
      Specified by:
      reload in interface FileConfiguration
    • reload

      public boolean reload(boolean checkReload)
    • refresh

      public void refresh() throws ConfigurationException
      Reloads the associated configuration file. This method first clears the content of this configuration, then the associated configuration file is loaded again. Updates on this configuration which have not yet been saved are lost. Calling this method is like invoking reload() without checking the reloading strategy.
      Throws:
      ConfigurationException - if an error occurs
      Since:
      1.7
    • configurationChanged

      public void configurationChanged()
      Send notification that the configuration has changed.
    • enterNoReload

      protected void enterNoReload()
      Enters the "No reloading mode". As long as this mode is active no reloading will be performed. This is necessary for some implementations of save() in derived classes, which may cause a reload while accessing the properties to save. This may cause the whole configuration to be erased. To avoid this, this method can be called first. After a call to this method there always must be a corresponding call of exitNoReload() later! (If necessary, finally blocks must be used to ensure this.
    • exitNoReload

      protected void exitNoReload()
      Leaves the "No reloading mode".
      See Also:
    • fireEvent

      protected void fireEvent(int type, String propName, Object propValue, boolean before)
      Sends an event to all registered listeners. This implementation ensures that no reloads are performed while the listeners are invoked. So infinite loops can be avoided that can be caused by event listeners accessing the configuration's properties when they are invoked.
      Overrides:
      fireEvent in class EventSource
      Parameters:
      type - the event type
      propName - the name of the property
      propValue - the value of the property
      before - the before update flag
    • getProperty

      public Object getProperty(String key)
      Description copied from class: BaseConfiguration
      Read property from underlying map.
      Specified by:
      getProperty in interface Configuration
      Overrides:
      getProperty in class BaseConfiguration
      Parameters:
      key - key to use for mapping
      Returns:
      object associated with the given configuration key.
    • isEmpty

      public boolean isEmpty()
      Description copied from class: BaseConfiguration
      Check if the configuration is empty
      Specified by:
      isEmpty in interface Configuration
      Overrides:
      isEmpty in class BaseConfiguration
      Returns:
      true if Configuration is empty, false otherwise.
    • containsKey

      public boolean containsKey(String key)
      Description copied from class: BaseConfiguration
      check if the configuration contains the key
      Specified by:
      containsKey in interface Configuration
      Overrides:
      containsKey in class BaseConfiguration
      Parameters:
      key - the configuration key
      Returns:
      true if Configuration contain given key, false otherwise.
    • getKeys

      public Iterator<String> getKeys()
      Returns an Iterator with the keys contained in this configuration. This implementation performs a reload if necessary before obtaining the keys. The Iterator returned by this method points to a snapshot taken when this method was called. Later changes at the set of keys (including those caused by a reload) won't be visible. This is because a reload can happen at any time during iteration, and it is impossible to determine how this reload affects the current iteration. When using the iterator a client has to be aware that changes of the configuration are possible at any time. For instance, if after a reload operation some keys are no longer present, the iterator will still return those keys because they were found when it was created.
      Specified by:
      getKeys in interface Configuration
      Overrides:
      getKeys in class BaseConfiguration
      Returns:
      an Iterator with the keys of this configuration
    • getEncoding

      public String getEncoding()
      Description copied from interface: FileConfiguration
      Return the encoding used to store the configuration file. If the value is null the default encoding is used.
      Specified by:
      getEncoding in interface FileConfiguration
      Returns:
      the current encoding
    • setEncoding

      public void setEncoding(String encoding)
      Description copied from interface: FileConfiguration
      Set the encoding used to store the configuration file. Set the encoding to null to use the default encoding.
      Specified by:
      setEncoding in interface FileConfiguration
      Parameters:
      encoding - the encoding to use
    • clone

      public Object clone()
      Creates a copy of this configuration. The new configuration object will contain the same properties as the original, but it will lose any connection to a source file (if one exists); this includes setting the source URL, base path, and file name to null. This is done to avoid race conditions if both the original and the copy are modified and then saved.
      Overrides:
      clone in class BaseConfiguration
      Returns:
      the copy
      Since:
      1.3
    • closeSilent

      protected void closeSilent(OutputStream out)
      A helper method for closing an output stream. Occurring exceptions will be ignored.
      Parameters:
      out - the output stream to be closed (may be null)
      Since:
      1.5