public abstract class AbstractFileConfiguration extends BaseConfiguration implements FileConfiguration, FileSystemBased
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:
setURL()
a full URL to the
configuration source can be specified. This is the most flexible way. Note
that the save()
methods support only file: URLs.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.setPath()
method a
full path to a configuration file can be provided as a string.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
.
Modifier and Type | Field and Description |
---|---|
protected boolean |
autoSave
The auto save flag.
|
protected String |
basePath
Stores the base path.
|
static int |
EVENT_CONFIG_CHANGED
Constant fro the configuration changed event.
|
static int |
EVENT_RELOAD
Constant for the configuration reload event.
|
protected String |
fileName
Stores the file name.
|
protected Object |
reloadLock
A lock object for protecting reload operations.
|
protected ReloadingStrategy |
strategy
Holds a reference to the reloading strategy.
|
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN
Constructor and Description |
---|
AbstractFileConfiguration()
Default constructor
|
AbstractFileConfiguration(File file)
Creates and loads the configuration from the specified file.
|
AbstractFileConfiguration(String fileName)
Creates and loads the configuration from the specified file.
|
AbstractFileConfiguration(URL url)
Creates and loads the configuration from the specified URL.
|
Modifier and Type | Method and Description |
---|---|
void |
addProperty(String key,
Object value)
Adds a new property to this configuration.
|
void |
clearProperty(String key)
Removes the specified property from this configuration.
|
Object |
clone()
Creates a copy of this configuration.
|
protected void |
closeSilent(OutputStream out)
A helper method for closing an output stream.
|
void |
configurationChanged()
Send notification that the configuration has changed.
|
boolean |
containsKey(String key)
check if the configuration contains the key
|
protected void |
enterNoReload()
Enters the "No reloading mode".
|
protected void |
exitNoReload()
Leaves the "No reloading mode".
|
protected void |
fireEvent(int type,
String propName,
Object propValue,
boolean before)
Sends an event to all registered listeners.
|
String |
getBasePath()
Return the base path.
|
String |
getEncoding()
Return the encoding used to store the configuration file.
|
File |
getFile()
Return the file where the configuration is stored.
|
String |
getFileName()
Return the name of the file.
|
FileSystem |
getFileSystem() |
Iterator<String> |
getKeys()
Returns an
Iterator with the keys contained in this
configuration. |
String |
getPath()
Returns the full path to the file this configuration is based on.
|
Object |
getProperty(String key)
Read property from underlying map.
|
ReloadingStrategy |
getReloadingStrategy()
Return the reloading strategy.
|
Object |
getReloadLock() |
URL |
getURL()
Return the URL where the configuration is stored.
|
boolean |
isAutoSave()
Tells if properties are automatically saved to the disk.
|
boolean |
isEmpty()
Check if the configuration is empty
|
void |
load()
Load the configuration from the underlying location.
|
void |
load(File file)
Load the configuration from the specified file.
|
void |
load(InputStream in)
Load the configuration from the specified stream, using the encoding
returned by
getEncoding() . |
void |
load(InputStream in,
String encoding)
Load the configuration from the specified stream, using the specified
encoding.
|
void |
load(String fileName)
Locate the specified file and load the configuration.
|
void |
load(URL url)
Load the configuration from the specified URL.
|
protected void |
possiblySave()
Save the configuration if the automatic persistence is enabled
and if a file is specified.
|
void |
refresh()
Reloads the associated configuration file.
|
void |
reload()
Performs a reload operation if necessary.
|
boolean |
reload(boolean checkReload) |
void |
resetFileSystem() |
void |
save()
Save the configuration.
|
void |
save(File file)
Save the configuration to the specified file.
|
void |
save(OutputStream out)
Save the configuration to the specified stream, using the encoding
returned by
getEncoding() . |
void |
save(OutputStream out,
String encoding)
Save the configuration to the specified stream, using the specified
encoding.
|
void |
save(String fileName)
Save the configuration to the specified file.
|
void |
save(URL url)
Save the configuration to the specified URL.
|
void |
setAutoSave(boolean autoSave)
Enable or disable the automatically saving of modified properties to the disk.
|
void |
setBasePath(String basePath)
Sets the base path.
|
void |
setEncoding(String encoding)
Set the encoding used to store the configuration file.
|
void |
setFile(File file)
Set the file where the configuration is stored.
|
void |
setFileName(String fileName)
Set the name of the file.
|
void |
setFileSystem(FileSystem fileSystem) |
void |
setPath(String path)
Sets the location of this configuration as a full or relative path name.
|
void |
setProperty(String key,
Object value)
Sets a new value for the specified property.
|
void |
setReloadingStrategy(ReloadingStrategy strategy)
Set the reloading strategy.
|
void |
setURL(URL url)
Set the location of this configuration as a URL.
|
addPropertyDirect, clear, clearPropertyDirect
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, subset
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, createErrorEvent, createEvent, fireError, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEvents
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
load, save
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, subset
public static final int EVENT_RELOAD
public static final int EVENT_CONFIG_CHANGED
protected String fileName
protected String basePath
protected boolean autoSave
protected ReloadingStrategy strategy
protected Object reloadLock
public AbstractFileConfiguration()
public AbstractFileConfiguration(String fileName) throws ConfigurationException
fileName
- The name of the file to load.ConfigurationException
- Error while loading the filepublic AbstractFileConfiguration(File file) throws ConfigurationException
file
- The file to load.ConfigurationException
- Error while loading the filepublic AbstractFileConfiguration(URL url) throws ConfigurationException
url
- The location of the file to load.ConfigurationException
- Error while loading the filepublic void setFileSystem(FileSystem fileSystem)
setFileSystem
in interface FileSystemBased
public void resetFileSystem()
resetFileSystem
in interface FileSystemBased
public FileSystem getFileSystem()
getFileSystem
in interface FileSystemBased
public Object getReloadLock()
public void load() throws ConfigurationException
load
in interface FileConfiguration
ConfigurationException
- if loading of the configuration failspublic void load(String fileName) throws ConfigurationException
load
in interface FileConfiguration
fileName
- the name of the file to be loadedConfigurationException
- if an error occurspublic void load(File file) throws ConfigurationException
load
in interface FileConfiguration
file
- the file to loadConfigurationException
- if an error occurspublic void load(URL url) throws ConfigurationException
load
in interface FileConfiguration
url
- the URL of the file to be loadedConfigurationException
- if an error occurspublic void load(InputStream in) throws ConfigurationException
getEncoding()
.load
in interface FileConfiguration
in
- the input streamConfigurationException
- if an error occurs during the load operationpublic void load(InputStream in, String encoding) throws ConfigurationException
load
in interface FileConfiguration
in
- the input streamencoding
- the encoding used. null
to use the default encodingConfigurationException
- if an error occurs during the load operationpublic void save() throws ConfigurationException
save
in interface FileConfiguration
ConfigurationException
- if an error occurs or no file name has
been set yetpublic void save(String fileName) throws ConfigurationException
save
in interface FileConfiguration
fileName
- the file nameConfigurationException
- if an error occurs during the save operationpublic void save(URL url) throws ConfigurationException
save
in interface FileConfiguration
url
- the URLConfigurationException
- if an error occurs during the save operationpublic void save(File file) throws ConfigurationException
setFile(java.io.File)
if you need it.save
in interface FileConfiguration
file
- the target fileConfigurationException
- if an error occurs during the save operationpublic void save(OutputStream out) throws ConfigurationException
getEncoding()
.save
in interface FileConfiguration
out
- the output streamConfigurationException
- if an error occurs during the save operationpublic void save(OutputStream out, String encoding) throws ConfigurationException
save
in interface FileConfiguration
out
- the output streamencoding
- the encoding to useConfigurationException
- if an error occurs during the save operationpublic String getFileName()
getFileName
in interface FileConfiguration
public void setFileName(String fileName)
setPath()
to set a full qualified file name.setFileName
in interface FileConfiguration
fileName
- the name of the filepublic String getBasePath()
getBasePath
in interface FileConfiguration
FileConfiguration.getBasePath()
public void setBasePath(String basePath)
setFileName()
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()
or setURL()
), the
base path is automatically set.setBasePath
in interface FileConfiguration
basePath
- the base path.public File getFile()
getFile
in interface FileConfiguration
public void setFile(File file)
setFile
in interface FileConfiguration
file
- the file where the configuration is storedpublic String getPath()
public void setPath(String path)
path
- the full path name of the configuration filepublic URL getURL()
getURL
in interface FileConfiguration
public void setURL(URL url)
setURL
in interface FileConfiguration
url
- the location of this configuration as URLpublic void setAutoSave(boolean autoSave)
FileConfiguration
setAutoSave
in interface FileConfiguration
autoSave
- true
to enable, false
to disablepublic boolean isAutoSave()
FileConfiguration
isAutoSave
in interface FileConfiguration
true
if auto-saving is enabled, false
otherwiseprotected void possiblySave()
public void addProperty(String key, Object value)
addProperty
in interface Configuration
addProperty
in class AbstractConfiguration
key
- the key of the new propertyvalue
- the valuepublic void setProperty(String key, Object value)
setProperty
in interface Configuration
setProperty
in class AbstractConfiguration
key
- the key of the affected propertyvalue
- the valuepublic void clearProperty(String key)
AbstractConfiguration
clearPropertyDirect()
, which will do the real work.clearProperty
in interface Configuration
clearProperty
in class AbstractConfiguration
key
- the key to be removedpublic ReloadingStrategy getReloadingStrategy()
FileConfiguration
getReloadingStrategy
in interface FileConfiguration
public void setReloadingStrategy(ReloadingStrategy strategy)
FileConfiguration
setReloadingStrategy
in interface FileConfiguration
strategy
- the reloading strategy to usepublic void reload()
EVENT_RELOAD
and contains the exception that caused the
event.reload
in interface FileConfiguration
public boolean reload(boolean checkReload)
public void refresh() throws ConfigurationException
reload()
without checking the reloading strategy.ConfigurationException
- if an error occurspublic void configurationChanged()
protected void enterNoReload()
save()
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 of exitNoReload()
later! (If
necessary, finally
blocks must be used to ensure this.protected void exitNoReload()
enterNoReload()
protected void fireEvent(int type, String propName, Object propValue, boolean before)
fireEvent
in class EventSource
type
- the event typepropName
- the name of the propertypropValue
- the value of the propertybefore
- the before update flagpublic Object getProperty(String key)
BaseConfiguration
getProperty
in interface Configuration
getProperty
in class BaseConfiguration
key
- key to use for mappingpublic boolean isEmpty()
BaseConfiguration
isEmpty
in interface Configuration
isEmpty
in class BaseConfiguration
true
if Configuration is empty,
false
otherwise.public boolean containsKey(String key)
BaseConfiguration
containsKey
in interface Configuration
containsKey
in class BaseConfiguration
key
- the configuration keytrue
if Configuration contain given key,
false
otherwise.public Iterator<String> getKeys()
Iterator
with the keys contained in this
configuration. This implementation performs a reload if necessary before
obtaining the keys. The Iterator
returned 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.getKeys
in interface Configuration
getKeys
in class BaseConfiguration
Iterator
with the keys of this configurationpublic String getEncoding()
FileConfiguration
getEncoding
in interface FileConfiguration
public void setEncoding(String encoding)
FileConfiguration
setEncoding
in interface FileConfiguration
encoding
- the encoding to usepublic Object clone()
clone
in class BaseConfiguration
protected void closeSilent(OutputStream out)
out
- the output stream to be closed (may be null)Copyright © 2001–2013 The Apache Software Foundation. All rights reserved.