@Override public com.google.common.base.Optional<Boolean> isHealthy() { try { final LdapConnection connection = connectionPool.getConnection(); try { if (connection.isConnected() && connection.isAuthenticated()) { connection.bind(); try { return com.google.common.base.Optional.of(true); } finally { connection.unBind(); } } } finally { connectionPool.releaseConnection(connection); } } catch (LdapException e) { LOG.warn("LdapException caught when checking health", e); exceptionNotifier.notify(String.format("LdapException caught when checking health (%s)", e.getMessage()), e); } return com.google.common.base.Optional.of(false); }
private static LdapConnectionPool createConnectionPool(LDAPConfiguration configuration) throws IOException { final LdapConnectionConfig config = new LdapConnectionConfig(); config.setLdapHost(configuration.getHostname()); config.setLdapPort(configuration.getPort()); config.setName(configuration.getBindDn()); config.setCredentials(configuration.getBindPassword()); final DefaultPoolableLdapConnectionFactory factory = new DefaultPoolableLdapConnectionFactory(config); final LdapConnectionPool pool = new LdapConnectionPool(factory); pool.setTestOnBorrow(configuration.isPoolTestOnBorrow()); pool.setTestOnReturn(configuration.isPoolTestOnReturn()); pool.setTestWhileIdle(configuration.isPoolTestWhileIdle()); pool.setMaxActive(configuration.getPoolMaxActive()); pool.setMaxIdle(configuration.getPoolMaxIdle()); pool.setMinIdle(configuration.getPoolMinIdle()); pool.setMaxWait(configuration.getPoolMaxWait()); switch (configuration.getPoolWhenExhaustedAction()) { case BLOCK: pool.setWhenExhaustedAction(LdapConnectionPool.WHEN_EXHAUSTED_BLOCK); break; case FAIL: pool.setWhenExhaustedAction(LdapConnectionPool.WHEN_EXHAUSTED_FAIL); break; case GROW: pool.setWhenExhaustedAction(LdapConnectionPool.WHEN_EXHAUSTED_GROW); break; default: pool.setWhenExhaustedAction(LdapConnectionPool.DEFAULT_WHEN_EXHAUSTED_ACTION); } return pool; }
/** * Calls the PoolMgr to get an Admin connection to the LDAP server. * * @return ldap connection. * @throws LdapException If we had an issue getting an LDAP connection */ public LdapConnection getAdminConnection() throws LdapException { try { return adminPool.getConnection(); } catch ( Exception e ) { throw new LdapException( e ); } }
@Override public void close() throws IOException { try { pool.releaseConnection(con); } catch (LdapException e) { throw new IOException("LDAP Release Connection error", e); } }
adminPool = new LdapConnectionPool( poolFactory ); adminPool.setTestOnBorrow( true ); adminPool.setWhenExhaustedAction( GenericObjectPool.WHEN_EXHAUSTED_GROW ); adminPool.setMaxActive( max ); adminPool.setMinIdle( min ); adminPool.setMaxIdle( -1 ); userPool = new LdapConnectionPool( poolFactory ); userPool.setTestOnBorrow( true ); userPool.setWhenExhaustedAction( GenericObjectPool.WHEN_EXHAUSTED_GROW ); userPool.setMaxActive( max ); userPool.setMinIdle( min ); userPool.setMaxIdle( -1 ); logPool = new LdapConnectionPool( poolFactory ); logPool.setTestOnBorrow( true ); logPool.setWhenExhaustedAction( GenericObjectPool.WHEN_EXHAUSTED_GROW ); logPool.setMaxActive( logmax ); logPool.setMinIdle( logmin );
adminPool = new LdapConnectionPool(adminConnectionFactory); adminPool.setTestOnBorrow(true); adminPool.setMaxActive(config.getAdminPoolConfig().getMaxActive()); adminPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
/** * Gets the admin pool. * * @param ldapServer the ldap server * @return the admin pool */ private static LdapConnectionPool getAdminPool( LdapServer ldapServer ) { int port = ldapServer.getPort(); if ( !POOLS.containsKey( port ) ) { LdapConnectionConfig config = new LdapConnectionConfig(); config.setLdapHost( Network.LOOPBACK_HOSTNAME ); config.setLdapPort( port ); config.setName( DEFAULT_ADMIN ); config.setCredentials( DEFAULT_PASSWORD ); DefaultPoolableLdapConnectionFactory factory = new DefaultPoolableLdapConnectionFactory( config ); LdapConnectionPool pool = new LdapConnectionPool( factory ); pool.setTestOnBorrow( true ); POOLS.put( port, pool ); } return POOLS.get( port ); }
public LdapUserDAO(SecurityServiceConfiguration config, boolean useCache) { this.connConfig = config.getLdapConnectionConfig(); this.requests = config.getLdapSearch(); this.useBindTemplate = config.isUseLdapBindTemplate(); this.bindTemplate = config.getLdapBindTemplate(); this.ldapBindAttribute = config.getLdapBindAttribute(); this.ldapSearchAttribute = "%" + config.getLdapSearchAttribute() + "%"; PoolableObjectFactory<LdapConnection> userPoolFactory = new ValidatingPoolableLdapConnectionFactory (connConfig); this.usersPool = new LdapConnectionPool(userPoolFactory); PoolableObjectFactory<LdapConnection> searchPoolFactory = new ValidatingPoolableLdapConnectionFactory (connConfig); this.searchPool = new LdapConnectionPool(searchPoolFactory); this.useCache = useCache; this.ldapUseConnectionPool = config.isLdapUseConnectionPool(); }
/** * Instantiates a new LDAP connection pool. * * @param connectionConfig The connection configuration * @param apiService The api service (codec) * @param timeout The connection timeout in millis * @param poolConfig The pool configuration */ public LdapConnectionPool( LdapConnectionConfig connectionConfig, LdapApiService apiService, long timeout, GenericObjectPoolConfig poolConfig ) { this( newPoolableConnectionFactory( connectionConfig, apiService, timeout ), poolConfig ); }
/** * Creates a new instance of LdapConnectionTemplate. * * @param connectionPool The pool to obtain connections from. */ public LdapConnectionTemplate( LdapConnectionPool connectionPool ) { if ( LOG.isDebugEnabled() ) { LOG.debug( I18n.msg( I18n.MSG_04174_CREATING_NEW_CONNECTION_TEMPLATE ) ); } this.connectionPool = connectionPool; this.passwordPolicyRequestControl = new PasswordPolicyDecorator( connectionPool.getLdapApiService() ); this.passwordPolicyResponder = new PasswordPolicyResponderImpl( connectionPool.getLdapApiService() ); this.modelFactory = new ModelFactoryImpl(); }
/** * Calls the PoolMgr to get an Log connection to the LDAP server. * * @return ldap connection. * @throws LdapException If we had an issue getting an LDAP connection */ public LdapConnection getLogConnection() throws LdapException { try { return logPool.getConnection(); } catch ( Exception e ) { throw new LdapException( e ); } }
/** * Calls the PoolMgr to close the Log LDAP connection. * * @param connection handle to ldap connection object. */ public void closeLogConnection(LdapConnection connection) { try { logPool.releaseConnection( connection ); } catch ( Exception e ) { throw new RuntimeException( e ); } }
@Override public void load() { config = new LdapConnectionConfig(); if (host != null) config.setLdapHost(host); if (port != null) config.setLdapPort(port); if (username != null) config.setName(username); if (password != null) config.setCredentials(password); if (usePool != null && usePool) { ValidatingPoolableLdapConnectionFactory factory = new ValidatingPoolableLdapConnectionFactory(config); connectionPool = new LdapConnectionPool(factory); connectionPool.setTestOnBorrow(true); } else connectionPool = null; }
return new LdapConnectionPool( poolableLdapConnectionFactory, poolConfig );
/** * Closes this provider and releases the internal pool. This should be called by Non-OSGi users of this provider. */ public void close() { if (adminPool != null) { try { adminPool.close(); } catch (Exception e) { log.warn("Error while closing LDAP connection pool", e); } adminPool = null; } if (userPool != null) { try { userPool.close(); } catch (Exception e) { log.warn("Error while closing LDAP connection pool", e); } userPool = null; } }
/** * Instantiates a new LDAP connection pool. * * @param connectionConfig The connection configuration * @param apiService The api service (codec) * @param timeout The connection timeout in millis * @param poolConfig The pool configuration */ public LdapConnectionPool( LdapConnectionConfig connectionConfig, LdapApiService apiService, long timeout, GenericObjectPoolConfig poolConfig ) { this( newPoolableConnectionFactory( connectionConfig, apiService, timeout ), poolConfig ); }
/** * Creates a new instance of LdapConnectionTemplate. * * @param connectionPool The pool to obtain connections from. */ public LdapConnectionTemplate( LdapConnectionPool connectionPool ) { if ( LOG.isDebugEnabled() ) { LOG.debug( I18n.msg( I18n.MSG_04174_CREATING_NEW_CONNECTION_TEMPLATE ) ); } this.connectionPool = connectionPool; this.passwordPolicyRequestControl = new PasswordPolicyDecorator( connectionPool.getLdapApiService() ); this.passwordPolicyResponder = new PasswordPolicyResponderImpl( connectionPool.getLdapApiService() ); this.modelFactory = new ModelFactoryImpl(); }
/** * Calls the PoolMgr to get an User connection to the LDAP server. * * @return ldap connection. * @throws LdapException If we had an issue getting an LDAP connection */ public LdapConnection getUserConnection() throws LdapException { try { return userPool.getConnection(); } catch ( Exception e ) { throw new LdapException( e ); } }