org.apache.commons.configuration

Class DatabaseConfiguration

public class DatabaseConfiguration extends AbstractConfiguration

Configuration stored in a database.

Since: 1.0

Version: $Revision: 514234 $, $Date: 2007-03-03 21:18:14 +0100 (Sa, 03 Mrz 2007) $

Author: Emmanuel Bourg

Field Summary
DataSourcedatasource
The datasource to connect to the database.
StringkeyColumn
The column containing the keys.
Stringname
The name of the configuration.
StringnameColumn
The column containing the name of the configuration.
Stringtable
The name of the table containing the configurations.
StringvalueColumn
The column containing the values.
Constructor Summary
DatabaseConfiguration(DataSource datasource, String table, String nameColumn, String keyColumn, String valueColumn, String name)
Build a configuration from a table containing multiple configurations.
DatabaseConfiguration(DataSource datasource, String table, String keyColumn, String valueColumn)
Build a configuration from a table.-
Method Summary
voidaddProperty(String key, Object value)
Adds a property to this configuration.
protected voidaddPropertyDirect(String key, Object obj)
Adds a property to this configuration.
voidclear()
Removes all entries from this configuration.
voidclearProperty(String key)
Removes the specified value from this configuration.
voidcloseQuietly(Connection conn, Statement stmt)
Close a Connection and, Statement.
booleancontainsKey(String key)
Checks whether this configuration contains the specified key.
protected ConnectiongetConnection()
Returns a Connection object.
DataSourcegetDatasource()
Returns the used DataSource object.
IteratorgetKeys()
Returns an iterator with the names of all properties contained in this configuration.
ObjectgetProperty(String key)
Returns the value of the specified property.
booleanisEmpty()
Checks if this configuration is empty.

Field Detail

datasource

private DataSource datasource
The datasource to connect to the database.

keyColumn

private String keyColumn
The column containing the keys.

name

private String name
The name of the configuration.

nameColumn

private String nameColumn
The column containing the name of the configuration.

table

private String table
The name of the table containing the configurations.

valueColumn

private String valueColumn
The column containing the values.

Constructor Detail

DatabaseConfiguration

public DatabaseConfiguration(DataSource datasource, String table, String nameColumn, String keyColumn, String valueColumn, String name)
Build a configuration from a table containing multiple configurations.

Parameters: datasource the datasource to connect to the database table the name of the table containing the configurations nameColumn the column containing the name of the configuration keyColumn the column containing the keys of the configuration valueColumn the column containing the values of the configuration name the name of the configuration

DatabaseConfiguration

public DatabaseConfiguration(DataSource datasource, String table, String keyColumn, String valueColumn)
Build a configuration from a table.-

Parameters: datasource the datasource to connect to the database table the name of the table containing the configurations keyColumn the column containing the keys of the configuration valueColumn the column containing the values of the configuration

Method Detail

addProperty

public void addProperty(String key, Object value)
Adds a property to this configuration. This implementation will temporarily disable list delimiter parsing, so that even if the value contains the list delimiter, only a single record will be written into the managed table. The implementation of getProperty() will take care about delimiters. So list delimiters are fully supported by DatabaseConfiguration, but internally treated a bit differently.

Parameters: key the key of the new property value the value to be added

addPropertyDirect

protected void addPropertyDirect(String key, Object obj)
Adds a property to this configuration. If this causes a database error, an error event will be generated of type EVENT_ADD_PROPERTY with the causing exception. The event's propertyName is set to the passed in property key, the propertyValue points to the passed in value.

Parameters: key the property key obj the value of the property to add

clear

public void clear()
Removes all entries from this configuration. If this causes a database error, an error event will be generated of type EVENT_CLEAR with the causing exception. Both the event's propertyName and the propertyValue will be undefined.

clearProperty

public void clearProperty(String key)
Removes the specified value from this configuration. If this causes a database error, an error event will be generated of type EVENT_CLEAR_PROPERTY with the causing exception. The event's propertyName will be set to the passed in key, the propertyValue will be undefined.

Parameters: key the key of the property to be removed

closeQuietly

private void closeQuietly(Connection conn, Statement stmt)
Close a Connection and, Statement. Avoid closing if null and hide any SQLExceptions that occur.

Parameters: conn The database connection to close stmt The statement to close

containsKey

public boolean containsKey(String key)
Checks whether this configuration contains the specified key. If this causes a database error, an error event will be generated of type EVENT_READ_PROPERTY with the causing exception. The event's propertyName will be set to the passed in key, the propertyValue will be undefined.

Parameters: key the key to be checked

Returns: a flag whether this key is defined

getConnection

protected Connection getConnection()
Returns a Connection object. This method is called when ever the database is to be accessed. This implementation returns a connection from the current DataSource.

Returns: the Connection object to be used

Throws: SQLException if an error occurs

Since: 1.4

getDatasource

public DataSource getDatasource()
Returns the used DataSource object.

Returns: the data source

Since: 1.4

getKeys

public Iterator getKeys()
Returns an iterator with the names of all properties contained in this configuration. If this causes a database error, an error event will be generated of type EVENT_READ_PROPERTY with the causing exception. Both the event's propertyName and the propertyValue will be undefined.

Returns: an iterator with the contained keys (an empty iterator in case of an error)

getProperty

public Object getProperty(String key)
Returns the value of the specified property. If this causes a database error, an error event will be generated of type EVENT_READ_PROPERTY with the causing exception. The event's propertyName is set to the passed in property key, the propertyValue is undefined.

Parameters: key the key of the desired property

Returns: the value of this property

isEmpty

public boolean isEmpty()
Checks if this configuration is empty. If this causes a database error, an error event will be generated of type EVENT_READ_PROPERTY with the causing exception. Both the event's propertyName and propertyValue will be undefined.

Returns: a flag whether this configuration is empty.