directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r898445 - in /directory: apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ shared/trunk/ldap-client-api/src/main/java/org/apache/directory/shared/ldap/client/api/
Date Tue, 12 Jan 2010 18:05:32 GMT
Author: elecharny
Date: Tue Jan 12 18:05:32 2010
New Revision: 898445

URL: http://svn.apache.org/viewvc?rev=898445&view=rev
Log:
Fixed the BindRequest client API so that the async bind returns a Future and not a BindResponse

Modified:
    directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java
    directory/shared/trunk/ldap-client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java

Modified: directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java?rev=898445&r1=898444&r2=898445&view=diff
==============================================================================
--- directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java
(original)
+++ directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientBindRequestTest.java
Tue Jan 12 18:05:32 2010
@@ -79,6 +79,8 @@
             BindResponse bindResponse = connection.bind( "uid=admin,ou=system", "secret"
);
             
             assertNotNull( bindResponse );
+            assertNotNull( bindResponse.getLdapResult() );
+            assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode()
);
             
             connection.unBind();
         }
@@ -114,7 +116,6 @@
 
         try
         {
-            long t0 = System.currentTimeMillis();
             lock.acquire();
             
             for ( ; i < nbLoop; i++)
@@ -125,14 +126,12 @@
                 bindRequest.setName( "uid=admin,ou=system" );
                 bindRequest.setCredentials( "secret" );
                 
-    
                 connection.bind( bindRequest, new BindListener()
                 {
                     public void bindCompleted( LdapConnection connection, BindResponse bindResponse
) throws LdapException
                     {
                         assertNotNull( bindResponse );
                         assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode()
);
-                        //System.out.println( "Bound" );
                         lock.release();
                     }
                 } );
@@ -140,15 +139,7 @@
                 lock.acquire();
                 lock.release();
                 connection.unBind();
-                
-                if ( i % 100 == 0 )
-                {
-                    System.out.println( i );
-                }
             }
-            
-            long t1 = System.currentTimeMillis();
-            System.out.println( (t1 - t0) );
         }
         catch ( LdapException le )
         {

Modified: directory/shared/trunk/ldap-client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap-client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java?rev=898445&r1=898444&r2=898445&view=diff
==============================================================================
--- directory/shared/trunk/ldap-client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java
(original)
+++ directory/shared/trunk/ldap-client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java
Tue Jan 12 18:05:32 2010
@@ -143,8 +143,8 @@
 import org.slf4j.LoggerFactory;
 
 /**
- * 
- *  Describe the methods to be implemented by the LdapConnection class.
+ * This class is the base for every operations sent or received to and
+ * from a LDAP server.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
@@ -991,7 +991,7 @@
         bindRequest.setName( name );
         bindRequest.setCredentials( credentials );
         
-        BindResponse response = bind( bindRequest, null );
+        BindResponse response = bind( bindRequest );
 
         if ( LOG.isDebugEnabled() )
         {
@@ -1018,7 +1018,49 @@
      */
     public BindResponse bind( BindRequest bindRequest ) throws LdapException
     {
-        return bind( bindRequest, null );
+        ResponseFuture responseFuture = bind( bindRequest, null );
+        
+        // Get the result from the future
+        try
+        {
+            // Read the response, waiting for it if not available immediately
+            long timeout = getTimeout( bindRequest.getTimeout() );
+            
+            // Get the response, blocking
+            BindResponse bindResponse = (BindResponse)responseFuture.get( timeout, TimeUnit.MILLISECONDS
);
+
+            // Everything is fine, return the response
+            LOG.debug( "Bind successful : {}", bindResponse );
+            
+            return bindResponse;
+        }
+        catch ( TimeoutException te )
+        {
+            // Send an abandon request
+            if( !responseFuture.isCancelled() )
+            {
+                abandon( bindRequest.getMessageId() );
+            }
+            
+            // We didn't received anything : this is an error
+            LOG.error( "Bind failed : timeout occured" );
+            throw new LdapException( TIME_OUT_ERROR );
+        }
+        catch ( Exception ie )
+        {
+            // Catch all other exceptions
+            LOG.error( NO_RESPONSE_ERROR, ie );
+            LdapException ldapException = new LdapException( NO_RESPONSE_ERROR );
+            ldapException.initCause( ie );
+            
+            // Send an abandon request
+            if( !responseFuture.isCancelled() )
+            {
+                abandon( bindRequest.getMessageId() );
+            }
+            
+            throw ldapException;
+        }
     }
         
 
@@ -1027,8 +1069,9 @@
      *
      * @param bindRequest The BindRequest to send
      * @param listener The listener 
+     * @return ResponseFuture A future
      */
-    public BindResponse bind( BindRequest bindRequest, BindListener bindListener ) throws
LdapException 
+    public ResponseFuture bind( BindRequest bindRequest, BindListener bindListener ) throws
LdapException 
     {
         // First try to connect, if we aren't already connected.
         connect();
@@ -1041,12 +1084,16 @@
 
         // Creates the messageID and stores it into the 
         // initial message and the transmitted message.
+        // As it's a Bind request, qe reset the MessageId 
+        // value to zero.
+        messageId.set( 0 );
         int newId = messageId.incrementAndGet();
         bindRequest.setMessageId( newId );
         bindMessage.setMessageId( newId );
         
         if( bindListener != null )
         {
+            // This is an asynchronous bind
             listenerMap.put( newId, bindListener );
         }
         
@@ -1101,62 +1148,21 @@
         LOG.debug( "-----------------------------------------------------------------" );
         LOG.debug( "Sending request \n{}", bindMessage );
 
+        // Create a future for this Bind opeation
         ResponseFuture responseFuture = new ResponseFuture( bindResponseQueue );
         futureMap.put( newId, responseFuture );
 
         // Send the request to the server
         ldapSession.write( bindMessage );
         
-        if ( bindListener == null )
-        {
-            // And get the result
-            try
-            {
-                // Read the response, waiting for it if not available immediately
-                long timeout = getTimeout( bindRequest.getTimeout() );
-                
-                // Get the response, blocking
-                BindResponse bindResponse = (BindResponse)responseFuture.get( timeout, TimeUnit.MILLISECONDS
);
-    
-                // Everything is fine, return the response
-                LOG.debug( "Bind successful : {}", bindResponse );
-                
-                return bindResponse;
-            }
-            catch ( TimeoutException te )
-            {
-                // Send an abandon request
-                if( !responseFuture.isCancelled() )
-                {
-                    abandon( bindRequest.getMessageId() );
-                }
-                
-                // We didn't received anything : this is an error
-                LOG.error( "Bind failed : timeout occured" );
-                throw new LdapException( TIME_OUT_ERROR );
-            }
-            catch ( Exception ie )
-            {
-                // Catch all other exceptions
-                LOG.error( NO_RESPONSE_ERROR, ie );
-                LdapException ldapException = new LdapException( NO_RESPONSE_ERROR );
-                ldapException.initCause( ie );
-                
-                // Send an abandon request
-                if( !responseFuture.isCancelled() )
-                {
-                    abandon( bindRequest.getMessageId() );
-                }
-                
-                throw ldapException;
-            }
-        }
-        else
+        if ( bindListener != null )
         {
-            // Return null.
+            // If this is an asynchronous operation, associate the ID
+            // with the operation listener
             listenerMap.put( newId, bindListener );
-            return null;
         }
+        
+        return responseFuture;
     }
     
     



Mime
View raw message