com.teamdev.filewatch
Class FileWatcher

java.lang.Object
  extended by com.teamdev.filewatch.FileWatcher
Direct Known Subclasses:
FATWatcher, FSEventWatcher, INotifyWatcher, KEventWatcher, NTFSWatcher

public abstract class FileWatcher
extends java.lang.Object

Provides the functionality for monitoring file events in a specified folder.


Field Summary
static java.lang.String FAKE_RENAMES_OPTION
           
static java.lang.String MOVE_EVENTS_OPTION
           
protected static java.lang.String WATCHING_FOLDER_REMOVED
           
 
Constructor Summary
protected FileWatcher(java.io.File folder)
          Creates a new watcher for the specified folder.
 
Method Summary
protected  boolean accept(FileEvent event)
          Verifies if the specified event should be filtered out or not using the associated FileEventFilter.
 void addFileEventsListener(FileEventsListener listener)
          Adds file events listener to this watcher.
 void addListener(FileEventsListener listener)
          Deprecated. use addFileEventsListener(com.teamdev.filewatch.FileEventsListener) instead
 void addListener(WatcherEventListener listener)
          Deprecated. use addWatcherListener(WatcherEventListener) instead
 void addWatcherListener(WatcherEventListener listener)
          Add watcher events listener.
static FileWatcher create(java.io.File folder)
          Factory method that creates a most efficient file system watcher depending on a current operating system and file system type of the specified folder.
 FileEventFilter getFilter()
          Returns a file event filter.
 java.io.File getFolder()
          Returns the currently watching directory.
 java.util.Set<WatchingAttributes> getOptions()
          Returns set of watching options.
 boolean isRunning()
          Returns state of the watcher.
protected abstract  java.util.List<FileEvent> readEvents()
          Reads next portion of file system events.
protected abstract  void release()
          Releases all native resources which are associated with a watcher instance.
 void removeFileEventsListener(FileEventsListener listener)
          Removes file events listener from this watcher.
 void removeListener(FileEventsListener listener)
          Deprecated. use removeFileEventsListener(FileEventsListener) instead
 void removeListener(WatcherEventListener listener)
          Deprecated. use removeWatcherListener(WatcherEventListener) instead
 void removeWatcherListener(WatcherEventListener listener)
          Removes file events listener from this watcher.
 void resetOptions()
          Clears options and initializes with all the attributes from WatchingAttributes.
 void setFilter(FileEventFilter filter)
          Sets a new file event filter.
 void setOptions(java.util.Set<WatchingAttributes> newOptions)
          Specifies the set of watcher options.
 void setOptions(WatchingAttributes... newOptions)
          Specifies the set of new watching options.
protected  void setRunning(boolean value)
          Sets the state of this watcher.
protected  void setRunning(boolean value, WatcherEvent event)
          Sets the state of this watcher.
 void start()
          Starts watching of the specified folder.
protected abstract  void startWatching()
          Notifies watcher that the watching is going to be started.
 void stop()
          Stops file system watcher.
protected abstract  void stopWatching()
          Notifies watcher that the watching is going to be stopped.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MOVE_EVENTS_OPTION

public static final java.lang.String MOVE_EVENTS_OPTION
See Also:
Constant Field Values

FAKE_RENAMES_OPTION

public static final java.lang.String FAKE_RENAMES_OPTION
See Also:
Constant Field Values

WATCHING_FOLDER_REMOVED

protected static final java.lang.String WATCHING_FOLDER_REMOVED
See Also:
Constant Field Values
Constructor Detail

FileWatcher

protected FileWatcher(java.io.File folder)
Creates a new watcher for the specified folder.

Parameters:
folder - a root folder to monitor file system events. Must not be null
Throws:
java.lang.IllegalArgumentException - if the passed instance is not a directory or does not exist
Method Detail

resetOptions

public final void resetOptions()
Clears options and initializes with all the attributes from WatchingAttributes.


setOptions

public void setOptions(java.util.Set<WatchingAttributes> newOptions)
Specifies the set of watcher options. New options set cannot be null and empty. At least one watching attribute (except WatchingAttributes.Subtree) must be specified.

Parameters:
newOptions - set of watcher options

setOptions

public void setOptions(WatchingAttributes... newOptions)
Specifies the set of new watching options. New options set cannot be null and empty. At least one watching attribute (except WatchingAttributes.Subtree must be specified.

Parameters:
newOptions - array of watcher options

start

public final void start()
Starts watching of the specified folder.


isRunning

public boolean isRunning()
Returns state of the watcher.

Returns:
true if watcher is running; false otherwise

setRunning

protected void setRunning(boolean value,
                          WatcherEvent event)
Sets the state of this watcher. Notifies the state listeners about changes in watcher state.

Parameters:
value - new watcher state; true if watcher is running; false otherwise
event - watcher event

setRunning

protected void setRunning(boolean value)
Sets the state of this watcher. Calling this method bypass setRunning with two parameters do not forget to call notifyFWListeners method also.

Parameters:
value - new watcher state; true if watcher is running; false otherwise

stop

public final void stop()
Stops file system watcher.


startWatching

protected abstract void startWatching()
Notifies watcher that the watching is going to be started.


readEvents

protected abstract java.util.List<FileEvent> readEvents()
Reads next portion of file system events. This method should have event-driven implementation: a file system generates some events and only then method should return them.

Returns:
portion of file system events

stopWatching

protected abstract void stopWatching()
Notifies watcher that the watching is going to be stopped.


release

protected abstract void release()
Releases all native resources which are associated with a watcher instance.


addListener

@Deprecated
public void addListener(FileEventsListener listener)
Deprecated. use addFileEventsListener(com.teamdev.filewatch.FileEventsListener) instead

Adds file events listener to this watcher.

Parameters:
listener - file system listener

addFileEventsListener

public void addFileEventsListener(FileEventsListener listener)
Adds file events listener to this watcher.

Parameters:
listener - file system listener

removeListener

@Deprecated
public void removeListener(FileEventsListener listener)
Deprecated. use removeFileEventsListener(FileEventsListener) instead

Removes file events listener from this watcher.

Parameters:
listener - file system listener

removeFileEventsListener

public void removeFileEventsListener(FileEventsListener listener)
Removes file events listener from this watcher.

Parameters:
listener - file system listener

addListener

@Deprecated
public void addListener(WatcherEventListener listener)
Deprecated. use addWatcherListener(WatcherEventListener) instead

Adds file events listener to this watcher.

Parameters:
listener - file system listener

addWatcherListener

public void addWatcherListener(WatcherEventListener listener)
Add watcher events listener.

Parameters:
listener - file system listener

removeListener

@Deprecated
public void removeListener(WatcherEventListener listener)
Deprecated. use removeWatcherListener(WatcherEventListener) instead

Removes watcher events listener from this watcher.

Parameters:
listener - file system listener

removeWatcherListener

public void removeWatcherListener(WatcherEventListener listener)
Removes file events listener from this watcher.

Parameters:
listener - file system listener

getFolder

public java.io.File getFolder()
Returns the currently watching directory.

Returns:
directory which is being watched

getFilter

public FileEventFilter getFilter()
Returns a file event filter.

Returns:
file event filter; null if no filter is assigned

setFilter

public void setFilter(FileEventFilter filter)
Sets a new file event filter.

Parameters:
filter - new file events filter

accept

protected boolean accept(FileEvent event)
Verifies if the specified event should be filtered out or not using the associated FileEventFilter. If there are no filters associated, returns true.

Parameters:
event - file event to verify
Returns:
true if there are no filter associated; otherwise calls FileEventFilter.accept(FileEvent) of the associated filter.

getOptions

public java.util.Set<WatchingAttributes> getOptions()
Returns set of watching options.

Returns:
set of WatchingAttributes options

create

public static FileWatcher create(java.io.File folder)
Factory method that creates a most efficient file system watcher depending on a current operating system and file system type of the specified folder.

Parameters:
folder - watching folder; must be an existing directory
Returns:
file watcher instance