com.ibm.ecm.configuration

Class DatabaseConfiguration

  1. java.lang.Object
  2. extended byorg.apache.commons.configuration.event.EventSource
  3. extended byorg.apache.commons.configuration.AbstractConfiguration
  4. extended byorg.apache.commons.configuration.DatabaseConfiguration
  5. extended bycom.ibm.ecm.configuration.DatabaseConfiguration
All implemented interfaces:
org.apache.commons.configuration.Configuration

  1. public class DatabaseConfiguration
  2. extends org.apache.commons.configuration.DatabaseConfiguration
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 is specified in the constructor.

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');
 
 
 Configuration config = new DatabaseConfiguration(datasource, "myconfig", "key", "value");
 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');
 
 
 Configuration config1 = new DatabaseConfiguration(datasource, "myconfigs", "name", "key", "value", "config1");
 String value1 = conf.getString("key1");
 
 Configuration config2 = new DatabaseConfiguration(datasource, "myconfigs", "name", "key", "value", "config2");
 String value2 = conf.getString("key2");
 
The configuration can be instructed to perform commits after database updates. This is achieved by setting the commits 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 the DataSource are in auto-commit mode.

Note: Like JDBC itself, protection against SQL injection is left to the user.

Since:
1.0
Version:
$Revision: 1158118 $, $Date: 2011-08-16 08:13:22 +0200 (Di, 16. Aug 2011) $

Field Summary

Fields inherited from class org.apache.commons.configuration.AbstractConfiguration
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN

Constructor Summary

Constructor and Description
DatabaseConfiguration(javax.sql.DataSource datasource,java.lang.String table,java.lang.String keyColumn,java.lang.String valueColumn)
Build a configuration from a table.
DatabaseConfiguration(javax.sql.DataSource datasource,java.lang.String table,java.lang.String keyColumn,java.lang.String valueColumn,boolean commits)
Creates a new instance of DatabaseConfiguration that operates on a database table containing a single configuration only.
DatabaseConfiguration(javax.sql.DataSource datasource,java.lang.String table,java.lang.String nameColumn,java.lang.String keyColumn,java.lang.String valueColumn,java.lang.String name)
Build a configuration from a table containing multiple configurations.
DatabaseConfiguration(javax.sql.DataSource datasource,java.lang.String table,java.lang.String nameColumn,java.lang.String keyColumn,java.lang.String valueColumn,java.lang.String name,boolean commits)
Creates a new instance of DatabaseConfiguration that operates on a database table containing multiple configurations.

Method Summary

Modifier and Type Method and Description
  1. java.lang.Object
getProperty(java.lang.String key)
Returns the value of the specified property.
Methods inherited from class org.apache.commons.configuration.DatabaseConfiguration
addProperty, addPropertyDirect, clear, clearPropertyDirect, containsKey, getConnection, getDatasource, getKeys, isDoCommits, isEmpty
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
addErrorLogListener, append, clearProperty, 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, setProperty, setThrowExceptionOnMissing, subset
Methods inherited from class org.apache.commons.configuration.event.EventSource
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, clone, createErrorEvent, createEvent, fireError, fireEvent, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEvents
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

DatabaseConfiguration

  1. public DatabaseConfiguration(javax.sql.DataSource datasource,
  2. java.lang.String table,
  3. java.lang.String nameColumn,
  4. java.lang.String keyColumn,
  5. java.lang.String valueColumn,
  6. java.lang.String name)
Build a configuration from a table containing multiple configurations. No commits are performed by the new configuration instance.
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

  1. public DatabaseConfiguration(javax.sql.DataSource datasource,
  2. java.lang.String table,
  3. java.lang.String nameColumn,
  4. java.lang.String keyColumn,
  5. java.lang.String valueColumn,
  6. java.lang.String name,
  7. boolean commits)
Creates a new instance of DatabaseConfiguration that operates on a database 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
commits - a flag whether the configuration should perform a commit after a database update

DatabaseConfiguration

  1. public DatabaseConfiguration(javax.sql.DataSource datasource,
  2. java.lang.String table,
  3. java.lang.String keyColumn,
  4. java.lang.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

DatabaseConfiguration

  1. public DatabaseConfiguration(javax.sql.DataSource datasource,
  2. java.lang.String table,
  3. java.lang.String keyColumn,
  4. java.lang.String valueColumn,
  5. boolean commits)
Creates a new instance of DatabaseConfiguration that operates on a database table containing a single configuration only.
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
commits - a flag whether the configuration should perform a commit after a database update

Method Detail

getProperty

  1. public java.lang.Object getProperty( java.lang.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.
Specified by:
getProperty in interface org.apache.commons.configuration.Configuration
Overrides:
getProperty in class org.apache.commons.configuration.DatabaseConfiguration
Parameters:
key - the key of the desired property
Returns:
the value of this property