Class FileHandlerReloadingDetector
- java.lang.Object
-
- org.apache.commons.configuration2.reloading.FileHandlerReloadingDetector
-
- All Implemented Interfaces:
ReloadingDetector
- Direct Known Subclasses:
VFSFileHandlerReloadingDetector
public class FileHandlerReloadingDetector extends java.lang.Object implements ReloadingDetector
A specialized implementation of
ReloadingDetector
which monitors a file specified by aFileHandler
.An instance of this class is passed a
FileHandler
at construction time. Each time theisReloadingRequired()
method is called, it checks whether theFileHandler
points to a valid location. If this is the case, the file's last modification time is obtained and compared with the last stored time. If it has changed, a reload operation should be performed.Because file I/O may be expensive it is possible to configure a refresh delay as a time in milliseconds. This is the minimum interval between two checks. If the
isReloadingRequired()
method is called in shorter intervals, it does not perform a check, but directly returns false.To initialize an instance either
isReloadingRequired()
orreloadingPerformed()
can be called. The first call ofisReloadingRequired
does not perform a check, but obtains the initial modification date of the monitored file.reloadingPerformed()
always obtains the file's modification date and stores it internally.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_REFRESH_DELAY_MILLIS
Constant for the default refresh delay.private FileHandler
fileHandler
The associated file handler.private static java.lang.String
JAR_PROTOCOL
Constant for the jar URL protocol.private long
lastCheckedMillis
The last time the file was checked for changes.private long
lastModifiedMillis
The last time the configuration file was modified.private long
refreshDelayMillis
The refresh delay.
-
Constructor Summary
Constructors Constructor Description FileHandlerReloadingDetector()
Creates a new instance ofFileHandlerReloadingDetector
with an uninitializedFileHandler
object.FileHandlerReloadingDetector(FileHandler handler)
Creates a new instance ofFileHandlerReloadingDetector
and initializes it with theFileHandler
to monitor and a default refresh delay.FileHandlerReloadingDetector(FileHandler handler, long refreshDelayMillis)
Creates a new instance ofFileHandlerReloadingDetector
and initializes it with theFileHandler
to monitor and the refresh delay.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.io.File
fileFromURL(java.net.URL url)
Helper method for transforming a URL into a file object.private java.io.File
getExistingFile()
Gets the monitoredFile
or null if it does not exist.protected java.io.File
getFile()
Gets theFile
object which is monitored by this object.FileHandler
getFileHandler()
Gets theFileHandler
associated with this object.protected long
getLastModificationDate()
Gets the date of the last modification of the monitored file.long
getRefreshDelay()
Gets the refresh delay.boolean
isReloadingRequired()
Checks whether all criteria for a reload operation are fulfilled.void
refresh()
Tells this implementation that the internally stored state should be refreshed.void
reloadingPerformed()
Notifies this object that a reload operation has been performed.protected void
updateLastModified(long timeMillis)
Updates the last modification date of the monitored file.
-
-
-
Field Detail
-
JAR_PROTOCOL
private static final java.lang.String JAR_PROTOCOL
Constant for the jar URL protocol.- See Also:
- Constant Field Values
-
DEFAULT_REFRESH_DELAY_MILLIS
private static final int DEFAULT_REFRESH_DELAY_MILLIS
Constant for the default refresh delay.- See Also:
- Constant Field Values
-
fileHandler
private final FileHandler fileHandler
The associated file handler.
-
refreshDelayMillis
private final long refreshDelayMillis
The refresh delay.
-
lastModifiedMillis
private long lastModifiedMillis
The last time the configuration file was modified.
-
lastCheckedMillis
private long lastCheckedMillis
The last time the file was checked for changes.
-
-
Constructor Detail
-
FileHandlerReloadingDetector
public FileHandlerReloadingDetector()
Creates a new instance ofFileHandlerReloadingDetector
with an uninitializedFileHandler
object. The file to be monitored has to be set later by manipulating the handler object returned bygetFileHandler()
.
-
FileHandlerReloadingDetector
public FileHandlerReloadingDetector(FileHandler handler)
Creates a new instance ofFileHandlerReloadingDetector
and initializes it with theFileHandler
to monitor and a default refresh delay.- Parameters:
handler
- theFileHandler
associated with this detector (can be null)
-
FileHandlerReloadingDetector
public FileHandlerReloadingDetector(FileHandler handler, long refreshDelayMillis)
Creates a new instance ofFileHandlerReloadingDetector
and initializes it with theFileHandler
to monitor and the refresh delay. The handler is directly used, no copy is created. So it is possible to change the location monitored by manipulating theFileHandler
object.- Parameters:
handler
- theFileHandler
associated with this detector (can be null)refreshDelayMillis
- the refresh delay; a value of 0 means that a check is performed in all cases
-
-
Method Detail
-
fileFromURL
private static java.io.File fileFromURL(java.net.URL url)
Helper method for transforming a URL into a file object. This method handles file: and jar: URLs.- Parameters:
url
- the URL to be converted- Returns:
- the resulting file or null
-
getExistingFile
private java.io.File getExistingFile()
Gets the monitoredFile
or null if it does not exist.- Returns:
- the monitored
File
or null
-
getFile
protected java.io.File getFile()
Gets theFile
object which is monitored by this object. This method is called every time the file's last modification time is needed. If it returns null, no check is performed. This base implementation obtains theFile
from the associatedFileHandler
. It can also deal with URLs to jar files.- Returns:
- the
File
to be monitored (can be null)
-
getFileHandler
public FileHandler getFileHandler()
Gets theFileHandler
associated with this object. The underlying handler is directly returned, so changing its location also changes the file monitored by this detector.- Returns:
- the associated
FileHandler
-
getLastModificationDate
protected long getLastModificationDate()
Gets the date of the last modification of the monitored file. A return value of 0 indicates, that the monitored file does not exist.- Returns:
- the last modification date in milliseconds.
-
getRefreshDelay
public long getRefreshDelay()
Gets the refresh delay. This is a time in milliseconds. TheisReloadingRequired()
method first checks whether the time since the previous check is more than this value in the past. Otherwise, no check is performed. This is a means to limit file I/O caused by this class.- Returns:
- the refresh delay used by this object
-
isReloadingRequired
public boolean isReloadingRequired()
Checks whether all criteria for a reload operation are fulfilled. This method is called by external components to find out when reloading should take place. This implementation checks whether the associatedFileHandler
points to a valid file and whether the last modification time of this time has changed since the last check. The refresh delay is taken into account, too; a check is only performed if at least this time has passed since the last check.- Specified by:
isReloadingRequired
in interfaceReloadingDetector
- Returns:
- true if a reload operation should be performed, false otherwise
-
refresh
public void refresh()
Tells this implementation that the internally stored state should be refreshed. This method is intended to be called after the creation of an instance.
-
reloadingPerformed
public void reloadingPerformed()
Notifies this object that a reload operation has been performed. This method is called afterreloadingRequired()
has returned true. It can be used to reset internal state in order to detect the next reload operation. This implementation updates the internally stored last modification date with the current modification date of the monitored file. So the next change is detected when this file is changed again.- Specified by:
reloadingPerformed
in interfaceReloadingDetector
-
updateLastModified
protected void updateLastModified(long timeMillis)
Updates the last modification date of the monitored file. The need for a reload is detected only if the file's modification date is different from this value.- Parameters:
timeMillis
- the new last modification date
-
-