You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2015/01/08 08:42:48 UTC
svn commit: r1650215 - in
/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api:
DefaultLdapConnectionFactory.java LdapConnectionConfig.java
LdapNetworkConnection.java
Author: elecharny
Date: Thu Jan 8 07:42:47 2015
New Revision: 1650215
URL: http://svn.apache.org/r1650215
Log:
o Exposed the timeout in the LdapConnectionConfig
o Changed the isConnected() check : if the connection is in a closing() state, we don't return true
o Added a retry in the connect() method, when the number of available socket is running out, giving the method a chance to cope with this socket starvation, if it's purely temporary
o Dispose the Connector when done with the connection
Modified:
directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java
directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java
directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java
Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java?rev=1650215&r1=1650214&r2=1650215&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java Thu Jan 8 07:42:47 2015
@@ -52,7 +52,7 @@ public class DefaultLdapConnectionFactor
public DefaultLdapConnectionFactory( LdapConnectionConfig config )
{
this.connectionConfig = config;
- this.timeout = config.getDefaultTimeout();
+ this.timeout = config.getTimeout();
}
Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java?rev=1650215&r1=1650214&r2=1650215&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java Thu Jan 8 07:42:47 2015
@@ -69,6 +69,9 @@ public class LdapConnectionConfig
/** A flag indicating if we are using SSL or not, default value is false */
private boolean useSsl = false;
+ /** The session timeout */
+ private long timeout = DEFAULT_TIMEOUT;
+
/** A flag indicating if we are using TLS or not, default value is false */
private boolean useTls = false;
@@ -101,7 +104,7 @@ public class LdapConnectionConfig
/** The class used to detect if an attribute is HR or not */
private BinaryAttributeDetector binaryAttributeDetector;
-
+
/** The Service to use internally when creating connections */
private LdapApiService ldapApiService;
@@ -305,6 +308,28 @@ public class LdapConnectionConfig
}
+ /**
+ * Gets the timeout.
+ *
+ * @return the timeout
+ */
+ public long getTimeout()
+ {
+ return timeout;
+ }
+
+
+ /**
+ * Sets the timeout.
+ *
+ * @return the timeout
+ */
+ public void setTimeout( long timeout )
+ {
+ this.timeout = timeout;
+ }
+
+
/**
* Gets the supported LDAP version.
*
Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java?rev=1650215&r1=1650214&r2=1650215&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java Thu Jan 8 07:42:47 2015
@@ -161,6 +161,7 @@ import org.apache.mina.core.session.IoSe
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.ProtocolEncoderException;
import org.apache.mina.filter.ssl.SslFilter;
+import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -241,7 +242,7 @@ public class LdapNetworkConnection exten
*/
public boolean isConnected()
{
- return ( ldapSession != null ) && connected.get();
+ return ( ldapSession != null ) && connected.get() && !ldapSession.isClosing();
}
@@ -501,6 +502,30 @@ public class LdapNetworkConnection exten
}
+ /**
+ * Create the connector
+ */
+ private void createConnector() throws LdapException
+ {
+ // Use only one thread inside the connector
+ connector = new NioSocketConnector( 1 );
+
+ ( ( SocketSessionConfig ) connector.getSessionConfig() ).setReuseAddress( true );
+
+ // Add the codec to the chain
+ connector.getFilterChain().addLast( "ldapCodec", ldapProtocolFilter );
+
+ // If we use SSL, we have to add the SslFilter to the chain
+ if ( config.isUseSsl() )
+ {
+ addSslFilter();
+ }
+
+ // Inject the protocolHandler
+ connector.setHandler( this );
+ }
+
+
//-------------------------- The methods ---------------------------//
/**
* {@inheritDoc}
@@ -516,39 +541,73 @@ public class LdapNetworkConnection exten
// Create the connector if needed
if ( connector == null )
{
- // Use only one thead inside the connector
- connector = new NioSocketConnector( 1 );
-
- // Add the codec to the chain
- connector.getFilterChain().addLast( "ldapCodec", ldapProtocolFilter );
-
- // If we use SSL, we have to add the SslFilter to the chain
- if ( config.isUseSsl() )
- {
- addSslFilter();
- }
-
- // Inject the protocolHandler
- connector.setHandler( this );
+ createConnector();
}
// Build the connection address
SocketAddress address = new InetSocketAddress( config.getLdapHost(), config.getLdapPort() );
// And create the connection future
- ConnectFuture connectionFuture = connector.connect( address );
+ long maxRetry = System.currentTimeMillis() + timeout;
+ ConnectFuture connectionFuture = null;
- // Wait until it's established
- try
+ while ( maxRetry > System.currentTimeMillis() )
{
- connectionFuture.await( timeout );
+ connectionFuture = connector.connect( address );
+
+ boolean result = false;
+ timeout = config.getTimeout();
+
+ // Wait until it's established
+ try
+ {
+ result = connectionFuture.await( timeout );
+ }
+ catch ( InterruptedException e )
+ {
+ connector.dispose();
+ connector = null;
+ LOG.debug( "Interrupted while waiting for connection to establish with server {}:{}",
+ config.getLdapHost(),
+ config.getLdapPort(), e );
+ throw new LdapOtherException( e.getMessage(), e );
+ }
+ finally
+ {
+ if ( result )
+ {
+ boolean isConnected = connectionFuture.isConnected();
+
+ if ( !isConnected )
+ {
+ LOG.debug( "------>> Cannot get the connection... Retrying" );
+
+ // Wait 500 ms and retry
+ try
+ {
+ Thread.sleep( 500 );
+ }
+ 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 );
+ }
+ }
+ else
+ {
+ break;
+ }
+ }
+ }
}
- catch ( InterruptedException e )
+
+ if ( connectionFuture == null )
{
- connector = null;
- LOG.debug( "Interrupted while waiting for connection to establish with server {}:{}", config.getLdapHost(),
- config.getLdapPort(), e );
- throw new LdapOtherException( e.getMessage(), e );
+ connector.dispose();
+ throw new InvalidConnectionException( "Cannot connect" );
}
boolean isConnected = connectionFuture.isConnected();
@@ -1761,18 +1820,6 @@ public class LdapNetworkConnection exten
connected.set( false );
- /*
- if ( ldapSession != null )
- {
- CloseFuture closeFuture = ldapSession.close( true );
-
- LOG.debug( "waiting for closeFuture" );
- closeFuture.awaitUninterruptibly();
- LOG.debug( "closeFuture done" );
- connected.set( false );
- }
- */
-
// Last, not least, reset the MessageId value
messageId.set( 0 );
@@ -1799,7 +1846,8 @@ public class LdapNetworkConnection exten
{
if ( timeout <= 0 )
{
- this.timeout = Long.MAX_VALUE;
+ // Set a date in the far future : 100 years
+ this.timeout = 1000L * 60L * 60L * 24L * 365L * 100L;
}
else
{