Search in sources :

Example 1 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class Test_DatabaseProviderFactory_createDbConnection method createPostgreSqlWithCustomProperties.

@Test
public void createPostgreSqlWithCustomProperties() {
    Map<String, Object> customProperties = new HashMap<String, Object>();
    customProperties.put(DbKeys.PORT_KEY, 123);
    DbConnPostgreSQL dbConnection = (DbConnPostgreSQL) DatabaseProviderFactory.createDbConnection(DbConnPostgreSQL.DATABASE_TYPE, "host", DbConnPostgreSQL.DEFAULT_PORT, "db", "user", "pass", customProperties);
    assertEquals(DbConnPostgreSQL.DATABASE_TYPE, dbConnection.getDbType());
    assertEquals("host", dbConnection.getHost());
    assertEquals("db", dbConnection.getDb());
    assertEquals("user", dbConnection.getUser());
    assertEquals("pass", dbConnection.getPassword());
    assertEquals("jdbc:postgresql://host:123/db", dbConnection.getURL());
}
Also used : HashMap(java.util.HashMap) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) BaseTest(com.axway.ats.core.BaseTest) Test(org.junit.Test)

Example 2 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class Test_DatabaseProviderFactory_createDbConnection method createPostgreSQLNoCustom.

@Test
public void createPostgreSQLNoCustom() {
    DbConnPostgreSQL dbConnection = (DbConnPostgreSQL) DatabaseProviderFactory.createDbConnection(DbConnPostgreSQL.DATABASE_TYPE, "host", DbConnPostgreSQL.DEFAULT_PORT, "db", "user", "pass");
    assertEquals(DbConnPostgreSQL.DATABASE_TYPE, dbConnection.getDbType());
    assertEquals("host", dbConnection.getHost());
    assertEquals("db", dbConnection.getDb());
    assertEquals("user", dbConnection.getUser());
    assertEquals("pass", dbConnection.getPassword());
    assertEquals("jdbc:postgresql://host:" + DbConnPostgreSQL.DEFAULT_PORT + "/db", dbConnection.getURL());
}
Also used : DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) BaseTest(com.axway.ats.core.BaseTest) Test(org.junit.Test)

Example 3 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class DbAccessFactory method getNewDbWriteAccessObjectViaPassiveDbAppender.

/**
 * Retrieves the DB info from the log4j2 system and then creates a new
 * instance for writing into the DB
 * This method differs from the getNewDbWriteAccessObject(),
 * because we use the PassiveDbAppender to retrieve the DB info
 *
 * @return DbWriteAccess Object
 * @throws DatabaseAccessException
 */
public SQLServerDbWriteAccess getNewDbWriteAccessObjectViaPassiveDbAppender(String callerId) throws DatabaseAccessException {
    PassiveDbAppender loggingAppender = PassiveDbAppender.getCurrentInstance(callerId);
    if (loggingAppender == null) {
        throw new DatabaseAccessException("Unable to initialize connection to the logging database as the ATS PassiveDbAppender for caller '" + ThreadsPerCaller.getCaller() + "' is not attached to log4j2 system");
    }
    DbConnection dbConnection = null;
    Exception mssqlException = DbUtils.isMSSQLDatabaseAvailable(loggingAppender.getAppenderConfig().getHost(), Integer.parseInt(loggingAppender.getAppenderConfig().getPort()), loggingAppender.getAppenderConfig().getDatabase(), loggingAppender.getAppenderConfig().getUser(), loggingAppender.getAppenderConfig().getPassword());
    SQLServerDbWriteAccess writeAccess = null;
    if (mssqlException == null) {
        // Create the database access layer
        if (DbKeys.SQL_SERVER_DRIVER_MICROSOFT.equalsIgnoreCase(loggingAppender.getAppenderConfig().getDriver())) {
            // Create DB connection based on the log4j2 system settings
            Map<String, Object> props = new HashMap<>();
            props.put(DbKeys.DRIVER, DbKeys.SQL_SERVER_DRIVER_MICROSOFT);
            dbConnection = new DbConnSQLServer(loggingAppender.getAppenderConfig().getHost(), Integer.parseInt(loggingAppender.getAppenderConfig().getPort()), loggingAppender.getAppenderConfig().getDatabase(), loggingAppender.getAppenderConfig().getUser(), loggingAppender.getAppenderConfig().getPassword(), null);
            writeAccess = new SQLServerDbWriteAccessMSSQL(dbConnection, false);
        } else if (DbKeys.SQL_SERVER_DRIVER_JTDS.equalsIgnoreCase(loggingAppender.getAppenderConfig().getDriver())) {
            // Create DB connection based on the log4j2 system settings
            Map<String, Object> props = new HashMap<>();
            props.put(DbKeys.DRIVER, DbKeys.SQL_SERVER_DRIVER_JTDS);
            dbConnection = new DbConnSQLServer(loggingAppender.getAppenderConfig().getHost(), Integer.parseInt(loggingAppender.getAppenderConfig().getPort()), loggingAppender.getAppenderConfig().getDatabase(), loggingAppender.getAppenderConfig().getUser(), loggingAppender.getAppenderConfig().getPassword(), props);
            writeAccess = new SQLServerDbWriteAccess(dbConnection, false);
        } else {
            throw new IllegalArgumentException("Appender configuration specified SQL Server driver to be '" + loggingAppender.getAppenderConfig().getDriver() + "' which is not supported");
        }
    } else {
        Exception pgsqlException = DbUtils.isPostgreSQLDatabaseAvailable(loggingAppender.getAppenderConfig().getHost(), Integer.parseInt(loggingAppender.getAppenderConfig().getPort()), loggingAppender.getAppenderConfig().getDatabase(), loggingAppender.getAppenderConfig().getUser(), loggingAppender.getAppenderConfig().getPassword());
        if (pgsqlException == null) {
            // Create DB connection based on the log4j2 system settings
            dbConnection = new DbConnPostgreSQL(loggingAppender.getAppenderConfig().getHost(), Integer.parseInt(loggingAppender.getAppenderConfig().getPort()), loggingAppender.getAppenderConfig().getDatabase(), loggingAppender.getAppenderConfig().getUser(), loggingAppender.getAppenderConfig().getPassword(), null);
            // Create the database access layer
            writeAccess = new PGDbWriteAccess(dbConnection, false);
        } else {
            String errMsg = "Neither MSSQL, nor PostgreSQL server at '" + loggingAppender.getAppenderConfig().getHost() + "' has database with name '" + loggingAppender.getAppenderConfig().getHost() + ":" + loggingAppender.getAppenderConfig().getPort() + "'. Exception for MSSQL is : \n\t" + mssqlException + "\n\nException for PostgreSQL is: \n\t" + pgsqlException;
            throw new DatabaseAccessException(errMsg);
        }
    }
    if (writeAccess != null) {
        writeAccess.setMaxNumberOfCachedEvents(Integer.parseInt(loggingAppender.getAppenderConfig().getChunkSize()));
    }
    return writeAccess;
}
Also used : DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) HashMap(java.util.HashMap) DbConnection(com.axway.ats.core.dbaccess.DbConnection) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) PassiveDbAppender(com.axway.ats.log.appenders.PassiveDbAppender) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) Map(java.util.Map) HashMap(java.util.HashMap) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException)

Example 4 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class DatabaseProviderFactory method getDatabaseProvider.

/**
 * Creates a database provider suitable for the given input arguments
 *
 * @param dbType
 * @param dbHost
 * @param dbName
 * @param dbUser
 * @param dbPass
 * @param dbPort
 * @param customProperties
 * @return
 */
public static synchronized DbProvider getDatabaseProvider(String dbType, String dbHost, String dbName, String dbUser, String dbPass, int dbPort, Map<String, Object> customProperties) {
    DbProvider dbProvider;
    if (dbType == null) {
        throw new IllegalArgumentException("Database type is not provided");
    }
    dbType = dbType.toUpperCase();
    if (dbName == null) {
        if ("MSSQL".equals(dbType)) {
            log.warn("Database name is empty! The connection will be made to the admin database!");
        } else {
            throw new IllegalArgumentException("Database name is not provided");
        }
    }
    // eventual custom property value
    if (dbPort > 0) {
        if (customProperties == null) {
            customProperties = new HashMap<String, Object>();
        }
        customProperties.put(DbKeys.PORT_KEY, dbPort);
    }
    // load a custom data provider
    if (dbProviders.containsKey(dbType)) {
        String[] classNames = dbProviders.get(dbType);
        DbConnection dbConnection = loadDbConnection(classNames[0], dbType, dbHost, dbName, dbUser, dbPass, dbPort, customProperties);
        return loadCustomDbProvider(classNames[1], dbConnection);
    }
    // load a common ATS supported data provider
    switch(dbType) {
        case DbConnSQLServer.DATABASE_TYPE:
            dbProvider = new MssqlDbProvider((DbConnSQLServer) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnPostgreSQL.DATABASE_TYPE:
            dbProvider = new PostgreSqlDbProvider((DbConnPostgreSQL) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnMySQL.DATABASE_TYPE:
            dbProvider = new MysqlDbProvider((DbConnMySQL) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnMariaDB.DATABASE_TYPE:
            dbProvider = new MariaDbDbProvider((DbConnMariaDB) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnOracle.DATABASE_TYPE:
            dbProvider = new OracleDbProvider((DbConnOracle) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnCassandra.DATABASE_TYPE:
            dbProvider = new CassandraDbProvider((DbConnCassandra) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
            break;
        default:
            {
                // should never happen
                throw new IllegalArgumentException("Database type " + dbType + " not supported");
            }
    }
    return dbProvider;
}
Also used : OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) DbConnMariaDB(com.axway.ats.core.dbaccess.mariadb.DbConnMariaDB) MssqlDbProvider(com.axway.ats.core.dbaccess.mssql.MssqlDbProvider) DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) PostgreSqlDbProvider(com.axway.ats.core.dbaccess.postgresql.PostgreSqlDbProvider) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) DbConnMySQL(com.axway.ats.core.dbaccess.mysql.DbConnMySQL) DbConnOracle(com.axway.ats.core.dbaccess.oracle.DbConnOracle) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider) OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) PostgreSqlDbProvider(com.axway.ats.core.dbaccess.postgresql.PostgreSqlDbProvider) MssqlDbProvider(com.axway.ats.core.dbaccess.mssql.MssqlDbProvider) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) MariaDbDbProvider(com.axway.ats.core.dbaccess.mariadb.MariaDbDbProvider) MariaDbDbProvider(com.axway.ats.core.dbaccess.mariadb.MariaDbDbProvider) DbConnCassandra(com.axway.ats.core.dbaccess.cassandra.DbConnCassandra) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)

Example 5 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class ActiveDbAppender method obtainDbReadAccessObject.

/**
 * Get {@link IDbReadAccess} using the appender's db configuration
 * @throws DatabaseAccessException In case of DB error
 */
public IDbReadAccess obtainDbReadAccessObject() throws DatabaseAccessException {
    DbConnection dbConnection = null;
    if (dbReadAccess == null) {
        Exception mssqlException = DbUtils.isMSSQLDatabaseAvailable(appenderConfig.getHost(), Integer.parseInt(appenderConfig.getPort()), appenderConfig.getDatabase(), appenderConfig.getUser(), appenderConfig.getPassword());
        if (mssqlException == null) {
            Map<String, Object> props = new HashMap<>();
            props.put(DbKeys.DRIVER, appenderConfig.getDriver().toUpperCase());
            dbConnection = new DbConnSQLServer(appenderConfig.getHost(), Integer.parseInt(appenderConfig.getPort()), appenderConfig.getDatabase(), appenderConfig.getUser(), appenderConfig.getPassword(), props);
            // create the db access layer
            dbReadAccess = new SQLServerDbReadAccess((DbConnSQLServer) dbConnection);
        } else {
            Exception pgsqlException = DbUtils.isPostgreSQLDatabaseAvailable(appenderConfig.getHost(), Integer.parseInt(appenderConfig.getPort()), appenderConfig.getDatabase(), appenderConfig.getUser(), appenderConfig.getPassword());
            if (pgsqlException == null) {
                dbConnection = new DbConnPostgreSQL(appenderConfig.getHost(), Integer.parseInt(appenderConfig.getPort()), appenderConfig.getDatabase(), appenderConfig.getUser(), appenderConfig.getPassword(), null);
                // create the db access layer
                dbReadAccess = new PGDbReadAccess((DbConnPostgreSQL) dbConnection);
            } else {
                String errMsg = "Neither MSSQL, nor PostgreSQL server at '" + appenderConfig.getHost() + ":" + appenderConfig.getPort() + "' has database with name '" + appenderConfig.getDatabase() + "'. Exception for MSSQL is : \n\t" + mssqlException + "\n\nException for PostgreSQL is: \n\t" + pgsqlException;
                throw new DatabaseAccessException(errMsg);
            }
        }
    }
    return dbReadAccess;
}
Also used : DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) HashMap(java.util.HashMap) SQLServerDbReadAccess(com.axway.ats.log.autodb.io.SQLServerDbReadAccess) PGDbReadAccess(com.axway.ats.log.autodb.io.PGDbReadAccess) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) DbConnection(com.axway.ats.core.dbaccess.DbConnection) DbAppenederException(com.axway.ats.log.autodb.exceptions.DbAppenederException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException)

Aggregations

DbConnPostgreSQL (com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL)10 DbConnection (com.axway.ats.core.dbaccess.DbConnection)4 DbConnSQLServer (com.axway.ats.core.dbaccess.mssql.DbConnSQLServer)4 HashMap (java.util.HashMap)4 DatabaseAccessException (com.axway.ats.log.autodb.exceptions.DatabaseAccessException)3 ResultSet (java.sql.ResultSet)3 BaseTest (com.axway.ats.core.BaseTest)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Map (java.util.Map)2 Test (org.junit.Test)2 DatabaseOperations (com.axway.ats.action.dbaccess.DatabaseOperations)1 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)1 DbConnCassandra (com.axway.ats.core.dbaccess.cassandra.DbConnCassandra)1 DbConnMariaDB (com.axway.ats.core.dbaccess.mariadb.DbConnMariaDB)1 MariaDbDbProvider (com.axway.ats.core.dbaccess.mariadb.MariaDbDbProvider)1 MssqlDbProvider (com.axway.ats.core.dbaccess.mssql.MssqlDbProvider)1 DbConnMySQL (com.axway.ats.core.dbaccess.mysql.DbConnMySQL)1 MysqlDbProvider (com.axway.ats.core.dbaccess.mysql.MysqlDbProvider)1 DbConnOracle (com.axway.ats.core.dbaccess.oracle.DbConnOracle)1