Example usage for org.springframework.jdbc.datasource SingleConnectionDataSource getConnection

List of usage examples for org.springframework.jdbc.datasource SingleConnectionDataSource getConnection

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource SingleConnectionDataSource getConnection.

Prototype

@Override
    public Connection getConnection() throws SQLException 

Source Link

Usage

From source file:eu.databata.engine.util.PropagatorRecreateDatabaseTool.java

public static void main(String[] args) {
    ClassLoader classLoader = PropagatorRecreateDatabaseTool.class.getClassLoader();
    InputStream resourceAsStream = classLoader.getResourceAsStream("databata.properties");
    Properties propagatorProperties = new Properties();
    try {/* w w w. j a  v a2  s.c o m*/
        propagatorProperties.load(resourceAsStream);
    } catch (FileNotFoundException e) {
        LOG.error("Sepecified file 'databata.properties' not found");
    } catch (IOException e) {
        LOG.error("Sepecified file 'databata.properties' cannot be loaded");
    }

    SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
    dataSource.setDriverClassName(propagatorProperties.getProperty("db.propagation.driver"));
    dataSource.setUrl(propagatorProperties.getProperty("db.propagation.dba.connection-url"));
    dataSource.setUsername(propagatorProperties.getProperty("db.propagation.dba.user"));
    dataSource.setPassword(propagatorProperties.getProperty("db.propagation.dba.password"));
    dataSource.setSuppressClose(true);

    String databaseName = "undefined";
    try {
        databaseName = dataSource.getConnection().getMetaData().getDatabaseProductName();
    } catch (SQLException e) {
        LOG.error("Cannot get connection by specified url", e);
    }
    String databaseCode = PropagationUtils.getDatabaseCode(databaseName);
    LOG.info("Database with code '" + databaseCode + "' is identified.");

    String submitFileName = "META-INF/databata/" + databaseCode + "_recreate_database.sql";
    String fileContent = "";
    try {
        fileContent = getFileContent(classLoader, submitFileName);
    } catch (IOException e) {
        LOG.info("File with name '" + submitFileName
                + "' cannot be read from classpath. Trying to load default submit file.");
    }
    if (fileContent == null || "".equals(fileContent)) {
        String defaultSubmitFileName = "META-INF/databata/" + databaseCode + "_recreate_database.default.sql";
        try {
            fileContent = getFileContent(classLoader, defaultSubmitFileName);
        } catch (IOException e) {
            LOG.info("File with name '" + defaultSubmitFileName
                    + "' cannot be read from classpath. Trying to load default submit file.");
        }
    }

    if (fileContent == null) {
        LOG.info("File content is empty. Stopping process.");
        return;
    }

    fileContent = replacePlaceholders(fileContent, propagatorProperties);

    SqlFile sqlFile = null;
    try {
        sqlFile = new SqlFile(fileContent, null, submitFileName, new SqlExecutionCallback() {

            @Override
            public void handleExecuteSuccess(String sql, int arg1, double arg2) {
                LOG.info("Sql is sucessfully executed \n ======== \n" + sql + "\n ======== \n");
            }

            @Override
            public void handleException(SQLException arg0, String sql) throws SQLException {
                LOG.info("Sql returned error \n ======== \n" + sql + "\n ======== \n");
            }
        }, null);
    } catch (IOException e) {
        LOG.error("Error when initializing SqlTool", e);
    }
    try {
        sqlFile.setConnection(dataSource.getConnection());
    } catch (SQLException e) {
        LOG.error("Error is occured when setting connection", e);
    }

    try {
        sqlFile.execute();
    } catch (SqlToolError e) {
        LOG.error("Error when creating user", e);
    } catch (SQLException e) {
        LOG.error("Error when creating user", e);
    }
}

From source file:eu.databata.engine.util.PropagatorRecreateUserTool.java

public static void main(String[] args) {
    if (args.length == 0) {
        printMessage();/*from  w ww  .jav a2s  .com*/
        return;
    }
    String scriptName = getScriptName(args[0]);
    if (scriptName == null) {
        printMessage();
        return;
    }
    ClassLoader classLoader = PropagatorRecreateUserTool.class.getClassLoader();
    InputStream resourceAsStream = classLoader.getResourceAsStream("databata.properties");
    Properties propagatorProperties = new Properties();
    try {
        propagatorProperties.load(resourceAsStream);
    } catch (FileNotFoundException e) {
        LOG.error("Sepecified file 'databata.properties' not found");
    } catch (IOException e) {
        LOG.error("Sepecified file 'databata.properties' cannot be loaded");
    }

    SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
    dataSource.setDriverClassName(propagatorProperties.getProperty("db.propagation.driver"));
    if ("-idb".equals(args[0])) {
        dataSource.setUrl(propagatorProperties.getProperty("db.propagation.dba.connection-url"));
        dataSource.setUsername(propagatorProperties.getProperty("db.propagation.dba.user"));
        dataSource.setPassword(propagatorProperties.getProperty("db.propagation.dba.password"));
    } else {
        dataSource.setUrl(propagatorProperties.getProperty("db.propagation.sa.connection-url"));
        dataSource.setUsername(propagatorProperties.getProperty("db.propagation.sa.user"));
        dataSource.setPassword(propagatorProperties.getProperty("db.propagation.sa.password"));
    }
    dataSource.setSuppressClose(true);

    String databaseName = "undefined";
    try {
        databaseName = dataSource.getConnection().getMetaData().getDatabaseProductName();
    } catch (SQLException e) {
        LOG.error("Cannot get connection by specified url", e);
        return;
    }
    String databaseCode = PropagationUtils.getDatabaseCode(databaseName);
    LOG.info("Database with code '" + databaseCode + "' is identified. for database '" + databaseName + "'");

    String submitFileName = "META-INF/databata/" + databaseCode + "_" + scriptName + ".sql";
    String fileContent = "";
    try {
        fileContent = getFileContent(classLoader, submitFileName);
    } catch (IOException e) {
        LOG.info("File with name '" + submitFileName
                + "' cannot be read from classpath. Trying to load default submit file.");
    }
    if (fileContent == null || "".equals(fileContent)) {
        String defaultSubmitFileName = "META-INF/databata/" + databaseCode + "_" + scriptName + ".default.sql";
        try {
            fileContent = getFileContent(classLoader, defaultSubmitFileName);
        } catch (IOException e) {
            LOG.info("File with name '" + defaultSubmitFileName
                    + "' cannot be read from classpath. Trying to load default submit file.");
        }
    }

    if (fileContent == null) {
        LOG.info("File content is empty. Stopping process.");
        return;
    }

    fileContent = replacePlaceholders(fileContent, propagatorProperties);

    SqlFile sqlFile = null;
    try {
        sqlFile = new SqlFile(fileContent, null, submitFileName, new SqlExecutionCallback() {

            @Override
            public void handleExecuteSuccess(String sql, int arg1, double arg2) {
                LOG.info("Sql is sucessfully executed \n ======== \n" + sql + "\n ======== \n");
            }

            @Override
            public void handleException(SQLException arg0, String sql) throws SQLException {
                LOG.info("Sql returned error \n ======== \n" + sql + "\n ======== \n");
            }
        }, null);
    } catch (IOException e) {
        LOG.error("Error when initializing SqlTool", e);
    }
    try {
        sqlFile.setConnection(dataSource.getConnection());
    } catch (SQLException e) {
        LOG.error("Error is occured when setting connection", e);
    }

    try {
        sqlFile.execute();
    } catch (SqlToolError e) {
        LOG.error("Error when creating user", e);
    } catch (SQLException e) {
        LOG.error("Error when creating user", e);
    }
}

From source file:fr.xebia.springframework.security.core.userdetails.jdbc.ExtendedJdbcUserDetailsManagerTest.java

@Before
public void before() throws Exception {
    SingleConnectionDataSource dataSource = new SingleConnectionDataSource("jdbc:h2:mem:jmx-demo-db", "sa", "",
            false);/* w  w w .  j a  v a2s . c om*/

    Connection connection = dataSource.getConnection();
    String createUsersTable = "create table users(username varchar(256), password varchar(256), enabled int, allowedRemoteAddresses varchar(256), comments varchar(256))";
    connection.createStatement().execute(createUsersTable);

    String createAuthoritiesTable = "create table authorities(username varchar(256), authority varchar(256))";
    connection.createStatement().execute(createAuthoritiesTable);

    userDetailsManager = new ExtendedJdbcUserDetailsManager();

    userDetailsManager.setDataSource(dataSource);
    simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);

}