Class AutoSaveListener

java.lang.Object
org.apache.commons.configuration2.io.FileHandlerListenerAdapter
org.apache.commons.configuration2.builder.AutoSaveListener
All Implemented Interfaces:
EventListener<ConfigurationEvent>, FileHandlerListener

class AutoSaveListener extends FileHandlerListenerAdapter implements EventListener<ConfigurationEvent>

A listener class implementing an auto save mechanism for file-based configurations.

Instances of this class are used by FileBasedConfigurationBuilder to save their managed configuration instances when they are changed. Objects are registered at Configuration objects as event listeners and thus can trigger save operations whenever a change event is received.

There is one complication however: Some configuration implementations fire change events during a load operation. Such events must be ignored to prevent corruption of the source file. This is achieved by monitoring the associated FileHandler: during load operations no auto-save is performed.

Since:
2.0
  • Field Details

    • log

      private final org.apache.commons.logging.Log log
      The logger.
    • builder

      private final FileBasedConfigurationBuilder<?> builder
      The associated builder.
    • handler

      private FileHandler handler
      Stores the file handler monitored by this listener.
    • loading

      private int loading
      A counter to keep track whether a load operation is currently in progress.
  • Constructor Details

    • AutoSaveListener

      public AutoSaveListener(FileBasedConfigurationBuilder<?> bldr)
      Creates a new instance of AutoSaveListener and initializes it with the associated builder.
      Parameters:
      bldr - the associated builder
  • Method Details

    • onEvent

      public void onEvent(ConfigurationEvent event)
      Notifies this event listener about the arrival of a new event. Typically, event listeners are registered at an event source providing an EventType. This event type acts as a filter; all events matched by the filter are passed to the listener. The type parameters defined by the EventType class and this interface guarantee that the events delivered to the handler are compatible with the concrete method signature of onEvent(). This implementation checks whether an auto-safe operation should be performed. This is the case if the event indicates that an update of the configuration has been performed and currently no load operation is in progress.
      Specified by:
      onEvent in interface EventListener<ConfigurationEvent>
      Parameters:
      event - the event
    • loading

      public void loading(FileHandler handler)
      Notification that the associated file is about to be loaded. This method is called immediately before the load operation. This implementation increments the counter for load operations in progress.
      Specified by:
      loading in interface FileHandlerListener
      Overrides:
      loading in class FileHandlerListenerAdapter
      Parameters:
      handler - the file handler
    • loaded

      public void loaded(FileHandler handler)
      Notification that the associated file has been loaded. This method is called directly after the load operation. This implementation decrements the counter for load operations in progress.
      Specified by:
      loaded in interface FileHandlerListener
      Overrides:
      loaded in class FileHandlerListenerAdapter
      Parameters:
      handler - the file handler
    • updateFileHandler

      public void updateFileHandler(FileHandler fh)
      Updates the FileHandler. This method is called by the builder when a new configuration instance was created which is associated with a new file handler. It updates the internal file handler reference and performs necessary listener registrations.
      Parameters:
      fh - the new FileHandler (can be null)
    • inLoadOperation

      private boolean inLoadOperation()
      Returns a flag whether a load operation is currently in progress.
      Returns:
      a flag whether a load operation is in progress
    • autoSaveRequired

      private boolean autoSaveRequired(ConfigurationEvent event)
      Checks whether an auto save operation has to be performed based on the passed in event and the current state of this object.
      Parameters:
      event - the configuration change event
      Returns:
      true if a save operation should be performed, false otherwise