Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.configuration2;
import javax.sql.DataSource;
import java.sql.Clob;
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 org.apache.commons.configuration2.convert.DisabledListDelimiterHandler;
import org.apache.commons.configuration2.convert.ListDelimiterHandler;
import org.apache.commons.configuration2.event.ConfigurationErrorEvent;
import org.apache.commons.configuration2.event.ConfigurationEvent;
import org.apache.commons.configuration2.event.EventType;
import org.apache.commons.configuration2.io.ConfigurationLogger;
import org.apache.commons.lang3.StringUtils;
/**
* 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 have to be specified using the corresponding
* properties.
*
* The recommended way to create an instance of {@code 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 {@code database()}
* method of the {@code Parameters} class. See the examples below for more
* details.
*
* 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
*/
public class DatabaseConfiguration extends AbstractConfiguration
{
/** Constant for the statement used by getProperty.*/
private static final String SQL_GET_PROPERTY = "SELECT * FROM %s WHERE %s =?";
/** Constant for the statement used by isEmpty.*/
private static final String SQL_IS_EMPTY = "SELECT count(*) FROM %s WHERE 1 = 1";
/** Constant for the statement used by clearProperty.*/
private static final String SQL_CLEAR_PROPERTY = "DELETE FROM %s WHERE %s =?";
/** Constant for the statement used by clear.*/
private static final String SQL_CLEAR = "DELETE FROM %s WHERE 1 = 1";
/** Constant for the statement used by getKeys.*/
private static final String SQL_GET_KEYS = "SELECT DISTINCT %s FROM %s WHERE 1 = 1";
/** The data source to connect to the database. */
private DataSource dataSource;
/** The configurationName of the table containing the configurations. */
private String table;
/** The column containing the configurationName of the configuration. */
private String configurationNameColumn;
/** The column containing the keys. */
private String keyColumn;
/** The column containing the values. */
private String valueColumn;
/** The configurationName of the configuration. */
private String configurationName;
/** A flag whether commits should be performed by this configuration. */
private boolean autoCommit;
/**
* Creates a new instance of {@code DatabaseConfiguration}.
*/
public DatabaseConfiguration()
{
initLogger(new ConfigurationLogger(DatabaseConfiguration.class));
addErrorLogListener();
}
/**
* Returns the {@code DataSource} for obtaining database connections.
*
* @return the {@code DataSource}
*/
public DataSource getDataSource()
{
return dataSource;
}
/**
* Sets the {@code DataSource} for obtaining database connections.
*
* @param dataSource the {@code DataSource}
*/
public void setDataSource(final DataSource dataSource)
{
this.dataSource = dataSource;
}
/**
* Returns the name of the table containing configuration data.
*
* @return the name of the table to be queried
*/
public String getTable()
{
return table;
}
/**
* Sets the name of the table containing configuration data.
*
* @param table the table name
*/
public void setTable(final String table)
{
this.table = table;
}
/**
* Returns the name of the table column with the configuration name.
*
* @return the name of the configuration name column
*/
public String getConfigurationNameColumn()
{
return configurationNameColumn;
}
/**
* Sets the name of the table column with the configuration name.
*
* @param configurationNameColumn the name of the column with the
* configuration name
*/
public void setConfigurationNameColumn(final String configurationNameColumn)
{
this.configurationNameColumn = configurationNameColumn;
}
/**
* Returns the name of the column containing the configuration keys.
*
* @return the name of the key column
*/
public String getKeyColumn()
{
return keyColumn;
}
/**
* Sets the name of the column containing the configuration keys.
*
* @param keyColumn the name of the key column
*/
public void setKeyColumn(final String keyColumn)
{
this.keyColumn = keyColumn;
}
/**
* Returns the name of the column containing the configuration values.
*
* @return the name of the value column
*/
public String getValueColumn()
{
return valueColumn;
}
/**
* Sets the name of the column containing the configuration values.
*
* @param valueColumn the name of the value column
*/
public void setValueColumn(final String valueColumn)
{
this.valueColumn = valueColumn;
}
/**
* Returns the name of this configuration instance.
*
* @return the name of this configuration
*/
public String getConfigurationName()
{
return configurationName;
}
/**
* Sets the name of this configuration instance.
*
* @param configurationName the name of this configuration
*/
public void setConfigurationName(final String configurationName)
{
this.configurationName = configurationName;
}
/**
* Returns a flag whether this configuration performs commits after database
* updates.
*
* @return a flag whether commits are performed
*/
public boolean isAutoCommit()
{
return autoCommit;
}
/**
* Sets the auto commit flag. If set to true, this configuration
* performs a commit after each database update.
*
* @param autoCommit the auto commit flag
*/
public void setAutoCommit(final boolean autoCommit)
{
this.autoCommit = autoCommit;
}
/**
* Returns the value of the specified property. If this causes a database
* error, an error event will be generated of type
* {@code READ} 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
*/
@Override
protected Object getPropertyInternal(final String key)
{
final JdbcOperation