public class DatabaseConfiguration extends AbstractConfiguration
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");
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.
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN
Constructor and Description |
---|
DatabaseConfiguration(DataSource datasource,
String table,
String keyColumn,
String valueColumn)
Build a configuration from a table.
|
DatabaseConfiguration(DataSource datasource,
String table,
String keyColumn,
String valueColumn,
boolean commits)
Creates a new instance of
DatabaseConfiguration that
operates on a database table containing a single configuration only. |
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 nameColumn,
String keyColumn,
String valueColumn,
String name,
boolean commits)
Creates a new instance of
DatabaseConfiguration that operates on
a database table containing multiple configurations. |
Modifier and Type | Method and Description |
---|---|
void |
addProperty(String key,
Object value)
Adds a property to this configuration.
|
protected void |
addPropertyDirect(String key,
Object obj)
Adds a property to this configuration.
|
void |
clear()
Removes all entries from this configuration.
|
protected void |
clearPropertyDirect(String key)
Removes the specified value from this configuration.
|
boolean |
containsKey(String key)
Checks whether this configuration contains the specified key.
|
protected Connection |
getConnection()
Deprecated.
Use a custom data source to change the connection used by the
class. To be removed in Commons Configuration 2.0
|
DataSource |
getDatasource()
Returns the used
DataSource object. |
Iterator<String> |
getKeys()
Returns an iterator with the names of all properties contained in this
configuration.
|
Object |
getProperty(String key)
Returns the value of the specified property.
|
boolean |
isDoCommits()
Returns a flag whether this configuration performs commits after database
updates.
|
boolean |
isEmpty()
Checks if this configuration is empty.
|
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
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, clone, createErrorEvent, createEvent, fireError, fireEvent, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEvents
public DatabaseConfiguration(DataSource datasource, String table, String nameColumn, String keyColumn, String valueColumn, String name)
datasource
- the datasource to connect to the databasetable
- the name of the table containing the configurationsnameColumn
- the column containing the name of the configurationkeyColumn
- the column containing the keys of the configurationvalueColumn
- the column containing the values of the configurationname
- the name of the configurationpublic DatabaseConfiguration(DataSource datasource, String table, String nameColumn, String keyColumn, String valueColumn, String name, boolean commits)
DatabaseConfiguration
that operates on
a database table containing multiple configurations.datasource
- the DataSource
to connect to the databasetable
- the name of the table containing the configurationsnameColumn
- the column containing the name of the configurationkeyColumn
- the column containing the keys of the configurationvalueColumn
- the column containing the values of the configurationname
- the name of the configurationcommits
- a flag whether the configuration should perform a commit
after a database updatepublic DatabaseConfiguration(DataSource datasource, String table, String keyColumn, String valueColumn)
datasource
- the datasource to connect to the databasetable
- the name of the table containing the configurationskeyColumn
- the column containing the keys of the configurationvalueColumn
- the column containing the values of the configurationpublic DatabaseConfiguration(DataSource datasource, String table, String keyColumn, String valueColumn, boolean commits)
DatabaseConfiguration
that
operates on a database table containing a single configuration only.datasource
- the DataSource
to connect to the databasetable
- the name of the table containing the configurationskeyColumn
- the column containing the keys of the configurationvalueColumn
- the column containing the values of the configurationcommits
- a flag whether the configuration should perform a commit
after a database updatepublic boolean isDoCommits()
public Object getProperty(String key)
EVENT_READ_PROPERTY
with the causing exception. The
event's propertyName
is set to the passed in property key,
the propertyValue
is undefined.key
- the key of the desired propertyprotected void addPropertyDirect(String key, Object obj)
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.addPropertyDirect
in class AbstractConfiguration
key
- the property keyobj
- the value of the property to addpublic void addProperty(String key, Object value)
getProperty()
will take care about delimiters. So list delimiters are fully supported
by DatabaseConfiguration
, but internally treated a bit
differently.addProperty
in interface Configuration
addProperty
in class AbstractConfiguration
key
- the key of the new propertyvalue
- the value to be addedpublic boolean isEmpty()
EVENT_READ_PROPERTY
with the causing exception. Both the event's propertyName
and propertyValue
will be undefined.public boolean containsKey(String key)
EVENT_READ_PROPERTY
with the causing exception. The
event's propertyName
will be set to the passed in key, the
propertyValue
will be undefined.key
- the key to be checkedprotected void clearPropertyDirect(String key)
EVENT_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 removedpublic void clear()
EVENT_CLEAR
with the causing exception. Both the
event's propertyName
and the propertyValue
will be undefined.clear
in interface Configuration
clear
in class AbstractConfiguration
public Iterator<String> getKeys()
EVENT_READ_PROPERTY
with the causing exception. Both the
event's propertyName
and the propertyValue
will be undefined.public DataSource getDatasource()
DataSource
object.@Deprecated protected Connection getConnection() throws SQLException
Connection
object. This method is called when
ever the database is to be accessed. This implementation returns a
connection from the current DataSource
.Connection
object to be usedSQLException
- if an error occursCopyright © 2001–2013 The Apache Software Foundation. All rights reserved.