com.cafesoft.cams.auth.login.module
Class TimeoutLDAPSocketFactory

java.lang.Object
  extended by com.cafesoft.cams.auth.login.module.TimeoutLDAPSocketFactory
All Implemented Interfaces:
com.novell.ldap.LDAPSocketFactory, Runnable

public class TimeoutLDAPSocketFactory
extends Object
implements com.novell.ldap.LDAPSocketFactory, Runnable

TimeoutLDAPSocketFactory implements an LDAPSocketFactory that provides connection timeout functionality. This is useful for Cams LDAP-based LoginModules when the default LDAPSocketFactory takes too long to fail when attempting to connect with an LDAP server, causing Cams agents to timeout while waiting for an authentication request. When multiple LoginModules are stacked, it may be desireable to enable authentication success based on the results from other LoginModules. This class essentially implements a way to interrupt an attempted connection to an LDAP server, which can take nearly 1 minute to fail in cases where the LDAP server host is unavailable due to a network outage.

This class will delegate to the specified LDAPSocketFactory if one is provided or will create a Socket directly if necessary. Because a new Thread is started to wait for the actual connection timeout, it may continue running well after the enclosing LoginModule has exited. The Thread will eventually stop and cleanup allocated resources.

The following example code shows how this class may be used:

 import com.cafesoft.cams.auth.login.module.TimeoutLDAPSocketFactory
 ...
    // Create an LDAPConnection to see if default LDAPSocketFactory is set
    LDAPConnection connection = new LDAPConnection();

    // Get the default LDAPSocketFactory (this may be null)
    LDAPSocketFactory defaultSocketFactory = connection.getSocketFactory();

    // Create an LDAPSocketFactory that will timeout on connection
    // (The timeout is set to 500 milliseconds for this example)
    LDAPSocketFactory timeoutSocketFactory =
       new TimeoutLDAPSocketFactory(defaultSocketFactory, 500L);

    // Now create a new LDAPConnection that uses the new factory
    connection = null;
    connection = new LDAPConnection(timeoutSocketFactory);

    // Attempt to connect with the LDAP server
    connection.connect();
 

Since:
02/05/04
See Also:
LDAPSocketFactory

Constructor Summary
TimeoutLDAPSocketFactory(com.novell.ldap.LDAPSocketFactory factory, long timeout)
          Create a new TimeoutLDAPSocketFactory.
 
Method Summary
 Socket createSocket(String host, int port)
          Create a new Socket to an LDAP server.
 void run()
          Attempt to connect with the LDAP server
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TimeoutLDAPSocketFactory

public TimeoutLDAPSocketFactory(com.novell.ldap.LDAPSocketFactory factory,
                                long timeout)
Create a new TimeoutLDAPSocketFactory.

Parameters:
factory - the real LDAPSocketFactory to which socket creation will be delegated. If null, then a standard Socket will be created.
timeout - the number of milliseconds to wait for connection before timing out.
Method Detail

createSocket

public Socket createSocket(String host,
                           int port)
                    throws IOException,
                           UnknownHostException
Create a new Socket to an LDAP server.

Specified by:
createSocket in interface com.novell.ldap.LDAPSocketFactory
Parameters:
host - the host name or IP address
port - the TCP port
Returns:
the Socket
Throws:
IOException - if unable to connect to the server at the specified host/port within the configured timeout.
UnknownHostException - if the host is unknown.

run

public void run()
Attempt to connect with the LDAP server

Specified by:
run in interface Runnable


Generated on 10:38:35 AM April 14, 2011, © 1996-2010 Cafésoft LLC. All rights reserved.