// Build the connection address
SocketAddress address = new InetSocketAddress( config.getLdapHost(), config.getLdapPort() );
// And create the connection future
ConnectFuture connectionFuture = connector.connect( address );
// Wait until it's established
try
{
connectionFuture.await( timeout );
}
catch ( InterruptedException e )
{
connector = null;
LOG.debug( "Interrupted while waiting for connection to establish with server {}:{}", config.getLdapHost(),
config.getLdapPort(), e );
throw new LdapOtherException( e.getMessage(), e );
}
boolean isConnected = connectionFuture.isConnected();
if ( !isConnected )
{
// disposing connector if not connected
try
{
close();
}
catch ( IOException ioe )
{
// Nothing to do
}
Throwable e = connectionFuture.getException();
if ( e != null )
{
StringBuilder message = new StringBuilder( "Cannot connect on the server: " );
// Special case for UnresolvedAddressException
// (most of the time no message is associated with this exception)
if ( ( e instanceof UnresolvedAddressException ) && ( e.getMessage() == null ) )
{
message.append( "Hostname '" );
message.append( config.getLdapHost() );
message.append( "' could not be resolved." );
throw new InvalidConnectionException( message.toString(), e );
}
// Default case
message.append( e.getMessage() );
throw new InvalidConnectionException( message.toString(), e );
}
return false;
}
// Get the close future for this session
CloseFuture closeFuture = connectionFuture.getSession().getCloseFuture();
// Add a listener to close the session in the session.
closeFuture.addListener( new IoFutureListener<IoFuture>()
{
public void operationComplete( IoFuture future )
{
// Process all the waiting operations and cancel them
LOG.debug( "received a NoD, closing everything" );
for ( int messageId : futureMap.keySet() )
{
ResponseFuture<?> responseFuture = futureMap.get( messageId );
LOG.debug( "closing {}", responseFuture );
responseFuture.cancel();
try
{
if ( responseFuture instanceof AddFuture )
{
( ( AddFuture ) responseFuture ).set( AddNoDResponse.PROTOCOLERROR );
}
else if ( responseFuture instanceof BindFuture )
{
( ( BindFuture ) responseFuture ).set( BindNoDResponse.PROTOCOLERROR );
}
else if ( responseFuture instanceof CompareFuture )
{
( ( CompareFuture ) responseFuture ).set( CompareNoDResponse.PROTOCOLERROR );
}
else if ( responseFuture instanceof DeleteFuture )
{
( ( DeleteFuture ) responseFuture ).set( DeleteNoDResponse.PROTOCOLERROR );
}
else if ( responseFuture instanceof ExtendedFuture )
{
( ( ExtendedFuture ) responseFuture ).set( ExtendedNoDResponse.PROTOCOLERROR );
}
else if ( responseFuture instanceof ModifyFuture )
{
( ( ModifyFuture ) responseFuture ).set( ModifyNoDResponse.PROTOCOLERROR );
}
else if ( responseFuture instanceof ModifyDnFuture )
{
( ( ModifyDnFuture ) responseFuture ).set( ModifyDnNoDResponse.PROTOCOLERROR );
}
else if ( responseFuture instanceof SearchFuture )
{
( ( SearchFuture ) responseFuture ).set( SearchNoDResponse.PROTOCOLERROR );
}
}
catch ( ExecutionException e )
{
LOG.error( "Error while processing the NoD for {}", responseFuture );
}
catch ( InterruptedException e )
{
LOG.error( "Error while processing the NoD for {}", responseFuture );
}
futureMap.remove( messageId );
}
futureMap.clear();
}
} );
// Get back the session
ldapSession = connectionFuture.getSession();
connected.set( true );
// Store the container into the session if we don't have one
@SuppressWarnings("unchecked")
LdapMessageContainer<MessageDecorator<? extends Message>> container =