use of com.axway.ats.core.dbaccess.DbConnection in project ats-framework by Axway.
the class EnvironmentConfigurator method apply.
@Override
public void apply() throws ConfigurationException {
for (Properties dbProperties : dbPropertiesList) {
int dbConfigurationIndex = -1;
if (dbProperties.get(DB_CONFIGURATION_INDEX) != null) {
dbConfigurationIndex = (Integer) dbProperties.get(DB_CONFIGURATION_INDEX);
}
ComponentRepository componentRepository = ComponentRepository.getInstance();
ComponentEnvironment componentEnvironment;
try {
componentEnvironment = componentRepository.getComponentEnvironment(component);
} catch (NoSuchComponentException nsce) {
throw new ConfigurationException("Error changing environment configuration. CAUSE: " + nsce.getMessage());
}
int foundDbConfigurationIndex = 0;
for (EnvironmentUnit environmentUnit : componentEnvironment.getEnvironmentUnits()) {
if (environmentUnit instanceof DatabaseEnvironmentUnit) {
if (foundDbConfigurationIndex == dbConfigurationIndex) {
DatabaseEnvironmentUnit dbEnvironmentUnit = (DatabaseEnvironmentUnit) environmentUnit;
DbConnection dbConnection = dbEnvironmentUnit.getDbConnection();
// the database port is kept in a list of custom properties
Map<String, Object> customProperties = dbConnection.getCustomProperties();
// get the new configuration properties
String newDbHost = chooseNewProperty(dbProperties.getProperty(DB_HOST), dbConnection.getHost());
String newDbName = chooseNewProperty(dbProperties.getProperty(DB_NAME), dbConnection.getDb());
String newDbUserName = chooseNewProperty(dbProperties.getProperty(DB_USER_NAME), dbConnection.getUser());
String newDbUserPassword = chooseNewProperty(dbProperties.getProperty(DB_USER_PASSWORD), dbConnection.getPassword());
Object newDbPort = chooseDbPort(dbProperties.get(DB_PORT), customProperties.get(DbKeys.PORT_KEY));
// create a new connection object
customProperties.put(DbKeys.PORT_KEY, newDbPort);
DbConnection newDbConnection = DatabaseProviderFactory.createDbConnection(dbConnection.getDbType(), newDbHost, newDbName, newDbUserName, newDbUserPassword, customProperties);
// apply the changes
dbEnvironmentUnit.setDbConnection(newDbConnection);
log.info("Database configuration for index " + dbConfigurationIndex + " is changed. DbConnection: " + newDbConnection.getDescription());
return;
} else {
// still searching the exact database configuration
foundDbConfigurationIndex++;
}
}
}
throw new ConfigurationException("Database configuration with index " + dbConfigurationIndex + " is not available");
}
}
use of com.axway.ats.core.dbaccess.DbConnection in project ats-framework by Axway.
the class ConfigurationParser method parseDbEnvironment.
/**
* Parse the database environment node in the Agent descriptor.
*
* @param dbEnvironmentNode the DB environment node.
* @throws AgentException on error.
*/
private EnvironmentUnit parseDbEnvironment(Node dbEnvironmentNode, String backupFolder) throws AgentException {
//create the connection descriptor
DbConnection dbConnection = createConnection(dbEnvironmentNode.getAttributes());
//read the tables
List<DbTable> dbTables = new ArrayList<DbTable>();
NodeList dbChildNodes = dbEnvironmentNode.getChildNodes();
for (int k = 0; k < dbChildNodes.getLength(); k++) {
Node dbChildNode = dbChildNodes.item(k);
if (dbChildNode.getNodeName().equals(TABLE)) {
String tableName = dbChildNode.getAttributes().getNamedItem("name").getNodeValue();
String[] columnsToExclude = {};
if (dbChildNode.getAttributes().getNamedItem("columnsToExclude") != null) {
columnsToExclude = dbChildNode.getAttributes().getNamedItem("columnsToExclude").getNodeValue().split(",");
}
DbTable dbTable = null;
// parse db table 'lock' attribute
if (dbChildNode.getAttributes().getNamedItem("lock") != null) {
if (dbConnection.getDbType().equals(DbConnOracle.DATABASE_TYPE)) {
log.warn("Db table 'lock' attribute is NOT implemented for Oracle yet. " + "Table locking is skipped for the moment.");
}
String nodeValue = dbChildNode.getAttributes().getNamedItem("lock").getNodeValue().trim();
if ("false".equalsIgnoreCase(nodeValue) || "true".equalsIgnoreCase(nodeValue)) {
dbTable = new DbTable(tableName, Arrays.asList(columnsToExclude), Boolean.parseBoolean(nodeValue));
} else {
log.warn("Invalid db table 'lock' attribute value '" + nodeValue + "'. Valid values are 'true' and 'false'. The default value 'true' will be used.");
}
}
if (dbTable == null) {
dbTable = new DbTable(tableName, Arrays.asList(columnsToExclude));
}
// parse db table 'autoIncrementResetValue' attribute
if (dbChildNode.getAttributes().getNamedItem(TABLE_ATTR_AUTOINCR_RESET_VALUE) != null) {
if (dbConnection.getDbType().equals(DbConnOracle.DATABASE_TYPE)) {
throw new AgentException("Db table 'autoIncrementResetValue' attribute is NOT implemented for Oracle yet.");
}
String autoInrcResetValue = dbChildNode.getAttributes().getNamedItem(TABLE_ATTR_AUTOINCR_RESET_VALUE).getNodeValue().trim();
try {
Integer.parseInt(autoInrcResetValue);
} catch (NumberFormatException nfe) {
throw new AgentException("Ivalid db table 'autoIncrementResetValue' attribute value '" + autoInrcResetValue + "'. It must be valid number.");
}
dbTable.setAutoIncrementResetValue(autoInrcResetValue);
}
dbTables.add(dbTable);
}
}
String backupFileName = componentName + "-" + dbConnection.getDb() + ".sql";
//create the environment unit
DatabaseEnvironmentUnit dbEnvironment = new DatabaseEnvironmentUnit(backupFolder, backupFileName, dbConnection, dbTables);
return dbEnvironment;
}
use of com.axway.ats.core.dbaccess.DbConnection in project ats-framework by Axway.
the class DbAccessFactory method getNewDbWriteAccessObject.
/**
* Retrieves the DB info from the log4j system
* and then creates a new instance for writing into the DB
*
* @return
* @throws DatabaseAccessException
*/
public DbWriteAccess getNewDbWriteAccessObject() throws DatabaseAccessException {
// Our DB appender keeps the DB connection info
ActiveDbAppender loggingAppender = ActiveDbAppender.getCurrentInstance();
if (loggingAppender == null) {
throw new DatabaseAccessException("Unable to initialize connection to the logging database as the ATS ActiveDbAppender is not attached to log4j system");
}
// Create DB connection based on the log4j system settings
DbConnection dbConnection = new DbConnSQLServer(loggingAppender.getHost(), loggingAppender.getDatabase(), loggingAppender.getUser(), loggingAppender.getPassword());
// Create the database access layer
return new DbWriteAccess(dbConnection, false);
}
Aggregations