o Added a check when a connection is pulled from the pool of connections. If the connection is not anymore valid, we will create a new connection.
o Added a test for the connection pool
git-svn-id: https://svn.apache.org/repos/asf/directory/shared/trunk@1477172 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java
index c6b0fa7..5414dd9 100644
--- a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java
+++ b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionConfig.java
@@ -98,6 +98,7 @@
/** The class used to detect if an attribute is HR or not */
private BinaryAttributeDetector binaryAttributeDetector;
+
/**
* Creates a default LdapConnectionConfig instance
*/
@@ -118,7 +119,7 @@
{
TrustManagerFactory tmFactory = TrustManagerFactory.getInstance( trustMgmtAlgo );
tmFactory.init( ( KeyStore ) null );
-
+
TrustManager factoryTrustManagers[] = tmFactory.getTrustManagers();
for ( int i = 0; i < factoryTrustManagers.length; i++ )
@@ -416,8 +417,8 @@
{
this.enabledCipherSuites = enabledCipherSuites;
}
-
-
+
+
/**
* @return the binaryAttributeDetector
*/
diff --git a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java
index 6916578..bf4c526 100644
--- a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java
+++ b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java
@@ -53,7 +53,7 @@
*/
public LdapConnection getConnection() throws Exception
{
- return ( LdapConnection ) super.borrowObject();
+ return super.borrowObject();
}
diff --git a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java
index d588b60..6713067 100644
--- a/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java
+++ b/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java
@@ -22,6 +22,9 @@
import org.apache.commons.pool.PoolableObjectFactory;
+import org.apache.directory.api.ldap.model.constants.SchemaConstants;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.name.Dn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -81,7 +84,7 @@
LOG.debug( "Creating a LDAP connection" );
LdapNetworkConnection connection = new LdapNetworkConnection( config );
-
+
try
{
connection.bind( config.getName(), config.getCredentials() );
@@ -89,14 +92,14 @@
catch ( Exception e )
{
LOG.warn( "Cannot bind : {}", e.getMessage() );
-
+
// We weren't able to bind : close the connection
connection.close();
-
+
// And re-throw the exception
throw e;
}
-
+
return connection;
}
@@ -117,6 +120,20 @@
{
LOG.debug( "Validating {}", connection );
- return connection.isConnected();
+ if ( connection.isConnected() )
+ {
+ try
+ {
+ return connection.lookup( Dn.ROOT_DSE, SchemaConstants.NO_ATTRIBUTE ) != null;
+ }
+ catch ( LdapException le )
+ {
+ return false;
+ }
+ }
+ else
+ {
+ return false;
+ }
}
}