org.apache.commons.configuration.reloading
Class FileChangedReloadingStrategy

java.lang.Object
  extended by org.apache.commons.configuration.reloading.FileChangedReloadingStrategy
All Implemented Interfaces:
ReloadingStrategy

public class FileChangedReloadingStrategy
extends java.lang.Object
implements ReloadingStrategy

A reloading strategy that will reload the configuration every time its underlying file is changed.

This reloading strategy does not actively monitor a configuration file, but is triggered by its associated configuration whenever properties are accessed. It then checks the configuration file's last modification date and causes a reload if this has changed.

To avoid permanent disc access on successive property lookups a refresh delay can be specified. This has the effect that the configuration file's last modification date is only checked once in this delay period. The default value for this refresh delay is 5 seconds.

This strategy only works with FileConfiguration instances.

Since:
1.1
Version:
$Revision: 606798 $, $Date: 2007-12-25 20:05:58 +0100 (Di, 25 Dez 2007) $
Author:
Emmanuel Bourg

Field Summary
protected  FileConfiguration configuration
          Stores a reference to the configuration to be monitored.
private static int DEFAULT_REFRESH_DELAY
          Constant for the default refresh delay.
private static java.lang.String JAR_PROTOCOL
          Constant for the jar URL protocol.
protected  long lastChecked
          The last time the file was checked for changes.
protected  long lastModified
          The last time the configuration file was modified.
protected  long refreshDelay
          The minimum delay in milliseconds between checks.
private  boolean reloading
          A flag whether a reload is required.
 
Constructor Summary
FileChangedReloadingStrategy()
           
 
Method Summary
private  java.io.File fileFromURL(java.net.URL url)
          Helper method for transforming a URL into a file object.
protected  java.io.File getFile()
          Returns the file that is monitored by this strategy.
 long getRefreshDelay()
          Return the minimal time in milliseconds between two reloadings.
protected  boolean hasChanged()
          Check if the configuration has changed since the last time it was loaded.
 void init()
          Initialize the strategy.
 void reloadingPerformed()
          Notify the strategy that the file has been reloaded.
 boolean reloadingRequired()
          Tell if the evaluation of the strategy requires to reload the configuration.
 void setConfiguration(FileConfiguration configuration)
          Set the configuration managed by this strategy.
 void setRefreshDelay(long refreshDelay)
          Set the minimal time between two reloadings.
protected  void updateLastModified()
          Update the last modified time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JAR_PROTOCOL

private static final java.lang.String JAR_PROTOCOL
Constant for the jar URL protocol.

See Also:
Constant Field Values

DEFAULT_REFRESH_DELAY

private static final int DEFAULT_REFRESH_DELAY
Constant for the default refresh delay.

See Also:
Constant Field Values

configuration

protected FileConfiguration configuration
Stores a reference to the configuration to be monitored.


lastModified

protected long lastModified
The last time the configuration file was modified.


lastChecked

protected long lastChecked
The last time the file was checked for changes.


refreshDelay

protected long refreshDelay
The minimum delay in milliseconds between checks.


reloading

private boolean reloading
A flag whether a reload is required.

Constructor Detail

FileChangedReloadingStrategy

public FileChangedReloadingStrategy()
Method Detail

setConfiguration

public void setConfiguration(FileConfiguration configuration)
Description copied from interface: ReloadingStrategy
Set the configuration managed by this strategy.

Specified by:
setConfiguration in interface ReloadingStrategy
Parameters:
configuration - the configuration to monitor

init

public void init()
Description copied from interface: ReloadingStrategy
Initialize the strategy.

Specified by:
init in interface ReloadingStrategy

reloadingRequired

public boolean reloadingRequired()
Description copied from interface: ReloadingStrategy
Tell if the evaluation of the strategy requires to reload the configuration.

Specified by:
reloadingRequired in interface ReloadingStrategy
Returns:
a flag whether a reload should be performed

reloadingPerformed

public void reloadingPerformed()
Description copied from interface: ReloadingStrategy
Notify the strategy that the file has been reloaded.

Specified by:
reloadingPerformed in interface ReloadingStrategy

getRefreshDelay

public long getRefreshDelay()
Return the minimal time in milliseconds between two reloadings.

Returns:
the refresh delay (in milliseconds)

setRefreshDelay

public void setRefreshDelay(long refreshDelay)
Set the minimal time between two reloadings.

Parameters:
refreshDelay - refresh delay in milliseconds

updateLastModified

protected void updateLastModified()
Update the last modified time.


hasChanged

protected boolean hasChanged()
Check if the configuration has changed since the last time it was loaded.

Returns:
a flag whether the configuration has changed

getFile

protected java.io.File getFile()
Returns the file that is monitored by this strategy. Note that the return value can be null under some circumstances.

Returns:
the monitored file

fileFromURL

private java.io.File fileFromURL(java.net.URL url)
Helper method for transforming a URL into a file object. This method handles file: and jar: URLs.

Parameters:
url - the URL to be converted
Returns:
the resulting file or null