Search in sources :

Example 1 with ComponentEnvironment

use of com.axway.ats.agent.core.ComponentEnvironment 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)

Example 2 with ComponentEnvironment

use of com.axway.ats.agent.core.ComponentEnvironment in project ats-framework by Axway.

the class MockDbEnvironmentUnit method getTestComponent.

private Component getTestComponent() {
    Component testComponent = new Component(COMPONENT_NAME);
    Map<String, Object> customProperties = new HashMap<String, Object>();
    customProperties.put(DbKeys.PORT_KEY, 3306);
    List<EnvironmentUnit> environmentUnits = new ArrayList<EnvironmentUnit>();
    environmentUnits.add(new MockDbEnvironmentUnit(new DbConnMySQL("host_1", "db_1", "user_1", "password_1", customProperties)));
    testComponent.setEnvironments(Arrays.asList(new ComponentEnvironment(COMPONENT_NAME, null, environmentUnits, "backupFolder")));
    return testComponent;
}
Also used : ComponentEnvironment(com.axway.ats.agent.core.ComponentEnvironment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DbConnMySQL(com.axway.ats.core.dbaccess.mysql.DbConnMySQL) EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) Component(com.axway.ats.agent.core.Component)

Aggregations

ComponentEnvironment (com.axway.ats.agent.core.ComponentEnvironment)2 EnvironmentUnit (com.axway.ats.environment.EnvironmentUnit)2 DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)2 Component (com.axway.ats.agent.core.Component)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 DbConnMySQL (com.axway.ats.core.dbaccess.mysql.DbConnMySQL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1