org.apache.commons.configuration.reloading

Class FileChangedReloadingStrategy

public class FileChangedReloadingStrategy extends 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: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $

Author: Emmanuel Bourg

Field Summary
protected FileConfigurationconfiguration
Stores a reference to the configuration to be monitored.
static intDEFAULT_REFRESH_DELAY
Constant for the default refresh delay.
static StringJAR_PROTOCOL
Constant for the jar URL protocol.
protected longlastChecked
The last time the file was checked for changes.
protected longlastModified
The last time the configuration file was modified.
protected longrefreshDelay
The minimum delay in milliseconds between checks.
Method Summary
FilefileFromURL(URL url)
Helper method for transforming a URL into a file object.
protected FilegetFile()
Returns the file that is monitored by this strategy.
longgetRefreshDelay()
Return the minimal time in milliseconds between two reloadings.
protected booleanhasChanged()
Check if the configuration has changed since the last time it was loaded.
voidinit()
voidreloadingPerformed()
booleanreloadingRequired()
voidsetConfiguration(FileConfiguration configuration)
voidsetRefreshDelay(long refreshDelay)
Set the minimal time between two reloadings.
protected voidupdateLastModified()
Update the last modified time.

Field Detail

configuration

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

DEFAULT_REFRESH_DELAY

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

JAR_PROTOCOL

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

lastChecked

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

lastModified

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

refreshDelay

protected long refreshDelay
The minimum delay in milliseconds between checks.

Method Detail

fileFromURL

private File fileFromURL(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

getFile

protected 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

getRefreshDelay

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

Returns: the refresh delay (in milliseconds)

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

init

public void init()

reloadingPerformed

public void reloadingPerformed()

reloadingRequired

public boolean reloadingRequired()

setConfiguration

public void setConfiguration(FileConfiguration configuration)

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.