You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2009/08/23 10:44:43 UTC

svn commit: r806944 - /directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncReplConsumer.java

Author: kayyagari
Date: Sun Aug 23 08:44:42 2009
New Revision: 806944

URL: http://svn.apache.org/viewvc?rev=806944&view=rev
Log:
o updated the code to use the latest client-api classes
o implemented IntermediateResponseListener
o added control decoders
o removed checkLdapResult() method
o merged connect() and bind() methods into bind() method

Modified:
    directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncReplConsumer.java

Modified: directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncReplConsumer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncReplConsumer.java?rev=806944&r1=806943&r2=806944&view=diff
==============================================================================
--- directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncReplConsumer.java (original)
+++ directory/apacheds/trunk/syncrepl/src/main/java/org/apache/directory/server/syncrepl/SyncReplConsumer.java Sun Aug 23 08:44:42 2009
@@ -27,7 +27,6 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 
 import javax.naming.ldap.Control;
@@ -36,40 +35,37 @@
 import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.server.core.entry.DefaultServerEntry;
 import org.apache.directory.server.core.entry.ServerModification;
-import org.apache.directory.server.ldap.replication.SyncreplConfiguration;
 import org.apache.directory.shared.asn1.codec.DecoderException;
 import org.apache.directory.shared.ldap.client.api.LdapConnection;
-import org.apache.directory.shared.ldap.client.api.LdapConnectionImpl;
+import org.apache.directory.shared.ldap.client.api.exception.LdapException;
+import org.apache.directory.shared.ldap.client.api.listeners.IntermediateResponseListener;
 import org.apache.directory.shared.ldap.client.api.listeners.SearchListener;
-import org.apache.directory.shared.ldap.client.api.messages.AbstractControl;
 import org.apache.directory.shared.ldap.client.api.messages.BindResponse;
-//import org.apache.directory.shared.ldap.client.api.messages.Control;
+import org.apache.directory.shared.ldap.client.api.messages.IntermediateResponse;
 import org.apache.directory.shared.ldap.client.api.messages.LdapResult;
 import org.apache.directory.shared.ldap.client.api.messages.SearchRequest;
-import org.apache.directory.shared.ldap.client.api.messages.SearchRequestImpl;
 import org.apache.directory.shared.ldap.client.api.messages.SearchResultDone;
 import org.apache.directory.shared.ldap.client.api.messages.SearchResultEntry;
 import org.apache.directory.shared.ldap.client.api.messages.SearchResultReference;
-import org.apache.directory.shared.ldap.codec.LdapConstants;
 import org.apache.directory.shared.ldap.codec.controls.replication.syncDoneValue.SyncDoneValueControlCodec;
+import org.apache.directory.shared.ldap.codec.controls.replication.syncDoneValue.SyncDoneValueControlDecoder;
 import org.apache.directory.shared.ldap.codec.controls.replication.syncInfoValue.SyncInfoValueControlCodec;
 import org.apache.directory.shared.ldap.codec.controls.replication.syncInfoValue.SyncInfoValueControlDecoder;
 import org.apache.directory.shared.ldap.codec.controls.replication.syncStateValue.SyncStateValueControlCodec;
+import org.apache.directory.shared.ldap.codec.controls.replication.syncStateValue.SyncStateValueControlDecoder;
 import org.apache.directory.shared.ldap.constants.SchemaConstants;
 import org.apache.directory.shared.ldap.entry.Entry;
 import org.apache.directory.shared.ldap.entry.EntryAttribute;
 import org.apache.directory.shared.ldap.entry.Modification;
 import org.apache.directory.shared.ldap.entry.ModificationOperation;
-import org.apache.directory.shared.ldap.filter.ExprNode;
-import org.apache.directory.shared.ldap.filter.FilterParser;
 import org.apache.directory.shared.ldap.filter.SearchScope;
 import org.apache.directory.shared.ldap.message.AliasDerefMode;
 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
 import org.apache.directory.shared.ldap.message.control.replication.SyncDoneValueControl;
 import org.apache.directory.shared.ldap.message.control.replication.SyncRequestValueControl;
 import org.apache.directory.shared.ldap.message.control.replication.SyncStateTypeEnum;
+import org.apache.directory.shared.ldap.message.control.replication.SyncStateValueControl;
 import org.apache.directory.shared.ldap.message.control.replication.SynchronizationModeEnum;
-import org.apache.directory.shared.ldap.name.LdapDN;
 import org.apache.directory.shared.ldap.util.StringTools;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -84,7 +80,7 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class SyncreplConsumer implements SearchListener
+public class SyncReplConsumer implements SearchListener, IntermediateResponseListener
 {
 
     /** the syncrepl configuration */
@@ -94,7 +90,7 @@
     private byte[] syncCookie;
 
     /** the logger */
-    private static final Logger LOG = LoggerFactory.getLogger( SyncreplConsumer.class );
+    private static final Logger LOG = LoggerFactory.getLogger( SyncReplConsumer.class );
 
     /** conection to the syncrepl provider */
     private LdapConnection connection;
@@ -105,7 +101,7 @@
     /** the syncrequest control */
     private SyncRequestValueControl syncReq;
 
-    /** a reference to the directoryservice */
+    /** a reference to the directoryService */
     private DirectoryService directoryService;
 
     /** the decoder for syncinfovalue control */
@@ -120,6 +116,9 @@
     /** the core session */
     private CoreSession session;
     
+    private SyncDoneValueControlDecoder syncDoneControlDecoder = new SyncDoneValueControlDecoder();
+    
+    private SyncStateValueControlDecoder syncStateControlDecoder = new SyncStateValueControlDecoder();
     /**
      * @return the config
      */
@@ -148,19 +147,6 @@
     }
 
 
-    /**
-     * A helper method to check that we didn't get an error.
-     */
-    private static void checkldapResult( LdapConnection connection, LdapResult ldapResult ) throws IOException
-    {
-        if ( ldapResult.getResultCode() != ResultCodeEnum.SUCCESS )
-        {
-            LOG.debug( "failed to bind on the server : " + ldapResult );
-            quit( connection );
-        }
-    }
-
-
     public void init( DirectoryService directoryservice ) throws Exception
     {
         this.directoryService = directoryservice;
@@ -174,43 +160,19 @@
     }
 
 
-    public boolean connect()
+    public boolean bind()
     {
-        String providerHost = config.getProviderHost();
-        int port = config.getPort();
-
-        // Create a connection
-        connection = new LdapConnection( providerHost, port );
-
         try
         {
-            // Connect to the server
-            boolean connected = connection.connect();
+            String providerHost = config.getProviderHost();
+            int port = config.getPort();
 
-            if ( !connected )
+            // Create a connection
+            if( connection == null )
             {
-                LOG.error( "Failed to connect to the syncrepl provder host {} running at port {}", providerHost, port );
-                return false;
+                connection = new LdapConnection( providerHost, port );
             }
-            else
-            {
-                return connected;
-            }
-        }
-        catch ( Exception e )
-        {
-            LOG.error( "Failed to connect to the syncrepl provder host {} running at port {}", providerHost, port );
-            LOG.error( e.getMessage(), e );
-        }
-
-        return false;
-    }
 
-
-    public boolean bind()
-    {
-        try
-        {
             // Do a bind
             BindResponse bindResponse = connection.bind( config.getBindDn(), config.getCredentials() );
 
@@ -251,7 +213,7 @@
     {
         String baseDn = config.getBaseDn();
 
-        searchRequest = new SearchRequestImpl();
+        searchRequest = new SearchRequest();
         
         searchRequest.setBaseDn( baseDn );
         searchRequest.setFilter( config.getFilter() );
@@ -292,17 +254,32 @@
         Control control = new SyncRequestValueControl();
         ((SyncRequestValueControl)control).setCookie( syncReq.getEncodedValue() );
 
-        searchRequest.add( control );
+        try
+        {
+            searchRequest.add( control );
+        }
+        catch( LdapException e )
+        {
+            // shouldn't happen
+            LOG.error( "Failed to add constrol to the search request", e );
+        }
     }
 
 
     public void handleSearchDone( SearchResultDone searchDone )
     {
         LOG.debug( "///////////////// handleSearchDone //////////////////" );
-        Map<String,Control> controls = searchDone.getControls();
         
-        Control ctrl = controls.get( SyncDoneValueControl.CONTROL_OID );
-        SyncDoneValueControlCodec syncDoneCtrl = ( SyncDoneValueControlCodec ) ctrl.getEncodedValue();
+        Control ctrl = searchDone.getControl( SyncDoneValueControl.CONTROL_OID );
+        SyncDoneValueControlCodec syncDoneCtrl = null;
+        try
+        {
+            syncDoneCtrl = ( SyncDoneValueControlCodec ) syncDoneControlDecoder.decode( ctrl.getEncodedValue() );
+        }
+        catch( Exception e )
+        {
+            LOG.error( "Failed to decode the syncDoneControlCodec", e );
+        }
 
         if ( syncDoneCtrl.getCookie() != null )
         {
@@ -359,8 +336,17 @@
         {
             Entry remoteEntry = syncResult.getEntry();
 
-            Control ctrl = syncResult.getCurrentControl();
-            SyncStateValueControlCodec syncStateCtrl = ( SyncStateValueControlCodec ) ctrl.getControlValue();
+            Control ctrl = syncResult.getControl( SyncStateValueControl.CONTROL_OID );
+            SyncStateValueControlCodec syncStateCtrl = null;
+            
+            try
+            {
+                syncStateCtrl = ( SyncStateValueControlCodec ) syncStateControlDecoder.decode( ctrl.getEncodedValue() );
+            }
+            catch( Exception e )
+            {
+                LOG.error( "Failed to decode syncStateControl", e );
+            }
 
             if ( syncStateCtrl.getCookie() != null )
             {
@@ -372,7 +358,9 @@
             SyncStateTypeEnum state = syncStateCtrl.getSyncStateType();
 
             LOG.debug( "state name {}", state.name() );
-            LOG.debug( "entryUUID = {}", UUID.nameUUIDFromBytes( syncStateCtrl.getEntryUUID() ) );
+
+            EntryAttribute entryUUID = remoteEntry.get( "entryUUID" );
+            LOG.debug( "entryUUID = {}", entryUUID.getString() );
 
             switch ( state )
             {
@@ -484,10 +472,9 @@
             }
 
             LOG.debug( "Trying to reconnect" );
-            connected = connect();
+            connected = bind();
         }
         
-        bind();
         startSync();
     }
 
@@ -549,14 +536,55 @@
     }
 
     
+    //====================== SearchListener methods ====================================
+    
+    
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.client.api.listeners.SearchListener#entryFound(org.apache.directory.shared.ldap.client.api.LdapConnection, org.apache.directory.shared.ldap.client.api.messages.SearchResultEntry)
+     */
+    public void entryFound( LdapConnection connection, SearchResultEntry searchResultEntry ) throws LdapException
+    {
+        handleSearchResult( searchResultEntry );
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.client.api.listeners.SearchListener#referralFound(org.apache.directory.shared.ldap.client.api.LdapConnection, org.apache.directory.shared.ldap.client.api.messages.SearchResultReference)
+     */
+    public void referralFound( LdapConnection connection, SearchResultReference searchResultReference )
+        throws LdapException
+    {
+        handleSearchReference( searchResultReference );
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.client.api.listeners.SearchListener#searchDone(org.apache.directory.shared.ldap.client.api.LdapConnection, org.apache.directory.shared.ldap.client.api.messages.SearchResultDone)
+     */
+    public void searchDone( LdapConnection connection, SearchResultDone searchResultDone ) throws LdapException
+    {
+        handleSearchDone( searchResultDone );
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.client.api.listeners.IntermediateResponseListener#responseReceived(org.apache.directory.shared.ldap.client.api.LdapConnection, org.apache.directory.shared.ldap.client.api.messages.IntermediateResponse)
+     */
+    public void responseReceived( LdapConnection connection, IntermediateResponse intermediateResponse )
+    {
+        handleSyncInfo( intermediateResponse.getResponseValue() );
+    }
+
+
     /**
-     * 
-     * performs a search on connection with updated syncrequest control.
+     * performs a search on connection with updated syncRequest control.
      *
      * @throws Exception in case of any problems encountered while searching
      */
     private void doSyncSearch() throws Exception
     {
+        SyncRequestValueControl syncReq = new SyncRequestValueControl();
+        
         if( config.isRefreshPersist() )
         {
             syncReq.setMode( SynchronizationModeEnum.REFRESH_AND_PERSIST );
@@ -567,9 +595,14 @@
             syncReq.setCookie( syncCookie );
         }
         
-        searchRequest.getCurrentControl().setControlValue( syncReq.getEncodedValue() );
+        SyncRequestValueControl syncReqControl = ( SyncRequestValueControl ) searchRequest.getControl( SyncRequestValueControl.CONTROL_OID );
+        
+        searchRequest.remove( syncReqControl );
 
-        connection.search( searchRequest );
+        searchRequest.add( syncReq );
+        
+        // Do the search
+        connection.search( searchRequest, this );
     }
 
     
@@ -585,6 +618,7 @@
             connection.close();
             LOG.info( "Connection closed for the server {}", config.getProviderHost() );
 
+            connection = null;
             // persist the cookie
             storeCookie();
         }
@@ -629,12 +663,13 @@
     {
         try
         {
-            if( cookieFile.exists() && cookieFile.length() > 0 )
+            if( cookieFile.exists() && ( cookieFile.length() > 0 ) )
             {
                 FileInputStream fin = new FileInputStream( cookieFile );
                 syncCookie = new byte[ fin.read() ];
                 fin.read( syncCookie );
                 fin.close();
+                
                 LOG.debug( "read the cookie from file: " + StringTools.utf8ToString( syncCookie ) );
             }
         }