Search in sources :

Example 1 with DriverManagerDataSource

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);
    }
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ConfigurationLocator(org.mifos.framework.util.ConfigurationLocator) ApplicationContext(org.springframework.context.ApplicationContext) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) BufferedReader(java.io.BufferedReader) ApplicationContextHolder(org.mifos.application.servicefacade.ApplicationContextHolder) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with DriverManagerDataSource

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");
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ApplicationContext(org.springframework.context.ApplicationContext) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) ApplicationContextHolder(org.mifos.application.servicefacade.ApplicationContextHolder)

Example 3 with DriverManagerDataSource

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;
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) IDatabaseDialect(org.pentaho.database.IDatabaseDialect) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with DriverManagerDataSource

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;
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Connection(java.sql.Connection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection) DatabaseConnection(org.dbunit.database.DatabaseConnection) IDatabaseConnection(org.dbunit.database.IDatabaseConnection)

Example 5 with DriverManagerDataSource

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;
}
Also used : SQLException(java.sql.SQLException) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) SchemaColumn(org.finra.herd.model.api.xml.SchemaColumn) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)66 Bean (org.springframework.context.annotation.Bean)29 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)20 Test (org.junit.jupiter.api.Test)9 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)8 DataSource (javax.sql.DataSource)7 SQLException (java.sql.SQLException)4 Properties (java.util.Properties)4 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 InitialContext (javax.naming.InitialContext)2 XADataSource (javax.sql.XADataSource)2 Before (org.junit.Before)2 ApplicationContextHolder (org.mifos.application.servicefacade.ApplicationContextHolder)2 ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)2