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

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

Introduction

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

Prototype

public synchronized void setDriverClassName(String driverClassName) 

Source Link

Document

Sets the jdbc driver class name.

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

Usage

From source file:org.wso2.carbon.identity.common.testng.MockInitialContextFactory.java

private static BasicDataSource createDb(String dbName, Class clazz, String[] files)
        throws TestCreationException {

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUsername("username");
    dataSource.setPassword("password");
    dataSource.setUrl("jdbc:h2:mem:test" + dbName);
    try (Connection connection = dataSource.getConnection()) {
        for (String f : files) {
            File fileFromClasspathResource = getClasspathAccessibleFile(f, clazz);
            String scriptPath = null;
            File tempFile = null;
            if (fileFromClasspathResource != null && fileFromClasspathResource.exists()) {
                scriptPath = fileFromClasspathResource.getAbsolutePath();
            } else {
                //This may be from jar.
                tempFile = copyTempFile(f, clazz);
                scriptPath = tempFile.getAbsolutePath();
            }//from ww  w  .  j a v a 2  s.  c  o m
            if (scriptPath != null) {
                try (Statement statement = connection.createStatement()) {
                    statement.executeUpdate("RUNSCRIPT FROM '" + scriptPath + "'"); //NOSONAR
                } catch (SQLException e) {
                    throw new TestCreationException(
                            "Error while loading data to the in-memory H2 Database located from resource : "
                                    + fileFromClasspathResource + "\nabsolute path : " + scriptPath,
                            e);
                }
            }
            if (tempFile != null) {
                tempFile.delete();
            }
        }
        return dataSource;
    } catch (SQLException e) {
        throw new TestCreationException("Error while creating the in-memory H2 Database : ", e);
    }
}

From source file:org.wso2.carbon.identity.oauth.dao.TestOAuthDAOBase.java

protected void initiateH2Base(String databaseName, String scriptPath) throws Exception {

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUsername("username");
    dataSource.setPassword("password");
    dataSource.setUrl("jdbc:h2:mem:test" + databaseName);
    try (Connection connection = dataSource.getConnection()) {
        connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + scriptPath + "'");
    }//from w w  w. j av  a  2s .c  o m
    dataSourceMap.put(databaseName, dataSource);
}

From source file:org.wso2.carbon.identity.oauth.endpoint.user.impl.TestUtils.java

public static void initiateH2Base() throws Exception {

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUsername("username");
    dataSource.setPassword("password");
    dataSource.setUrl("jdbc:h2:mem:test" + DB_NAME);
    try (Connection connection = dataSource.getConnection()) {
        connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + getFilePath(H2_SCRIPT_NAME) + "'");
    }//w  w  w. j a  v  a 2  s. c o  m
    dataSourceMap.put(DB_NAME, dataSource);
}

From source file:org.wso2.carbon.identity.oauth.endpoint.util.TestOAthEndpointBase.java

protected void initiateInMemoryH2() throws Exception {
    BasicDataSource dataSource = new BasicDataSource();

    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUsername("username");
    dataSource.setPassword("password");
    dataSource.setUrl("jdbc:h2:mem:test");

    connection = dataSource.getConnection();
    connection.createStatement().executeUpdate("RUNSCRIPT FROM 'src/test/resources/dbscripts/h2.sql'");
}

From source file:org.wso2.carbon.identity.oauth2.dao.util.DAOUtils.java

public static void initializeDataSource(String databaseName, String scriptPath) throws Exception {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUsername("username");
    dataSource.setPassword("password");
    dataSource.setUrl("jdbc:h2:mem:" + databaseName);

    try (Connection connection = dataSource.getConnection()) {
        connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + scriptPath + "'");
    }/*from  w w  w  .  j  av  a2s .  c  o m*/
    dataSourceMap.put(databaseName, dataSource);
}

From source file:org.wso2.carbon.identity.openidconnect.DefaultOIDCClaimsCallbackHandlerTest.java

@BeforeClass
public void setUp() throws Exception {

    System.setProperty(CarbonBaseConstants.CARBON_HOME, CARBON_HOME);
    BasicDataSource dataSource1 = new BasicDataSource();
    dataSource1.setDriverClassName("org.h2.Driver");
    dataSource1.setUsername("username");
    dataSource1.setPassword("password");
    dataSource1.setUrl("jdbc:h2:mem:test" + DB_NAME);
    connection = dataSource1.getConnection();
    connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + getFilePath(H2_SCRIPT_NAME) + "'");

    mockStatic(FrameworkUtils.class);
    when(FrameworkUtils.getMultiAttributeSeparator()).thenReturn(MULTI_ATTRIBUTE_SEPARATOR_DEFAULT);

    RequestObjectService requestObjectService = Mockito.mock(RequestObjectService.class);
    List<RequestedClaim> requestedClaims = Collections.emptyList();
    when(requestObjectService.getRequestedClaimsForIDToken(anyString())).thenReturn(requestedClaims);
    when(requestObjectService.getRequestedClaimsForUserInfo(anyString())).thenReturn(requestedClaims);

    // Skipping filtering with user consent.
    // TODO: Remove mocking claims filtering based on consent when fixing https://github.com/wso2/product-is/issues/2676
    OpenIDConnectClaimFilterImpl openIDConnectClaimFilter = spy(new OpenIDConnectClaimFilterImpl());
    when(openIDConnectClaimFilter.getClaimsFilteredByUserConsent(anyMap(), any(AuthenticatedUser.class),
            anyString(), anyString())).thenAnswer(invocation -> invocation.getArguments()[0]);
    OpenIDConnectServiceComponentHolder.getInstance().getOpenIDConnectClaimFilters()
            .add(openIDConnectClaimFilter);

    OpenIDConnectServiceComponentHolder.setRequestObjectService(requestObjectService);
    defaultOIDCClaimsCallbackHandler = new DefaultOIDCClaimsCallbackHandler();

}

From source file:org.wso2.carbon.identity.openidconnect.DefaultOIDCClaimsCallbackHandlerTest.java

private JWTClaimsSet getJwtClaimSet(JWTClaimsSet.Builder jwtClaimsSetBuilder,
        OAuthTokenReqMessageContext requestMsgCtx) {

    OAuthServerConfiguration mockOAuthServerConfiguration = PowerMockito.mock(OAuthServerConfiguration.class);
    DataSource dataSource = mock(DataSource.class);
    mockStatic(JDBCPersistenceManager.class);
    JDBCPersistenceManager jdbcPersistenceManager = mock(JDBCPersistenceManager.class);
    mockStatic(OAuthServerConfiguration.class);
    when(OAuthServerConfiguration.getInstance()).thenReturn(mockOAuthServerConfiguration);
    when(mockOAuthServerConfiguration.isConvertOriginalClaimsFromAssertionsToOIDCDialect()).thenReturn(true);
    JWTClaimsSet jwtClaimsSet = null;//from  ww  w  .  java  2 s.  c o m

    try {
        if (connection.isClosed()) {

            BasicDataSource dataSource1 = new BasicDataSource();
            dataSource1.setDriverClassName("org.h2.Driver");
            dataSource1.setUsername("username");
            dataSource1.setPassword("password");
            dataSource1.setUrl("jdbc:h2:mem:test" + DB_NAME);
            Connection connection1 = null;
            connection1 = dataSource1.getConnection();
            Mockito.when(dataSource.getConnection()).thenReturn(connection1);

        } else {
            Mockito.when(dataSource.getConnection()).thenReturn(connection);
        }
    } catch (Exception e) {
        log.error("Error while obtaining the datasource. ");
    }

    Mockito.when(jdbcPersistenceManager.getInstance()).thenReturn(jdbcPersistenceManager);
    Mockito.when(jdbcPersistenceManager.getDataSource()).thenReturn(dataSource);
    jwtClaimsSet = defaultOIDCClaimsCallbackHandler.handleCustomClaims(jwtClaimsSetBuilder, requestMsgCtx);

    //return jwtClaimsSet;

    return jwtClaimsSet;
}

From source file:org.wso2.carbon.identity.openidconnect.util.TestUtils.java

public static void initiateH2Base() throws SQLException {

    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUsername("username");
    dataSource.setPassword("password");
    dataSource.setUrl("jdbc:h2:mem:test" + DB_NAME);
    Connection connection = dataSource.getConnection();
    connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + getFilePath(H2_SCRIPT_NAME) + "'");

    dataSourceMap.put(DB_NAME, dataSource);
}

From source file:org.wso2.carbon.registry.core.jdbc.utils.RegistryDataSource.java

public RegistryDataSource(DataBaseConfiguration config) {

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(config.getDbUrl());
    basicDataSource.setDriverClassName(config.getDriverName());
    basicDataSource.setUsername(config.getUserName());
    basicDataSource.setPassword(config.getResolvedPassword());

    if (config.getMaxActive() != null) {
        basicDataSource.setMaxActive(Integer.parseInt(config.getMaxActive()));
    } else {/*w  ww .  j  av  a  2  s  . c  o m*/
        basicDataSource.setMaxActive(DEFAULT_MAX_ACTIVE);
    }

    if (config.getMaxWait() != null) {
        basicDataSource.setMaxWait(Integer.parseInt(config.getMaxWait()));
    } else {
        basicDataSource.setMaxWait(DEFAULT_MAX_WAIT);
    }

    if (config.getMaxIdle() != null) {
        basicDataSource.setMaxIdle(Integer.parseInt(config.getMaxIdle()));
    }

    if (config.getMinIdle() != null) {
        basicDataSource.setMinIdle(Integer.parseInt(config.getMinIdle()));
    } else {
        basicDataSource.setMinIdle(DEFAULT_MIN_IDLE);
    }

    this.dataSource = basicDataSource;
}

From source file:org.wso2.carbon.registry.core.test.performance.BasicPerformanceTest.java

public void setUp() {
    //String connURL = "jdbc:log4jdbc:derby:target/REG1_DB";
    //String connURL = "jdbc:derby:target/REG1_DB";
    String connURL = "jdbc:mysql://localhost:3306/registry1";
    BasicDataSource ds = new BasicDataSource();
    //ds.setUrl(connURL + ";");
    //ds.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl(connURL);//from ww w.  ja  va 2s . c  o m
    ds.setUsername("root");
    ds.setPassword("password");

    //        ds.setMaxWait(1000*60*2);

    ds.setMaxActive(150);
    ds.setMaxIdle(1000 * 60 * 2);
    ds.setMinIdle(5);
    //ds.setDriverClassName("net.sf.log4jdbc.DriverSpy");

    //DerbyDatabaseCreator creator = new DerbyDatabaseCreator(ds);
    DatabaseCreator creator = new DatabaseCreator(ds);
    try {
        creator.createRegistryDatabase();
    } catch (Exception e) {
        fail("Failed to create database. Caused by: " + e.getMessage());
    }
    //String fileName = "target/db/registry";
    //File file = new File(fileName);

    //if (! file.exists()) {
    //creator.createDefaultDatabaseTables();
    //}

    //        UserRealm realm = new DefaultRealm();
    //        DefaultRealmConfig config = (DefaultRealmConfig) realm.getBootstrapRealmConfiguration();
    //        config.setConnectionURL(connURL);
    //        realm.init(config);
    //        UserRealm registryRealm = new UserRealm(realm);
    //
    //        InputStream configStream =
    //            Thread.currentThread().getContextClassLoader().getResourceAsStream("registry.xml");
    //        RegistryContext regContext = new RegistryContext(configStream, registryRealm);
    //        embeddedRegistryService = new EmbeddedRegistryService(regContext);
    //        adminRegistry = embeddedRegistryService.getUserRegistry(
    //            RegistryConstants.ADMIN_USER, RegistryConstants.ADMIN_PASSWORD);
    System.out.println("~~~~~setup method done~~~~~");
}