public class FileHandlerReloadingDetector extends Object implements ReloadingDetector
A specialized implementation of ReloadingDetector
which monitors a file specified by a FileHandler
.
An instance of this class is passed a FileHandler
at construction time. Each time the
isReloadingRequired()
method is called, it checks whether the FileHandler
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()
or reloadingPerformed()
can be called. The
first call of isReloadingRequired
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.
Constructor and Description |
---|
FileHandlerReloadingDetector()
Creates a new instance of
FileHandlerReloadingDetector with an uninitialized FileHandler object. |
FileHandlerReloadingDetector(FileHandler handler)
Creates a new instance of
FileHandlerReloadingDetector and initializes it with the FileHandler to
monitor and a default refresh delay. |
FileHandlerReloadingDetector(FileHandler handler,
long refreshDelayMillis)
Creates a new instance of
FileHandlerReloadingDetector and initializes it with the FileHandler to
monitor and the refresh delay. |
Modifier and Type | Method and Description |
---|---|
protected File |
getFile()
Returns the
File object which is monitored by this object. |
FileHandler |
getFileHandler()
Returns the
FileHandler associated with this object. |
protected long |
getLastModificationDate()
Returns the date of the last modification of the monitored file.
|
long |
getRefreshDelay()
Returns 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.
|
public FileHandlerReloadingDetector(FileHandler handler, long refreshDelayMillis)
FileHandlerReloadingDetector
and initializes it with the FileHandler
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 the FileHandler
object.handler
- the FileHandler
associated with this detector (can be null)refreshDelayMillis
- the refresh delay; a value of 0 means that a check is performed in all casespublic FileHandlerReloadingDetector(FileHandler handler)
FileHandlerReloadingDetector
and initializes it with the FileHandler
to
monitor and a default refresh delay.handler
- the FileHandler
associated with this detector (can be null)public FileHandlerReloadingDetector()
FileHandlerReloadingDetector
with an uninitialized FileHandler
object. The
file to be monitored has to be set later by manipulating the handler object returned by getFileHandler()
.public FileHandler getFileHandler()
FileHandler
associated with this object. The underlying handler is directly returned, so changing
its location also changes the file monitored by this detector.FileHandler
public long getRefreshDelay()
isReloadingRequired()
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.public boolean isReloadingRequired()
FileHandler
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.isReloadingRequired
in interface ReloadingDetector
public void reloadingPerformed()
reloadingRequired()
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.reloadingPerformed
in interface ReloadingDetector
public void refresh()
protected long getLastModificationDate()
protected void updateLastModified(long timeMillis)
timeMillis
- the new last modification dateprotected File getFile()
File
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 the
File
from the associated FileHandler
. It can also deal with URLs to jar files.File
to be monitored (can be null)Copyright © 2001–2022 The Apache Software Foundation. All rights reserved.