org.xins.common.io
Class FileWatcher

java.lang.Object
  extended byjava.lang.Thread
      extended byorg.xins.common.io.FileWatcher
All Implemented Interfaces:
Runnable

public final class FileWatcher
extends Thread

File watcher thread. This thread checks if a file 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).

Since:
XINS 1.0.0
Version:
$Revision: 1.31 $ $Date: 2006/08/28 09:12:34 $
Author:
Ernst de Haan

Nested Class Summary
static interface FileWatcher.Listener
          Interface for file watcher listeners.
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
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.
 int getInterval()
          Returns the current interval.
 void run()
          Runs this thread.
 void setInterval(int newInterval)
          Changes the file check interval.
 
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
 

Constructor Detail

FileWatcher

public FileWatcher(String file,
                   int interval,
                   FileWatcher.Listener listener)
            throws IllegalArgumentException
Creates a new FileWatcher for the specified file, with the specified interval.

Parameters:
file - the name of the file to watch, cannot be null.
interval - the interval in seconds, must be greater than or equal to 1.
listener - the object to notify on events, cannot be null.
Throws:
IllegalArgumentException - if file == null || listener == null || interval < 1

FileWatcher

public FileWatcher(String file,
                   FileWatcher.Listener listener)
            throws IllegalArgumentException
Creates a new FileWatcher for the specified file.

The interval must be set before the thread can be started.

Parameters:
file - the name of the file to watch, cannot be null.
listener - the object to notify on events, cannot be null.
Throws:
IllegalArgumentException - if file == null || listener == null
Since:
XINS 1.2.0
Method Detail

run

public void run()
         throws IllegalStateException
Runs this thread. This method should not be called directly, call Thread.start() instead. That method will call this method.

Throws:
IllegalStateException - if Thread.currentThread() != this, if the thread is already running or should stop, or if the interval was not set yet.

getInterval

public int getInterval()
Returns the current interval.

Returns:
interval the current interval in seconds, always greater than or equal to 1, except if the interval is not initialized yet, in which case 0 is returned.

setInterval

public void setInterval(int newInterval)
                 throws IllegalArgumentException
Changes the file check interval.

Parameters:
newInterval - the new interval in seconds, must be greater than or equal to 1.
Throws:
IllegalArgumentException - if interval < 1

end

public void end()
         throws IllegalStateException
Stops this thread.

Throws:
IllegalStateException - if the thread is currently not running or already stopping.

check

public void check()
Checks if the file changed. The following algorithm is used:

Since:
XINS 1.2.0


See http://www.xins.org/.