Search in sources :

Example 1 with LDAPConnectionPool

use of com.unboundid.ldap.sdk.LDAPConnectionPool in project oxCore by GluuFederation.

the class LDAPConnectionProvider method createSSLConnectionPoolWithPreviousProtocols.

private LDAPConnectionPool createSSLConnectionPoolWithPreviousProtocols(SSLUtil sslUtil, BindRequest bindRequest, LDAPConnectionOptions connectionOptions, int maxConnections) throws LDAPException {
    for (int i = 1; i < SSL_PROTOCOLS.length; i++) {
        String protocol = SSL_PROTOCOLS[i];
        try {
            FailoverServerSet failoverSet = new FailoverServerSet(this.addresses, this.ports, sslUtil.createSSLSocketFactory(protocol), connectionOptions);
            LDAPConnectionPool connectionPool = new LDAPConnectionPool(failoverSet, bindRequest, maxConnections);
            log.info("Server supports: '" + protocol + "'");
            return connectionPool;
        } catch (GeneralSecurityException ex) {
            log.debug("Server not supports: '" + protocol + "'", ex);
        } catch (LDAPException ex) {
            // Error when LDAP server not supports specified encryption
            if (ex.getResultCode() != ResultCode.SERVER_DOWN) {
                throw ex;
            }
            log.debug("Server not supports: '" + protocol + "'", ex);
        }
    }
    return null;
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) LDAPException(com.unboundid.ldap.sdk.LDAPException) GeneralSecurityException(java.security.GeneralSecurityException) FailoverServerSet(com.unboundid.ldap.sdk.FailoverServerSet)

Example 2 with LDAPConnectionPool

use of com.unboundid.ldap.sdk.LDAPConnectionPool in project oxCore by GluuFederation.

the class LDAPConnectionProvider method createConnectionPoolWithWaitImpl.

private LDAPConnectionPool createConnectionPoolWithWaitImpl(Properties props, FailoverServerSet failoverSet, BindRequest bindRequest, LDAPConnectionOptions connectionOptions, int maxConnections, SSLUtil sslUtil) throws LDAPException {
    String connectionPoolMaxWaitTime = props.getProperty("connection-pool-max-wait-time");
    int connectionPoolMaxWaitTimeSeconds = 30;
    if (StringHelper.isNotEmpty(connectionPoolMaxWaitTime)) {
        connectionPoolMaxWaitTimeSeconds = Integer.parseInt(connectionPoolMaxWaitTime);
    }
    log.debug("Using LDAP connection pool timeout: '" + connectionPoolMaxWaitTimeSeconds + "'");
    LDAPConnectionPool createdConnectionPool = null;
    LDAPException lastException = null;
    int attempt = 0;
    long currentTime = System.currentTimeMillis();
    long maxWaitTime = currentTime + connectionPoolMaxWaitTimeSeconds * 1000;
    do {
        attempt++;
        if (attempt > 0) {
            log.info("Attempting to create connection pool: " + attempt);
        }
        try {
            createdConnectionPool = createConnectionPoolImpl(failoverSet, bindRequest, connectionOptions, maxConnections, sslUtil);
            break;
        } catch (LDAPException ex) {
            if (ex.getResultCode().intValue() != ResultCode.CONNECT_ERROR_INT_VALUE) {
                throw ex;
            }
            lastException = ex;
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            log.error("Exception happened in sleep", ex);
            return null;
        }
        currentTime = System.currentTimeMillis();
    } while (maxWaitTime > currentTime);
    if ((createdConnectionPool == null) && (lastException != null)) {
        throw lastException;
    }
    return createdConnectionPool;
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) LDAPException(com.unboundid.ldap.sdk.LDAPException)

Example 3 with LDAPConnectionPool

use of com.unboundid.ldap.sdk.LDAPConnectionPool in project oxCore by GluuFederation.

the class LdapConnectionProvider method createSSLConnectionPoolWithPreviousProtocols.

private LDAPConnectionPool createSSLConnectionPoolWithPreviousProtocols(SSLUtil sslUtil, BindRequest bindRequest, LDAPConnectionOptions connectionOptions, int maxConnections) throws LDAPException {
    for (int i = 1; i < SSL_PROTOCOLS.length; i++) {
        String protocol = SSL_PROTOCOLS[i];
        try {
            FailoverServerSet failoverSet = new FailoverServerSet(this.addresses, this.ports, sslUtil.createSSLSocketFactory(protocol), connectionOptions);
            LDAPConnectionPool connectionPool = new LDAPConnectionPool(failoverSet, bindRequest, maxConnections);
            LOG.info("Server supports: '" + protocol + "'");
            return connectionPool;
        } catch (GeneralSecurityException ex) {
            LOG.debug("Server not supports: '" + protocol + "'", ex);
        } catch (LDAPException ex) {
            // Error when LDAP server not supports specified encryption
            if (ex.getResultCode() != ResultCode.SERVER_DOWN) {
                throw ex;
            }
            LOG.debug("Server not supports: '" + protocol + "'", ex);
        }
    }
    return null;
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) LDAPException(com.unboundid.ldap.sdk.LDAPException) GeneralSecurityException(java.security.GeneralSecurityException) FailoverServerSet(com.unboundid.ldap.sdk.FailoverServerSet)

Example 4 with LDAPConnectionPool

use of com.unboundid.ldap.sdk.LDAPConnectionPool in project zm-mailbox by Zimbra.

the class TestLdapConnection method backgroundHealthCheck.

@Test
public void backgroundHealthCheck() throws Exception {
    SKIP_FOR_INMEM_LDAP_SERVER(SkipTestReason.CONNECTION_POOL_HEALTH_CHECK);
    // 5 secs
    final long BACKGROUND_HEALTH_CHECK_INTERVAL = 5000;
    Map<KnownKey, String> lcKeysToModify = new HashMap<KnownKey, String>();
    lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "false");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_after_exception_enabled, "false");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_enabled, "true");
    lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_interval_millis, Long.valueOf(BACKGROUND_HEALTH_CHECK_INTERVAL).toString());
    Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
    final int MAX_POOL_SIZE = LC.ldap_connect_pool_maxsize.intValue();
    final int NUM_CONNS = 10;
    ExternalLdapConfig ldapConfig = new ExternalLdapConfig(LDAP_URL_BACKGROUND, START_TLS_ENABLED, null, BIND_DN, BIND_PASSWORD, null, null);
    LDAPConnectionPool connPool = populateConnPool(ldapConfig, NUM_CONNS);
    // stop ldap server here
    System.out.println("Before health check, availConns = " + connPool.getCurrentAvailableConnections());
    stopLdap();
    // wait for the health check interval to trigger health check
    long waitFor = BACKGROUND_HEALTH_CHECK_INTERVAL + 1000;
    System.out.println("Waiting for " + waitFor + " msecs");
    Thread.sleep(waitFor);
    System.out.println("After health check, availConns = " + connPool.getCurrentAvailableConnections());
    assertEquals(0, connPool.getCurrentAvailableConnections());
    // put the config key back
    setLocalConfig(origLCKeyValues);
    startLdap();
    // get a connection now, should be successful
    UBIDLdapContext zlc = getContext(ldapConfig);
    closeContext(zlc);
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) KnownKey(com.zimbra.common.localconfig.KnownKey) ExternalLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig) HashMap(java.util.HashMap) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext) Test(org.junit.Test)

Example 5 with LDAPConnectionPool

use of com.unboundid.ldap.sdk.LDAPConnectionPool in project zm-mailbox by Zimbra.

the class TestLdapConnection method populateConnPool.

private LDAPConnectionPool populateConnPool(ExternalLdapConfig ldapConfig, int numConns) throws Exception {
    final int MAX_POOL_SIZE = LC.ldap_connect_pool_maxsize.intValue();
    assertTrue(numConns < MAX_POOL_SIZE);
    LDAPConnectionPool connPool = null;
    List<UBIDLdapContext> zlcs = Lists.newArrayList();
    for (int i = 0; i < numConns; i++) {
        UBIDLdapContext zlc = getContext(ldapConfig);
        zlcs.add(zlc);
        if (connPool == null) {
            connPool = zlc.getConnectionPool();
        // System.out.println("backgroundHealthCheck pool is " + connPool.getConnectionPoolName());
        } else {
            // verify all zlcs use the same conn pool
            assertTrue(connPool == zlc.getConnectionPool());
        }
    }
    assertEquals(MAX_POOL_SIZE, connPool.getMaximumAvailableConnections());
    // number of connections that are currently available for use in this connection pool
    assertEquals(0, connPool.getCurrentAvailableConnections());
    for (int i = 0; i < numConns; i++) {
        UBIDLdapContext zlc = zlcs.get(i);
        closeContext(zlc);
    }
    // There should be NUM_CONNS conns in the pool
    assertEquals(numConns, connPool.getCurrentAvailableConnections());
    return connPool;
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext)

Aggregations

LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)12 LDAPException (com.unboundid.ldap.sdk.LDAPException)5 UBIDLdapContext (com.zimbra.cs.ldap.unboundid.UBIDLdapContext)5 Test (org.junit.Test)4 KnownKey (com.zimbra.common.localconfig.KnownKey)3 ExternalLdapConfig (com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig)3 HashMap (java.util.HashMap)3 FailoverServerSet (com.unboundid.ldap.sdk.FailoverServerSet)2 ServiceException (com.zimbra.common.service.ServiceException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 BindRequest (com.unboundid.ldap.sdk.BindRequest)1 GetEntryLDAPConnectionPoolHealthCheck (com.unboundid.ldap.sdk.GetEntryLDAPConnectionPoolHealthCheck)1 PostConnectProcessor (com.unboundid.ldap.sdk.PostConnectProcessor)1 ServerSet (com.unboundid.ldap.sdk.ServerSet)1 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)1 StartTLSPostConnectProcessor (com.unboundid.ldap.sdk.StartTLSPostConnectProcessor)1 SSLContext (javax.net.ssl.SSLContext)1 Ignore (org.junit.Ignore)1