From 1ad53161c35dfbd2c08b1f124d237f5f3cad9110 Mon Sep 17 00:00:00 2001
From: jdemendi <jdemendi@localhost>
Date: Thu, 19 Jul 2007 07:46:13 +0000
Subject: [PATCH] This set of changes is aiming at having a more consistent and clean code when an operation is canceled.

---
 opends/src/server/org/opends/server/core/ModifyDNOperationBasis.java |  222 ++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 136 insertions(+), 86 deletions(-)

diff --git a/opends/src/server/org/opends/server/core/ModifyDNOperationBasis.java b/opends/src/server/org/opends/server/core/ModifyDNOperationBasis.java
index 0dedeba..ca64821 100644
--- a/opends/src/server/org/opends/server/core/ModifyDNOperationBasis.java
+++ b/opends/src/server/org/opends/server/core/ModifyDNOperationBasis.java
@@ -52,10 +52,13 @@
 import org.opends.server.types.operation.PreParseModifyDNOperation;
 import static org.opends.server.core.CoreConstants.*;
 import static org.opends.server.loggers.AccessLogger.*;
+
 import org.opends.server.types.DebugLogLevel;
 import org.opends.server.workflowelement.localbackend.*;
+
 import static org.opends.server.loggers.ErrorLogger.*;
 import static org.opends.server.loggers.debug.DebugLogger.*;
+
 import org.opends.server.loggers.debug.DebugLogger;
 import org.opends.server.loggers.debug.DebugTracer;
 import static org.opends.server.messages.CoreMessages.*;
@@ -610,7 +613,6 @@
   public final void run()
   {
     setResultCode(ResultCode.UNDEFINED);
-    boolean workflowExecuted = false;
 
     // Get the plugin config manager that will be used for invoking plugins.
     PluginConfigManager pluginConfigManager =
@@ -627,9 +629,14 @@
       return;
     }
 
+
+    // This flag is set to true as soon as a workflow has been executed.
+    boolean workflowExecuted = false;
+
+
     // Create a labeled block of code that we can break out of if a problem is
     // detected.
-    modifyDNProcessing:
+modifyDNProcessing:
     {
       // Invoke the pre-parse modify DN plugins.
       PreParsePluginResult preParseResult =
@@ -665,14 +672,10 @@
       logModifyDNRequest(this);
 
 
-      // Check for and handle a request to cancel this operation.
+      // Check for a request to cancel this operation.
       if (cancelRequest != null)
       {
-        indicateCancelled(cancelRequest);
-        setProcessingStopTime();
-        logModifyDNResponse(this);
-        pluginConfigManager.invokePostResponseModifyDNPlugins(this);
-        return;
+        break modifyDNProcessing;
       }
 
       // Process the entry DN, newRDN, and newSuperior elements from their raw
@@ -699,89 +702,160 @@
       workflowExecuted = true;
     }
 
-    // Check for and handle a request to cancel this operation.
-    if ((getCancelRequest() != null) ||
-        (getCancelResult() == CancelResult.CANCELED))
+    // Check for a terminated connection.
+    if (getCancelResult() == CancelResult.CANCELED)
     {
-      if (getCancelRequest() != null){
-        indicateCancelled(getCancelRequest());
-      }
+      // Stop the processing timer.
       setProcessingStopTime();
+
+      // Log the add response message.
       logModifyDNResponse(this);
-      invokePostResponsePlugins();
+
       return;
     }
 
+    // Check for and handle a request to cancel this operation.
+    if (cancelRequest != null)
+    {
+      indicateCancelled(cancelRequest);
+
+      // Stop the processing timer.
+      setProcessingStopTime();
+
+      // Log the modify DN response message.
+      logModifyDNResponse(this);
+
+      // Invoke the post-response modify DN plugins.
+      invokePostResponsePlugins(workflowExecuted);
+
+      return;
+    }
+
+    // Indicate that it is now too late to attempt to cancel the operation.
+    setCancelResult(CancelResult.TOO_LATE);
+
     // Stop the processing timer.
     setProcessingStopTime();
 
     // Send the modify DN response to the client.
     clientConnection.sendResponse(this);
 
-
     // Log the modify DN response.
     logModifyDNResponse(this);
 
-    // If a workflow has been executed to process the operation, then
-    // call the post response plugins against the operations attached
-    // to this operation.
+    // Notifies any persistent searches that might be registered with the
+    // server.
+    notifyPersistentSearches(workflowExecuted);
+
+    // Invoke the post-response modify DN plugins.
+    invokePostResponsePlugins(workflowExecuted);
+  }
+
+
+  /**
+   * Invokes the post response plugins. If a workflow has been executed
+   * then invoke the post response plugins provided by the workflow
+   * elements of the worklfow, otherwise invoke the post reponse plugins
+   * that have been registered with the current operation.
+   *
+   * @param workflowExecuted <code>true</code> if a workflow has been
+   *                         executed
+   */
+  private void invokePostResponsePlugins(boolean workflowExecuted)
+  {
+    // Get the plugin config manager that will be used for invoking plugins.
+    PluginConfigManager pluginConfigManager =
+      DirectoryServer.getPluginConfigManager();
+
+    // Invoke the post response plugins
     if (workflowExecuted)
     {
-      // Check wether there are local operations in attachments
+      // Invoke the post response plugins that have been registered by
+      // the workflow elements
       List localOperations =
         (List)getAttachment(Operation.LOCALBACKENDOPERATIONS);
-      if (localOperations != null && (! localOperations.isEmpty())){
-        for (Object localOperation : localOperations)
+
+      if (localOperations != null)
+      {
+        for (Object localOp : localOperations)
         {
-          LocalBackendModifyDNOperation localOp =
-            (LocalBackendModifyDNOperation)localOperation;
-          // Notify any persistent searches that might be registered with
-          // the server.
-          if (getResultCode() == ResultCode.SUCCESS)
-          {
-            for (PersistentSearch persistentSearch :
-              DirectoryServer.getPersistentSearches())
-            {
-              try
-              {
-                persistentSearch.processModifyDN(
-                    localOp,
-                    localOp.getOriginalEntry(),
-                    localOp.getUpdatedEntry());
-              }
-              catch (Exception e)
-              {
-                if (debugEnabled())
-                {
-                  TRACER.debugCaught(DebugLogLevel.ERROR, e);
-                }
-
-                int    msgID   = MSGID_MODDN_ERROR_NOTIFYING_PERSISTENT_SEARCH;
-                String message = getMessage(msgID,
-                    String.valueOf(persistentSearch),
-                    getExceptionMessage(e));
-                logError(ErrorLogCategory.CORE_SERVER,
-                    ErrorLogSeverity.SEVERE_ERROR,
-                    message, msgID);
-
-                DirectoryServer.deregisterPersistentSearch(persistentSearch);
-              }
-            }
-          }
-
-          // Invoke the post-response modify DN plugins.
-          pluginConfigManager.invokePostResponseModifyDNPlugins(localOp);
+          LocalBackendModifyDNOperation localOperation =
+            (LocalBackendModifyDNOperation)localOp;
+          pluginConfigManager.invokePostResponseModifyDNPlugins(localOperation);
         }
       }
     }
-    else {
-      // Invoke the post-response modify DN plugins.
+    else
+    {
+      // Invoke the post response plugins that have been registered with
+      // the current operation
       pluginConfigManager.invokePostResponseModifyDNPlugins(this);
     }
   }
 
 
   /**
+   * Notifies any persistent searches that might be registered with the server.
+   * If no workflow has been executed then don't notify persistent searches.
+   *
+   * @param workflowExecuted <code>true</code> if a workflow has been
+   *                         executed
+   */
+  private void notifyPersistentSearches(boolean workflowExecuted)
+  {
+    if (! workflowExecuted)
+    {
+      return;
+    }
+
+    List localOperations =
+      (List)getAttachment(Operation.LOCALBACKENDOPERATIONS);
+
+    if (localOperations != null)
+    {
+      for (Object localOperation : localOperations)
+      {
+        LocalBackendModifyDNOperation localOp =
+          (LocalBackendModifyDNOperation)localOperation;
+        // Notify any persistent searches that might be registered with
+        // the server.
+        if (getResultCode() == ResultCode.SUCCESS)
+        {
+          for (PersistentSearch persistentSearch :
+            DirectoryServer.getPersistentSearches())
+          {
+            try
+            {
+              persistentSearch.processModifyDN(
+                  localOp,
+                  localOp.getOriginalEntry(),
+                  localOp.getUpdatedEntry());
+            }
+            catch (Exception e)
+            {
+              if (debugEnabled())
+              {
+                TRACER.debugCaught(DebugLogLevel.ERROR, e);
+              }
+
+              int    msgID   = MSGID_MODDN_ERROR_NOTIFYING_PERSISTENT_SEARCH;
+              String message = getMessage(msgID,
+                  String.valueOf(persistentSearch),
+                  getExceptionMessage(e));
+              logError(ErrorLogCategory.CORE_SERVER,
+                  ErrorLogSeverity.SEVERE_ERROR,
+                  message, msgID);
+
+              DirectoryServer.deregisterPersistentSearch(persistentSearch);
+            }
+          }
+        }
+      }
+    }
+  }
+
+
+  /**
    * Updates the error message and the result code of the operation.
    *
    * This method is called because no workflows were found to process
@@ -893,30 +967,6 @@
 
 
   /**
-   * Execute the postResponseModifyPlugins.
-   */
-  private void invokePostResponsePlugins()
-  {
-    // Get the plugin config manager that will be used for invoking plugins.
-    PluginConfigManager pluginConfigManager =
-      DirectoryServer.getPluginConfigManager();
-
-    // Check wether there are local operations in attachments
-    List localOperations =
-      (List)getAttachment(Operation.LOCALBACKENDOPERATIONS);
-
-    if (localOperations != null && (! localOperations.isEmpty())){
-      for (Object localOp : localOperations)
-      {
-        LocalBackendModifyDNOperation localOperation =
-          (LocalBackendModifyDNOperation)localOp;
-        // Invoke the post-response add plugins.
-        pluginConfigManager.invokePostResponseModifyDNPlugins(localOperation);
-      }
-    }
-  }
-
-  /**
    * {@inheritDoc}
    */
   public DN getNewDN()

--
Gitblit v1.10.0