org.mmbase.util
Class FileWatcher

java.lang.Object
  extended by org.mmbase.util.FileWatcher
Direct Known Subclasses:
ResourceWatcher.ResourceFileWatcher

public abstract class FileWatcher
extends Object

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.

Since:
MMBase-1.4
Version:
$Id: FileWatcher.java 41036 2010-02-15 22:30:54Z michiel $
Author:
Ceki Gülcü, Eduard Witteveen, Michiel Meeuwissen

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

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 long THREAD_DELAY
The one thread doing al the work also needs a delay.


future

static ScheduledFuture<?> future

fileWatchers

static FileWatcher.FileWatcherRunner fileWatchers
Constructor Detail

FileWatcher

protected FileWatcher()

FileWatcher

protected FileWatcher(boolean c)
Method Detail

scheduleFileWatcherRunner

static void scheduleFileWatcherRunner()
Since:
MMBase-1.9.2

shutdown

public static void shutdown()
Since:
MMBase-1.8

start

public void start()

onChange

public abstract void onChange(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.

Parameters:
delay - The delay in milliseconds

getDelay

public long getDelay()
Since:
MMBase-1.9.2

isRunning

public boolean isRunning()
Since:
MMBase-1.9.2

isContinueAfterChange

public boolean isContinueAfterChange()
Since:
MMBase-1.9.2

getLastCheck

public Date getLastCheck()
Since:
MMBase-1.9.2

add

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

Parameters:
file - The file which has to be monitored..
Throws:
RuntimeException - If file is null

contains

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

Parameters:
file - the file to be checked.
Since:
MMBase-1.6

remove

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


getFiles

public Set<File> getFiles()
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 LinkedHashSet).

Since:
MMBase-1.8.

clear

public void clear()
Removes all files, this watcher will end up watching nothing.

Since:
MMBase-1.8

exit

public void exit()
Stops watching.


toString

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

Overrides:
toString in class Object

getFileWatchers

public static Set<FileWatcher> getFileWatchers()
Since:
MMBase-1.9.2

main

public static void main(String[] args)
Javadoc:


MMBase 2.0-SNAPSHOT - null