|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.mmbase.util.FileWatcher
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.
| 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(File file)
Add's a file to be checked... |
void |
clear()
Removes all files, this watcher will end up watching nothing. |
boolean |
contains(File file)
Wether the file is being watched or not. |
boolean |
equals(Object o)
|
void |
exit()
Stops watching. |
Set |
getFiles()
Returns a (modifiable) Set of all files (File object) of this FileWatcher. |
int |
hashCode()
|
static void |
main(String[] args)
|
abstract void |
onChange(File file)
Put here the stuff that has to be executed, when a file has been changed. |
void |
remove(File file)
Remove file from the watch-list |
void |
setDelay(long delay)
Set the delay to observe between each check of the file changes. |
static void |
shutdown()
|
void |
start()
|
String |
toString()
Shows the 'contents' of the filewatcher. |
| Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
public static final long DEFAULT_DELAY
public static long THREAD_DELAY
| Constructor Detail |
protected FileWatcher()
protected FileWatcher(boolean c)
| Method Detail |
public static void shutdown()
public void start()
public abstract void onChange(File file)
file - The file that was changed..public void setDelay(long delay)
public void add(File file)
file - The file which has to be monitored..
RuntimeException - If file is nullpublic boolean contains(File file)
file - the file to be checked.public void remove(File file)
public Set getFiles()
LinkedHashSet).
public void clear()
public void exit()
public String toString()
public boolean equals(Object o)
public int hashCode()
Object.hashCode()public static void main(String[] args)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||