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());
}
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());
}
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;
}
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;
}
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;
}
Aggregations