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 Summary
FieldsModifier and TypeFieldDescriptionprivate final FileBasedConfigurationBuilder<?>
The associated builder.private FileHandler
Stores the file handler monitored by this listener.private int
A counter to keep track whether a load operation is currently in progress.private final org.apache.commons.logging.Log
The logger. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ofAutoSaveListener
and initializes it with the associated builder. -
Method Summary
Modifier and TypeMethodDescriptionprivate boolean
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
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
Updates theFileHandler
.Methods inherited from class org.apache.commons.configuration2.io.FileHandlerListenerAdapter
locationChanged, saved, saving
-
Field Details
-
log
private final org.apache.commons.logging.Log logThe logger. -
builder
The associated builder. -
handler
Stores the file handler monitored by this listener. -
loading
private int loadingA counter to keep track whether a load operation is currently in progress.
-
-
Constructor Details
-
AutoSaveListener
Creates a new instance ofAutoSaveListener
and initializes it with the associated builder.- Parameters:
bldr
- the associated builder
-
-
Method Details
-
onEvent
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
-
loading
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
-
loaded
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
-
updateFileHandler
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)
-
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
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
-