Search in sources :

Example 6 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 7 with LDAPConnectionPool

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

the class LdapConnectionPool method createConnPool.

private static LDAPConnectionPool createConnPool(LdapServerConfig config) throws LdapException {
    LdapServerPool serverPool = new LdapServerPool(config);
    ServerSet serverSet = serverPool.getServerSet();
    BindRequest bindRequest = createBindRequest(config);
    PostConnectProcessor postConnectProcessor = null;
    if (serverPool.getConnectionType() == LdapConnType.STARTTLS) {
        SSLContext startTLSContext = LdapSSLUtil.createSSLContext(config.sslAllowUntrustedCerts());
        postConnectProcessor = new StartTLSPostConnectProcessor(startTLSContext);
    }
    LDAPConnectionPool connPool = null;
    try {
        connPool = new LDAPConnectionPool(serverSet, bindRequest, config.getConnPoolInitSize(), config.getConnPoolMaxSize(), postConnectProcessor);
        connPool.setRetryFailedOperationsDueToInvalidConnections(true);
    } catch (LDAPException e) {
        throw UBIDLdapException.mapToLdapException(e);
    }
    return connPool;
}
Also used : ServerSet(com.unboundid.ldap.sdk.ServerSet) LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) LDAPException(com.unboundid.ldap.sdk.LDAPException) PostConnectProcessor(com.unboundid.ldap.sdk.PostConnectProcessor) StartTLSPostConnectProcessor(com.unboundid.ldap.sdk.StartTLSPostConnectProcessor) BindRequest(com.unboundid.ldap.sdk.BindRequest) SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) SSLContext(javax.net.ssl.SSLContext) StartTLSPostConnectProcessor(com.unboundid.ldap.sdk.StartTLSPostConnectProcessor)

Example 8 with LDAPConnectionPool

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

the class LdapConnectionPool method addToPoolMap.

private static synchronized void addToPoolMap(LDAPConnectionPool connPool) {
    String poolName = connPool.getConnectionPoolName();
    LDAPConnectionPool pool = connPools.get(poolName);
    assert (pool == null);
    connPools.put(poolName, connPool);
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool)

Example 9 with LDAPConnectionPool

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

the class LdapConnectionPool method createConnectionPool.

static LDAPConnectionPool createConnectionPool(String connPoolName, LdapServerConfig config) throws LdapException {
    LDAPConnectionPool connPool = null;
    if (InMemoryLdapServer.isOn()) {
        connPool = createConnPoolToInMemoryLdapServer(config);
    } else {
        connPool = createConnPool(config);
    }
    connPool.setConnectionPoolName(connPoolName);
    connPool.setMaxWaitTimeMillis(config.getConnPoolTimeoutMillis());
    boolean onCheckoutHealthCheckEnabled = config.isConnPoolHelathCheckOnCheckoutEnabled();
    boolean backgroundHealthCheckEnabled = !onCheckoutHealthCheckEnabled;
    // because otherwise it has no effect anyway.
    if (backgroundHealthCheckEnabled) {
        connPool.setHealthCheckIntervalMillis(config.getConnPoolHelathCheckBackgroundIntervalMillis());
    }
    GetEntryLDAPConnectionPoolHealthCheck healthChecker = new GetEntryLDAPConnectionPoolHealthCheck(// entryDN (null means root DSE)
    null, // maxResponseTime
    config.getConnPoolHelathCheckMaxResponseTimeMillis(), // invokeOnCreate
    false, // invokeOnCheckout
    onCheckoutHealthCheckEnabled, // invokeOnRelease
    false, // invokeForBackgroundChecks
    backgroundHealthCheckEnabled, // invokeOnException
    false);
    connPool.setHealthCheck(healthChecker);
    addToPoolMap(connPool);
    return connPool;
}
Also used : LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) GetEntryLDAPConnectionPoolHealthCheck(com.unboundid.ldap.sdk.GetEntryLDAPConnectionPoolHealthCheck)

Example 10 with LDAPConnectionPool

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

the class TestLdapConnection method onCheckoutHealthCheck.

@Test
public void onCheckoutHealthCheck() throws Exception {
    SKIP_FOR_INMEM_LDAP_SERVER(SkipTestReason.CONNECTION_POOL_HEALTH_CHECK);
    Map<KnownKey, String> lcKeysToModify = new HashMap<KnownKey, String>();
    lcKeysToModify.put(LC.ldap_connect_pool_health_check_on_checkout_enabled, "true");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_after_exception_enabled, "false");
    // lcKeysToModify.put(LC.ldap_connect_pool_health_check_background_enabled, "false");
    Map<KnownKey, String> origLCKeyValues = setLocalConfig(lcKeysToModify);
    final int NUM_CONNS = 10;
    ExternalLdapConfig ldapConfig = new ExternalLdapConfig(LDAP_URL_ON_CHECKOUT, 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();
    // try to get a connection from the pool to trigger health check
    boolean caughtException = false;
    try {
        UBIDLdapContext zlc = getContext(ldapConfig);
    } catch (ServiceException e) {
        caughtException = true;
    }
    assertTrue(caughtException);
    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) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) UBIDLdapContext(com.zimbra.cs.ldap.unboundid.UBIDLdapContext) Test(org.junit.Test)

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