org.pf.settings.impl
Class ReloadableSettings

java.lang.Object
  extended by java.util.Observable
      extended by org.pf.settings.impl.ReloadableSettings
All Implemented Interfaces:
ReadOnlySettings, TriggerClient

public class ReloadableSettings
extends java.util.Observable
implements ReadOnlySettings, TriggerClient

Implements settings that will be automatically reloaded if their underlying file has been modified. The interval for checking the file for changes can be specified.

Version:
1.3
Author:
Manfred Duchrow

Constructor Summary
ReloadableSettings(long checkInterval, SettingsFileReader reader)
          Initialize the new instance with the interval after which the file has to be checked for changes and a reader that can read the file structure into a settings object.
ReloadableSettings(long checkInterval, SettingsFileReader reader, boolean mustExist)
          Initialize the new instance with the name of underlying file, the interval after which the file has to be checked for changes and a reader that can read the file structure into a settings object.
 
Method Summary
 boolean canBeTriggeredBy(Trigger trigger)
          Returns whether or not the trigger is allowed to activate the trigger client's triggeredBy() method.
 java.lang.String[] getCategoryNames()
          Returns an array of all currently known categories.
 ReadOnlySettings getDefaults()
          Gets the defaults that are looked up, if a setting can't be found in the main settings object.
 java.lang.String[] getKeyNamesOf(java.lang.String categoryName)
          Returns all currently known key names in the category with the specified name.
 java.lang.String[] getKeyNamesOfDefaultCategory()
          Returns all currently known key names in the default category.
 java.lang.String getName()
          Returns the name of the settings object as a whole.
 java.lang.String getSettingsNameOf(java.lang.String keyName)
          Returns the name of the name of the settings where the specified key is found in the default category.
 java.lang.String getSettingsNameOf(java.lang.String categoryName, java.lang.String keyName)
          Returns the name of the name of the settings where the specified category and key are found.
 java.lang.String getValueOf(java.lang.String keyName)
          Returns the value of keyName in the default category.
 java.lang.String getValueOf(java.lang.String categoryName, java.lang.String keyName)
          Returns the value of keyName in the specified category.
 void setDefaults(ReadOnlySettings defaults)
          Sets defaults that must be looked up, if a setting can't be found in the main settings object.
 void setName(java.lang.String aName)
          Sets the name of the settings object as a whole.
 void startReloading(long checkInterval)
          Starts the automatic reloading of changes in the underlying file.
 void stopReloading()
          Stops the automatic reloading of changes in the underlying file.
 boolean triggeredBy(Trigger trigger)
          This method will be called by a trigger whenever its timing says so.
 
Methods inherited from class java.util.Observable
addObserver, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReloadableSettings

public ReloadableSettings(long checkInterval,
                          SettingsFileReader reader)
                   throws ReloadableSettingsException
Initialize the new instance with the interval after which the file has to be checked for changes and a reader that can read the file structure into a settings object. If the file can't be found an exception will be thrown.

Parameters:
checkInterval - the interval in seconds after which the file must be checked
reader - The reader that can read the settings file
Throws:
ReloadableSettingsException

ReloadableSettings

public ReloadableSettings(long checkInterval,
                          SettingsFileReader reader,
                          boolean mustExist)
                   throws ReloadableSettingsException
Initialize the new instance with the name of underlying file, the interval after which the file has to be checked for changes and a reader that can read the file structure into a settings object. If mustExist is set to false and the settings file doesn't exist, the settings object is still valid and might be just empty. However it keeps checking the file and whenever it exists it will be read in.

Parameters:
checkInterval - the interval in seconds after which the file must be checked
reader - The reader that can read the settings file
mustExist - If true, the settings file must exist. If it doesn't exist an exception will be thrown
Throws:
ReloadableSettingsException
Method Detail

getName

public java.lang.String getName()
Returns the name of the settings object as a whole. This might be a simple name or a resource locator or a filename. However, naming a setting is obtional and therefore this method can also return null.

Specified by:
getName in interface ReadOnlySettings

setName

public void setName(java.lang.String aName)
Sets the name of the settings object as a whole. This might be a simple name or a resource locator or a filename.

Specified by:
setName in interface ReadOnlySettings

getValueOf

public java.lang.String getValueOf(java.lang.String categoryName,
                                   java.lang.String keyName)
Returns the value of keyName in the specified category.

Specified by:
getValueOf in interface ReadOnlySettings
Parameters:
categoryName - The name of the category (null means the default category)
keyName - The name of a key inside the category
Returns:
The associated value or null if either the category or the key could not be found

getValueOf

public java.lang.String getValueOf(java.lang.String keyName)
Returns the value of keyName in the default category.

Specified by:
getValueOf in interface ReadOnlySettings
Parameters:
keyName - The name of a key inside the default category
Returns:
The associated value or null if the key could not be found

getCategoryNames

public java.lang.String[] getCategoryNames()
Returns an array of all currently known categories. An empty string is the name for the default category!

Specified by:
getCategoryNames in interface ReadOnlySettings

getKeyNamesOf

public java.lang.String[] getKeyNamesOf(java.lang.String categoryName)
Returns all currently known key names in the category with the specified name. If the category name is null or an empty string, the default category's keys will be returned.

Specified by:
getKeyNamesOf in interface ReadOnlySettings
Parameters:
categoryName - The name of the category the keys are wanted from

getKeyNamesOfDefaultCategory

public java.lang.String[] getKeyNamesOfDefaultCategory()
Returns all currently known key names in the default category.

Specified by:
getKeyNamesOfDefaultCategory in interface ReadOnlySettings

getDefaults

public ReadOnlySettings getDefaults()
Gets the defaults that are looked up, if a setting can't be found in the main settings object.

Specified by:
getDefaults in interface ReadOnlySettings
Returns:
A settings object with default values or null

setDefaults

public void setDefaults(ReadOnlySettings defaults)
Sets defaults that must be looked up, if a setting can't be found in the main settings object.

Specified by:
setDefaults in interface ReadOnlySettings
Parameters:
defaults - A settings object with default values or null

getSettingsNameOf

public java.lang.String getSettingsNameOf(java.lang.String categoryName,
                                          java.lang.String keyName)
Returns the name of the name of the settings where the specified category and key are found. That is a lookup in this settings object and then, if not found, in its defaults and so on.

Specified by:
getSettingsNameOf in interface ReadOnlySettings
Parameters:
categoryName - The name of the category (null means the default category)
keyName - The name of a key inside the category
Returns:
The name of the settings object or null

getSettingsNameOf

public java.lang.String getSettingsNameOf(java.lang.String keyName)
Returns the name of the name of the settings where the specified key is found in the default category. That is a lookup in this settings object and then, if not found, in its defaults and so on.

Specified by:
getSettingsNameOf in interface ReadOnlySettings
Parameters:
keyName - The name of a key inside the default category
Returns:
The name of the settings object or null

canBeTriggeredBy

public boolean canBeTriggeredBy(Trigger trigger)
Returns whether or not the trigger is allowed to activate the trigger client's triggeredBy() method.
This method will be called, whenever the trigger's timing allows the trigger to execute the trigger client's service method. So, eventually the trigger client decides itself, if its service method will be called this time.

Specified by:
canBeTriggeredBy in interface TriggerClient
Parameters:
trigger - The trigger that calls this method
Returns:
true, if the service method triggeredBy() can be called
See Also:
Trigger

triggeredBy

public boolean triggeredBy(Trigger trigger)
This method will be called by a trigger whenever its timing says so. It reloads the settings and then notifies all observers that are registered.

Specified by:
triggeredBy in interface TriggerClient
Parameters:
trigger - The trigger that calls this method
Returns:
true, if the trigger should continue, otherwise false
See Also:
Trigger

stopReloading

public void stopReloading()
Stops the automatic reloading of changes in the underlying file.


startReloading

public void startReloading(long checkInterval)
Starts the automatic reloading of changes in the underlying file.

Parameters:
checkInterval - The interval (in milliseconds) for checking the file's modification timestamp