|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Thread org.xins.common.io.FileWatcher
File watcher thread. This thread checks if a file or a set of files changed and if it has, it notifies the listener. The check is performed every n seconds, where n can be configured.
Initially this thread will be a daemon thread. This can be changed by
calling Thread.setDaemon(boolean)
.
Nested Class Summary | |
static interface |
FileWatcher.Listener
Interface for file watcher listeners. |
Field Summary | |
protected String |
_filePaths
The string representation of the files to watch. |
protected long |
_lastModified
Timestamp of the last modification of the file. |
Fields inherited from class java.lang.Thread |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
FileWatcher(String[] files,
int interval,
FileWatcher.Listener listener)
Creates a new FileWatcher for the specified set of files,
with the specified interval. |
|
FileWatcher(String file,
FileWatcher.Listener listener)
Creates a new FileWatcher for the specified file.
|
|
FileWatcher(String file,
int interval,
FileWatcher.Listener listener)
Creates a new FileWatcher for the specified file, with the
specified interval. |
Method Summary | |
void |
check()
Checks if the file changed. |
void |
end()
Stops this thread. |
protected void |
firstCheck()
Performs the first check on the file to determine the date the file was last modified. |
int |
getInterval()
Returns the current interval. |
protected long |
getLastModified()
Gets the time at which the last file was modified. |
void |
run()
Runs this thread. |
void |
setInterval(int newInterval)
Changes the file check interval. |
protected void |
storeFiles(String[] files)
Stores the files in a class variable. |
Methods inherited from class java.lang.Thread |
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected String _filePaths
null
.
protected long _lastModified
-1L
indicates that the file could not be found the last
time this was checked.
Initially this field is -1L
.
Constructor Detail |
public FileWatcher(String file, FileWatcher.Listener listener) throws IllegalArgumentException
FileWatcher
for the specified file.
The interval must be set before the thread can be started.
file
- the name of the file to watch, cannot be null
.listener
- the object to notify on events, cannot be null
.
IllegalArgumentException
- if file == null || listener == null
public FileWatcher(String file, int interval, FileWatcher.Listener listener) throws IllegalArgumentException
FileWatcher
for the specified file, with the
specified interval.
file
- the name of the file to watch, cannot be null
.interval
- the interval in seconds, must be greater than or equal to 0.
if the interval is 0 the interval must be set before the thread can
be started.listener
- the object to notify on events, cannot be null
.
IllegalArgumentException
- if file == null || listener == null || interval < 0
public FileWatcher(String[] files, int interval, FileWatcher.Listener listener) throws IllegalArgumentException
FileWatcher
for the specified set of files,
with the specified interval.
files
- the name of the files to watch, cannot be null
.
It should also have at least one file and none of the file should be null
.interval
- the interval in seconds, must be greater than or equal to 0.
if the interval is 0 the interval must be set before the thread can
be started.listener
- the object to notify on events, cannot be null
.
IllegalArgumentException
- if files == null || listener == null || interval < 0 || files.length < 1
or if one of the file is null
.Method Detail |
protected void storeFiles(String[] files)
files
- the String files to check, cannot be null
.protected void firstCheck()
SecurityException
, then this
exception is logged and ignored.
public void run() throws IllegalStateException
Thread.start()
instead. That method will call this method.
IllegalStateException
- if Thread.currentThread()
!= this
, if the thread
is already running or should stop, or if the interval was not set
yet.public int getInterval()
public void setInterval(int newInterval) throws IllegalArgumentException
newInterval
- the new interval in seconds, must be greater than or equal to 1.
IllegalArgumentException
- if interval < 1
public void end() throws IllegalStateException
IllegalStateException
- if the thread is currently not running or already stopping.public void check()
SecurityException
to be thrown, then
FileWatcher.Listener.securityException(SecurityException)
is called
and the method returns;
FileWatcher.Listener.fileNotFound()
is called and the method returns;
FileWatcher.Listener.fileFound()
is called and the method
returns;
FileWatcher.Listener.fileModified()
is called and the method returns;
FileWatcher.Listener.fileNotModified()
is called and the method
returns.
protected long getLastModified() throws SecurityException
SecurityException
- if one of the file could not be read because of a security issue.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |