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

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

Introduction

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

Prototype

public synchronized void setUsername(String username) 

Source Link

Document

Sets the #username .

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

Usage

From source file:org.onecmdb.utils.wsdl.OneCMDBTransform.java

public IDataSource getDataSource() throws Exception {
    if (this.source != null) {
        Properties p = new Properties();
        FileInputStream in = new FileInputStream(this.source);
        boolean loaded = false;
        try {//from  www .  ja v a 2  s  . co m
            p.loadFromXML(in);
            loaded = true;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            in.close();
        }
        if (!loaded) {
            in = new FileInputStream(this.source);
            try {
                p.load(in);
            } finally {
                in.close();
            }
        }
        return (getDataSource(p));
    }

    if (this.jdbcSource != null) {
        Properties p = new Properties();
        FileInputStream in = new FileInputStream(this.jdbcSource);
        try {
            p.loadFromXML(in);
        } finally {
            in.close();
        }
        BasicDataSource jdbcSrc = new BasicDataSource();
        jdbcSrc.setUrl(p.getProperty("db.url"));
        jdbcSrc.setDriverClassName(p.getProperty("db.driverClass"));
        jdbcSrc.setUsername(p.getProperty("db.user"));
        jdbcSrc.setPassword(p.getProperty("db.password"));

        JDBCDataSourceWrapper src = new JDBCDataSourceWrapper();
        src.setDataSource(jdbcSrc);

        return (src);
    }
    // Else plain file...
    if (fileSource == null) {
        throw new IOException("No data source specified!");
    }
    if ("xml".equalsIgnoreCase(sourceType) || fileSource.endsWith(".xml")) {
        XMLDataSource dSource = new XMLDataSource();
        dSource.addURL(new URL(fileSource));
        return (dSource);
    }
    if ("csv".equalsIgnoreCase(sourceType) || fileSource.endsWith(".csv")) {
        CSVDataSource dSource = new CSVDataSource();
        dSource.addURL(new URL("file:" + fileSource));
        if (csvProperty != null) {
            HashMap<String, String> map = toMap(csvProperty, ",");
            String headerLines = map.get("headerLines");
            String delimiter = map.get("delimiter");
            String colTextDel = map.get("colTextDel");
            if (headerLines != null) {
                dSource.setHeaderLines(Long.parseLong(headerLines));
            }
            dSource.setColDelimiter(delimiter);
            dSource.setTextDelimiter(colTextDel);
        }
        return (dSource);
    }
    throw new IOException("Data source <" + fileSource + "> extension not supported!");
}

From source file:org.openbravo.erpCommon.modules.ImportModule.java

/**
 * Inserts in database the Vector<DynaBean> with its dependencies
 * //from  w ww  . j  ava 2s.c om
 * @param dModulesToInstall
 * @param dependencies1
 * @param newModule
 * @throws Exception
 */
private void insertDynaModulesInDB(Vector<DynaBean> dModulesToInstall, Vector<DynaBean> dependencies1,
        Vector<DynaBean> dbPrefix, boolean newModule) throws Exception {
    final Properties obProperties = new Properties();
    obProperties.load(new FileInputStream(obDir + "/config/Openbravo.properties"));

    final String url = obProperties.getProperty("bbdd.url")
            + (obProperties.getProperty("bbdd.rdbms").equals("POSTGRE")
                    ? "/" + obProperties.getProperty("bbdd.sid")
                    : "");

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(obProperties.getProperty("bbdd.driver"));
    ds.setUrl(url);
    ds.setUsername(obProperties.getProperty("bbdd.user"));
    ds.setPassword(obProperties.getProperty("bbdd.password"));

    final Connection conn = ds.getConnection();

    Integer seqNo = new Integer(ImportModuleData.selectSeqNo(pool));

    for (final DynaBean module : dModulesToInstall) {
        seqNo += 10;
        module.set("ISDEFAULT", "N");
        module.set("STATUS", "I");
        module.set("SEQNO", seqNo);
        module.set("UPDATE_AVAILABLE", null);
        module.set("UPGRADE_AVAILABLE", null);
        log4j.info("Inserting in DB info for module: " + module.get("NAME"));

        String moduleId = (String) module.get("AD_MODULE_ID");

        // Clean temporary tables
        ImportModuleData.cleanModuleInstall(pool, moduleId);
        ImportModuleData.cleanModuleDBPrefixInstall(pool, moduleId);
        ImportModuleData.cleanModuleDependencyInstall(pool, moduleId);

        String type = (String) module.get("TYPE");
        String applyConfigScript = "Y";
        if ("T".equals(type)) {
            if (newModule && V3_TEMPLATE_ID.equals(moduleId)) {
                // When installing V3 template do not apply its config script
                applyConfigScript = "N";
            } else {
                org.openbravo.model.ad.module.Module template = OBDal.getInstance()
                        .get(org.openbravo.model.ad.module.Module.class, moduleId);
                applyConfigScript = template == null ? "Y" : template.isApplyConfigurationScript() ? "Y" : "N";
            }
        }

        // Insert data in temporary tables
        ImportModuleData.insertModuleInstall(pool, moduleId, (String) module.get("NAME"),
                (String) module.get("VERSION"), (String) module.get("DESCRIPTION"), (String) module.get("HELP"),
                (String) module.get("URL"), type, (String) module.get("LICENSE"),
                (String) module.get("ISINDEVELOPMENT"), (String) module.get("ISDEFAULT"), seqNo.toString(),
                (String) module.get("JAVAPACKAGE"), (String) module.get("LICENSETYPE"),
                (String) module.get("AUTHOR"), (String) module.get("STATUS"),
                (String) module.get("UPDATE_AVAILABLE"), (String) module.get("ISTRANSLATIONREQUIRED"),
                (String) module.get("AD_LANGUAGE"), (String) module.get("HASCHARTOFACCOUNTS"),
                (String) module.get("ISTRANSLATIONMODULE"), (String) module.get("HASREFERENCEDATA"),
                (String) module.get("ISREGISTERED"), (String) module.get("UPDATEINFO"),
                (String) module.get("UPDATE_VER_ID"), (String) module.get("REFERENCEDATAINFO"),
                applyConfigScript);

        // Set installed for modules being updated
        ImportModuleData.setModuleUpdated(pool, (String) module.get("AD_MODULE_ID"));

        addLog("@ModuleInstalled@ " + module.get("NAME") + " - " + module.get("VERSION"), MSG_SUCCESS);
    }
    for (final DynaBean module : dependencies1) {
        ImportModuleData.insertModuleDependencyInstall(pool, (String) module.get("AD_MODULE_DEPENDENCY_ID"),
                (String) module.get("AD_MODULE_ID"), (String) module.get("AD_DEPENDENT_MODULE_ID"),
                (String) module.get("STARTVERSION"), (String) module.get("ENDVERSION"),
                (String) module.get("ISINCLUDED"), (String) module.get("DEPENDANT_MODULE_NAME"));
    }
    for (final DynaBean module : dbPrefix) {
        ImportModuleData.insertModuleDBPrefixInstall(pool, (String) module.get("AD_MODULE_DBPREFIX_ID"),
                (String) module.get("AD_MODULE_ID"), (String) module.get("NAME"));
    }

    conn.close();
}

From source file:org.openbravo.service.system.SystemValidationTask.java

private Platform getPlatform() {
    final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties();

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(props.getProperty("bbdd.driver"));
    if (props.getProperty("bbdd.rdbms").equals("POSTGRE")) {
        ds.setUrl(props.getProperty("bbdd.url") + "/" + props.getProperty("bbdd.sid"));
    } else {/*  www  .  j a v  a 2s . c o  m*/
        ds.setUrl(props.getProperty("bbdd.url"));
    }
    ds.setUsername(props.getProperty("bbdd.user"));
    ds.setPassword(props.getProperty("bbdd.password"));
    return PlatformFactory.createNewPlatformInstance(ds);
}

From source file:org.openbravo.test.system.SystemValidatorTest.java

private Database createDatabaseObject(Module module) {
    final Properties props = OBPropertiesProvider.getInstance().getOpenbravoProperties();

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(props.getProperty("bbdd.driver"));
    if (props.getProperty("bbdd.rdbms").equals("POSTGRE")) {
        ds.setUrl(props.getProperty("bbdd.url") + "/" + props.getProperty("bbdd.sid"));
    } else {/*from  ww  w.  ja v a 2s.c om*/
        ds.setUrl(props.getProperty("bbdd.url"));
    }
    ds.setUsername(props.getProperty("bbdd.user"));
    ds.setPassword(props.getProperty("bbdd.password"));
    Platform platform = PlatformFactory.createNewPlatformInstance(ds);
    platform.getModelLoader().setOnlyLoadTableColumns(true);

    if (module != null) {
        final String dbPrefix = module.getModuleDBPrefixList().get(0).getName();
        final ExcludeFilter filter = DBSMOBUtil.getInstance()
                .getExcludeFilter(new File(props.getProperty("source.path")));
        filter.addPrefix(dbPrefix);

        return platform.loadModelFromDatabase(filter, dbPrefix, true, module.getId());
    }

    return platform.loadModelFromDatabase(null);
}

From source file:org.openrdf.sail.generaldb.GeneralDBStore.java

protected DataSource lookupDataSource(String url, String user, String password) throws NamingException {
    if (url.startsWith("jdbc:")) {
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(url);/* w  w w  .  ja va 2 s.com*/
        ds.setUsername(user);
        ds.setPassword(password);
        setBasicDataSource(ds);
        return ds;
    }
    return (DataSource) new InitialContext().lookup(url);
}

From source file:org.openrdf.sail.rdbms.mysql.MySqlStore.java

@Override
public void initialize() throws SailException {
    try {/*ww  w .j  a va2s  . c o m*/
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        throw new RdbmsException(e.toString(), e);
    }
    StringBuilder url = new StringBuilder();
    url.append("jdbc:mysql:");
    if (serverName != null) {
        url.append("//").append(serverName);
        if (portNumber > 0) {
            url.append(":").append(portNumber);
        }
        url.append("/");
    }
    url.append(databaseName);
    url.append("?useUnicode=yes&characterEncoding=UTF-8");
    for (Entry<String, String> e : getProperties().entrySet()) {
        url.append("&");
        url.append(enc(e.getKey()));
        url.append("=");
        url.append(enc(e.getValue()));
    }
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(url.toString());
    if (user != null) {
        ds.setUsername(user);
    } else {
        ds.setUsername(System.getProperty("user.name"));
    }
    if (password != null) {
        ds.setPassword(password);
    }
    MySqlConnectionFactory factory = new MySqlConnectionFactory();
    factory.setSail(this);
    factory.setDataSource(ds);
    setBasicDataSource(ds);
    setConnectionFactory(factory);
    super.initialize();
}

From source file:org.openrdf.sail.rdbms.postgresql.PgSqlStore.java

@Override
public void initialize() throws SailException {
    try {//from   ww  w  .  j a v  a2  s  . c o  m
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        throw new RdbmsException(e.toString(), e);
    }
    StringBuilder url = new StringBuilder();
    url.append("jdbc:postgresql:");
    if (serverName != null) {
        url.append("//").append(serverName);
        if (portNumber > 0) {
            url.append(":").append(portNumber);
        }
        url.append("/");
    }
    url.append(databaseName);
    Iterator<Entry<String, String>> iter;
    iter = getProperties().entrySet().iterator();
    if (iter.hasNext()) {
        url.append("?");
    }
    while (iter.hasNext()) {
        Entry<String, String> e = iter.next();
        url.append(enc(e.getKey()));
        url.append("=");
        url.append(enc(e.getValue()));
        if (iter.hasNext()) {
            url.append("&");
        }
    }
    BasicDataSource ds = new BasicDataSource();
    ds.setUrl(url.toString());
    if (user != null) {
        ds.setUsername(user);
    } else {
        ds.setUsername(System.getProperty("user.name"));
    }
    if (password != null) {
        ds.setPassword(password);
    }
    PgSqlConnectionFactory factory = new PgSqlConnectionFactory();
    factory.setSail(this);
    factory.setDataSource(ds);
    setBasicDataSource(ds);
    setConnectionFactory(factory);
    super.initialize();
}

From source file:org.openrdf.sail.rdbms.RdbmsStore.java

private DataSource lookupDataSource(String url, String user, String password) throws NamingException {
    if (url.startsWith("jdbc:")) {
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(url);//from   w w w  .j a  v a2  s .co  m
        ds.setUsername(user);
        ds.setPassword(password);
        setBasicDataSource(ds);
        return ds;
    }
    return (DataSource) new InitialContext().lookup(url);
}

From source file:org.orbisgis.geoserver.h2gis.datastore.H2GISDataStoreFactory.java

@Override
protected DataSource createDataSource(Map params, SQLDialect dialect) throws IOException {
    String database = (String) DATABASE.lookUp(params);
    String host = (String) HOST.lookUp(params);
    Boolean mvcc = (Boolean) MVCC.lookUp(params);
    Boolean mvstore = (Boolean) MVSTORE.lookUp(params);
    BasicDataSource dataSource = new BasicDataSource();

    if (host != null && !host.equals("")) {
        Integer port = (Integer) PORT.lookUp(params);
        if (port != null) {
            dataSource.setUrl("jdbc:h2:tcp://" + host + ":" + port + "/" + database);
        } else {//  w  ww .j  a  v a 2  s  . com
            dataSource.setUrl("jdbc:h2:tcp://" + host + "/" + database);
        }
    } else if (baseDirectory == null) {
        //use current working directory
        dataSource.setUrl("jdbc:h2:" + database + ";AUTO_SERVER=TRUE" + (mvcc != null ? (";MVCC=" + mvcc) : "")
                + (mvstore != null ? (";MVSTORE=" + mvstore) : ""));
    } 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 + ";AUTO_SERVER=TRUE"
                + (mvcc != null ? (";MVCC=" + mvcc) : "") + (mvstore != null ? (";MVSTORE=" + mvstore) : ""));
    }

    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);

    // if we got here the database has been created, now verify it has the H2GIS extension
    // and eventually try to create them
    JDBCDataStore closer = new JDBCDataStore();
    Connection cx = null;
    try {
        cx = dataSource.getConnection();
        //Add the spatial function
        if (!JDBCUtilities.tableExists(cx, "PUBLIC.GEOMETRY_COLUMNS")) {
            CreateSpatialExtension.initSpatialExtension(cx);
        }
    } catch (SQLException e) {
        throw new IOException("Failed to create the target database", e);
    } finally {
        closer.closeSafe(cx);
    }

    return new DBCPDataSource(dataSource);
}

From source file:org.overlord.dtgov.devsvr.bpm.DTGovBpmDevServer.java

/**
 * Creates an in-memory datasource./*ww w .  j  a v a  2  s . c  o m*/
 * @throws SQLException
 */
private static DataSource createInMemoryDatasource() throws SQLException {
    System.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(Driver.class.getName());
    ds.setUsername("sa");
    ds.setPassword("");
    ds.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    Connection connection = ds.getConnection();
    connection.close();
    return ds;
}