org.entityfs.util.obs.polling
Class PollingDirectoryMonitorer

java.lang.Object
  extended by org.entityfs.util.obs.polling.PollingDirectoryMonitorer
All Implemented Interfaces:
Observer, Observable, DirectoryMonitorer

public class PollingDirectoryMonitorer
extends Object
implements DirectoryMonitorer, Observer

This class polls a directory and generates events when it detects changes. It is used for detecting changes that are made from other processes, for instance for monitoring a directory for incoming files. EntityObserver objects register themselves as listeners to instances of this class.

The directory is polled every time an outside ticker generates an event. A ticker is any kind of Observable, for instance a TimerTicker. An application that does not want to start its own threads (maybe because it is running in an application server) can use a JMX timer.

The following events are generated:

This monitorer does not generate any events until it is start():ed.

The monitorer uses a ErrorHandlingStrategy for dealing with errors from EntityObserver:s. The default strategy, LoggingErrorHandlingStrategy rethrows all errors that it encounters. This can potentially break the ticker if it is not implemented to handle exceptions from its observers.

Since:
1.0
Author:
Karl Gustafsson

Field Summary
static long DEFAULT_STABILIZED_LIMIT_MSECS
          Five seconds.
 
Constructor Summary
PollingDirectoryMonitorer(DirectoryView dv, Observable ticker)
           
PollingDirectoryMonitorer(DirectoryView dv, Observable ticker, ErrorHandlingStrategy ehs)
           
 
Method Summary
 void addObserver(EntityObserver obs)
          Add an observer.
 int countObservers()
          Get the number of observers.
 void deleteObserver(EntityObserver obs)
          Delete an observer.
 void deleteObservers()
          Delete all observers.
 DirectoryView getDirectoryView()
          Get the monitored directory.
 ErrorHandlingStrategy getErrorHandlingStrategy()
           
 long getStabilizedLimitMSecs()
           
 long getTimestampOfLastEvent()
          Get the timestamp of the last event fired from this object.
 void setStabilizedLimitMSecs(long limit)
           
 void start()
          Start the monitorer.
 void stop()
          Stop the monitorer.
 void update(Observable obs, Object o)
          This is called by the ticker when it is time to poll the monitored directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_STABILIZED_LIMIT_MSECS

public static final long DEFAULT_STABILIZED_LIMIT_MSECS
Five seconds.

See Also:
Constant Field Values
Constructor Detail

PollingDirectoryMonitorer

public PollingDirectoryMonitorer(DirectoryView dv,
                                 Observable ticker)

PollingDirectoryMonitorer

public PollingDirectoryMonitorer(DirectoryView dv,
                                 Observable ticker,
                                 ErrorHandlingStrategy ehs)
Method Detail

setStabilizedLimitMSecs

public void setStabilizedLimitMSecs(long limit)

getStabilizedLimitMSecs

public long getStabilizedLimitMSecs()

addObserver

public void addObserver(EntityObserver obs)
Add an observer.

Specified by:
addObserver in interface Observable
Parameters:
obs - The observer.

deleteObserver

public void deleteObserver(EntityObserver obs)
Delete an observer.

Specified by:
deleteObserver in interface Observable
Parameters:
obs - The observer.

deleteObservers

public void deleteObservers()
Delete all observers.

Specified by:
deleteObservers in interface Observable

countObservers

public int countObservers()
Get the number of observers.

Specified by:
countObservers in interface Observable
Returns:
The number of observers.

getErrorHandlingStrategy

public ErrorHandlingStrategy getErrorHandlingStrategy()

getTimestampOfLastEvent

public long getTimestampOfLastEvent()
Get the timestamp of the last event fired from this object. (The System.currentTimeMillis() timestamp.)

Specified by:
getTimestampOfLastEvent in interface DirectoryMonitorer
Returns:
The timestamp

start

public void start()
Start the monitorer. No events are generated before the monitorer is started. Make sure that the ticker is also started, if that is necessary.

The monitorer can be stopped by calling stop().

Specified by:
start in interface DirectoryMonitorer

stop

public void stop()
Stop the monitorer. After this method is called, no events are generated until start() is called.

Specified by:
stop in interface DirectoryMonitorer

update

public void update(Observable obs,
                   Object o)
This is called by the ticker when it is time to poll the monitored directory.

Specified by:
update in interface Observer

getDirectoryView

public DirectoryView getDirectoryView()
Get the monitored directory.

Specified by:
getDirectoryView in interface DirectoryMonitorer
Returns:
The monitored directory.