Example usage for org.apache.commons.pool2.impl GenericObjectPool getFactory

List of usage examples for org.apache.commons.pool2.impl GenericObjectPool getFactory

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericObjectPool getFactory.

Prototype

public PooledObjectFactory<T> getFactory() 

Source Link

Document

Obtain a reference to the factory used to create, destroy and validate the objects used by this pool.

Usage

From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

/**
 * Verifies that we can create a pooled jdbc data source using the JDBC .jar-file supplied in the global configuration
 * file./*w  w  w. j  av  a2  s  . com*/
 *
 * @throws Exception
 */
@Test
public void testLoadJdbcDriverUsingCustomClassLoader() throws Exception {
    ConnectionFactory driverConnectionFactory = createConnectionFactory(false);

    ObjectName poolName = new ObjectName("no.difi.oxalis", "connectionPool", "TestPool");
    PoolableConnectionFactory factory = new PoolableConnectionFactory(driverConnectionFactory, poolName);

    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);

    pool.setMaxTotal(10);
    pool.setMaxWaitMillis(100);

    assertEquals(pool.getFactory(), factory);

    PoolableConnectionFactory pcf = (PoolableConnectionFactory) ((GenericObjectPool<?>) pool).getFactory();
    ObjectPool<PoolableConnection> pool1 = pcf.getPool();

    PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(pool);

    Connection connection = poolingDataSource.getConnection();
    assertNotNull(connection);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select current_date()");

    assertTrue(resultSet.next());
}

From source file:net.identio.server.service.authentication.ldap.LdapAuthenticationProvider.java

public AuthenticationResult validate(AuthMethod authMethod, Authentication authentication,
        TransactionData transactionData) {

    LdapAuthMethod ldapAuthMethod = (LdapAuthMethod) authMethod;
    UserPasswordAuthentication userPwAuthentication = (UserPasswordAuthentication) authentication;

    boolean validation;

    String userId = userPwAuthentication.getUserId();
    String password = userPwAuthentication.getPassword();

    GenericObjectPool<InitialLdapContext> pool = pools.get(authMethod.getName());

    InitialLdapContext ctx = null;

    try {/*from   w  w w . j  av  a  2  s  .c  om*/
        ctx = pool.borrowObject();

        // First we search the user
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        String searchFilter = ldapAuthMethod.getUserSearchFilter().replace("#UID",
                SecurityUtils.escapeLDAPSearchFilter(userId));

        NamingEnumeration<SearchResult> results = ctx.search(ldapAuthMethod.getBaseDn(), searchFilter,
                controls);

        SearchResult result;

        if (results.hasMoreElements()) {
            result = results.next();

            if (results.hasMoreElements()) {
                LOG.error("User ID {} is not unique in LDAP {}", userId, authMethod.getName());
                return new AuthenticationResult().setStatus(AuthenticationResultStatus.FAIL)
                        .setErrorStatus(AuthenticationErrorStatus.USER_NOT_UNIQUE);
            }
        } else {
            LOG.error("User ID {} does not exist in LDAP {}", userId, authMethod.getName());
            return new AuthenticationResult().setStatus(AuthenticationResultStatus.FAIL)
                    .setErrorStatus(AuthenticationErrorStatus.INVALID_CREDENTIALS);
        }

        // Try to bind with the found user id
        validation = ((LdapConnectionFactory) pool.getFactory()).authenticate(authMethod.getName(),
                result.getNameInNamespace(), password);

        pool.returnObject(ctx);

        if (validation) {
            LOG.info("User {} successfully authenticated with {}", userId, authMethod.getName());
            return new AuthenticationResult().setStatus(AuthenticationResultStatus.SUCCESS).setUserId(userId)
                    .setAuthMethod(authMethod).setAuthLevel(authMethod.getAuthLevel());
        } else {
            LOG.error("Authentication failed for user {} with {}", userId, authMethod.getName());
            return new AuthenticationResult().setStatus(AuthenticationResultStatus.FAIL)
                    .setErrorStatus(AuthenticationErrorStatus.INVALID_CREDENTIALS);
        }

    } catch (Exception ex) {

        // Discard context
        try {
            if (ctx != null) {
                pool.invalidateObject(ctx);
            }
        } catch (Exception ex2) {
            LOG.error("An error occurend when authenticating user");
        }

        return new AuthenticationResult().setStatus(AuthenticationResultStatus.FAIL)
                .setErrorStatus(AuthenticationErrorStatus.TECHNICAL_ERROR);
    }

}

From source file:org.moneta.healthcheck.DbcpConnectionPoolHealthCheck.java

public DbcpConnectionPoolHealthCheck(GenericObjectPool<PoolableConnection> pool, String poolName) {
    Validate.notNull(pool, "Null connection pool not allowed.");
    Validate.notEmpty(poolName, "Null or blank poolName not allowed.");
    Validate.isTrue(pool.getFactory() instanceof PoolableConnectionFactory,
            "this check only handles connection pools using PoolableConnectionFactory");
    connectionPool = pool;/*w  w w .  ja  va  2  s.  co  m*/
    this.poolName = poolName;
    this.setMaxWaitingConnections(3);
}

From source file:org.moneta.healthcheck.DbcpConnectionPoolHealthCheck.java

@Override
protected Result check() throws Exception {

    GenericObjectPool<PoolableConnection> pool = (GenericObjectPool<PoolableConnection>) connectionPool;
    if (pool.getNumWaiters() > maxWaitingConnections) {
        return Result.unhealthy(
                "Overloaded connection pool.  name=" + poolName + " nbrWaiters=" + pool.getNumWaiters());
    }/*from www.j  a  va  2s  .  co  m*/

    PoolableConnectionFactory poolFactory = (PoolableConnectionFactory) pool.getFactory();
    PoolableConnection conn = null;
    try {
        conn = pool.borrowObject();
        poolFactory.validateConnection(conn);
    } catch (Exception e) {
        return Result
                .unhealthy("Database connection validation error.  error=" + ExceptionUtils.getStackTrace(e));
    } finally {
        DbUtils.closeQuietly(conn);
    }

    return Result.healthy();
}