
org.apache.commons.configuration.DatabaseConfiguration Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.configuration;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.logging.LogFactory;
/**
* 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 {@code 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
* {@code DataSource} are in auto-commit mode.
*
* Note: Like JDBC itself, protection against SQL injection is left to the user.
* @since 1.0
*
* @author Emmanuel Bourg
* @version $Id: DatabaseConfiguration.java 1208807 2011-11-30 21:34:29Z oheger $
*/
public class DatabaseConfiguration extends AbstractConfiguration
{
/** The datasource to connect to the database. */
private DataSource datasource;
/** The name of the table containing the configurations. */
private String table;
/** The column containing the name of the configuration. */
private String nameColumn;
/** The column containing the keys. */
private String keyColumn;
/** The column containing the values. */
private String valueColumn;
/** The name of the configuration. */
private String name;
/** A flag whether commits should be performed by this configuration. */
private final boolean doCommits;
/**
* Build a configuration from a table containing multiple configurations.
* No commits are performed by the new configuration instance.
*
* @param datasource the datasource to connect to the database
* @param table the name of the table containing the configurations
* @param nameColumn the column containing the name of the configuration
* @param keyColumn the column containing the keys of the configuration
* @param valueColumn the column containing the values of the configuration
* @param name the name of the configuration
*/
public DatabaseConfiguration(DataSource datasource, String table, String nameColumn,
String keyColumn, String valueColumn, String name)
{
this(datasource, table, nameColumn, keyColumn, valueColumn, name, false);
}
/**
* Creates a new instance of {@code DatabaseConfiguration} that operates on
* a database table containing multiple configurations.
*
* @param datasource the {@code DataSource} to connect to the database
* @param table the name of the table containing the configurations
* @param nameColumn the column containing the name of the configuration
* @param keyColumn the column containing the keys of the configuration
* @param valueColumn the column containing the values of the configuration
* @param name the name of the configuration
* @param commits a flag whether the configuration should perform a commit
* after a database update
*/
public DatabaseConfiguration(DataSource datasource, String table,
String nameColumn, String keyColumn, String valueColumn,
String name, boolean commits)
{
this.datasource = datasource;
this.table = table;
this.nameColumn = nameColumn;
this.keyColumn = keyColumn;
this.valueColumn = valueColumn;
this.name = name;
doCommits = commits;
setLogger(LogFactory.getLog(getClass()));
addErrorLogListener(); // log errors per default
}
/**
* Build a configuration from a table.
*
* @param datasource the datasource to connect to the database
* @param table the name of the table containing the configurations
* @param keyColumn the column containing the keys of the configuration
* @param valueColumn the column containing the values of the configuration
*/
public DatabaseConfiguration(DataSource datasource, String table, String keyColumn, String valueColumn)
{
this(datasource, table, null, keyColumn, valueColumn, null);
}
/**
* Creates a new instance of {@code DatabaseConfiguration} that
* operates on a database table containing a single configuration only.
*
* @param datasource the {@code DataSource} to connect to the database
* @param table the name of the table containing the configurations
* @param keyColumn the column containing the keys of the configuration
* @param valueColumn the column containing the values of the configuration
* @param commits a flag whether the configuration should perform a commit
* after a database update
*/
public DatabaseConfiguration(DataSource datasource, String table,
String keyColumn, String valueColumn, boolean commits)
{
this(datasource, table, null, keyColumn, valueColumn, null, commits);
}
/**
* Returns a flag whether this configuration performs commits after database
* updates.
*
* @return a flag whether commits are performed
*/
public boolean isDoCommits()
{
return doCommits;
}
/**
* Returns the value of the specified property. If this causes a database
* error, an error event will be generated of type
* {@code EVENT_READ_PROPERTY} with the causing exception. The
* event's {@code propertyName} is set to the passed in property key,
* the {@code propertyValue} is undefined.
*
* @param key the key of the desired property
* @return the value of this property
*/
public Object getProperty(String key)
{
Object result = null;
// build the query
StringBuilder query = new StringBuilder("SELECT * FROM ");
query.append(table).append(" WHERE ");
query.append(keyColumn).append("=?");
if (nameColumn != null)
{
query.append(" AND " + nameColumn + "=?");
}
Connection conn = null;
PreparedStatement pstmt = null;
try
{
conn = getConnection();
// bind the parameters
pstmt = conn.prepareStatement(query.toString());
pstmt.setString(1, key);
if (nameColumn != null)
{
pstmt.setString(2, name);
}
ResultSet rs = pstmt.executeQuery();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy