Class AbstractFileConfiguration
- All Implemented Interfaces:
Cloneable, Configuration, FileConfiguration, FileSystemBased
- Direct Known Subclasses:
AbstractHierarchicalFileConfiguration.FileConfigurationDelegate, INIConfiguration, PropertiesConfiguration
Partial implementation of the FileConfiguration interface.
Developers of file based configuration may want to extend this class,
the two methods left to implement are FileConfiguration.load(Reader)
and FileConfiguration.save(Writer).
This base class already implements a couple of ways to specify the location of the file this configuration is based on. The following possibilities exist:
- URLs: With the method
setURL()a full URL to the configuration source can be specified. This is the most flexible way. Note that thesave()methods support only file: URLs. - Files: The
setFile()method allows to specify the configuration source as a file. This can be either a relative or an absolute file. In the former case the file is resolved based on the current directory. - As file paths in string form: With the
setPath()method a full path to a configuration file can be provided as a string. - Separated as base path and file name: This is the native form in which
the location is stored. The base path is a string defining either a local
directory or a URL. It can be set using the
setBasePath()method. The file name, non surprisingly, defines the name of the configuration file.
The configuration source to be loaded can be specified using one of the
methods described above. Then the parameterless load() method can be
called. Alternatively, one of the load() methods can be used which is
passed the source directly. These methods typically do not change the
internally stored file; however, if the configuration is not yet associated
with a configuration source, the first call to one of the load()
methods sets the base path and the source URL. This fact has to be taken
into account when calling load() multiple times with different file
paths.
Note that the load() methods do not wipe out the configuration's
content before the new configuration file is loaded. Thus it is very easy to
construct a union configuration by simply loading multiple configuration
files, e.g.
config.load(configFile1); config.load(configFile2);
After executing this code fragment, the resulting configuration will
contain both the properties of configFile1 and configFile2. On the other
hand, if the current configuration file is to be reloaded, clear()
should be called first. Otherwise the properties are doubled. This behavior
is analogous to the behavior of the load(InputStream) method
in java.util.Properties.
- Since:
- 1.0-rc2
- Version:
- $Id: AbstractFileConfiguration.java 1234118 2012-01-20 20:36:04Z oheger $
- Author:
- Emmanuel Bourg
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanThe auto save flag.protected StringStores the base path.static final intConstant fro the configuration changed event.static final intConstant for the configuration reload event.protected StringStores the file name.protected ObjectA lock object for protecting reload operations.protected ReloadingStrategyHolds a reference to the reloading strategy.Fields inherited from class AbstractConfiguration
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructorCreates and loads the configuration from the specified file.AbstractFileConfiguration(String fileName) Creates and loads the configuration from the specified file.Creates and loads the configuration from the specified URL. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddProperty(String key, Object value) Adds a new property to this configuration.voidclearProperty(String key) Removes the specified property from this configuration.clone()Creates a copy of this configuration.protected voidcloseSilent(OutputStream out) A helper method for closing an output stream.voidSend notification that the configuration has changed.booleancontainsKey(String key) check if the configuration contains the keyprotected voidEnters the "No reloading mode".protected voidLeaves the "No reloading mode".protected voidSends an event to all registered listeners.Return the base path.Return the encoding used to store the configuration file.getFile()Return the file where the configuration is stored.Return the name of the file.getKeys()Returns anIteratorwith the keys contained in this configuration.getPath()Returns the full path to the file this configuration is based on.getProperty(String key) Read property from underlying map.Return the reloading strategy.getURL()Return the URL where the configuration is stored.booleanTells if properties are automatically saved to the disk.booleanisEmpty()Check if the configuration is emptyvoidload()Load the configuration from the underlying location.voidLoad the configuration from the specified file.voidload(InputStream in) Load the configuration from the specified stream, using the encoding returned bygetEncoding().voidload(InputStream in, String encoding) Load the configuration from the specified stream, using the specified encoding.voidLocate the specified file and load the configuration.voidLoad the configuration from the specified URL.protected voidSave the configuration if the automatic persistence is enabled and if a file is specified.voidrefresh()Reloads the associated configuration file.voidreload()Performs a reload operation if necessary.booleanreload(boolean checkReload) voidvoidsave()Save the configuration.voidSave the configuration to the specified file.voidsave(OutputStream out) Save the configuration to the specified stream, using the encoding returned bygetEncoding().voidsave(OutputStream out, String encoding) Save the configuration to the specified stream, using the specified encoding.voidSave the configuration to the specified file.voidSave the configuration to the specified URL.voidsetAutoSave(boolean autoSave) Enable or disable the automatically saving of modified properties to the disk.voidsetBasePath(String basePath) Sets the base path.voidsetEncoding(String encoding) Set the encoding used to store the configuration file.voidSet the file where the configuration is stored.voidsetFileName(String fileName) Set the name of the file.voidsetFileSystem(FileSystem fileSystem) voidSets the location of this configuration as a full or relative path name.voidsetProperty(String key, Object value) Sets a new value for the specified property.voidsetReloadingStrategy(ReloadingStrategy strategy) Set the reloading strategy.voidSet the location of this configuration as a URL.Methods inherited from class BaseConfiguration
addPropertyDirect, clear, clearPropertyDirectMethods inherited from class AbstractConfiguration
addErrorLogListener, append, copy, createInterpolator, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDefaultListDelimiter, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getList, getList, getListDelimiter, getLogger, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, getSubstitutor, interpolate, interpolate, interpolatedConfiguration, interpolateHelper, isDelimiterParsingDisabled, isScalarValue, isThrowExceptionOnMissing, resolveContainerStore, setDefaultListDelimiter, setDelimiter, setDelimiterParsingDisabled, setListDelimiter, setLogger, setThrowExceptionOnMissing, subsetMethods inherited from class EventSource
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, createErrorEvent, createEvent, fireError, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEventsMethods inherited from class Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Configuration
clear, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getKeys, getList, getList, getLong, getLong, getLong, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, subsetMethods inherited from interface FileConfiguration
load, save
-
Field Details
-
EVENT_RELOAD
-
EVENT_CONFIG_CHANGED
Constant fro the configuration changed event.- See Also:
-
fileName
-
basePath
-
autoSave
The auto save flag. -
strategy
Holds a reference to the reloading strategy. -
reloadLock
A lock object for protecting reload operations.
-
-
Constructor Details
-
AbstractFileConfiguration
-
AbstractFileConfiguration
Creates and loads the configuration from the specified file. The passed in string must be a valid file name, either absolute or relativ.- Parameters:
fileName- The name of the file to load.- Throws:
ConfigurationException- Error while loading the file- Since:
- 1.1
-
AbstractFileConfiguration
Creates and loads the configuration from the specified file.- Parameters:
file- The file to load.- Throws:
ConfigurationException- Error while loading the file- Since:
- 1.1
-
AbstractFileConfiguration
Creates and loads the configuration from the specified URL.- Parameters:
url- The location of the file to load.- Throws:
ConfigurationException- Error while loading the file- Since:
- 1.1
-
-
Method Details
-
setFileSystem
- Specified by:
setFileSystemin interfaceFileSystemBased
-
resetFileSystem
- Specified by:
resetFileSystemin interfaceFileSystemBased
-
getFileSystem
- Specified by:
getFileSystemin interfaceFileSystemBased
-
getReloadLock
-
load
Load the configuration from the underlying location.- Specified by:
loadin interfaceFileConfiguration- Throws:
ConfigurationException- if loading of the configuration fails
-
load
Locate the specified file and load the configuration. If the configuration is already associated with a source, the current source is not changed. Otherwise (i.e. this is the first load operation), the source URL and the base path are set now based on the source to be loaded.- Specified by:
loadin interfaceFileConfiguration- Parameters:
fileName- the name of the file to be loaded- Throws:
ConfigurationException- if an error occurs
-
load
Load the configuration from the specified file. If the configuration is already associated with a source, the current source is not changed. Otherwise (i.e. this is the first load operation), the source URL and the base path are set now based on the source to be loaded.- Specified by:
loadin interfaceFileConfiguration- Parameters:
file- the file to load- Throws:
ConfigurationException- if an error occurs
-
load
Load the configuration from the specified URL. If the configuration is already associated with a source, the current source is not changed. Otherwise (i.e. this is the first load operation), the source URL and the base path are set now based on the source to be loaded.- Specified by:
loadin interfaceFileConfiguration- Parameters:
url- the URL of the file to be loaded- Throws:
ConfigurationException- if an error occurs
-
load
Load the configuration from the specified stream, using the encoding returned bygetEncoding().- Specified by:
loadin interfaceFileConfiguration- Parameters:
in- the input stream- Throws:
ConfigurationException- if an error occurs during the load operation
-
load
Load the configuration from the specified stream, using the specified encoding. If the encoding is null the default encoding is used.- Specified by:
loadin interfaceFileConfiguration- Parameters:
in- the input streamencoding- the encoding used.nullto use the default encoding- Throws:
ConfigurationException- if an error occurs during the load operation
-
save
Save the configuration. Before this method can be called a valid file name must have been set.- Specified by:
savein interfaceFileConfiguration- Throws:
ConfigurationException- if an error occurs or no file name has been set yet
-
save
Save the configuration to the specified file. This doesn't change the source of the configuration, use setFileName() if you need it.- Specified by:
savein interfaceFileConfiguration- Parameters:
fileName- the file name- Throws:
ConfigurationException- if an error occurs during the save operation
-
save
Save the configuration to the specified URL. This doesn't change the source of the configuration, use setURL() if you need it.- Specified by:
savein interfaceFileConfiguration- Parameters:
url- the URL- Throws:
ConfigurationException- if an error occurs during the save operation
-
save
Save the configuration to the specified file. The file is created automatically if it doesn't exist. This doesn't change the source of the configuration, usesetFile(File)if you need it.- Specified by:
savein interfaceFileConfiguration- Parameters:
file- the target file- Throws:
ConfigurationException- if an error occurs during the save operation
-
save
Save the configuration to the specified stream, using the encoding returned bygetEncoding().- Specified by:
savein interfaceFileConfiguration- Parameters:
out- the output stream- Throws:
ConfigurationException- if an error occurs during the save operation
-
save
Save the configuration to the specified stream, using the specified encoding. If the encoding is null the default encoding is used.- Specified by:
savein interfaceFileConfiguration- Parameters:
out- the output streamencoding- the encoding to use- Throws:
ConfigurationException- if an error occurs during the save operation
-
getFileName
Return the name of the file.- Specified by:
getFileNamein interfaceFileConfiguration- Returns:
- the file name
-
setFileName
Set the name of the file. The passed in file name can contain a relative path. It must be used when referring files with relative paths from classpath. UsesetPath()to set a full qualified file name.- Specified by:
setFileNamein interfaceFileConfiguration- Parameters:
fileName- the name of the file
-
getBasePath
Return the base path.- Specified by:
getBasePathin interfaceFileConfiguration- Returns:
- the base path
- See Also:
-
setBasePath
Sets the base path. The base path is typically either a path to a directory or a URL. Together with the value passed to thesetFileName()method it defines the location of the configuration file to be loaded. The strategies for locating the file are quite tolerant. For instance if the file name is already an absolute path or a fully defined URL, the base path will be ignored. The base path can also be a URL, in which case the file name is interpreted in this URL's context. Because the base path is used by some of the derived classes for resolving relative file names it should contain a meaningful value. If other methods are used for determining the location of the configuration file (e.g.setFile()orsetURL()), the base path is automatically set.- Specified by:
setBasePathin interfaceFileConfiguration- Parameters:
basePath- the base path.
-
getFile
Return the file where the configuration is stored. If the base path is a URL with a protocol different than "file", or the configuration file is within a compressed archive, the return value will not point to a valid file object.- Specified by:
getFilein interfaceFileConfiguration- Returns:
- the file where the configuration is stored; this can be null
-
setFile
Set the file where the configuration is stored. The passed in file is made absolute if it is not yet. Then the file's path component becomes the base path and its name component becomes the file name.- Specified by:
setFilein interfaceFileConfiguration- Parameters:
file- the file where the configuration is stored
-
getPath
Returns the full path to the file this configuration is based on. The return value is a valid File path only if this configuration is based on a file on the local disk. If the configuration was loaded from a packed archive the returned value is the string form of the URL from which the configuration was loaded.- Returns:
- the full path to the configuration file
-
setPath
Sets the location of this configuration as a full or relative path name. The passed in path should represent a valid file name on the file system. It must not be used to specify relative paths for files that exist in classpath, either plain file system or compressed archive, because this method expands any relative path to an absolute one which may end in an invalid absolute path for classpath references.- Parameters:
path- the full path name of the configuration file
-
getURL
Return the URL where the configuration is stored.- Specified by:
getURLin interfaceFileConfiguration- Returns:
- the configuration's location as URL
-
setURL
Set the location of this configuration as a URL. For loading this can be an arbitrary URL with a supported protocol. If the configuration is to be saved, too, a URL with the "file" protocol should be provided.- Specified by:
setURLin interfaceFileConfiguration- Parameters:
url- the location of this configuration as URL
-
setAutoSave
Description copied from interface:FileConfigurationEnable or disable the automatically saving of modified properties to the disk.- Specified by:
setAutoSavein interfaceFileConfiguration- Parameters:
autoSave-trueto enable,falseto disable
-
isAutoSave
Description copied from interface:FileConfigurationTells if properties are automatically saved to the disk.- Specified by:
isAutoSavein interfaceFileConfiguration- Returns:
trueif auto-saving is enabled,falseotherwise
-
possiblySave
Save the configuration if the automatic persistence is enabled and if a file is specified. -
addProperty
Adds a new property to this configuration. This implementation checks if the auto save mode is enabled and saves the configuration if necessary.- Specified by:
addPropertyin interfaceConfiguration- Overrides:
addPropertyin classAbstractConfiguration- Parameters:
key- the key of the new propertyvalue- the value
-
setProperty
Sets a new value for the specified property. This implementation checks if the auto save mode is enabled and saves the configuration if necessary.- Specified by:
setPropertyin interfaceConfiguration- Overrides:
setPropertyin classAbstractConfiguration- Parameters:
key- the key of the affected propertyvalue- the value
-
clearProperty
Description copied from class:AbstractConfigurationRemoves the specified property from this configuration. This implementation performs some preparations and then delegates toclearPropertyDirect(), which will do the real work.- Specified by:
clearPropertyin interfaceConfiguration- Overrides:
clearPropertyin classAbstractConfiguration- Parameters:
key- the key to be removed
-
getReloadingStrategy
Description copied from interface:FileConfigurationReturn the reloading strategy.- Specified by:
getReloadingStrategyin interfaceFileConfiguration- Returns:
- the reloading strategy currently used
-
setReloadingStrategy
Description copied from interface:FileConfigurationSet the reloading strategy.- Specified by:
setReloadingStrategyin interfaceFileConfiguration- Parameters:
strategy- the reloading strategy to use
-
reload
Performs a reload operation if necessary. This method is called on each access of this configuration. It asks the associated reloading strategy whether a reload should be performed. If this is the case, the configuration is cleared and loaded again from its source. If this operation causes an exception, the registered error listeners will be notified. The error event passed to the listeners is of typeEVENT_RELOADand contains the exception that caused the event.- Specified by:
reloadin interfaceFileConfiguration
-
reload
-
refresh
Reloads the associated configuration file. This method first clears the content of this configuration, then the associated configuration file is loaded again. Updates on this configuration which have not yet been saved are lost. Calling this method is like invokingreload()without checking the reloading strategy.- Throws:
ConfigurationException- if an error occurs- Since:
- 1.7
-
configurationChanged
Send notification that the configuration has changed. -
enterNoReload
Enters the "No reloading mode". As long as this mode is active no reloading will be performed. This is necessary for some implementations ofsave()in derived classes, which may cause a reload while accessing the properties to save. This may cause the whole configuration to be erased. To avoid this, this method can be called first. After a call to this method there always must be a corresponding call ofexitNoReload()later! (If necessary,finallyblocks must be used to ensure this. -
exitNoReload
-
fireEvent
Sends an event to all registered listeners. This implementation ensures that no reloads are performed while the listeners are invoked. So infinite loops can be avoided that can be caused by event listeners accessing the configuration's properties when they are invoked.- Overrides:
fireEventin classEventSource- Parameters:
type- the event typepropName- the name of the propertypropValue- the value of the propertybefore- the before update flag
-
getProperty
Description copied from class:BaseConfigurationRead property from underlying map.- Specified by:
getPropertyin interfaceConfiguration- Overrides:
getPropertyin classBaseConfiguration- Parameters:
key- key to use for mapping- Returns:
- object associated with the given configuration key.
-
isEmpty
Description copied from class:BaseConfigurationCheck if the configuration is empty- Specified by:
isEmptyin interfaceConfiguration- Overrides:
isEmptyin classBaseConfiguration- Returns:
trueif Configuration is empty,falseotherwise.
-
containsKey
Description copied from class:BaseConfigurationcheck if the configuration contains the key- Specified by:
containsKeyin interfaceConfiguration- Overrides:
containsKeyin classBaseConfiguration- Parameters:
key- the configuration key- Returns:
trueif Configuration contain given key,falseotherwise.
-
getKeys
Returns anIteratorwith the keys contained in this configuration. This implementation performs a reload if necessary before obtaining the keys. TheIteratorreturned by this method points to a snapshot taken when this method was called. Later changes at the set of keys (including those caused by a reload) won't be visible. This is because a reload can happen at any time during iteration, and it is impossible to determine how this reload affects the current iteration. When using the iterator a client has to be aware that changes of the configuration are possible at any time. For instance, if after a reload operation some keys are no longer present, the iterator will still return those keys because they were found when it was created.- Specified by:
getKeysin interfaceConfiguration- Overrides:
getKeysin classBaseConfiguration- Returns:
- an
Iteratorwith the keys of this configuration
-
getEncoding
Description copied from interface:FileConfigurationReturn the encoding used to store the configuration file. If the value is null the default encoding is used.- Specified by:
getEncodingin interfaceFileConfiguration- Returns:
- the current encoding
-
setEncoding
Description copied from interface:FileConfigurationSet the encoding used to store the configuration file. Set the encoding to null to use the default encoding.- Specified by:
setEncodingin interfaceFileConfiguration- Parameters:
encoding- the encoding to use
-
clone
Creates a copy of this configuration. The new configuration object will contain the same properties as the original, but it will lose any connection to a source file (if one exists); this includes setting the source URL, base path, and file name to null. This is done to avoid race conditions if both the original and the copy are modified and then saved.- Overrides:
clonein classBaseConfiguration- Returns:
- the copy
- Since:
- 1.3
-
closeSilent
A helper method for closing an output stream. Occurring exceptions will be ignored.- Parameters:
out- the output stream to be closed (may be null)- Since:
- 1.5
-