use of org.springframework.jdbc.datasource.DriverManagerDataSource in project head by mifos.
the class ETLReportDWHelper method execute.
@Override
public void execute(final long timeInMillis) throws BatchJobException {
new ApplicationContextHolder();
ArrayList<String> errors = new ArrayList<String>();
ApplicationContext ach = ApplicationContextHolder.getApplicationContext();
DriverManagerDataSource ds = (DriverManagerDataSource) ach.getBean("dataSource");
DriverManagerDataSource dsDW = (DriverManagerDataSource) ach.getBean("dataSourcePentahoDW");
Pattern pat = Pattern.compile(DATA_WAREHOUSE_DB_NAME_PATTERN);
Matcher m = pat.matcher(dsDW.getUrl());
String nameOfDataBase = null;
if (m.find()) {
nameOfDataBase = m.group(6);
}
if (!nameOfDataBase.equals("")) {
try {
dsDW.getConnection();
} catch (SQLException ex) {
errors.add("Data Warehouse is not configured");
throw new BatchJobException("Data warehouse database", errors);
}
ConfigurationLocator configurationLocator = new ConfigurationLocator();
String configPath = configurationLocator.getConfigurationDirectory();
createPropertiesFileForPentahoDWReports(ds, dsDW);
String path = configPath + "/ETL/MifosDataWarehouseETL/" + FILENAME;
String jarPath = configPath + "/ETL/mifos-etl-plugin-1.0-SNAPSHOT.one-jar.jar";
String javaHome = System.getProperty("java.home") + "/bin/java";
String pathToLog = configPath + "/ETL/log";
if (File.separatorChar == '\\') {
// windows platform
javaHome = javaHome.replaceAll("/", "\\\\");
javaHome = '"' + javaHome + '"';
jarPath = jarPath.replaceAll("/", "\\\\");
path = path.replaceAll("/", "\\\\");
pathToLog = pathToLog.replaceAll("/", "\\\\");
}
PrintWriter fw = null;
try {
boolean hasErrors = false;
boolean notRun = true;
ProcessBuilder processBuilder = new ProcessBuilder(javaHome, "-jar", jarPath, path, "false", dsDW.getUsername(), dsDW.getPassword(), dsDW.getUrl());
processBuilder.redirectErrorStream(true);
Process p = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
if (new File(pathToLog).exists()) {
new File(pathToLog).delete();
}
File file = new File(pathToLog);
fw = new PrintWriter(file);
while ((line = reader.readLine()) != null) {
fw.println(line);
if (line.matches("^ERROR.*")) {
hasErrors = true;
}
notRun = false;
}
if (notRun) {
errors.add("Data Warehouse is not configured properly");
throw new BatchJobException("Data warehouse database", errors);
}
if (hasErrors) {
errors.add("ETL error, for more details see log file: " + pathToLog);
throw new BatchJobException("ETL error", errors);
}
} catch (IOException ex) {
throw new BatchJobException(ex.getCause());
} finally {
if (fw != null) {
fw.close();
}
}
} else {
errors.add("Data Warehouse is not configured");
throw new BatchJobException("Data warehouse database", errors);
}
}
use of org.springframework.jdbc.datasource.DriverManagerDataSource in project head by mifos.
the class JNDIException method checkConfigurationDwDatabase.
public void checkConfigurationDwDatabase(HttpServletRequest request) {
new ApplicationContextHolder();
ApplicationContext ach = ApplicationContextHolder.getApplicationContext();
DriverManagerDataSource dsDW = (DriverManagerDataSource) ach.getBean("dataSourcePentahoDW");
Pattern pat = Pattern.compile("(jdbc:mysql://)(.*)(:)([0-9]+)(/)([a-zA-Z]*)(?)(.*)");
Matcher m = pat.matcher(dsDW.getUrl());
String nameOfDataBase = null;
if (m.find()) {
nameOfDataBase = m.group(6);
}
if (nameOfDataBase.equals("")) {
request.getSession().setAttribute("configureDwDatabase", "false");
} else {
try {
dsDW.getConnection();
request.getSession().setAttribute("configureDwDatabase", "true");
} catch (SQLException ex) {
request.getSession().setAttribute("configureDwDatabase", "false");
}
}
}
use of org.springframework.jdbc.datasource.DriverManagerDataSource in project pentaho-platform by pentaho.
the class PooledDatasourceHelper method convert.
@VisibleForTesting
static DataSource convert(IDatabaseConnection databaseConnection, Supplier<IDatabaseDialectService> dialectSupplier) throws DBDatasourceServiceException {
// From Spring
DriverManagerDataSource basicDatasource = new DriverManagerDataSource();
IDatabaseDialect dialect = Optional.ofNullable(dialectSupplier.get()).orElseThrow(() -> new DBDatasourceServiceException(Messages.getInstance().getErrorString("PooledDatasourceHelper.ERROR_0001_DATASOURCE_CANNOT_LOAD_DIALECT_SVC"))).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)) {
initDriverClass(basicDatasource, dialect, driverClassName, databaseConnection.getName());
} 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())) {
initDriverClass(basicDatasource, dialect, dialect.getNativeDriver(), databaseConnection.getName());
} 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());
return basicDatasource;
}
use of org.springframework.jdbc.datasource.DriverManagerDataSource in project ORCID-Source by ORCID.
the class DBUnitTest method getDBConnection.
public static IDatabaseConnection getDBConnection() throws Exception {
DriverManagerDataSource dataSource = (DriverManagerDataSource) context.getBean("simpleDataSource");
Connection jdbcConnection = dataSource.getConnection();
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new CustomDataTypeFactory());
return connection;
}
use of org.springframework.jdbc.datasource.DriverManagerDataSource in project herd by FINRAOS.
the class RelationalTableRegistrationHelperServiceImpl method retrieveRelationalTableColumnsImpl.
/**
* Retrieves a list of actual schema columns for the specified relational table. This method uses actual JDBC connection to retrieve a description of table
* columns.
*
* @param relationalStorageAttributesDto the relational storage attributes DTO
* @param relationalSchemaName the name of the relational database schema
* @param relationalTableName the name of the relational table
*
* @return the list of schema columns for the specified relational table
*/
List<SchemaColumn> retrieveRelationalTableColumnsImpl(RelationalStorageAttributesDto relationalStorageAttributesDto, String relationalSchemaName, String relationalTableName) {
// Get the JDBC password value.
String password = getPassword(relationalStorageAttributesDto);
// Create and initialize a driver manager data source (a simple implementation of the standard JDBC interface).
// We only support PostgreSQL database type.
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setUrl(relationalStorageAttributesDto.getJdbcUrl());
driverManagerDataSource.setUsername(relationalStorageAttributesDto.getJdbcUsername());
driverManagerDataSource.setPassword(password);
driverManagerDataSource.setDriverClassName(JdbcServiceImpl.DRIVER_POSTGRES);
// Create an empty result list.
List<SchemaColumn> schemaColumns = new ArrayList<>();
// Connect to the database and retrieve the relational table columns.
try (Connection connection = driverManagerDataSource.getConnection()) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
// Check if the specified relational table exists in the database.
try (ResultSet tables = databaseMetaData.getTables(null, relationalSchemaName, relationalTableName, null)) {
Assert.isTrue(tables.next(), String.format("Relational table with \"%s\" name not found under \"%s\" schema at jdbc.url=\"%s\" for jdbc.username=\"%s\".", relationalTableName, relationalSchemaName, driverManagerDataSource.getUrl(), driverManagerDataSource.getUsername()));
}
// Retrieve the relational table columns.
try (ResultSet columns = databaseMetaData.getColumns(null, null, relationalTableName, null)) {
while (columns.next()) {
SchemaColumn schemaColumn = new SchemaColumn();
schemaColumn.setName(columns.getString("COLUMN_NAME"));
schemaColumn.setType(columns.getString("TYPE_NAME"));
schemaColumn.setSize(columns.getString("COLUMN_SIZE"));
schemaColumn.setRequired(columns.getInt("NULLABLE") == 0);
schemaColumn.setDefaultValue(columns.getString("COLUMN_DEF"));
schemaColumns.add(schemaColumn);
}
}
} catch (SQLException e) {
throw new IllegalArgumentException(String.format("Failed to retrieve description of a relational table with \"%s\" name under \"%s\" schema " + "at jdbc.url=\"%s\" using jdbc.username=\"%s\". Reason: %s", relationalTableName, relationalSchemaName, driverManagerDataSource.getUrl(), driverManagerDataSource.getUsername(), e.getMessage()), e);
}
return schemaColumns;
}
Aggregations