org.mmbase.util
Class FileWatcher

java.lang.Object
  extended byorg.mmbase.util.FileWatcher

public abstract class FileWatcher
extends java.lang.Object

This will run as a thread after it has been started. It will check every interval if one of it's files has been changed. When one of them has been changed, the OnChange method will be called, with the file that was changed. After that the thread will stop. To stop a running thread, call the method exit(); Example: class FooFileWatcher extends FileWatcher { public FooFileWatcher() { super(true); // true: keep reading. } public void onChange(File file) { System.out.println(file.getAbsolutePath()); } } // create new instance FooFileWatcher watcher = new FooFileWatcher(); // set inteval watcher.setDelay(10 * 1000); watcher.add(new File("/tmp/foo.txt")); watcher.start(); watcher.add(new File("/tmp/foo.txt")); wait(100*1000); watcher.exit(); Thanks to contributions by Mathias Bogaert. Licence was changed from apache 1.1 to Mozilla.

Since:
MMBase-1.4
Version:
$Id: FileWatcher.java,v 1.21 2004/02/16 15:31:07 keesj Exp $
Author:
Ceki Gülcü, Eduard Witteveen, Michiel Meeuwissen

Field Summary
static long DEFAULT_DELAY
          The default delay between every file modification check, set to 60 seconds.
static long THREAD_DELAY
          The one thread doing al the work also needs a delay.
 
Constructor Summary
protected FileWatcher()
           
protected FileWatcher(boolean c)
           
 
Method Summary
 void add(java.io.File file)
          Add's a file to be checked...
 boolean contains(java.io.File file)
          Wether the file is being watched or not.
 boolean equals(java.lang.Object o)
           
 void exit()
           
static void main(java.lang.String[] args)
           
protected abstract  void onChange(java.io.File file)
          Put here the stuff that has to be executed, when a file has been changed.
 void remove(java.io.File file)
          Remove file from the watch-list
 void setDelay(long delay)
          Set the delay to observe between each check of the file changes.
 void start()
           
 java.lang.String toString()
          Shows the 'contents' of the filewatcher.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_DELAY

public static final long DEFAULT_DELAY
The default delay between every file modification check, set to 60 seconds.

See Also:
Constant Field Values

THREAD_DELAY

public static final long THREAD_DELAY
The one thread doing al the work also needs a delay.

See Also:
Constant Field Values
Constructor Detail

FileWatcher

protected FileWatcher()

FileWatcher

protected FileWatcher(boolean c)
Method Detail

start

public void start()

onChange

protected abstract void onChange(java.io.File file)
Put here the stuff that has to be executed, when a file has been changed.

Parameters:
file - The file that was changed..

setDelay

public void setDelay(long delay)
Set the delay to observe between each check of the file changes.


add

public void add(java.io.File file)
Add's a file to be checked...

Parameters:
file - The file which has to be monitored..
Throws:
java.lang.RuntimeException - If file is null or does not exist.

contains

public boolean contains(java.io.File file)
Wether the file is being watched or not.

Since:
MMBase-1.6

remove

public void remove(java.io.File file)
Remove file from the watch-list


exit

public void exit()

toString

public java.lang.String toString()
Shows the 'contents' of the filewatcher. It shows a list of files/last modified timestamps.


equals

public boolean equals(java.lang.Object o)

main

public static void main(java.lang.String[] args)
Warning: No Java Documentation Available.


MMBase build 1.7.1.20041002