Class DatabaseConfiguration
- java.lang.Object
-
- org.apache.commons.configuration2.event.BaseEventSource
-
- org.apache.commons.configuration2.AbstractConfiguration
-
- org.apache.commons.configuration2.DatabaseConfiguration
-
- All Implemented Interfaces:
Configuration
,EventSource
,ImmutableConfiguration
,SynchronizerSupport
public class DatabaseConfiguration extends AbstractConfiguration
Configuration stored in a database. The properties are retrieved from a table containing at least one column for the keys, and one column for the values. It's possible to store several configurations in the same table by adding a column containing the name of the configuration. The name of the table and the columns have to be specified using the corresponding properties.The recommended way to create an instance of
DatabaseConfiguration
is to use a configuration builder. The builder is configured with a special parameters object defining the database structures used by the configuration. Such an object can be created using thedatabase()
method of theParameters
class. See the examples below for more details.Example 1 - One configuration per table
CREATE TABLE myconfig ( `key` VARCHAR NOT NULL PRIMARY KEY, `value` VARCHAR ); INSERT INTO myconfig (key, value) VALUES ('foo', 'bar'); BasicConfigurationBuilder<DatabaseConfiguration> builder = new BasicConfigurationBuilder<DatabaseConfiguration>(DatabaseConfiguration.class); builder.configure( Parameters.database() .setDataSource(dataSource) .setTable("myconfig") .setKeyColumn("key") .setValueColumn("value") ); Configuration config = builder.getConfiguration(); String value = config.getString("foo");
Example 2 - Multiple configurations per table
CREATE TABLE myconfigs ( `name` VARCHAR NOT NULL, `key` VARCHAR NOT NULL, `value` VARCHAR, CONSTRAINT sys_pk_myconfigs PRIMARY KEY (`name`, `key`) ); INSERT INTO myconfigs (name, key, value) VALUES ('config1', 'key1', 'value1'); INSERT INTO myconfigs (name, key, value) VALUES ('config2', 'key2', 'value2'); BasicConfigurationBuilder<DatabaseConfiguration> builder = new BasicConfigurationBuilder<DatabaseConfiguration>(DatabaseConfiguration.class); builder.configure( Parameters.database() .setDataSource(dataSource) .setTable("myconfigs") .setKeyColumn("key") .setValueColumn("value") .setConfigurationNameColumn("name") .setConfigurationName("config1") ); Configuration config1 = new DatabaseConfiguration(dataSource, "myconfigs", "name", "key", "value", "config1"); String value1 = conf.getString("key1");
The configuration can be instructed to perform commits after database updates. This is achieved by setting thecommits
parameter of the constructors to true. If commits should not be performed (which is the default behavior), it should be ensured that the connections returned by theDataSource
are in auto-commit mode.Note: Like JDBC itself, protection against SQL injection is left to the user.
- Since:
- 1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
DatabaseConfiguration.AbstractJdbcOperation<T>
An internally used helper class for simplifying database access through plain JDBC.
-
Field Summary
Fields Modifier and Type Field Description private boolean
autoCommit
A flag whether commits should be performed by this configuration.private java.lang.String
configurationName
The configurationName of the configuration.private java.lang.String
configurationNameColumn
The column containing the configurationName of the configuration.private javax.sql.DataSource
dataSource
The data source to connect to the database.private java.lang.String
keyColumn
The column containing the keys.private static java.lang.String
SQL_CLEAR
Constant for the statement used by clear.private static java.lang.String
SQL_CLEAR_PROPERTY
Constant for the statement used by clearProperty.private static java.lang.String
SQL_GET_KEYS
Constant for the statement used by getKeys.private static java.lang.String
SQL_GET_PROPERTY
Constant for the statement used by getProperty.private static java.lang.String
SQL_IS_EMPTY
Constant for the statement used by isEmpty.private java.lang.String
table
The configurationName of the table containing the configurations.private java.lang.String
valueColumn
The column containing the values.
-
Constructor Summary
Constructors Constructor Description DatabaseConfiguration()
Creates a new instance ofDatabaseConfiguration
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
addPropertyDirect(java.lang.String key, java.lang.Object obj)
Adds a property to this configuration.protected void
addPropertyInternal(java.lang.String key, java.lang.Object value)
Adds a property to this configuration.protected void
clearInternal()
Removes all entries from this configuration.protected void
clearPropertyDirect(java.lang.String key)
Removes the specified value from this configuration.protected void
close(java.sql.Connection conn, java.sql.Statement stmt, java.sql.ResultSet rs)
Close the specified database objects.protected boolean
containsKeyInternal(java.lang.String key)
Checks whether this configuration contains the specified key.protected boolean
containsValueInternal(java.lang.Object value)
Tests whether this configuration contains one or more matches to this value.private static java.lang.Object
convertClob(java.sql.Clob clob)
Converts a CLOB to a string.protected java.lang.Object
extractPropertyValue(java.sql.ResultSet rs)
Extracts the value of a property from the given result set.java.lang.String
getConfigurationName()
Gets the name of this configuration instance.java.lang.String
getConfigurationNameColumn()
Gets the name of the table column with the configuration name.javax.sql.DataSource
getDatasource()
Deprecated.UsegetDataSource()
javax.sql.DataSource
getDataSource()
Gets theDataSource
for obtaining database connections.java.lang.String
getKeyColumn()
Gets the name of the column containing the configuration keys.protected java.util.Iterator<java.lang.String>
getKeysInternal()
Returns an iterator with the names of all properties contained in this configuration.protected java.lang.Object
getPropertyInternal(java.lang.String key)
Gets the value of the specified property.java.lang.String
getTable()
Gets the name of the table containing configuration data.java.lang.String
getValueColumn()
Gets the name of the column containing the configuration values.boolean
isAutoCommit()
Returns a flag whether this configuration performs commits after database updates.protected boolean
isEmptyInternal()
Checks if this configuration is empty.void
setAutoCommit(boolean autoCommit)
Sets the auto commit flag.void
setConfigurationName(java.lang.String configurationName)
Sets the name of this configuration instance.void
setConfigurationNameColumn(java.lang.String configurationNameColumn)
Sets the name of the table column with the configuration name.void
setDataSource(javax.sql.DataSource dataSource)
Sets theDataSource
for obtaining database connections.void
setKeyColumn(java.lang.String keyColumn)
Sets the name of the column containing the configuration keys.void
setTable(java.lang.String table)
Sets the name of the table containing configuration data.void
setValueColumn(java.lang.String valueColumn)
Sets the name of the column containing the configuration values.-
Methods inherited from class org.apache.commons.configuration2.AbstractConfiguration
addErrorLogListener, addProperty, append, beginRead, beginWrite, clear, clearProperty, cloneInterpolator, contains, containsKey, containsValue, copy, endRead, endWrite, get, get, getArray, getArray, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getCollection, getCollection, getConfigurationDecoder, getConversionHandler, getDouble, getDouble, getDouble, getDuration, getDuration, getEncodedString, getEncodedString, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getKeys, getKeys, getKeysInternal, getKeysInternal, getList, getList, getList, getList, getListDelimiterHandler, getLogger, getLong, getLong, getLong, getProperties, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, getSynchronizer, immutableSubset, initLogger, installInterpolator, interpolate, interpolate, interpolatedConfiguration, isEmpty, isScalarValue, isThrowExceptionOnMissing, lock, setConfigurationDecoder, setConversionHandler, setDefaultLookups, setInterpolator, setListDelimiterHandler, setLogger, setParentInterpolator, setPrefixLookups, setProperty, setPropertyInternal, setSynchronizer, setThrowExceptionOnMissing, size, sizeInternal, subset, unlock
-
Methods inherited from class org.apache.commons.configuration2.event.BaseEventSource
addEventListener, clearErrorListeners, clearEventListeners, clone, copyEventListeners, createErrorEvent, createEvent, fireError, fireEvent, getEventListenerRegistrations, getEventListeners, isDetailEvents, removeEventListener, setDetailEvents
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.configuration2.ImmutableConfiguration
getEnum, getEnum
-
-
-
-
Field Detail
-
SQL_GET_PROPERTY
private static final java.lang.String SQL_GET_PROPERTY
Constant for the statement used by getProperty.- See Also:
- Constant Field Values
-
SQL_IS_EMPTY
private static final java.lang.String SQL_IS_EMPTY
Constant for the statement used by isEmpty.- See Also:
- Constant Field Values
-
SQL_CLEAR_PROPERTY
private static final java.lang.String SQL_CLEAR_PROPERTY
Constant for the statement used by clearProperty.- See Also:
- Constant Field Values
-
SQL_CLEAR
private static final java.lang.String SQL_CLEAR
Constant for the statement used by clear.- See Also:
- Constant Field Values
-
SQL_GET_KEYS
private static final java.lang.String SQL_GET_KEYS
Constant for the statement used by getKeys.- See Also:
- Constant Field Values
-
dataSource
private javax.sql.DataSource dataSource
The data source to connect to the database.
-
table
private java.lang.String table
The configurationName of the table containing the configurations.
-
configurationNameColumn
private java.lang.String configurationNameColumn
The column containing the configurationName of the configuration.
-
keyColumn
private java.lang.String keyColumn
The column containing the keys.
-
valueColumn
private java.lang.String valueColumn
The column containing the values.
-
configurationName
private java.lang.String configurationName
The configurationName of the configuration.
-
autoCommit
private boolean autoCommit
A flag whether commits should be performed by this configuration.
-
-
Method Detail
-
convertClob
private static java.lang.Object convertClob(java.sql.Clob clob) throws java.sql.SQLException
Converts a CLOB to a string.- Parameters:
clob
- the CLOB to be converted- Returns:
- the extracted string value
- Throws:
java.sql.SQLException
- if an error occurs
-
addPropertyDirect
protected void addPropertyDirect(java.lang.String key, java.lang.Object obj)
Adds a property to this configuration. If this causes a database error, an error event will be generated of typeADD_PROPERTY
with the causing exception. The event'spropertyName
is set to the passed in property key, thepropertyValue
points to the passed in value.- Specified by:
addPropertyDirect
in classAbstractConfiguration
- Parameters:
key
- the property keyobj
- the value of the property to add
-
addPropertyInternal
protected void addPropertyInternal(java.lang.String key, java.lang.Object value)
Adds a property to this configuration. This implementation temporarily disables list delimiter parsing, so that even if the value contains the list delimiter, only a single record is written into the managed table. The implementation ofgetProperty()
takes care about delimiters. So list delimiters are fully supported byDatabaseConfiguration
, but internally treated a bit differently.- Overrides:
addPropertyInternal
in classAbstractConfiguration
- Parameters:
key
- the key of the new propertyvalue
- the value to be added
-
clearInternal
protected void clearInternal()
Removes all entries from this configuration. If this causes a database error, an error event will be generated of typeCLEAR
with the causing exception. Both the event'spropertyName
and thepropertyValue
will be undefined.- Overrides:
clearInternal
in classAbstractConfiguration
-
clearPropertyDirect
protected void clearPropertyDirect(java.lang.String key)
Removes the specified value from this configuration. If this causes a database error, an error event will be generated of typeCLEAR_PROPERTY
with the causing exception. The event'spropertyName
will be set to the passed in key, thepropertyValue
will be undefined.- Specified by:
clearPropertyDirect
in classAbstractConfiguration
- Parameters:
key
- the key of the property to be removed
-
close
protected void close(java.sql.Connection conn, java.sql.Statement stmt, java.sql.ResultSet rs)
Close the specified database objects. Avoid closing if null and hide any SQLExceptions that occur.- Parameters:
conn
- The database connection to closestmt
- The statement to closers
- the result set to close
-
containsKeyInternal
protected boolean containsKeyInternal(java.lang.String key)
Checks whether this configuration contains the specified key. If this causes a database error, an error event will be generated of typeREAD
with the causing exception. The event'spropertyName
will be set to the passed in key, thepropertyValue
will be undefined.- Specified by:
containsKeyInternal
in classAbstractConfiguration
- Parameters:
key
- the key to be checked- Returns:
- a flag whether this key is defined
-
containsValueInternal
protected boolean containsValueInternal(java.lang.Object value)
Tests whether this configuration contains one or more matches to this value. This operation stops at first match but may be more expensive than the containsKey method.- Specified by:
containsValueInternal
in classAbstractConfiguration
- Parameters:
value
- the value in question- Returns:
true
if and only if some key maps to thevalue
argument in this configuration as determined by theequals
method;false
otherwise.- Since:
- 2.11.0
-
extractPropertyValue
protected java.lang.Object extractPropertyValue(java.sql.ResultSet rs) throws java.sql.SQLException
Extracts the value of a property from the given result set. The passed inResultSet
was created by a SELECT statement on the underlying database table. This implementation reads the value of the column determined by thevalueColumn
property. Normally the contained value is directly returned. However, if it is of typeCLOB
, text is extracted as string.- Parameters:
rs
- the currentResultSet
- Returns:
- the value of the property column
- Throws:
java.sql.SQLException
- if an error occurs
-
getConfigurationName
public java.lang.String getConfigurationName()
Gets the name of this configuration instance.- Returns:
- the name of this configuration
-
getConfigurationNameColumn
public java.lang.String getConfigurationNameColumn()
Gets the name of the table column with the configuration name.- Returns:
- the name of the configuration name column
-
getDatasource
@Deprecated public javax.sql.DataSource getDatasource()
Deprecated.UsegetDataSource()
Gets the usedDataSource
object.- Returns:
- the data source
- Since:
- 1.4
-
getDataSource
public javax.sql.DataSource getDataSource()
Gets theDataSource
for obtaining database connections.- Returns:
- the
DataSource
-
getKeyColumn
public java.lang.String getKeyColumn()
Gets the name of the column containing the configuration keys.- Returns:
- the name of the key column
-
getKeysInternal
protected java.util.Iterator<java.lang.String> getKeysInternal()
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 typeREAD
with the causing exception. Both the event'spropertyName
and thepropertyValue
will be undefined.- Specified by:
getKeysInternal
in classAbstractConfiguration
- Returns:
- an iterator with the contained keys (an empty iterator in case of an error)
-
getPropertyInternal
protected java.lang.Object getPropertyInternal(java.lang.String key)
Gets the value of the specified property. If this causes a database error, an error event will be generated of typeREAD
with the causing exception. The event'spropertyName
is set to the passed in property key, thepropertyValue
is undefined.- Specified by:
getPropertyInternal
in classAbstractConfiguration
- Parameters:
key
- the key of the desired property- Returns:
- the value of this property
-
getTable
public java.lang.String getTable()
Gets the name of the table containing configuration data.- Returns:
- the name of the table to be queried
-
getValueColumn
public java.lang.String getValueColumn()
Gets the name of the column containing the configuration values.- Returns:
- the name of the value column
-
isAutoCommit
public boolean isAutoCommit()
Returns a flag whether this configuration performs commits after database updates.- Returns:
- a flag whether commits are performed
-
isEmptyInternal
protected boolean isEmptyInternal()
Checks if this configuration is empty. If this causes a database error, an error event will be generated of typeREAD
with the causing exception. Both the event'spropertyName
andpropertyValue
will be undefined.- Specified by:
isEmptyInternal
in classAbstractConfiguration
- Returns:
- a flag whether this configuration is empty.
-
setAutoCommit
public void setAutoCommit(boolean autoCommit)
Sets the auto commit flag. If set to true, this configuration performs a commit after each database update.- Parameters:
autoCommit
- the auto commit flag
-
setConfigurationName
public void setConfigurationName(java.lang.String configurationName)
Sets the name of this configuration instance.- Parameters:
configurationName
- the name of this configuration
-
setConfigurationNameColumn
public void setConfigurationNameColumn(java.lang.String configurationNameColumn)
Sets the name of the table column with the configuration name.- Parameters:
configurationNameColumn
- the name of the column with the configuration name
-
setDataSource
public void setDataSource(javax.sql.DataSource dataSource)
Sets theDataSource
for obtaining database connections.- Parameters:
dataSource
- theDataSource
-
setKeyColumn
public void setKeyColumn(java.lang.String keyColumn)
Sets the name of the column containing the configuration keys.- Parameters:
keyColumn
- the name of the key column
-
setTable
public void setTable(java.lang.String table)
Sets the name of the table containing configuration data.- Parameters:
table
- the table name
-
setValueColumn
public void setValueColumn(java.lang.String valueColumn)
Sets the name of the column containing the configuration values.- Parameters:
valueColumn
- the name of the value column
-
-