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 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 FileHandlerStores the file handler monitored by this listener.private intA counter to keep track whether a load operation is currently in progress.private final org.apache.commons.logging.LogThe logger. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ofAutoSaveListenerand initializes it with the associated builder. -
Method Summary
Modifier and TypeMethodDescriptionprivate booleanChecks whether an auto save operation has to be performed based on the passed in event and the current state of this object.private booleanReturns a flag whether a load operation is currently in progress.voidloaded(FileHandler handler) Notification that the associated file has been loaded.voidloading(FileHandler handler) Notification that the associated file is about to be loaded.voidonEvent(ConfigurationEvent event) Notifies this event listener about the arrival of a new event.voidUpdates 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 ofAutoSaveListenerand initializes it with the associated builder.- Parameters:
bldr- the associated builder
-
-
Method Details
-
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
-
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
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:
loadedin interfaceFileHandlerListener- Overrides:
loadedin classFileHandlerListenerAdapter- Parameters:
handler- the file handler
-
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:
loadingin interfaceFileHandlerListener- Overrides:
loadingin classFileHandlerListenerAdapter- Parameters:
handler- the file handler
-
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 theEventTypeclass 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:
onEventin interfaceEventListener<ConfigurationEvent>- Parameters:
event- the event
-
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)
-