Search in sources :

Example 1 with NoSuchComponentException

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");
    }
}
Also used : ComponentEnvironment(com.axway.ats.agent.core.ComponentEnvironment) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) ComponentRepository(com.axway.ats.agent.core.ComponentRepository) Properties(java.util.Properties) NoSuchComponentException(com.axway.ats.agent.core.exceptions.NoSuchComponentException) DbConnection(com.axway.ats.core.dbaccess.DbConnection)

Aggregations

ComponentEnvironment (com.axway.ats.agent.core.ComponentEnvironment)1 ComponentRepository (com.axway.ats.agent.core.ComponentRepository)1 NoSuchComponentException (com.axway.ats.agent.core.exceptions.NoSuchComponentException)1 DbConnection (com.axway.ats.core.dbaccess.DbConnection)1 EnvironmentUnit (com.axway.ats.environment.EnvironmentUnit)1 DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)1 Properties (java.util.Properties)1