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

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

Introduction

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

Prototype

public synchronized void setPassword(String password) 

Source Link

Document

Sets the #password .

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

Usage

From source file:org.geotoolkit.db.h2.H2StoreTest.java

/**
 * TODO reuse JDBC feature store test classes.
 * /*from   www  .  ja  va  2s  .  co  m*/
 * @throws DataStoreException
 * @throws FactoryException 
 */
@Test
public void createAndReadTest() throws DataStoreException, FactoryException {

    final CoordinateReferenceSystem crs = CRS.decode("EPSG:4326", true);

    final BasicDataSource ds = new BasicDataSource();
    ds.setUrl("jdbc:h2:mem:test");
    ds.setUsername("user");
    ds.setPassword("pwd");

    final ParameterValueGroup params = H2FeatureStoreFactory.PARAMETERS_DESCRIPTOR.createValue();
    Parameters.getOrCreate(H2FeatureStoreFactory.USER, params).setValue("user");
    Parameters.getOrCreate(H2FeatureStoreFactory.PASSWORD, params).setValue("pwd");
    Parameters.getOrCreate(H2FeatureStoreFactory.PORT, params).setValue(5555);
    Parameters.getOrCreate(H2FeatureStoreFactory.DATABASE, params).setValue("sirs");
    Parameters.getOrCreate(H2FeatureStoreFactory.HOST, params).setValue("localhost");
    Parameters.getOrCreate(H2FeatureStoreFactory.SIMPLETYPE, params).setValue(Boolean.FALSE);
    Parameters.getOrCreate(H2FeatureStoreFactory.DATASOURCE, params).setValue(ds);

    final FeatureStore store = new H2FeatureStoreFactory().create(params);

    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("route");
    AttributeDescriptor add = ftb.add("id", String.class);
    add.getUserData().put(HintsPending.PROPERTY_IS_IDENTIFIER, Boolean.TRUE);
    ftb.add("geom", LineString.class, crs);
    final FeatureType featureType = ftb.buildFeatureType();

    store.createFeatureType(ftb.getName(), featureType);

    for (Name n : store.getNames()) {
        final FeatureType ft = store.getFeatureType(n);
        System.out.println(store.getFeatureType(n));

        final GeometryFactory GF = new GeometryFactory();
        final LineString geom = GF
                .createLineString(new Coordinate[] { new Coordinate(10, 20), new Coordinate(30, 40) });

        final Feature f = FeatureUtilities.defaultFeature(ft, "id-0");
        f.setPropertyValue("id", "test");
        f.setPropertyValue("geom", geom);

        store.addFeatures(n, Collections.singleton(f));

        final FeatureCollection res = store.createSession(true).getFeatureCollection(QueryBuilder.all(n));
        for (Feature rf : res) {
            System.out.println(rf);
        }

    }

}

From source file:org.geotools.data.h2.H2DataStoreFactory.java

protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException {
    String database = (String) DATABASE.lookUp(params);
    String host = (String) HOST.lookUp(params);
    BasicDataSource dataSource = new BasicDataSource();

    if (host != null && !host.equals("")) {
        Integer port = (Integer) PORT.lookUp(params);
        if (port != null && !port.equals("")) {
            dataSource.setUrl("jdbc:h2:tcp://" + host + ":" + port + "/" + database);
        } else {//  ww w  . ja v  a 2 s.  c o  m
            dataSource.setUrl("jdbc:h2:tcp://" + host + "/" + database);
        }
    } else if (baseDirectory == null) {
        //use current working directory
        dataSource.setUrl("jdbc:h2:" + database);
    } else {
        //use directory specified if the patch is relative
        String location;
        if (!new File(database).isAbsolute()) {
            location = new File(baseDirectory, database).getAbsolutePath();
        } else {
            location = database;
        }

        dataSource.setUrl("jdbc:h2:file:" + location);
    }

    String username = (String) USER.lookUp(params);
    if (username != null) {
        dataSource.setUsername(username);
    }
    String password = (String) PASSWD.lookUp(params);
    if (password != null) {
        dataSource.setPassword(password);
    }

    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setPoolPreparedStatements(false);

    return new DBCPDataSource(dataSource);
}

From source file:org.geotools.jdbc.JDBCDataStoreFactory.java

/**
 * DataSource access allowing SQL use: intended to allow client code to query available schemas.
 * <p>//from  w w  w  . j a v a  2s .com
 * This DataSource is the clients responsibility to close() when they are finished using it.
 * </p> 
 * @param params Map of connection parameter.
 * @return DataSource for SQL use
 * @throws IOException
 */
public BasicDataSource createDataSource(Map params) throws IOException {
    //create a datasource
    BasicDataSource dataSource = new BasicDataSource();

    // driver
    dataSource.setDriverClassName(getDriverClassName());

    // url
    dataSource.setUrl(getJDBCUrl(params));

    // username
    String user = (String) USER.lookUp(params);
    dataSource.setUsername(user);

    // password
    String passwd = (String) PASSWD.lookUp(params);
    if (passwd != null) {
        dataSource.setPassword(passwd);
    }

    // max wait
    Integer maxWait = (Integer) MAXWAIT.lookUp(params);
    if (maxWait != null && maxWait != -1) {
        dataSource.setMaxWait(maxWait * 1000);
    }

    // connection pooling options
    Integer minConn = (Integer) MINCONN.lookUp(params);
    if (minConn != null) {
        dataSource.setMinIdle(minConn);
    }

    Integer maxConn = (Integer) MAXCONN.lookUp(params);
    if (maxConn != null) {
        dataSource.setMaxActive(maxConn);
    }

    Boolean validate = (Boolean) VALIDATECONN.lookUp(params);
    if (validate != null && validate && getValidationQuery() != null) {
        dataSource.setTestOnBorrow(true);
        dataSource.setValidationQuery(getValidationQuery());
    }

    // some datastores might need this
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    return dataSource;
}

From source file:org.geotools.jdbc.JDBCTestSetup.java

/**
 * Creates a data source by reading properties from a file called 'db.properties', 
 * located paralell to the test setup instance.
 *//*  www  .  ja va2  s. c  o  m*/
protected DataSource createDataSource() throws IOException {
    Properties db = fixture;

    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName(db.getProperty("driver"));
    dataSource.setUrl(db.getProperty("url"));

    if (db.containsKey("user")) {
        dataSource.setUsername(db.getProperty("user"));
    } else if (db.containsKey("username")) {
        dataSource.setUsername(db.getProperty("username"));
    }
    if (db.containsKey("password")) {
        dataSource.setPassword(db.getProperty("password"));
    }

    dataSource.setPoolPreparedStatements(true);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);
    dataSource.setMinIdle(1);
    dataSource.setMaxActive(4);
    // if we cannot get a connection within 5 seconds give up
    dataSource.setMaxWait(5000);

    initializeDataSource(dataSource, db);

    // return a closeable data source (DisposableDataSource interface)
    // so that the connection pool will be tore down on datastore dispose
    return new DBCPDataSource(dataSource);
}

From source file:org.geotools.referencing.factory.epsg.OracleDialectEpsgMediatorStarvationOnlineStressTest.java

protected DataSource connect(String user, String password, String url, Properties params) throws SQLException {
    //DataSource origional = super.connect( user, password, url, params );

    BasicDataSource origional = new BasicDataSource();
    origional.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    origional.setUsername(user);//www .  j a  va2  s . c  o  m
    origional.setPassword(password);
    origional.setUrl(url);
    origional.setMaxActive(10);
    origional.setMaxIdle(1);
    return origional;
}

From source file:org.geowebcache.diskquota.jdbc.JDBCQuotaStoreFactory.java

private DataSource getDataSource(JDBCConfiguration config) throws ConfigurationException {
    try {//from w w w . j a  va  2s .c  o  m
        DataSource ds = null;
        if (config.getJNDISource() != null) {
            InitialContext context = new InitialContext();
            ds = (DataSource) context.lookup(config.getJNDISource());
        } else if (config.getConnectionPool() != null) {
            ConnectionPoolConfiguration cp = config.getConnectionPool();

            BasicDataSource bds = new BasicDataSource();
            bds.setDriverClassName(cp.getDriver());
            bds.setUrl(cp.getUrl());
            bds.setUsername(cp.getUsername());
            bds.setPassword(cp.getPassword());
            bds.setPoolPreparedStatements(true);
            bds.setMaxOpenPreparedStatements(cp.getMaxOpenPreparedStatements());
            bds.setMinIdle(cp.getMinConnections());
            bds.setMaxActive(cp.getMaxConnections());
            bds.setMaxWait(cp.getConnectionTimeout() * 1000);
            bds.setValidationQuery(cp.getValidationQuery());

            ds = bds;
        }

        // verify the datasource works
        Connection c = null;
        try {
            c = ds.getConnection();
        } catch (SQLException e) {
            throw new ConfigurationException("Failed to get a database connection: " + e.getMessage(), e);
        } finally {
            if (c != null) {
                try {
                    c.close();
                } catch (SQLException e) {
                    // nothing we can do about it, but at least let the admin know
                    log.debug("An error occurred while closing the test JDBC connection: " + e.getMessage(), e);
                }
            }
        }

        return ds;
    } catch (NamingException e) {
        throw new ConfigurationException("Failed to locate the data source in JNDI", e);
    }
}

From source file:org.gogoego.util.db.DBSessionFactory.java

public void connectPool(String name, String url, String driverClass, String username, String password) {

    final BasicDataSource ds = new BasicDataSource() {
        /**/*  w ww .j  av a  2s .  com*/
         * DBCP 1.4's implementation is ... unexpected.  You can set a custom
         * classloader, but in fact it won't be used to load the driver.
         * Bug reported on dev@commons list.
         */
        @Override
        protected ConnectionFactory createConnectionFactory() throws SQLException {
            try {
                Driver driver = loadDriver();

                String user = username;
                if (user != null)
                    connectionProperties.put("user", user);

                String pwd = password;
                if (pwd != null)
                    connectionProperties.put("password", pwd);

                ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, url,
                        connectionProperties);
                return driverConnectionFactory;
            } catch (Exception ex) {
                throw new SQLException(ex);
            }
        }
    };

    ds.setDriverClassName(driverClass);
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setUrl(url);
    DBSessionFactory.registerDataSource(name, ds);
}

From source file:org.gvsig.fmap.dal.store.jdbc.JDBCResource.java

protected DataSource createDataSource() {
    JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this.getParameters();
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
    dataSource.setUsername(jdbcParams.getUser());
    dataSource.setPassword(jdbcParams.getPassword());
    dataSource.setUrl(jdbcParams.getUrl());

    dataSource.setMaxWait(60L * 1000); // FIXME

    // FIXME Set Pool parameters:
    /*// w  ww . ja va2s .c o m
    dataSource.setMaxActive(maxActive);
    dataSource.setMaxIdle(maxActive);
    dataSource.setMaxOpenPreparedStatements(maxActive);
    dataSource.setMaxWait(maxActive);
    dataSource.setInitialSize(initialSize);
    dataSource.setDefaultReadOnly(defaultReadOnly);
    dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setMinIdle(minIdle);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setTestOnReturn(testOnReturn);
    dataSource.setTestWhileIdle(testOnReturn);
    dataSource
       .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            
    dataSource.setAccessToUnderlyingConnectionAllowed(allow);
    dataSource.setLoginTimeout(seconds);
    dataSource.setLogWriter(out);
     */
    return dataSource;
}

From source file:org.gvsig.fmap.dal.store.jdbc.JDBCResource.java

protected void connectToDB() throws DataException {
    if (this.dataSource != null) {
        return;/*from   ww  w.  j av a 2  s.  c om*/
    }
    JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this.getParameters();
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
    dataSource.setUsername(jdbcParams.getUser());
    dataSource.setPassword(jdbcParams.getPassword());
    dataSource.setUrl(jdbcParams.getUrl());

    dataSource.setMaxWait(60L * 1000); // FIXME

    // FIXME Set Pool parameters:
    /*
    dataSource.setMaxActive(maxActive);
    dataSource.setMaxIdle(maxActive);
    dataSource.setMaxOpenPreparedStatements(maxActive);
    dataSource.setMaxWait(maxActive);
    dataSource.setInitialSize(initialSize);
    dataSource.setDefaultReadOnly(defaultReadOnly);
    dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setMinIdle(minIdle);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setTestOnReturn(testOnReturn);
    dataSource.setTestWhileIdle(testOnReturn);
    dataSource
    .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            
    dataSource.setAccessToUnderlyingConnectionAllowed(allow);
    dataSource.setLoginTimeout(seconds);
    dataSource.setLogWriter(out);
    */

    this.dataSource = dataSource;
}

From source file:org.gvsig.fmap.dal.store.mysql.MySQLResource.java

protected DataSource createDataSource() {
    MySQLResourceParameters jdbcParams = (MySQLResourceParameters) this.getParameters();
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
    dataSource.setUsername(jdbcParams.getUser());
    dataSource.setPassword(jdbcParams.getPassword());
    dataSource.setUrl(jdbcParams.getUrl());

    dataSource.setMaxWait(60L * 1000); // FIXME

    // FIXME Set Pool parameters:
    /*//from   ww  w  .j a v  a  2  s  .c  om
    dataSource.setMaxActive(maxActive);
    dataSource.setMaxIdle(maxActive);
    dataSource.setMaxOpenPreparedStatements(maxActive);
    dataSource.setMaxWait(maxActive);
    dataSource.setInitialSize(initialSize);
    dataSource.setDefaultReadOnly(defaultReadOnly);
    dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    dataSource.setMinIdle(minIdle);
    dataSource.setTestOnBorrow(testOnBorrow);
    dataSource.setTestOnReturn(testOnReturn);
    dataSource.setTestWhileIdle(testOnReturn);
    dataSource
       .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            
    dataSource.setAccessToUnderlyingConnectionAllowed(allow);
    dataSource.setLoginTimeout(seconds);
    dataSource.setLogWriter(out);
     */
    return dataSource;
}