|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectclasspathhelper.filechangelistener.FileChangeMonitor
Utility class used to monitor files, or files within directories.
When file changes are detected FileChangedEvent
events are
fired to registered IFileChangeListener
listeners.
The frequency (in time) with which scans for changes occur as well how many scans to wait before reporting changes are both configurable at construction time.
A single instance of FileChangeMonitor
can monitor
several files and directories located in many locations. First
create an instance and register one or more listeners.
FileChangeMonitor monitor = new FileChangeMonitor();
monitor.addFileChangeListener(listener);
Then add the files or directories that should be monitored.
monitor.addSource("/somefile.txt");
monitor.addSource("/somedir");
It is also possible to use standard java.io.FileFilter
and java.io.FilenameFilter
instances when monitoring
directories to restrict which files are viewed.
// add directory only monitoring jar files.
monitor.addSource("/lib", new FileNameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
});
By default a monitored directory will not recursively monitor
sub directories. Passing in true
when a directory
is specified will monitor all sub files and directories.
monitor.addSource("/classes", true);
When using recursive monitoring, FileFilter
and
FilenameFilter
will also restrict which subdirectories
are monitored.
Once a monitor is constructed and sources have been added to the
monitor it needs to be started
. A monitor
can also be temporarily stopped
(and subsequently
restarted).
When a monitor is nolonger needed it should be
cancelled
. A cancelled monitor cannot be
restarted.
This class uses a Timer
for scheduling and
performing the monitoring tasks.
Constructor Summary | |
FileChangeMonitor()
Creates a monitor using the default (60s) scan rate and default (2 scans) quiescent count. |
|
FileChangeMonitor(long theScanRate,
int theQuiescentScans)
Creates a monitor specifying the scan rate and the quiescent scan count. |
Method Summary | |
void |
addFileChangeListener(IFileChangeListener listener)
Adds a listener for file change events. |
void |
addSource(java.io.File file)
Adds a new file or directory to be monitored for changes. |
void |
addSource(java.io.File file,
boolean includeSubDirs)
Adds a new directory for monitoring, while explicitly indicating whether or not subdirectories should also be scanned. |
void |
addSource(java.io.File file,
boolean includeSubDirs,
java.io.FileFilter filter)
Adds a new directory for monitoring, while explicitly indicating whether or not subdirectories should also be scanned. |
void |
addSource(java.io.File file,
boolean includeSubDirs,
java.io.FilenameFilter filter)
Adds a new directory for monitoring, while explicitly indicating whether or not subdirectories should also be scanned. |
void |
addSource(java.io.File file,
java.io.FileFilter filter)
Adds a new directory for monitoring, while providing a filter that will restrict which files are monitored. |
void |
addSource(java.io.File file,
java.io.FilenameFilter filter)
Adds a new directory for monitoring, while providing a filter that will restrict which files are monitored. |
void |
addSource(java.lang.String file)
Adds a new file or directory to be monitored for changes. |
void |
addSource(java.lang.String file,
boolean includeSubDirs)
Adds a new directory for monitoring, while explicitly indicating whether or not subdirectories should also be scanned. |
void |
addSource(java.lang.String file,
boolean includeSubDirs,
java.io.FileFilter filter)
Adds a new directory for monitoring, while explicitly indicating whether or not subdirectories should also be scanned. |
void |
addSource(java.lang.String file,
boolean includeSubDirs,
java.io.FilenameFilter filter)
Adds a new directory for monitoring, while explicitly indicating whether or not subdirectories should also be scanned. |
void |
addSource(java.lang.String file,
java.io.FileFilter filter)
Adds a new directory for monitoring, while providing a filter that will restrict which files are monitored. |
void |
addSource(java.lang.String file,
java.io.FilenameFilter filter)
Adds a new directory for monitoring, while providing a filter that will restrict which files are monitored. |
void |
cancel()
Call used to permanently stop a monitor. |
protected void |
finalize()
|
protected void |
firePendingFileChangedEvents()
Fires all pending events to all registered listeners. |
protected void |
monitor()
Internal method (actually called by the timer thread when its schedule task is running) that performs the actualy file monitoring. |
protected void |
postFileChangedEvent(FileChangedEvent evt)
Method called from the sub monitors to post an event (for later firing to the listeners). |
boolean |
removeFileChangeListener(IFileChangeListener listener)
Removes a previously registered listener. |
void |
startMonitor()
Call used to start the process of monitoring files previously added via addSource() . |
void |
stopMonitor()
Call used to stop a previously started
monitor. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public FileChangeMonitor()
Creates a monitor using the default (60s) scan rate and default (2 scans) quiescent count.
seeFileChangeMonitor(long, int)
for more
information on scan and quiescent scan values.
public FileChangeMonitor(long theScanRate, int theQuiescentScans)
Creates a monitor specifying the scan rate and the quiescent scan count.
theScanRate
- The time in milliseconds to wait
between scanning for changes.theQuiescentScans
- The number of scans (with
no changes detected) before reporting changes. It
is sometimes useful to wait for changes to settle
down before reporting them (e.g. if every file change
triggers an expensive operation).Method Detail |
public void addSource(java.lang.String file)
file
- The path to a file or directory.public void addSource(java.lang.String file, boolean includeSubDirs)
file
- The directory path.includeSubDirs
- flag indicating whether or not to include
recursive subdirectories in monitoring.
java.lang.IllegalArgumentException
- If file does not
specify a directory and includeSubDirs is
true
.public void addSource(java.lang.String file, java.io.FileFilter filter)
file
- The directory path.filter
- The filter used to restrict the files to
monitor.
java.lang.IllegalArgumentException
- If file does not
specify a directory and filter is not null.public void addSource(java.lang.String file, boolean includeSubDirs, java.io.FileFilter filter)
file
- The directory path.includeSubDirs
- flag indicating whether or not to include
recursive subdirectories in monitoring.filter
- The filter used to restrict the files to
monitor.
java.lang.IllegalArgumentException
- If file does not
specify a directory and includeSubDirs is
true
or filter is not null.public void addSource(java.lang.String file, java.io.FilenameFilter filter)
file
- The directory path.filter
- The filter used to restrict the files to
monitor.
java.lang.IllegalArgumentException
- If file does not
specify a directory and filter is not null.public void addSource(java.lang.String file, boolean includeSubDirs, java.io.FilenameFilter filter)
file
- The directory path.includeSubDirs
- flag indicating whether or not to include
recursive subdirectories in monitoring.filter
- The filter used to restrict the files to
monitor.
java.lang.IllegalArgumentException
- If file does not
specify a directory and includeSubDirs is
true
or filter is not null.public void addSource(java.io.File file)
file
- The file or directory.public void addSource(java.io.File file, boolean includeSubDirs)
file
- The directory.includeSubDirs
- flag indicating whether or not to include
recursive subdirectories in monitoring.
java.lang.IllegalArgumentException
- If file does not
specify a directory and includeSubDirs is
true
.public void addSource(java.io.File file, java.io.FileFilter filter)
file
- The directory.filter
- The filter used to restrict the files to
monitor.
java.lang.IllegalArgumentException
- If file does not
specify a directory and filter is not null.public void addSource(java.io.File file, boolean includeSubDirs, java.io.FileFilter filter)
file
- The directory.includeSubDirs
- flag indicating whether or not to include
recursive subdirectories in monitoring.filter
- The filter used to restrict the files to
monitor.
java.lang.IllegalArgumentException
- If file does not
specify a directory and includeSubDirs is
true
or filter is not null.public void addSource(java.io.File file, java.io.FilenameFilter filter)
file
- The directory.filter
- The filter used to restrict the files to
monitor.
java.lang.IllegalArgumentException
- If file does not
specify a directory and filter is not null.public void addSource(java.io.File file, boolean includeSubDirs, java.io.FilenameFilter filter)
file
- The directory.includeSubDirs
- flag indicating whether or not to include
recursive subdirectories in monitoring.filter
- The filter used to restrict the files to
monitor.
java.lang.IllegalArgumentException
- If file does not
specify a directory and includeSubDirs is
true
or filter is not null.public void addFileChangeListener(IFileChangeListener listener)
listener
- The listener.public boolean removeFileChangeListener(IFileChangeListener listener)
listener
- The listener to remove.
true
if the listener was previously
registred and successfully removed. Otherwise false
public void startMonitor()
addSource()
. This method can also be called
to restart the monitor after a call to stopMonitor()
.
public void stopMonitor()
started
monitor.
public void cancel()
stopMonitor()
.
Once a monitor is cancelled it should be discared.
protected void monitor()
protected void postFileChangedEvent(FileChangedEvent evt)
evt
- The event to post.protected void firePendingFileChangedEvents()
protected void finalize() throws java.lang.Throwable
java.lang.Throwable
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |