Class DbConfigurationManager<CommonAccessException extends Exception>
- Type Parameters:
CommonAccessException- the common access exception type of the underlyingDataSourceand so of theDbConfigurationManagerto be created.
The DbConfigurationManager manages configuration values in a database table and an in-memory cache.
One must specify a DataSource. One can
set a different table name. One can
create the table if it does not exist.
A configuration value is basically a String. There are several methods for conversion of other values to
string during get() and set() and it is highly recommended to rely on these specialized get()-methods and according
objects it set() and not to convert them in the business code by yourself. These classes are supported:
A configuration value can be null. This is treated as a valid value and it is a difference, whether a
configuration value not exists or exists and is set to null. For Strings null and an empty
String both are stored as null. All get()-methods will return null in this case. So there is
(intentionally) no way to store an empty String.
A configuration value is identified by a key that is a HierarchicalName. It is highly recommended to group
your configuration values according to their sense and common usage the same way as you group java-classes into
packages. All configuration values that belong to the same sub-tree can be loaded together.
The get()-methods have an optional StorageType-parameter, that controls whether a configuration value
should be loaded from the database, from the in-memory cache or
from the database only if it is not found in the in-memory cache. The
methods without the StorageType-parameter behave like StorageType.DEFAULT, i.e. they use the
StorageType that was specified to use as the default during construction of the
DbConfigurationManager (@see
DbConfigurationManagerConstructorParameters.setDefaultStorageType(StorageType)).
Usual approaches are:
- Simply use the get()-methods with
StorageType.DB_IF_NOTHING_IN_MEMORYif there are only a few configuration values at all. Loada whole hierarchy of configuration values at some initialization point and use them later withStorageType.MEMORYorStorageType.DB_IF_NOTHING_IN_MEMORY.
A root name can be provided in
which case all entry-keys will start with this root name. If the key specified as a method parameter already
starts with the root name it will be used unchanged, else the
parameter will be added to the root name before use.
The configuration values can be encrypted/decrypted automatically. Simply
specify a Cipher and Pair of Keys
.
One can fine-tune what should happen if a requested configuration value is not found.
This DbConfigurationManager does no transaction handling. It simple executes its SQL-statements and relies on
a suitable surrounding transaction management. If used in a JEE-container this is usually exactly the desired
behavior. If used in a stand-alone application, one should care about the transaction around the invocations of
methods of this class.
Apart from transactional aspects the methods are synchronized, so an instance of this DbConfigurationManager
can be used in several threads concurrently.
- Author:
- agz
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe maximum string length (toString()-result) of aHierarchicalNameto be used as a key by thisDbConfigurationManager.static final StringThe regular expression, a table name must match.static final intThe maximum string length of the serialized (and if encryption is active encrypted) representation of a configuration value to be used by thisDbConfigurationManager. -
Constructor Summary
ConstructorsConstructorDescriptionDbConfigurationManager(DbConfigurationManagerConstructorParameters<CommonAccessException> parameters) Creates a newDbConfigurationManagerwith the specifiedDbConfigurationManagerConstructorParameters. -
Method Summary
Modifier and TypeMethodDescriptionvoidTries to find out whether the underlying database table exists and if not so to create it by executing an SQL-CREATE-TABLE-statement.intdelete(HierarchicalName hierarchicalName, boolean includingSubvalues) Deletes the configuration value for the specifiedHierarchicalNamefrom the database and internal cache.booleanexists(HierarchicalName hierarchicalName) Tests whether a configuration value with the specifiedHierarchicalNameexists.booleanexists(HierarchicalName hierarchicalName, StorageType storageType) Tests whether a configuration value with the specifiedHierarchicalNameexists.final BinarygetBinary(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to aBinary.final BinarygetBinary(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to aBinary.final BooleangetBoolean(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to aBoolean.final BooleangetBoolean(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to aBoolean.final DategetDateTime(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to aDate.final DategetDateTime(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to aDate.final DecimalgetDecimal(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to aDecimal.final DecimalgetDecimal(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to aDecimal.final DoublegetDouble(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to aDouble.final DoublegetDouble(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to aDouble.final FloatgetFloat(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to aFloat.final FloatgetFloat(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to aFloat.getHierarchicalNames(HierarchicalName hierarchicalName) getHierarchicalNames(HierarchicalName hierarchicalName, StorageType storageType) final GuidgetId(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to aGuid.final GuidgetId(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to aGuid.final IntegergetInteger(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to anInteger.final IntegergetInteger(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to anInteger.final LonggetLong(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalNameand converts it to aLong.final LonggetLong(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalNameand converts it to aLong.getString(HierarchicalName hierarchicalName) Gets the configuration value with the specifiedHierarchicalName.getString(HierarchicalName hierarchicalName, StorageType storageType) Gets the configuration value with the specifiedHierarchicalName.voidload(HierarchicalName hierarchicalName) Loads the configuration value for the specifiedHierarchicalNamefrom the database and stores it into the internal cache.voidload(HierarchicalName hierarchicalName, boolean includingSubvalues) Loads the configuration value for the specifiedHierarchicalNamefrom the database and stores it into the internal cache.voidset(HierarchicalName hierarchicalName, Object value) Sets the configuration value with the specifiedHierarchicalName.voidset(Map<HierarchicalName, Object> mapHierarchicalName2Value) Sets all the specified configuration values (values of the specified map) according to their specifiedHierarchicalNames (keys of the specified map.
-
Field Details
-
TABLE_NAME_REGEX
The regular expression, a table name must match.- See Also:
-
NAME_MAX_LENGTH
public static final int NAME_MAX_LENGTHThe maximum string length (toString()-result) of aHierarchicalNameto be used as a key by thisDbConfigurationManager.- See Also:
-
VALUE_MAX_LENGTH
public static final int VALUE_MAX_LENGTHThe maximum string length of the serialized (and if encryption is active encrypted) representation of a configuration value to be used by thisDbConfigurationManager.- See Also:
-
-
Constructor Details
-
DbConfigurationManager
public DbConfigurationManager(DbConfigurationManagerConstructorParameters<CommonAccessException> parameters) Creates a newDbConfigurationManagerwith the specifiedDbConfigurationManagerConstructorParameters. The specifiedDbConfigurationManagerConstructorParametersmust not benull.- Parameters:
parameters-DbConfigurationManagerConstructorParameters
-
-
Method Details
-
createTableIfNotExistent
Tries to find out whether the underlying database table exists and if not so to create it by executing an SQL-CREATE-TABLE-statement. This method does not analyze whether an existing database table is correct.- Throws:
IllegalStateException- on errors during the existence checkCommonAccessException- on errors during creation of the table
-
delete
public int delete(HierarchicalName hierarchicalName, boolean includingSubvalues) throws CommonAccessException Deletes the configuration value for the specifiedHierarchicalNamefrom the database and internal cache. If specified to include sub-values all configuration values that namesstart withthe specifiedHierarchicalNameare deleted. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalNameincludingSubvalues- whether to include sub-values- Returns:
- count of manipulated rows
- Throws:
CommonAccessException- on errors during db-operations
-
load
public void load(HierarchicalName hierarchicalName, boolean includingSubvalues) throws CommonAccessException Loads the configuration value for the specifiedHierarchicalNamefrom the database and stores it into the internal cache. If specified to include sub-values all configuration values that namesstart withthe specifiedHierarchicalNameare loaded. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalNameincludingSubvalues- whether to include sub-values- Throws:
CommonAccessException- on errors during db-operations
-
load
Loads the configuration value for the specifiedHierarchicalNamefrom the database and stores it into the internal cache. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Throws:
CommonAccessException- on errors during db-operations
-
exists
public boolean exists(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Tests whether a configuration value with the specifiedHierarchicalNameexists. The operation is executed according to the specifiedStorageType. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- whether such a configuration value exists
- Throws:
CommonAccessException- on errors during db-operations
-
exists
Tests whether a configuration value with the specifiedHierarchicalNameexists. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- whether such a configuration value exists
- Throws:
CommonAccessException- on errors during db-operations
-
getHierarchicalNames
public Collection<HierarchicalName> getHierarchicalNames(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets aCollectionof allHierarchicalNames thatstarts withthe specifiedHierarchicalName. The operation is executed according to the specifiedStorageType.StorageType.DB_IF_NOTHING_IN_MEMORYis treated equally toStorageType.DBin this method to avoid incomplete results. If no such value is found an emptyCollectionwill be returned. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNameall results will start withstorageType-StorageType- Returns:
CollectionofHierarchicalNames- Throws:
CommonAccessException- on errors during db-operations
-
getHierarchicalNames
public Collection<HierarchicalName> getHierarchicalNames(HierarchicalName hierarchicalName) throws CommonAccessException Gets aCollectionof allHierarchicalNames thatstarts withthe specifiedHierarchicalName. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found an emptyCollectionwill be returned. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalNameall results will start with- Returns:
CollectionofHierarchicalNames- Throws:
CommonAccessException- on errors during db-operations
-
getString
public String getString(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalName. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getString
Gets the configuration value with the specifiedHierarchicalName. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getId
public final Guid getId(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to aGuid. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getId
Gets the configuration value with the specifiedHierarchicalNameand converts it to aGuid. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getInteger
public final Integer getInteger(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to anInteger. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getInteger
Gets the configuration value with the specifiedHierarchicalNameand converts it to anInteger. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getLong
public final Long getLong(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to aLong. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getLong
Gets the configuration value with the specifiedHierarchicalNameand converts it to aLong. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getDecimal
public final Decimal getDecimal(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to aDecimal. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getDecimal
Gets the configuration value with the specifiedHierarchicalNameand converts it to aDecimal. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getFloat
public final Float getFloat(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to aFloat. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getFloat
Gets the configuration value with the specifiedHierarchicalNameand converts it to aFloat. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getDouble
public final Double getDouble(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to aDouble. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getDouble
Gets the configuration value with the specifiedHierarchicalNameand converts it to aDouble. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getBoolean
public final Boolean getBoolean(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to aBoolean. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getBoolean
Gets the configuration value with the specifiedHierarchicalNameand converts it to aBoolean. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getDateTime
public final Date getDateTime(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to aDate. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getDateTime
Gets the configuration value with the specifiedHierarchicalNameand converts it to aDate. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getBinary
public final Binary getBinary(HierarchicalName hierarchicalName, StorageType storageType) throws CommonAccessException Gets the configuration value with the specifiedHierarchicalNameand converts it to aBinary. The operation is executed according to the specifiedStorageType. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull. The specifiedStorageTypemust not benull.- Parameters:
hierarchicalName-HierarchicalNamestorageType-StorageType- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
getBinary
Gets the configuration value with the specifiedHierarchicalNameand converts it to aBinary. The operation is executed according to the defaultStorageTypeconfigured during construction of thisDbConfigurationManager. If no such value is found, the behavior is defined by theNotFoundReactionTypespecified during construction of thisDbConfigurationManager. The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalName- Returns:
- the configuration value
- Throws:
CommonAccessException- on errors during db-operations
-
set
Sets the configuration value with the specifiedHierarchicalName. Saves it both in the database and in the in-memory cache. The object must be one of the following: The specifiedHierarchicalNamemust not benull.- Parameters:
hierarchicalName-HierarchicalNamevalue- the configuration value- Throws:
CommonAccessException- on errors during db-operations
-
set
public void set(Map<HierarchicalName, Object> mapHierarchicalName2Value) throws CommonAccessExceptionSets all the specified configuration values (values of the specified map) according to their specifiedHierarchicalNames (keys of the specified map. Saves them both in the database and in the in-memory cache. Every value-object must be one of the following: All the specifiedHierarchicalNames must not benull. The specifiedMapmust not benull.- Parameters:
mapHierarchicalName2Value-Mapof configuration values (key =HierarchicalName, value = configuration value (Object))- Throws:
CommonAccessException- on errors during db-operations
-