org.apache.commons.vfs.impl
Class DefaultFileMonitor

java.lang.Object
  extended by org.apache.commons.vfs.impl.DefaultFileMonitor
All Implemented Interfaces:
java.lang.Runnable, FileMonitor

public class DefaultFileMonitor
extends java.lang.Object
implements java.lang.Runnable, FileMonitor

A polling FileMonitor implementation.

The DefaultFileMonitor is a Thread based polling file system monitor with a 1 second delay.

Design:

There is a Map of monitors known as FileMonitorAgents. With the thread running, each FileMonitorAgent object is asked to "check" on the file it is responsible for. To do this check, the cache is cleared.

With each file delete, the FileMonitorAgent of the parent is asked to re-build its list of children, so that they can be accurately checked when there are new children.
New files are detected during each "check" as each file does a check for new children. If new children are found, create events are fired recursively if recursive descent is enabled.

For performance reasons, added a delay that increases as the number of files monitored increases. The default is a delay of 1 second for every 1000 files processed.


Example usage:

 FileSystemManager fsManager = VFS.getManager();
 FileObject listendir = fsManager.resolveFile("/home/username/monitored/");
 

DefaultFileMonitor fm = new DefaultFileMonitor(new CustomFileListener()); fm.setRecursive(true); fm.addFile(listendir); fm.start();

(where CustomFileListener is a class that implements the FileListener interface.)

Version:
$Revision: 484943 $ $Date: 2006-12-09 08:42:06 +0100 (Sa, 09 Dez 2006) $
Author:
Christopher Ottley

Nested Class Summary
private static class DefaultFileMonitor.FileMonitorAgent
          File monitor agent.
 
Field Summary
private  java.util.Stack addStack
          File objects to be added to the monitor map.
private  int checksPerRun
          Set the number of files to check until a delay will be inserted
private  long delay
          Set the delay between checks
private  java.util.Stack deleteStack
          File objects to be removed from the monitor map.
private  FileListener listener
          A listener object that if set, is notified on file creation and deletion.
private static org.apache.commons.logging.Log log
           
private  java.util.Map monitorMap
          Map from FileName to FileObject being monitored.
private  java.lang.Thread monitorThread
          The low priority thread used for checking the files being monitored.
private  boolean recursive
          A flag used to determine if adding files to be monitored should be recursive.
private  boolean shouldRun
          A flag used to determine if the monitor thread should be running.
 
Constructor Summary
DefaultFileMonitor(FileListener listener)
           
 
Method Summary
private  void _addFile(FileObject file)
          Adds a file to be monitored.
 void addFile(FileObject file)
          Adds a file to be monitored.
 int getChecksPerRun()
          get the number of files to check per run
 long getDelay()
          Get the delay between runs
(package private)  FileListener getFileListener()
          Access method to get the current FileListener object notified when there are changes with the files added.
 boolean isRecursive()
          Access method to get the recursive setting when adding files for monitoring.
protected  void queueAddFile(FileObject file)
          Queues a file for addition to be monitored.
protected  void queueRemoveFile(FileObject file)
          Queues a file for removal from being monitored.
 void removeFile(FileObject file)
          Removes a file from being monitored.
 void run()
          Asks the agent for each file being monitored to check its file for changes.
 void setChecksPerRun(int checksPerRun)
          set the number of files to check per run.
 void setDelay(long delay)
          Set the delay between runs
 void setRecursive(boolean newRecursive)
          Access method to set the recursive setting when adding files for monitoring.
 void start()
          Starts monitoring the files that have been added.
 void stop()
          Stops monitoring the files that have been added.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log

monitorMap

private final java.util.Map monitorMap
Map from FileName to FileObject being monitored.


monitorThread

private java.lang.Thread monitorThread
The low priority thread used for checking the files being monitored.


deleteStack

private java.util.Stack deleteStack
File objects to be removed from the monitor map.


addStack

private java.util.Stack addStack
File objects to be added to the monitor map.


shouldRun

private boolean shouldRun
A flag used to determine if the monitor thread should be running.


recursive

private boolean recursive
A flag used to determine if adding files to be monitored should be recursive.


delay

private long delay
Set the delay between checks


checksPerRun

private int checksPerRun
Set the number of files to check until a delay will be inserted


listener

private final FileListener listener
A listener object that if set, is notified on file creation and deletion.

Constructor Detail

DefaultFileMonitor

public DefaultFileMonitor(FileListener listener)
Method Detail

isRecursive

public boolean isRecursive()
Access method to get the recursive setting when adding files for monitoring.


setRecursive

public void setRecursive(boolean newRecursive)
Access method to set the recursive setting when adding files for monitoring.


getFileListener

FileListener getFileListener()
Access method to get the current FileListener object notified when there are changes with the files added.


addFile

public void addFile(FileObject file)
Adds a file to be monitored.

Specified by:
addFile in interface FileMonitor

_addFile

private void _addFile(FileObject file)
Adds a file to be monitored.


removeFile

public void removeFile(FileObject file)
Removes a file from being monitored.

Specified by:
removeFile in interface FileMonitor

queueRemoveFile

protected void queueRemoveFile(FileObject file)
Queues a file for removal from being monitored.


getDelay

public long getDelay()
Get the delay between runs


setDelay

public void setDelay(long delay)
Set the delay between runs


getChecksPerRun

public int getChecksPerRun()
get the number of files to check per run


setChecksPerRun

public void setChecksPerRun(int checksPerRun)
set the number of files to check per run. a additional delay will be added if there are more files to check

Parameters:
checksPerRun - a value less than 1 will disable this feature

queueAddFile

protected void queueAddFile(FileObject file)
Queues a file for addition to be monitored.


start

public void start()
Starts monitoring the files that have been added.


stop

public void stop()
Stops monitoring the files that have been added.


run

public void run()
Asks the agent for each file being monitored to check its file for changes.

Specified by:
run in interface java.lang.Runnable