use of com.axway.ats.agent.core.exceptions.NoSuchComponentException 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");
}
}
Aggregations