|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.mmbase.util.FileWatcher
public abstract class FileWatcher
Original javadoc.
This will schedule a job 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.
License 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.
| Nested Class Summary | |
|---|---|
(package private) static class |
FileWatcher.FileWatcherRunner
The one thread to handle all FileWatchers. |
| Field Summary | |
|---|---|
static long |
DEFAULT_DELAY
The default delay between every file modification check, set to 60 seconds. |
(package private) static FileWatcher.FileWatcherRunner |
fileWatchers
|
(package private) static ScheduledFuture<?> |
future
|
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. |
void |
exit()
Stops watching. |
long |
getDelay()
|
Set<File> |
getFiles()
Returns a (modifiable) Set of all files (File object) of this FileWatcher. |
static Set<FileWatcher> |
getFileWatchers()
|
Date |
getLastCheck()
|
boolean |
isContinueAfterChange()
|
boolean |
isRunning()
|
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 |
(package private) static void |
scheduleFileWatcherRunner()
|
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, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final long DEFAULT_DELAY
public static long THREAD_DELAY
static ScheduledFuture<?> future
static FileWatcher.FileWatcherRunner fileWatchers
| Constructor Detail |
|---|
protected FileWatcher()
protected FileWatcher(boolean c)
| Method Detail |
|---|
static void scheduleFileWatcherRunner()
public static void shutdown()
public void start()
public abstract void onChange(File file)
file - The file that was changed..public void setDelay(long delay)
delay - The delay in millisecondspublic long getDelay()
public boolean isRunning()
public boolean isContinueAfterChange()
public Date getLastCheck()
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<File> getFiles()
LinkedHashSet).
public void clear()
public void exit()
public String toString()
toString in class Objectpublic static Set<FileWatcher> getFileWatchers()
public static void main(String[] args)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||