com.springsource.util.io
Class FileSystemChecker

java.lang.Object
  extended by com.springsource.util.io.FileSystemChecker

public final class FileSystemChecker
extends java.lang.Object

Checks a directory on the file system for modifications. Maintains a known state of the files to determine if the changes are new files, modified files or deleted files.

Modification notifications (FileSystemEvents) are published to a set of configured FileSystemListeners. New listeners can be safely added at runtime.

By default, all files in the directory are monitored. Files can be excluded from monitoring using a regex pattern.

Concurrent Semantics
Thread-safe.

Since:
1.0
Author:
Rob Harrop

Nested Class Summary
private static class FileSystemChecker.MonitorRecord
           
 
Field Summary
private  java.io.File checkDir
           
private  java.lang.Object checkLock
           
private  java.util.Map<java.lang.String,java.lang.Long> fileState
          The files we know about -- with their last modified date (as a Long) are in fileState.
Files that are changing or are new are monitored in monitorRecords.
private  java.io.FilenameFilter includeFilter
           
private  java.util.List<FileSystemListener> listeners
           
private  java.util.Map<java.lang.String,FileSystemChecker.MonitorRecord> monitorRecords
           
 
Constructor Summary
FileSystemChecker(java.io.File checkDir)
          Creates a new FileSystemChecker.
FileSystemChecker(java.io.File checkDir, java.lang.String excludePattern)
          Creates a new FileSystemChecker.
 
Method Summary
 void addListener(FileSystemListener listener)
          Add a new FileSystemListener to this FileSystemChecker.
 void check()
          Instructs this FileSystemChecker to check the configured directory and notifies any registered listeners of changes to the directory files.
private  boolean isKnown(java.io.File file)
          Is file known to us? (In fileState.)
private  java.lang.String key(java.io.File file)
          Gets the record key for the supplied File.
private  java.lang.Long knownLastModified(java.io.File file)
          Gets the recorded last modified timestamp for the supplied File.
private  java.io.File[] listCurrentDirFiles()
          Lists the Files currently in the check directory.
private  void notifyListeners(java.lang.String file, FileSystemEvent event)
           
private  void populateInitialState()
          Initialises known files (fileState) from the check directory and starts monitoring them.
private  void setKnownFileState(java.io.File file)
          Sets the state of the supplied File into our known files map (fileState).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

checkDir

private final java.io.File checkDir

checkLock

private final java.lang.Object checkLock

fileState

private final java.util.Map<java.lang.String,java.lang.Long> fileState
The files we know about -- with their last modified date (as a Long) are in fileState.
Files that are changing or are new are monitored in monitorRecords.

Invariant: all monitored files are in fileState.
As soon as a file is notified (to listeners) it is no longer monitored. Thus files in fileState and not monitored can be assumed to have been notified about already.


monitorRecords

private final java.util.Map<java.lang.String,FileSystemChecker.MonitorRecord> monitorRecords

listeners

private final java.util.List<FileSystemListener> listeners

includeFilter

private final java.io.FilenameFilter includeFilter
Constructor Detail

FileSystemChecker

public FileSystemChecker(java.io.File checkDir)
Creates a new FileSystemChecker. Identifies changes on all files in checkDir.

Parameters:
checkDir - the directory to check.

FileSystemChecker

public FileSystemChecker(java.io.File checkDir,
                         java.lang.String excludePattern)
Creates a new FileSystemChecker. Identifies changes to on all files, except thus that match excludePattern.

Parameters:
checkDir - the directory to check -- File must exist and be a directory
excludePattern - regular expression for files to exclude.
Method Detail

addListener

public void addListener(FileSystemListener listener)
Add a new FileSystemListener to this FileSystemChecker.

Parameters:
listener - the listener to add.

check

public void check()
Instructs this FileSystemChecker to check the configured directory and notifies any registered listeners of changes to the directory files.


notifyListeners

private void notifyListeners(java.lang.String file,
                             FileSystemEvent event)

populateInitialState

private void populateInitialState()
Initialises known files (fileState) from the check directory and starts monitoring them.


listCurrentDirFiles

private java.io.File[] listCurrentDirFiles()
Lists the Files currently in the check directory.

Returns:
the Files that are in the check directory.

setKnownFileState

private void setKnownFileState(java.io.File file)
Sets the state of the supplied File into our known files map (fileState).

Parameters:
the - File to record state for.

knownLastModified

private java.lang.Long knownLastModified(java.io.File file)
Gets the recorded last modified timestamp for the supplied File.

Parameters:
file - the File to check for.
Returns:
the last modified timestamp, or null if no timestamp is recorded.

isKnown

private boolean isKnown(java.io.File file)
Is file known to us? (In fileState.)

Parameters:
file - the File to check for.
Returns:
true if in the map (domain), false otherwise.

key

private java.lang.String key(java.io.File file)
Gets the record key for the supplied File.

Parameters:
file - the File to get the key for.
Returns:
the record key.