Example usage for org.apache.commons.dbcp BasicDataSource setUrl

List of usage examples for org.apache.commons.dbcp BasicDataSource setUrl

Introduction

In this page you can find the example usage for org.apache.commons.dbcp BasicDataSource setUrl.

Prototype

public synchronized void setUrl(String url) 

Source Link

Document

Sets the #url .

Note: this method currently has no effect once the pool has been initialized.

Usage

From source file:org.tdmx.lib.control.datasource.DynamicDataSource.java

private synchronized void createDataSource(DatabaseConnectionInfo dci) throws SQLException {
    // race condition avoidance
    if (connectionDataSourceMap.get(dci) != null) {
        return;/*from  www. j av a 2s. c  om*/
    }
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl(dci.getUrl());
    bds.setDriverClassName(dci.getDriverClassname());
    bds.setUsername(dci.getUsername());
    bds.setPassword(dci.getPassword());
    bds.setMaxActive(100);
    bds.setMinIdle(0);
    bds.setInitialSize(1);
    bds.setMinEvictableIdleTimeMillis(60000);
    bds.setLogWriter(logWriter);
    connectionDataSourceMap.put(dci, bds);
}

From source file:org.unidle.config.DataConfiguration.java

@Bean(destroyMethod = "close")
public DataSource dataSource() {
    final BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(dataSourceDriverClass);
    dataSource.setUrl(dataSourceUrl);
    dataSource.setUsername(dataSourceUsername);
    dataSource.setPassword(dataSourcePassword);

    return dataSource;
}

From source file:org.unitils.database.config.PropertiesDataSourceFactory.java

public DataSource createDataSource() {
    logger.info("Creating data source. Driver: " + config.getDriverClassName() + ", url: " + config.getUrl()
            + ", user: " + config.getUserName() + ", password: <not shown>");
    BasicDataSource dataSource = getNewDataSource();
    dataSource.setDriverClassName(config.getDriverClassName());
    dataSource.setUsername(config.getUserName());
    dataSource.setPassword(config.getPassword());
    dataSource.setUrl(config.getUrl());
    return dataSource;

}

From source file:org.unitils.database.core.DataSourceWrapperFactory.java

protected DataSource createDataSource(DatabaseConfiguration databaseConfiguration) {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setDriverClassName(databaseConfiguration.getDriverClassName());
    dataSource.setUsername(databaseConfiguration.getUserName());
    dataSource.setPassword(databaseConfiguration.getPassword());
    dataSource.setUrl(databaseConfiguration.getUrl());
    return dataSource;
}

From source file:org.wsm.database.tools.util.BaseLoadTestConnectionManager.java

protected BasicDataSource setupBasicDataSourcePooling() {
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl(ConnectionManager.connUrl);
    bds.setUsername(ConnectionManager.currentDbProps.getUserName());
    bds.setPassword(ConnectionManager.currentDbProps.getPassword());
    return bds;/*from   w  w w  .ja  va 2s.com*/
}

From source file:org.wso2.carbon.andes.utils.MessageBrokerDBUtil.java

/**
 * Set database configuration parameters to BasicDataSource object.
 *
 * @param configuration data source configurations
 * @throws org.wso2.carbon.andes.service.exception.ConfigurationException
 *///from   w  w  w .j  a  va  2 s  . c o  m
private void setMBStoreRdbmsConfiguration(DataSourceConfiguration configuration) throws ConfigurationException {

    String driver;
    String dbUrl;
    String username;
    String password;

    ArrayList<HashMap<Object, String>> dbConfigurationList = configuration.getConfigurationMap();

    int dbConfigurationNumber = MESSAGE_STORE_DATA_SOURCE;

    for (Object dbConfiguration : dbConfigurationList) {

        HashMap<String, String> dbConfigurationMap = (HashMap<String, String>) dbConfiguration;

        try {
            driver = dbConfigurationMap.get(DB_DRIVER);
            dbUrl = dbConfigurationMap.get(DB_URL);
            username = dbConfigurationMap.get(DB_USERNAME);
            password = dbConfigurationMap.get(DB_PASSWORD);
        } catch (Exception e) {
            log.error("Unexpected error occurred while reading data source configuration map. "
                    + " Database configurations not set properly. ", e);
            throw new ConfigurationException("Unexpected error occurred while reading data"
                    + " source configuration map.Database configurations" + " not set properly. ", e);
        }

        if (log.isDebugEnabled()) {
            log.debug("Initializing data source configurations for MB RDBMS store");
            log.debug("Data Source Configurations :");
            log.debug(DB_DRIVER + " : " + driver);
            log.debug(DB_URL + " : " + dbUrl);

        }

        if (StringUtils.isBlank(dbUrl)) {
            log.warn("Required database url unspecified. So Message Broker RDBMS Store "
                    + "will not work as expected.");
        }
        if (StringUtils.isBlank(driver)) {
            log.warn("Required database driver unspecified. So Message Broker RDBMS Store "
                    + "will not work as expected.");
        }
        if (StringUtils.isBlank(username)) {
            log.warn("Required database username unspecified. So Message Broker RDBMS Store "
                    + "will not work as expected.");
        }
        if (StringUtils.isBlank(password)) {
            log.warn("Required database password unspecified. So Message Broker RDBMS Store "
                    + "will not work as expected.");
        }

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(driver);
        basicDataSource.setUrl(dbUrl);
        basicDataSource.setUsername(username);
        basicDataSource.setPassword(password);

        if (dbConfigurationNumber == MESSAGE_STORE_DATA_SOURCE) {
            messageStoreDataSource = basicDataSource;
            isMessageStoreDataSourceSet = true;
        }

        if (dbConfigurationNumber == CONTEXT_STORE_DATA_SOURCE) {
            contextStoreDataSource = basicDataSource;
            isContextStoreDatasourceSet = true;
        }

        dbConfigurationNumber++;

    }

}

From source file:org.wso2.carbon.apimgt.hybrid.gateway.usage.publisher.dao.UploadedUsageFileInfoDAOTest.java

private static void initializeDatabase(String configFilePath) {
    InputStream in;/*www.  ja  v  a2  s  .com*/
    try {
        in = FileUtils.openInputStream(new File(configFilePath));
        StAXOMBuilder builder = new StAXOMBuilder(in);
        String dataSource = builder.getDocumentElement().getFirstChildWithName(new QName("DataSourceName"))
                .getText();
        OMElement databaseElement = builder.getDocumentElement().getFirstChildWithName(new QName("Database"));
        String databaseURL = databaseElement.getFirstChildWithName(new QName("URL")).getText();
        String databaseUser = databaseElement.getFirstChildWithName(new QName("Username")).getText();
        String databasePass = databaseElement.getFirstChildWithName(new QName("Password")).getText();
        String databaseDriver = databaseElement.getFirstChildWithName(new QName("Driver")).getText();

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(databaseDriver);
        basicDataSource.setUrl(databaseURL);
        basicDataSource.setUsername(databaseUser);
        basicDataSource.setPassword(databasePass);

        // Create initial context
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
        try {
            InitialContext.doLookup("java:/comp/env/jdbc/WSO2AM_DB");
        } catch (NamingException e) {
            InitialContext ic = new InitialContext();
            ic.createSubcontext("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");
            ic.bind("java:/comp/env/jdbc/WSO2AM_DB", basicDataSource);
        }
    } catch (XMLStreamException | IOException | NamingException e) {
        log.error(e);
    }
}

From source file:org.wso2.carbon.apimgt.impl.dao.test.APIMgtDAOTest.java

private void initializeDatabase(String configFilePath) {

    InputStream in = null;//w w w  . jav  a 2s.co  m
    try {
        in = FileUtils.openInputStream(new File(configFilePath));
        StAXOMBuilder builder = new StAXOMBuilder(in);
        String dataSource = builder.getDocumentElement().getFirstChildWithName(new QName("DataSourceName"))
                .getText();
        OMElement databaseElement = builder.getDocumentElement().getFirstChildWithName(new QName("Database"));
        String databaseURL = databaseElement.getFirstChildWithName(new QName("URL")).getText();
        String databaseUser = databaseElement.getFirstChildWithName(new QName("Username")).getText();
        String databasePass = databaseElement.getFirstChildWithName(new QName("Password")).getText();
        String databaseDriver = databaseElement.getFirstChildWithName(new QName("Driver")).getText();

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(databaseDriver);
        basicDataSource.setUrl(databaseURL);
        basicDataSource.setUsername(databaseUser);
        basicDataSource.setPassword(databasePass);

        // Create initial context
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
        try {
            InitialContext.doLookup("java:/comp/env/jdbc/WSO2AM_DB");
        } catch (NamingException e) {
            InitialContext ic = new InitialContext();
            ic.createSubcontext("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");

            ic.bind("java:/comp/env/jdbc/WSO2AM_DB", basicDataSource);
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.carbon.apimgt.impl.dao.test.CertificateMgtDaoTest.java

private static void initializeDatabase(String configFilePath)
        throws IOException, XMLStreamException, NamingException {

    InputStream in;/*from  w w w  .ja  v  a2 s .  com*/
    in = FileUtils.openInputStream(new File(configFilePath));
    StAXOMBuilder builder = new StAXOMBuilder(in);
    String dataSource = builder.getDocumentElement().getFirstChildWithName(new QName("DataSourceName"))
            .getText();
    OMElement databaseElement = builder.getDocumentElement().getFirstChildWithName(new QName("Database"));
    String databaseURL = databaseElement.getFirstChildWithName(new QName("URL")).getText();
    String databaseUser = databaseElement.getFirstChildWithName(new QName("Username")).getText();
    String databasePass = databaseElement.getFirstChildWithName(new QName("Password")).getText();
    String databaseDriver = databaseElement.getFirstChildWithName(new QName("Driver")).getText();

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(databaseDriver);
    basicDataSource.setUrl(databaseURL);
    basicDataSource.setUsername(databaseUser);
    basicDataSource.setPassword(databasePass);

    // Create initial context
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    try {
        InitialContext.doLookup("java:/comp/env/jdbc/WSO2AM_DB");
    } catch (NamingException e) {
        InitialContext ic = new InitialContext();
        ic.createSubcontext("java:");
        ic.createSubcontext("java:/comp");
        ic.createSubcontext("java:/comp/env");
        ic.createSubcontext("java:/comp/env/jdbc");

        ic.bind("java:/comp/env/jdbc/WSO2AM_DB", basicDataSource);
    }
}

From source file:org.wso2.carbon.appfactory.core.util.AppFactoryDBUtil.java

public static void initializeDatasource() throws AppFactoryException {
    AppFactoryConfiguration configuration = ServiceHolder.getAppFactoryConfiguration();
    String datasourceName = configuration.getFirstProperty(AppFactoryConstants.DATASOURCE_NAME);
    if (datasourceName != null) {
        InitialContext context;//from w  ww.j ava2s.c om
        try {
            context = new InitialContext();
        } catch (NamingException e) {
            String msg = "Could get JNDI initial context.Unable to get datasource for appfactory";
            log.error(msg, e);
            throw new AppFactoryException(msg, e);
        }
        try {
            AppFactoryDBUtil.dataSource = (DataSource) context.lookup(datasourceName);
        } catch (NamingException e) {
            String msg = "Could not found data source " + datasourceName + ".Please make sure the "
                    + "datasource is configured in appfactory.xml";
            log.error(msg, e);
            throw new AppFactoryException(msg, e);
        }
    } else {
        //This is only needed for unit test .
        DBConfiguration dbConfiguration;
        dbConfiguration = getDBConfig(configuration);
        String dbUrl = dbConfiguration.getDbUrl();
        String driver = dbConfiguration.getDriverName();
        String username = dbConfiguration.getUserName();
        String password = dbConfiguration.getPassword();
        if (dbUrl == null || driver == null || username == null || password == null) {
            log.warn("Required DB configuration parameters unspecified. So App Factory "
                    + "will not work as expected.");
        }

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(driver);
        basicDataSource.setUrl(dbUrl);
        basicDataSource.setUsername(username);
        basicDataSource.setPassword(password);
        dataSource = basicDataSource;
    }
    setupDatabase();
}