|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.configuration.AbstractConfiguration | +--org.apache.commons.configuration.BaseConfiguration | +--org.apache.commons.configuration.AbstractFileConfiguration
Partial implementation of the FileConfiguration
interface.
Developpers of file based configuration may want to extend this class,
the two methods left to implement are
and FileConfiguration.load(Reader)
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.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
.
Field Summary | |
protected boolean |
autoSave
The auto save flag. |
protected String |
basePath
Stores the base path. |
protected String |
fileName
Stores the file name. |
protected ReloadingStrategy |
strategy
Holds a reference to the reloading strategy. |
Fields inherited from class org.apache.commons.configuration.AbstractConfiguration |
END_TOKEN, START_TOKEN |
Constructor Summary | |
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. |
Method Summary | |
protected void |
addPropertyDirect(String key,
Object obj)
Adds a key/value pair to the map. |
void |
clearProperty(String key)
Remove a property from the configuration. |
boolean |
containsKey(String key)
Check if the configuration contains the specified key. |
protected void |
enterNoReload()
Enters the "No reloading mode". |
protected void |
exitNoReload()
Leaves the "No reloading mode". |
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. |
Iterator |
getKeys()
Get the list of the keys contained in the configuration. |
String |
getPath()
Returns the full path to the file this configuration is based on. |
Object |
getProperty(String key)
Gets a property from the configuration. |
ReloadingStrategy |
getReloadingStrategy()
Return the reloading strategy. |
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 |
reload()
Reload the configuration. |
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 if it's a file URL. |
void |
setAutoSave(boolean autoSave)
Enable or disable the automatical saving of modified properties to the disk. |
void |
setBasePath(String basePath)
Set 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 |
setPath(String path)
Sets the location of this configuration as a full path name. |
void |
setReloadingStrategy(ReloadingStrategy strategy)
Set the reloading strategy. |
void |
setURL(URL url)
Set the location of this configuration as a URL. |
Methods inherited from class org.apache.commons.configuration.BaseConfiguration |
clear |
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration |
addProperty, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getKeys, getList, getList, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, interpolate, interpolate, interpolateHelper, isThrowExceptionOnMissing, resolveContainerStore, setDelimiter, setProperty, setThrowExceptionOnMissing, subset |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.apache.commons.configuration.FileConfiguration |
load, save |
Methods inherited from interface org.apache.commons.configuration.Configuration |
addProperty, 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, setProperty, subset |
Field Detail |
protected String fileName
protected String basePath
protected boolean autoSave
protected ReloadingStrategy strategy
Constructor Detail |
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 fileMethod Detail |
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
public void setBasePath(String basePath)
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
org.apache.commons.configuration.FileConfiguration
autoSave
- true
to enable, false
to disablepublic boolean isAutoSave()
FileConfiguration
isAutoSave
in interface FileConfiguration
org.apache.commons.configuration.FileConfiguration
true
if auto-saving is enabled, false
otherwiseprotected void possiblySave()
protected void addPropertyDirect(String key, Object obj)
BaseConfiguration
addPropertyDirect
in class BaseConfiguration
org.apache.commons.configuration.BaseConfiguration
key
- key to use for mappingvalue
- object to storepublic void clearProperty(String key)
Configuration
clearProperty
in interface Configuration
clearProperty
in class BaseConfiguration
org.apache.commons.configuration.Configuration
key
- the key to remove along with corresponding value.public ReloadingStrategy getReloadingStrategy()
FileConfiguration
getReloadingStrategy
in interface FileConfiguration
org.apache.commons.configuration.FileConfiguration
public void setReloadingStrategy(ReloadingStrategy strategy)
FileConfiguration
setReloadingStrategy
in interface FileConfiguration
org.apache.commons.configuration.FileConfiguration
strategy
- the reloading strategy to usepublic void reload()
FileConfiguration
reload
in interface FileConfiguration
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()
public Object getProperty(String key)
Configuration
getProperty
in interface Configuration
getProperty
in class BaseConfiguration
org.apache.commons.configuration.Configuration
key
- property to retrievepublic boolean isEmpty()
Configuration
isEmpty
in interface Configuration
isEmpty
in class BaseConfiguration
org.apache.commons.configuration.Configuration
true
if the configuration contains no property,
false
otherwise.public boolean containsKey(String key)
Configuration
containsKey
in interface Configuration
containsKey
in class BaseConfiguration
org.apache.commons.configuration.Configuration
key
- the key whose presence in this configuration is to be testedtrue
if the configuration contains a value for this
key, false
otherwisepublic Iterator getKeys()
Configuration
remove()
method is specific to
a concrete implementation. It may remove the corresponding
property from the configuration, but this is not guaranteed. In any case
it is no replacement for calling
Configuration.clearProperty(String)
for this property. So it is
highly recommended to avoid using the iterator's remove()
method.getKeys
in interface Configuration
getKeys
in class BaseConfiguration
org.apache.commons.configuration.Configuration
public String getEncoding()
FileConfiguration
getEncoding
in interface FileConfiguration
org.apache.commons.configuration.FileConfiguration
public void setEncoding(String encoding)
FileConfiguration
setEncoding
in interface FileConfiguration
org.apache.commons.configuration.FileConfiguration
encoding
- the encoding to use
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |