Search in sources :

Example 1 with DbConnection

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");
    }
}
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 DbConnection

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;
}
Also used : AgentException(com.axway.ats.agent.core.exceptions.AgentException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DbConnection(com.axway.ats.core.dbaccess.DbConnection) DbTable(com.axway.ats.environment.database.model.DbTable)

Example 3 with DbConnection

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);
}
Also used : ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) DbConnection(com.axway.ats.core.dbaccess.DbConnection)

Aggregations

DbConnection (com.axway.ats.core.dbaccess.DbConnection)3 DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)2 ComponentEnvironment (com.axway.ats.agent.core.ComponentEnvironment)1 ComponentRepository (com.axway.ats.agent.core.ComponentRepository)1 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 NoSuchComponentException (com.axway.ats.agent.core.exceptions.NoSuchComponentException)1 DbConnSQLServer (com.axway.ats.core.dbaccess.mssql.DbConnSQLServer)1 EnvironmentUnit (com.axway.ats.environment.EnvironmentUnit)1 DbTable (com.axway.ats.environment.database.model.DbTable)1 ActiveDbAppender (com.axway.ats.log.appenders.ActiveDbAppender)1 DatabaseAccessException (com.axway.ats.log.autodb.exceptions.DatabaseAccessException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1