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 2013/04/29 18:12:42 UTC

svn commit: r1477172 - in /directory: apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/ shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/

Author: elecharny
Date: Mon Apr 29 16:12:42 2013
New Revision: 1477172

URL: http://svn.apache.org/r1477172
Log:
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

Added:
    directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/LdapConnectionPoolTest.java
Modified:
    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/LdapConnectionPool.java
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java

Added: directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/LdapConnectionPoolTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/LdapConnectionPoolTest.java?rev=1477172&view=auto
==============================================================================
--- directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/LdapConnectionPoolTest.java (added)
+++ directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/LdapConnectionPoolTest.java Mon Apr 29 16:12:42 2013
@@ -0,0 +1,152 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+package org.apache.directory.shared.client.api;
+
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.directory.api.ldap.model.name.Dn;
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.apache.directory.ldap.client.api.LdapConnectionConfig;
+import org.apache.directory.ldap.client.api.LdapConnectionPool;
+import org.apache.directory.ldap.client.api.PoolableLdapConnectionFactory;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.constants.ServerDNConstants;
+import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
+import org.apache.directory.server.core.integ.FrameworkRunner;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+/**
+ * A test class for the connection pool.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(FrameworkRunner.class)
+@CreateLdapServer(transports =
+    { @CreateTransport(protocol = "LDAP") })
+public class LdapConnectionPoolTest extends AbstractLdapTestUnit
+{
+    /** The connection pool */
+    private LdapConnectionPool pool;
+
+    /** The Constant DEFAULT_HOST. */
+    private static final String DEFAULT_HOST = "localhost";
+
+    /** The Constant DEFAULT_ADMIN. */
+    private static final String DEFAULT_ADMIN = ServerDNConstants.ADMIN_SYSTEM_DN;
+
+    /** The Constant DEFAULT_PASSWORD. */
+    private static final String DEFAULT_PASSWORD = "secret";
+
+    /**
+     * A thread used to test the connection
+     */
+    private class ConnectionThread extends Thread
+    {
+        int threadNumber;
+        CountDownLatch counter;
+
+
+        public ConnectionThread( int threadNumber, CountDownLatch counter )
+        {
+            this.threadNumber = threadNumber;
+            this.counter = counter;
+        }
+
+
+        @Override
+        public void run()
+        {
+            try
+            {
+                for ( int i = 0; i < 100; i++ )
+                {
+                    LdapConnection connection = pool.getConnection();
+
+                    connection.lookup( Dn.ROOT_DSE, "1.1 " );
+
+                    pool.releaseConnection( connection );
+
+                    counter.countDown();
+                }
+            }
+            catch ( Exception e )
+            {
+                // Do nothing
+            }
+        }
+    }
+
+
+    @Before
+    public void setUp() throws Exception
+    {
+        int port = getLdapServer().getPort();
+
+        LdapConnectionConfig config = new LdapConnectionConfig();
+        config.setLdapHost( "localHost" );
+        config.setLdapPort( port );
+        config.setName( DEFAULT_ADMIN );
+        config.setCredentials( DEFAULT_PASSWORD );
+        PoolableLdapConnectionFactory factory = new PoolableLdapConnectionFactory( config );
+        pool = new LdapConnectionPool( factory );
+        pool.setTestOnBorrow( true );
+        pool.setWhenExhaustedAction( GenericObjectPool.WHEN_EXHAUSTED_GROW );
+    }
+
+
+    @After
+    public void tearDown() throws Exception
+    {
+        pool.close();
+    }
+
+
+    /**
+     * Test the creation of many connections
+     */
+    @Test
+    public void testManyConnections() throws Exception
+    {
+        CountDownLatch counter = new CountDownLatch( 10000 );
+
+        long t0 = System.currentTimeMillis();
+
+        for ( int i = 0; i < 100; i++ )
+        {
+            ConnectionThread thread = new ConnectionThread( i, counter );
+
+            thread.start();
+        }
+
+        boolean result = counter.await( 100, TimeUnit.SECONDS );
+
+        long t1 = System.currentTimeMillis();
+
+        System.out.println( "Time to create and use 10 000 connections = " + ( t1 - t0 ) );
+    }
+}

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=1477172&r1=1477171&r2=1477172&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 Mon Apr 29 16:12:42 2013
@@ -98,6 +98,7 @@ public class LdapConnectionConfig
     /** The class used to detect if an attribute is HR or not */
     private BinaryAttributeDetector binaryAttributeDetector;
 
+
     /**
      * Creates a default LdapConnectionConfig instance
      */
@@ -118,7 +119,7 @@ public class LdapConnectionConfig
         {
             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 @@ public class LdapConnectionConfig
     {
         this.enabledCipherSuites = enabledCipherSuites;
     }
-    
-    
+
+
     /**
      * @return the binaryAttributeDetector
      */

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java?rev=1477172&r1=1477171&r2=1477172&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java Mon Apr 29 16:12:42 2013
@@ -53,7 +53,7 @@ public class LdapConnectionPool extends 
      */
     public LdapConnection getConnection() throws Exception
     {
-        return ( LdapConnection ) super.borrowObject();
+        return super.borrowObject();
     }
 
 

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java?rev=1477172&r1=1477171&r2=1477172&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java Mon Apr 29 16:12:42 2013
@@ -22,6 +22,9 @@ package org.apache.directory.ldap.client
 
 
 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 @@ public class PoolableLdapConnectionFacto
         LOG.debug( "Creating a LDAP connection" );
 
         LdapNetworkConnection connection = new LdapNetworkConnection( config );
-        
+
         try
         {
             connection.bind( config.getName(), config.getCredentials() );
@@ -89,14 +92,14 @@ public class PoolableLdapConnectionFacto
         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 @@ public class PoolableLdapConnectionFacto
     {
         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;
+        }
     }
 }