public class DatabaseConfiguration extends AbstractConfiguration
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 the database()
method of the Parameters
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 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.
Constructor and Description |
---|
DatabaseConfiguration()
Creates a new instance of
DatabaseConfiguration . |
Modifier and Type | Method and Description |
---|---|
protected void |
addPropertyDirect(String key,
Object obj)
Adds a property to this configuration.
|
protected void |
addPropertyInternal(String key,
Object value)
Adds a property to this configuration.
|
protected void |
clearInternal()
Removes all entries from this configuration.
|
protected void |
clearPropertyDirect(String key)
Removes the specified value from this configuration.
|
protected void |
close(Connection conn,
Statement stmt,
ResultSet rs)
Close the specified database objects.
|
protected boolean |
containsKeyInternal(String key)
Checks whether this configuration contains the specified key.
|
protected Object |
extractPropertyValue(ResultSet rs)
Extracts the value of a property from the given result set.
|
String |
getConfigurationName()
Returns the name of this configuration instance.
|
String |
getConfigurationNameColumn()
Returns the name of the table column with the configuration name.
|
DataSource |
getDatasource()
Returns the used
DataSource object. |
DataSource |
getDataSource()
Returns the
DataSource for obtaining database connections. |
String |
getKeyColumn()
Returns the name of the column containing the configuration keys.
|
protected Iterator<String> |
getKeysInternal()
Returns an iterator with the names of all properties contained in this
configuration.
|
protected Object |
getPropertyInternal(String key)
Returns the value of the specified property.
|
String |
getTable()
Returns the name of the table containing configuration data.
|
String |
getValueColumn()
Returns 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(String configurationName)
Sets the name of this configuration instance.
|
void |
setConfigurationNameColumn(String configurationNameColumn)
Sets the name of the table column with the configuration name.
|
void |
setDataSource(DataSource dataSource)
Sets the
DataSource for obtaining database connections. |
void |
setKeyColumn(String keyColumn)
Sets the name of the column containing the configuration keys.
|
void |
setTable(String table)
Sets the name of the table containing configuration data.
|
void |
setValueColumn(String valueColumn)
Sets the name of the column containing the configuration values.
|
addErrorLogListener, addProperty, append, beginRead, beginWrite, clear, clearProperty, cloneInterpolator, containsKey, copy, endRead, endWrite, get, get, getArray, getArray, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getCollection, getCollection, getConfigurationDecoder, getConversionHandler, getDouble, getDouble, getDouble, getEncodedString, getEncodedString, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getKeys, 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
addEventListener, clearErrorListeners, clearEventListeners, clone, copyEventListeners, createErrorEvent, createEvent, fireError, fireEvent, getEventListenerRegistrations, getEventListeners, isDetailEvents, removeEventListener, setDetailEvents
public DatabaseConfiguration()
DatabaseConfiguration
.public DataSource getDataSource()
DataSource
for obtaining database connections.DataSource
public void setDataSource(DataSource dataSource)
DataSource
for obtaining database connections.dataSource
- the DataSource
public String getTable()
public void setTable(String table)
table
- the table namepublic String getConfigurationNameColumn()
public void setConfigurationNameColumn(String configurationNameColumn)
configurationNameColumn
- the name of the column with the
configuration namepublic String getKeyColumn()
public void setKeyColumn(String keyColumn)
keyColumn
- the name of the key columnpublic String getValueColumn()
public void setValueColumn(String valueColumn)
valueColumn
- the name of the value columnpublic String getConfigurationName()
public void setConfigurationName(String configurationName)
configurationName
- the name of this configurationpublic boolean isAutoCommit()
public void setAutoCommit(boolean autoCommit)
autoCommit
- the auto commit flagprotected Object getPropertyInternal(String key)
READ
with the causing exception. The
event's propertyName
is set to the passed in property key,
the propertyValue
is undefined.getPropertyInternal
in class AbstractConfiguration
key
- the key of the desired propertyprotected void addPropertyDirect(String key, Object obj)
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.addPropertyDirect
in class AbstractConfiguration
key
- the property keyobj
- the value of the property to addprotected void addPropertyInternal(String key, Object value)
getProperty()
takes care about delimiters. So list delimiters are fully supported
by DatabaseConfiguration
, but internally treated a bit
differently.addPropertyInternal
in class AbstractConfiguration
key
- the key of the new propertyvalue
- the value to be addedprotected boolean isEmptyInternal()
READ
with the causing exception. Both the event's propertyName
and propertyValue
will be undefined.isEmptyInternal
in class AbstractConfiguration
protected boolean containsKeyInternal(String key)
READ
with the causing exception. The
event's propertyName
will be set to the passed in key, the
propertyValue
will be undefined.containsKeyInternal
in class AbstractConfiguration
key
- the key to be checkedprotected void clearPropertyDirect(String key)
CLEAR_PROPERTY
with the causing exception. The
event's propertyName
will be set to the passed in key, the
propertyValue
will be undefined.clearPropertyDirect
in class AbstractConfiguration
key
- the key of the property to be removedprotected void clearInternal()
CLEAR
with the causing exception. Both the
event's propertyName
and the propertyValue
will be undefined.clearInternal
in class AbstractConfiguration
protected Iterator<String> getKeysInternal()
READ
with the causing exception. Both the
event's propertyName
and the propertyValue
will be undefined.getKeysInternal
in class AbstractConfiguration
public DataSource getDatasource()
DataSource
object.protected void close(Connection conn, Statement stmt, ResultSet rs)
conn
- The database connection to closestmt
- The statement to closers
- the result set to closeprotected Object extractPropertyValue(ResultSet rs) throws SQLException
ResultSet
was created by a SELECT statement on the underlying
database table. This implementation reads the value of the column
determined by the valueColumn
property. Normally the contained
value is directly returned. However, if it is of type CLOB
, text
is extracted as string.rs
- the current ResultSet
SQLException
- if an error occursCopyright © 2001–2020 The Apache Software Foundation. All rights reserved.