public static DataSource convert( IDatabaseConnection databaseConnection ) throws DBDatasourceServiceException {
DriverManagerDataSource basicDatasource = new DriverManagerDataSource(); // From Spring
IDatabaseDialectService databaseDialectService =
PentahoSystem.get( IDatabaseDialectService.class, PentahoSessionHolder.getSession() );
IDatabaseDialect dialect = databaseDialectService.getDialect( databaseConnection );
if ( databaseConnection.getDatabaseType() == null && dialect == null ) {
// We do not have enough information to create a DataSource. Throwing exception
throw new DBDatasourceServiceException( Messages.getInstance().getErrorString(
"PooledDatasourceHelper.ERROR_0001_DATASOURCE_CREATE_ERROR_NO_DIALECT", databaseConnection.getName() ) );
}
if ( databaseConnection.getDatabaseType().getShortName().equals( "GENERIC" ) ) { //$NON-NLS-1$
String driverClassName =
databaseConnection.getAttributes().get( GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS );
if ( !StringUtils.isEmpty( driverClassName ) ) {
basicDatasource.setDriverClassName( driverClassName );
} else {
// We do not have enough information to create a DataSource. Throwing exception
throw new DBDatasourceServiceException( Messages.getInstance().getErrorString(
"PooledDatasourceHelper.ERROR_0002_DATASOURCE_CREATE_ERROR_NO_CLASSNAME", databaseConnection.getName() ) );
}
} else {
if ( !StringUtils.isEmpty( dialect.getNativeDriver() ) ) {
basicDatasource.setDriverClassName( dialect.getNativeDriver() );
} else {
// We do not have enough information to create a DataSource. Throwing exception
throw new DBDatasourceServiceException( Messages.getInstance().getErrorString(
"PooledDatasourceHelper.ERROR_0003_DATASOURCE_CREATE_ERROR_NO_DRIVER", databaseConnection.getName() ) );
}
}
try {
basicDatasource.setUrl( dialect.getURLWithExtraOptions( databaseConnection ) );
} catch ( DatabaseDialectException e ) {
basicDatasource.setUrl( null );
}
basicDatasource.setUsername( databaseConnection.getUsername() );
basicDatasource.setPassword( databaseConnection.getPassword() );