From 377bb430a4fd748502a7a71391d06f79340f5c1d Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 19 Mar 2008 17:47:00 +0000
Subject: [PATCH] Fix for issue 3078 (dsreplication interactive mode not consistent with dsconfig)

---
 opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java |  172 ++++++++++++++++++++++++++++++++++++++++++-
 opends/src/messages/messages/admin_tool.properties                             |   17 +++
 2 files changed, 183 insertions(+), 6 deletions(-)

diff --git a/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java b/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
index 68626dc..360e3e9 100644
--- a/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
+++ b/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
@@ -114,6 +114,8 @@
 import org.opends.server.util.cli.CLIException;
 import org.opends.server.util.cli.ConsoleApplication;
 import org.opends.server.util.cli.LDAPConnectionConsoleInteraction;
+import org.opends.server.util.cli.MenuBuilder;
+import org.opends.server.util.cli.MenuResult;
 import org.opends.server.util.table.TableBuilder;
 import org.opends.server.util.table.TextTablePrinter;
 
@@ -141,6 +143,57 @@
   private static final Logger LOG =
     Logger.getLogger(ReplicationCliMain.class.getName());
 
+  /**
+   * The enumeration containing the different options we display when we ask
+   * the user to provide the subcommand interactively.
+   */
+  private enum SubcommandChoice
+  {
+    /**
+     * Enable replication.
+     */
+    ENABLE(INFO_REPLICATION_ENABLE_MENU_PROMPT.get()),
+    /**
+     * Disable replication.
+     */
+    DISABLE(INFO_REPLICATION_DISABLE_MENU_PROMPT.get()),
+    /**
+     * Initialize replication.
+     */
+    INITIALIZE(INFO_REPLICATION_INITIALIZE_MENU_PROMPT.get()),
+    /**
+     * Initialize All.
+     */
+    INITIALIZE_ALL(INFO_REPLICATION_INITIALIZE_ALL_MENU_PROMPT.get()),
+    /**
+     * Pre external initialization.
+     */
+    PRE_EXTERNAL_INITIALIZATION(
+        INFO_REPLICATION_PRE_EXTERNAL_INITIALIZATION_MENU_PROMPT.get()),
+    /**
+     * Post external initialization.
+     */
+    POST_EXTERNAL_INITIALIZATION(
+        INFO_REPLICATION_POST_EXTERNAL_INITIALIZATION_MENU_PROMPT.get()),
+    /**
+     * Replication status.
+     */
+    STATUS(INFO_REPLICATION_STATUS_MENU_PROMPT.get()),
+    /**
+     * Cancel operation.
+     */
+    CANCEL(null);
+    private Message prompt;
+    private SubcommandChoice(Message prompt)
+    {
+      this.prompt = prompt;
+    }
+    Message getPrompt()
+    {
+      return prompt;
+    }
+  };
+
   // The argument parser to be used.
   private ReplicationCliArgumentParser argParser;
   private LDAPConnectionConsoleInteraction ci = null;
@@ -367,10 +420,72 @@
         }
         else
         {
-          println(ERR_REPLICATION_VALID_SUBCOMMAND_NOT_FOUND.get());
-          println(Message.raw(argParser.getUsage()));
-          returnValue = ERROR_USER_DATA;
-          subcommandLaunched = false;
+          if (argParser.isInteractive())
+          {
+            String subCommand = null;
+            switch (promptForSubcommand())
+            {
+            case ENABLE:
+              subCommand =
+                ReplicationCliArgumentParser.ENABLE_REPLICATION_SUBCMD_NAME;
+              break;
+
+            case DISABLE:
+              subCommand =
+                ReplicationCliArgumentParser.DISABLE_REPLICATION_SUBCMD_NAME;
+              break;
+
+            case INITIALIZE:
+              subCommand =
+                ReplicationCliArgumentParser.INITIALIZE_REPLICATION_SUBCMD_NAME;
+              break;
+
+            case INITIALIZE_ALL:
+              subCommand =
+                ReplicationCliArgumentParser.
+                INITIALIZE_ALL_REPLICATION_SUBCMD_NAME;
+              break;
+
+            case PRE_EXTERNAL_INITIALIZATION:
+              subCommand = ReplicationCliArgumentParser.
+              PRE_EXTERNAL_INITIALIZATION_SUBCMD_NAME;
+              break;
+
+            case POST_EXTERNAL_INITIALIZATION:
+              subCommand = ReplicationCliArgumentParser.
+                 POST_EXTERNAL_INITIALIZATION_SUBCMD_NAME;
+              break;
+
+            case STATUS:
+              subCommand =
+                ReplicationCliArgumentParser.STATUS_REPLICATION_SUBCMD_NAME;
+              break;
+
+            default:
+              // User cancelled
+              returnValue = USER_CANCELLED;
+            }
+
+            if (subCommand != null)
+            {
+              String[] newArgs = new String[args.length + 1];
+              newArgs[0] = subCommand;
+              for (int i=0; i<args.length ; i++)
+              {
+                newArgs[i+1] = args[i];
+              }
+              // The server (if requested) has already been initialized.
+              return execute(newArgs, false);
+            }
+          }
+          else
+          {
+            println(ERR_REPLICATION_VALID_SUBCOMMAND_NOT_FOUND.get(
+                ToolConstants.OPTION_LONG_NO_PROMPT));
+            println(Message.raw(argParser.getUsage()));
+            returnValue = ERROR_USER_DATA;
+            subcommandLaunched = false;
+          }
         }
 
         // Display the log file only if the operation is successful (when there
@@ -3000,6 +3115,10 @@
                   Installer.WARNING_CLOCK_DIFFERENCE_THRESOLD_MINUTES)));
         }
       }
+      printlnProgress();
+      printProgress(INFO_REPLICATION_POST_ENABLE_INFO.get("dsreplication",
+          ReplicationCliArgumentParser.ENABLE_REPLICATION_SUBCMD_NAME));
+      printlnProgress();
     }
 
     if (ctx1 != null)
@@ -4196,6 +4315,11 @@
               }
             }
           }
+          if (confirmationLimitReached)
+          {
+            suffixes.clear();
+            break;
+          }
         }
       }
     }
@@ -7423,4 +7547,44 @@
     }
     return hostPort;
   }
+
+  /**
+   * Prompts the user for the subcommand that should be executed.
+   * @return the subcommand choice of the user.
+   */
+  private SubcommandChoice promptForSubcommand()
+  {
+    SubcommandChoice returnValue;
+    MenuBuilder<SubcommandChoice> builder =
+      new MenuBuilder<SubcommandChoice>(this);
+    builder.setPrompt(INFO_REPLICATION_SUBCOMMAND_PROMPT.get());
+    builder.addCancelOption(false);
+    for (SubcommandChoice choice : SubcommandChoice.values())
+    {
+      if (choice != SubcommandChoice.CANCEL)
+      {
+        builder.addNumberedOption(choice.getPrompt(),
+            MenuResult.success(choice));
+      }
+    }
+    try
+    {
+      MenuResult<SubcommandChoice> m = builder.toMenu().run();
+      if (m.isSuccess())
+      {
+        returnValue = m.getValue();
+      }
+      else
+      {
+       // The user cancelled
+        returnValue = SubcommandChoice.CANCEL;
+      }
+    }
+    catch (CLIException ce)
+    {
+      returnValue = SubcommandChoice.CANCEL;
+      LOG.log(Level.WARNING, "Error reading input: "+ce, ce);
+    }
+    return returnValue;
+  }
 }
diff --git a/opends/src/messages/messages/admin_tool.properties b/opends/src/messages/messages/admin_tool.properties
index 4b6cdf5..7c62239 100644
--- a/opends/src/messages/messages/admin_tool.properties
+++ b/opends/src/messages/messages/admin_tool.properties
@@ -551,7 +551,7 @@
 SEVERE_ERR_REPLICATION_SAME_REPLICATION_PORT=You have provided the same \
  replication port (%s) for two servers located on the same machine (%s).
 SEVERE_ERR_REPLICATION_VALID_SUBCOMMAND_NOT_FOUND=Could not find a valid \
- subcommand.
+ subcommand.  You must specify a subcommand when using the option {%s}.
 SEVERE_ERR_REPLICATION_STATUS_QUIET=The {%s} subcommand is not compatible with \
  the {%s} argument.
 INFO_REPLICATION_SUCCESSFUL=The operation has been successfully completed
@@ -592,7 +592,7 @@
 INFO_REPLICATION_ENABLE_REPLICATIONPORT2_PROMPT=Replication port for the \
  second server (the port must be free)
 INFO_REPLICATION_ENABLE_SECURE2_PROMPT=Do want replication to use encrypted \
- communication when connecting to replication port %s on the second server? 
+ communication when connecting to replication port %s on the second server?
 INFO_REPLICATION_ENABLE_BINDDN2_PROMPT=Bind DN for the second server
 INFO_REPLICATION_ENABLE_PASSWORD2_PROMPT=Password for %s on the second server:
 INFO_REPLICATION_INITIALIZE_SOURCE_CONNECTION_PARAMETERS=>>>> Specify OpenDS \
@@ -798,3 +798,16 @@
  %s for more information.
 SEVERE_ERR_LAUNCHING_PRE_EXTERNAL_INITIALIZATION=Error launching the operation.
 SEVERE_ERR_LAUNCHING_POST_EXTERNAL_INITIALIZATION=Error launching the operation.
+INFO_REPLICATION_SUBCOMMAND_PROMPT=What do you want to do?
+INFO_REPLICATION_ENABLE_MENU_PROMPT=Enable Replication
+INFO_REPLICATION_DISABLE_MENU_PROMPT=Disable Replication
+INFO_REPLICATION_INITIALIZE_MENU_PROMPT=Initialize Replication on one Server
+INFO_REPLICATION_INITIALIZE_ALL_MENU_PROMPT=Initialize All Servers
+INFO_REPLICATION_PRE_EXTERNAL_INITIALIZATION_MENU_PROMPT=Pre External \
+ Initialization
+INFO_REPLICATION_POST_EXTERNAL_INITIALIZATION_MENU_PROMPT=Post External \
+ Initialization
+INFO_REPLICATION_STATUS_MENU_PROMPT=Display Replication Status
+INFO_REPLICATION_POST_ENABLE_INFO=Replication has been successfully enabled.  \
+ Note that for replication to work you must initialize the contents of the \
+ base DNs that are being replicated (use %s %s to do so).

--
Gitblit v1.10.0