| java.lang.Object org.mmbase.util.FileWatcher
FileWatcher | abstract public class FileWatcher (Code) | | Original javadoc.
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.
MMBase javadoc
This code was originally borrowed from the log4j project (as can still be seen from the authors),
it was however quite heavily adapted. You are probably better of using a
ResourceWatcher (since MMBase 1.8), because that does not watch only files. Its implementation does of course use
FileWatcher, for the 'file' part of the watching.
author: Ceki Gülcü author: Eduard Witteveen author: Michiel Meeuwissen since: MMBase-1.4 version: $Id: FileWatcher.java,v 1.48 2007/06/21 15:50:22 nklasens Exp $ |
Field Summary | |
final public static long | DEFAULT_DELAY The default delay between every file modification check, set to 60
seconds. | public static long | THREAD_DELAY The one thread doing al the work also needs a delay. |
Method Summary | |
public void | add(File file) Add's a file to be checked... | public void | clear() Removes all files, this watcher will end up watching nothing. | public boolean | contains(File file) Wether the file is being watched or not. | public boolean | equals(Object o) | public void | exit() Stops watching. | public Set<File> | getFiles() Returns a (modifiable) Set of all files (File object) of this FileWatcher. | public int | hashCode() | public static void | main(String[] args) | abstract public void | onChange(File file) Put here the stuff that has to be executed, when a file has been changed. | public void | remove(File file) | public void | setDelay(long delay) Set the delay to observe between each check of the file changes. | public static void | shutdown() | public void | start() | public String | toString() Shows the 'contents' of the filewatcher. |
DEFAULT_DELAY | final public static long DEFAULT_DELAY(Code) | | The default delay between every file modification check, set to 60
seconds.
|
THREAD_DELAY | public static long THREAD_DELAY(Code) | | The one thread doing al the work also needs a delay.
|
FileWatcher | protected FileWatcher()(Code) | | |
FileWatcher | protected FileWatcher(boolean c)(Code) | | |
add | public void add(File file)(Code) | | Add's a file to be checked...
Parameters: file - The file which has to be monitored.. throws: RuntimeException - If file is null |
clear | public void clear()(Code) | | Removes all files, this watcher will end up watching nothing.
since: MMBase-1.8 |
contains | public boolean contains(File file)(Code) | | Wether the file is being watched or not.
Parameters: file - the file to be checked. since: MMBase-1.6 |
exit | public void exit()(Code) | | Stops watching.
|
getFiles | public Set<File> getFiles()(Code) | | Returns a (modifiable) Set of all files (File object) of this FileWatcher. If you change it, you change the
FileWatcher. The order of the Set is predictable (backed by a
java.util.LinkedHashSet ).
since: MMBase-1.8. |
onChange | abstract public void onChange(File file)(Code) | | Put here the stuff that has to be executed, when a file has been changed.
Parameters: file - The file that was changed.. |
remove | public void remove(File file)(Code) | | Remove file from the watch-list
|
setDelay | public void setDelay(long delay)(Code) | | Set the delay to observe between each check of the file changes.
Parameters: delay - The delay in milliseconds |
shutdown | public static void shutdown()(Code) | | since: MMBase-1.8 |
start | public void start()(Code) | | |
toString | public String toString()(Code) | | Shows the 'contents' of the filewatcher. It shows a list of files/last modified timestamps.
|
|
|