From 6eff2f30885195442e269d45963e5e4539532642 Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Mon, 15 Jul 2024 09:48:07 +0000
Subject: [PATCH] [#167] FIX control-panel ResetUserPasswordTask unpredictable result (wait async result) (#357)

---
 opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/ResetUserPasswordTask.java |   68 +++++++++++++++++++---------------
 1 files changed, 38 insertions(+), 30 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/ResetUserPasswordTask.java b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/ResetUserPasswordTask.java
index 6d625be..48520bc 100644
--- a/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/ResetUserPasswordTask.java
+++ b/opendj-server-legacy/src/main/java/org/opends/guitools/controlpanel/task/ResetUserPasswordTask.java
@@ -27,6 +27,7 @@
 import java.util.List;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.concurrent.ExecutionException;
 
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.opendj.ldap.DN;
@@ -197,44 +198,51 @@
         passwordModifyRequest.setOldPassword(currentPassword);
       }
       passwordModifyRequest.setNewPassword(newPassword);
-      connectionWrapper.getConnection()
-                       .extendedRequestAsync(passwordModifyRequest)
-                       .thenOnResultOrException(
-                         new ResultHandler<PasswordModifyExtendedResult>()
-                         {
-                           @Override
-                           public void handleResult(final PasswordModifyExtendedResult passwordModifyExtendedResult)
-                           {
-                             if (lastException == null && currentPassword != null)
+      try {
+          connectionWrapper.getConnection()
+                           .extendedRequestAsync(passwordModifyRequest)
+                           .thenOnResultOrException(
+                             new ResultHandler<PasswordModifyExtendedResult>()
                              {
-                               try
+                               @Override
+                               public void handleResult(final PasswordModifyExtendedResult passwordModifyExtendedResult)
                                {
-                                 // The connections must be updated, just update the environment, which
-                                 // is what we use to clone connections and to launch scripts.
-                                 // The environment will also be used if we want to reconnect.
-                                 rebind(getInfo().getConnection());
-                                 if (getInfo().getUserDataConnection() != null)
+                                 if (lastException == null && currentPassword != null)
                                  {
-                                   rebind(getInfo().getUserDataConnection());
+                                   try
+                                   {
+                                     // The connections must be updated, just update the environment, which
+                                     // is what we use to clone connections and to launch scripts.
+                                     // The environment will also be used if we want to reconnect.
+                                     rebind(getInfo().getConnection());
+                                     if (getInfo().getUserDataConnection() != null)
+                                     {
+                                       rebind(getInfo().getUserDataConnection());
+                                     }
+                                   }
+                                   catch (final LdapException e)
+                                   {
+                                     lastException = e;
+                                     state = State.FINISHED_WITH_ERROR;
+                                   }
                                  }
+                                 state = State.FINISHED_SUCCESSFULLY;
                                }
-                               catch (final LdapException e)
+                             },
+                             new ExceptionHandler<LdapException>()
+                             {
+                               @Override
+                               public void handleException(final LdapException e)
                                {
-                                 lastException = e;
                                  state = State.FINISHED_WITH_ERROR;
                                }
-                             }
-                             state = State.FINISHED_SUCCESSFULLY;
-                           }
-                         },
-                         new ExceptionHandler<LdapException>()
-                         {
-                           @Override
-                           public void handleException(final LdapException e)
-                           {
-                             state = State.FINISHED_WITH_ERROR;
-                           }
-                         });
+                             }).get();
+      } catch (Exception e) {
+          if (state != State.FINISHED_WITH_ERROR) {
+            lastException = e;
+            state = State.FINISHED_WITH_ERROR;
+          }
+      }
   }
 
   private void rebind(ConnectionWrapper conn) throws LdapException

--
Gitblit v1.10.0