Class AutoSaveListener
- java.lang.Object
-
- org.apache.commons.configuration2.io.FileHandlerListenerAdapter
-
- org.apache.commons.configuration2.builder.AutoSaveListener
-
- All Implemented Interfaces:
EventListener<ConfigurationEvent>
,FileHandlerListener
final 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 atConfiguration
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 Summary
Fields Modifier and Type Field Description private FileBasedConfigurationBuilder<?>
builder
The associated builder.private FileHandler
handler
Stores the file handler monitored by this listener.private int
loading
A counter to keep track whether a load operation is currently in progress.private org.apache.commons.logging.Log
log
The logger.
-
Constructor Summary
Constructors Constructor Description AutoSaveListener(FileBasedConfigurationBuilder<?> bldr)
Creates a new instance ofAutoSaveListener
and initializes it with the associated builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.private boolean
inLoadOperation()
Returns a flag whether a load operation is currently in progress.void
loaded(FileHandler handler)
Notification that the associated file has been loaded.void
loading(FileHandler handler)
Notification that the associated file is about to be loaded.void
onEvent(ConfigurationEvent event)
Notifies this event listener about the arrival of a new event.void
updateFileHandler(FileHandler fh)
Updates theFileHandler
.-
Methods inherited from class org.apache.commons.configuration2.io.FileHandlerListenerAdapter
locationChanged, saved, saving
-
-
-
-
Field Detail
-
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 Detail
-
AutoSaveListener
public AutoSaveListener(FileBasedConfigurationBuilder<?> bldr)
Creates a new instance ofAutoSaveListener
and initializes it with the associated builder.- Parameters:
bldr
- the associated builder
-
-
Method Detail
-
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
-
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
-
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 interfaceFileHandlerListener
- Overrides:
loaded
in classFileHandlerListenerAdapter
- Parameters:
handler
- the file handler
-
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 interfaceFileHandlerListener
- Overrides:
loading
in classFileHandlerListenerAdapter
- Parameters:
handler
- the file handler
-
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 anEventType
. This event type acts as a filter; all events matched by the filter are passed to the listener. The type parameters defined by theEventType
class and this interface guarantee that the events delivered to the handler are compatible with the concrete method signature ofonEvent()
. 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 interfaceEventListener<ConfigurationEvent>
- Parameters:
event
- the event
-
updateFileHandler
public void updateFileHandler(FileHandler fh)
Updates theFileHandler
. 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 newFileHandler
(can be null)
-
-