From cddb76e70b7127f5e0d0088bd59de99ebd1359e1 Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Mon, 27 May 2013 13:07:05 +0000
Subject: [PATCH] OPENDJ-928 Update tool: add option to automatically accept the license

---
 opends/src/server/org/opends/server/tools/upgrade/VerificationCallback.java |    6 +
 opends/src/server/org/opends/server/tools/ToolConstants.java                |    5 
 opends/src/messages/messages/tools.properties                               |    2 
 opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java       |   30 ++----
 opends/src/server/org/opends/server/util/cli/ConsoleApplication.java        |   97 ++++++++++++-----------
 opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java           |   56 ++++++++++---
 opends/src/server/org/opends/server/tools/upgrade/Upgrade.java              |   42 ++++++----
 7 files changed, 141 insertions(+), 97 deletions(-)

diff --git a/opends/src/messages/messages/tools.properties b/opends/src/messages/messages/tools.properties
index 4423a26..4a28ef6 100644
--- a/opends/src/messages/messages/tools.properties
+++ b/opends/src/messages/messages/tools.properties
@@ -2684,6 +2684,8 @@
 INFO_UPGRADE_TASK_ADD_CONFIG_FILE_1853=Adding '%s' configuration file
 SEVERE_ERR_UPGRADE_ADD_CONFIG_FILE_FAILS_1854=An error occurred while adding \
 configuration file '%s': %s
+INFO_OPTION_ACCEPT_LICENSE_1855=Automatically accepts the product license \
+(if present)
 
 # Upgrade tasks
 INFO_UPGRADE_TASK_6869_SUMMARY_10000=Fixing de-DE collation matching rule OID
diff --git a/opends/src/server/org/opends/server/tools/ToolConstants.java b/opends/src/server/org/opends/server/tools/ToolConstants.java
index 5a88cda..2366914 100644
--- a/opends/src/server/org/opends/server/tools/ToolConstants.java
+++ b/opends/src/server/org/opends/server/tools/ToolConstants.java
@@ -798,8 +798,9 @@
   public static final String OPTION_LONG_RESTORE_UPGRADE = "restore";
 
   /**
-   * The value for the long option for backup all.
+   * The value for the long option to automatically
+   * accept the license if present.
    */
-  public static final String OPTION_LONG_BACKUP_ALL = "backupAll";
+  public static final String OPTION_LONG_ACCEPT_LICENSE = "acceptLicense";
 }
 
diff --git a/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java b/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java
index dd67572..f4a5d5e 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java
@@ -396,7 +396,7 @@
       if (context.checkCLIUserOption(handler, IGNORE_ERRORS_MODE)
           == ConfirmationCallback.YES)
       {
-        context = new UpgradeContext(fromVersion, toVersion, true);
+        context.setIgnoreErrorsMode(true);
       }
 
       /*
@@ -580,7 +580,7 @@
       throw new ClientException(EXIT_CODE_SUCCESS, message);
     }
 
-    // TODO The upgrade only supports version >= 2.4.5.
+    // The upgrade only supports version >= 2.4.5.
     if (context.getFromVersion().compareTo(UPGRADESUPPORTSVERSIONFROM) < 0)
     {
       throw new ClientException(EXIT_CODE_ERROR,
@@ -647,29 +647,39 @@
         // and force to accept it.
         context.notify(handler, INFO_LICENSE_DETAILS_CLI_LABEL.get());
 
-        final int answer = context.confirmYN(handler,
-            INFO_LICENSE_ACCEPT.get(), ConfirmationCallback.NO);
+        if (context.checkCLIUserOption(handler, ACCEPT_LICENSE_MODE)
+            == ConfirmationCallback.NO)
+        {
 
-        if (answer == ConfirmationCallback.NO)
-        {
-          System.exit(EXIT_CODE_SUCCESS);
-        }
-        else if (answer == ConfirmationCallback.YES)
-        {
-          // Creates the file
-          LicenseFile.setApproval(true);
-          LicenseFile.createFileLicenseApproved();
+          final int answer =
+              context.confirmYN(handler, INFO_LICENSE_ACCEPT.get(),
+                  ConfirmationCallback.NO);
+
+          if (answer == ConfirmationCallback.NO)
+          {
+            System.exit(EXIT_CODE_SUCCESS);
+          }
+          else if (answer == ConfirmationCallback.YES)
+          {
+            createLicenseApproval();
+          }
         }
         else
         {
-          context.notify(handler,
-              INFO_LICENSE_CLI_ACCEPT_INVALID_RESPONSE.get());
+          context.notify(handler, INFO_LICENSE_ACCEPT.get());
+          context.notify(handler, INFO_PROMPT_YES_COMPLETE_ANSWER.get());
+          createLicenseApproval();
         }
       }
     }
   }
 
-
+  private static void createLicenseApproval()
+  {
+    // Creates the file
+    LicenseFile.setApproval(true);
+    LicenseFile.createFileLicenseApproved();
+  }
 
   // Prevent instantiation.
   private Upgrade()
diff --git a/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java b/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java
index 9834d20..eb78921 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java
@@ -95,6 +95,7 @@
   private BooleanArgument force;
   private BooleanArgument quietMode;
   private BooleanArgument verbose;
+  private BooleanArgument acceptLicense;
 
 
   // The argument which should be used to request usage information.
@@ -197,7 +198,7 @@
   }
 
   /**
-   * Force the upgrade. All answers will be forced to 'yes'.
+   * Force the upgrade. All critical questions will be forced to 'yes'.
    *
    * @return {@code true} if the upgrade process is forced.
    */
@@ -208,6 +209,7 @@
 
   /**
    * Force to ignore the errors during the upgrade process.
+   * Continues rather than fails.
    *
    * @return {@code true} if the errors are forced to be ignored.
    */
@@ -216,6 +218,16 @@
     return ignoreErrors.isPresent();
   }
 
+  /**
+   * Automatically accepts the license if it's present.
+   *
+   * @return {@code true} if license is accepted by default.
+   */
+  public boolean isAcceptLicense()
+  {
+    return acceptLicense.isPresent();
+  }
+
   // Displays the provided message followed by a help usage reference.
   private void displayMessageAndUsageReference(final Message message)
   {
@@ -263,10 +275,14 @@
           OPTION_LONG_FORCE_UPGRADE,
           INFO_UPGRADE_OPTION_FORCE.get(OPTION_LONG_NO_PROMPT));
 
+      acceptLicense = new BooleanArgument(OPTION_LONG_ACCEPT_LICENSE, null,
+          OPTION_LONG_ACCEPT_LICENSE, INFO_OPTION_ACCEPT_LICENSE.get());
+
       showUsageArgument =
           new BooleanArgument("help", OPTION_SHORT_HELP, OPTION_LONG_HELP,
               INFO_DESCRIPTION_USAGE.get());
 
+
       // Register the global arguments.
       parser.addGlobalArgument(showUsageArgument);
       parser.setUsageArgument(showUsageArgument, this.getOutputStream());
@@ -277,6 +293,7 @@
       parser.addGlobalArgument(quietMode);
       parser.addGlobalArgument(force);
       parser.addGlobalArgument(ignoreErrors);
+      parser.addGlobalArgument(acceptLicense);
 
       globalArgumentsInitialized = true;
     }
@@ -352,14 +369,15 @@
     catch (ClientException ex)
     {
       LOG.log(SEVERE, ex.getMessage());
-      println(ERROR, ex.getMessageObject(), 0);
+      println(Style.ERROR, ex.getMessageObject(), 0);
 
       return ex.getExitCode();
     }
     catch (Exception ex)
     {
       LOG.log(SEVERE, ex.getMessage());
-      println(ERROR, ERR_UPGRADE_MAIN_UPGRADE_PROCESS.get(ex.getMessage()), 0);
+      println(Style.ERROR, ERR_UPGRADE_MAIN_UPGRADE_PROCESS.get(ex
+          .getMessage()), 0);
 
       return EXIT_CODE_ERROR;
     }
@@ -391,13 +409,14 @@
         switch (fnc.getMessageSubType())
         {
         case TITLE_CALLBACK:
-          println(TITLE, Message.raw(fnc.getMessage()), 0);
+          println(Style.TITLE, Message.raw(fnc.getMessage()), 0);
           break;
         case SUBTITLE_CALLBACK:
-          println(SUBTITLE, Message.raw(fnc.getMessage()), 4);
+          println(Style.SUBTITLE, Message.raw(fnc.getMessage()),
+              4);
           break;
         case NOTICE_CALLBACK:
-          println(NOTICE, Message.raw(fnc.getMessage()), 0);
+          println(Style.NOTICE, Message.raw(fnc.getMessage()), 0);
           break;
         default:
           LOG.log(SEVERE, "Unsupported message type: "
@@ -439,8 +458,8 @@
             {
               if (!isInteractive() && !isForceUpgrade())
               {
-                println(ERROR, ERR_UPGRADE_USER_INTERACTION_REQUIRED.get(
-                    OPTION_LONG_NO_PROMPT, OPTION_LONG_FORCE_UPGRADE), 0);
+                println(Style.ERROR, ERR_UPGRADE_USER_INTERACTION_REQUIRED
+                    .get(OPTION_LONG_NO_PROMPT, OPTION_LONG_FORCE_UPGRADE), 0);
                 cc.setSelectedIndex(ConfirmationCallback.NO);
                 return;
               }
@@ -455,15 +474,15 @@
             {
               if (!isInteractive() && !isForceUpgrade())
               {
-                println(ERROR, ERR_UPGRADE_USER_INTERACTION_REQUIRED.get(
-                    OPTION_LONG_NO_PROMPT, OPTION_LONG_FORCE_UPGRADE), 0);
+                println(Style.ERROR, ERR_UPGRADE_USER_INTERACTION_REQUIRED
+                    .get(OPTION_LONG_NO_PROMPT, OPTION_LONG_FORCE_UPGRADE), 0);
                 cc.setSelectedIndex(ConfirmationCallback.NO);
                 return;
               }
             }
 
             // Does the user specify the ignore errors mode ?
-            if(opt == IGNORE_ERRORS_MODE) {
+            if (opt == IGNORE_ERRORS_MODE) {
               if (!isIgnoreErrors())
               {
                 cc.setSelectedIndex(ConfirmationCallback.NO);
@@ -471,6 +490,15 @@
               }
               cc.setSelectedIndex(ConfirmationCallback.YES);
             }
+
+            if (opt == ACCEPT_LICENSE_MODE) {
+              if (!isAcceptLicense())
+              {
+                cc.setSelectedIndex(ConfirmationCallback.NO);
+                return;
+              }
+              cc.setSelectedIndex(ConfirmationCallback.YES);
+            }
           }
           return;
         }
@@ -531,7 +559,9 @@
             String value = null;
             try
             {
-              value = readInput(Message.raw(prompt), defaultOption, SUBTITLE);
+              value =
+                  readInput(Message.raw(prompt), defaultOption,
+                      Style.SUBTITLE);
             }
             catch (CLIException e)
             {
@@ -581,7 +611,7 @@
           // Displays the prompt
           prompt.append(" ").append(
               UpgradeContext.getDefaultOption(cc.getSelectedIndex()));
-          println(SUBTITLE, Message.raw(prompt), 0);
+          println(Style.SUBTITLE, Message.raw(prompt), 0);
           LOG.log(INFO, UpgradeContext.getDefaultOption(cc.getSelectedIndex()));
         }
       }
diff --git a/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java b/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java
index 28eebaf..8f8e384 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java
@@ -66,7 +66,7 @@
   /**
    * If ignore errors is enabled.
    */
-  private final boolean isIgnoreErrorsMode;
+  private boolean isIgnoreErrorsMode;
 
   /**
    * Constructor for the upgrade context.
@@ -84,24 +84,6 @@
   }
 
   /**
-   * Constructor for the upgrade context.
-   *
-   * @param fromVersion
-   *          The version number from we upgrade from.
-   * @param toVersion
-   *          The version number we want to upgrade to.
-   * @param isIgnoreErrorsMode
-   *          If ignore error mode is enabled.
-   */
-  UpgradeContext(final BuildVersion fromVersion, final BuildVersion toVersion,
-      final boolean isIgnoreErrorsMode)
-  {
-    this.fromVersion = fromVersion;
-    this.toVersion = toVersion;
-    this.isIgnoreErrorsMode = isIgnoreErrorsMode;
-  }
-
-  /**
    * Returns the old version.
    *
    * @return The old version.
@@ -132,6 +114,16 @@
   }
 
   /**
+   * Sets the ignore errors mode.
+   *
+   * @param isIgnoreErrorsMode {@true} if ignore error mode is activated.
+   */
+  public void setIgnoreErrorsMode(boolean isIgnoreErrorsMode)
+  {
+    this.isIgnoreErrorsMode = isIgnoreErrorsMode;
+  }
+
+  /**
    * Sends notification message to the application via the call-back handler.
    *
    * @param handler
diff --git a/opends/src/server/org/opends/server/tools/upgrade/VerificationCallback.java b/opends/src/server/org/opends/server/tools/upgrade/VerificationCallback.java
index 642cbcf..4c48523 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/VerificationCallback.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/VerificationCallback.java
@@ -57,6 +57,11 @@
   public static final int CANNOT_BE_REVERTED = 2;
 
   /**
+   * An identifier of the accept license mode.
+   */
+  public static final int ACCEPT_LICENSE_MODE = 3;
+
+  /**
    * The identifier of ignore errors mode.
    */
   public static final int IGNORE_ERRORS_MODE = 5;
@@ -66,6 +71,7 @@
    */
   public static final int MANDATORY_USER_INTERACTION = 6;
 
+
   // The required options for the verification callback.
   private int[] requiredOptions;
 
diff --git a/opends/src/server/org/opends/server/util/cli/ConsoleApplication.java b/opends/src/server/org/opends/server/util/cli/ConsoleApplication.java
index 3d9b5e8..4288cfd 100644
--- a/opends/src/server/org/opends/server/util/cli/ConsoleApplication.java
+++ b/opends/src/server/org/opends/server/util/cli/ConsoleApplication.java
@@ -68,6 +68,7 @@
 import org.opends.server.util.PasswordReader;
 import org.opends.server.util.SetupUtils;
 
+
 /**
  * This class provides an abstract base class which can be used as the basis of
  * a console-based application.
@@ -101,31 +102,34 @@
   }
 
   /**
-   * Defines a title in the console application.
+   * Defines the different line styles for output.
    */
-  public final static int TITLE = 0;
-  /**
-   * Defines a subtitle in the console application.
-   */
-  public final static int SUBTITLE = 1;
-  /**
-   * Defines a notice in the console application.
-   */
-  public final static int NOTICE = 2;
-  /**
-   * Defines a normal line in the console application.
-   */
-  public final static int NORMAL = 3;
-
-  /**
-   * Defines an error line in the console application.
-   */
-  public final static int ERROR = 4;
-
-  /**
-   * Defines a break line in the console application.
-   */
-  public final static int BREAKLINE = 5;
+  public enum Style {
+    /**
+     * Defines a title.
+     */
+    TITLE,
+    /**
+     * Defines a subtitle.
+     */
+    SUBTITLE,
+    /**
+     * Defines a notice.
+     */
+    NOTICE,
+    /**
+     * Defines a normal line.
+     */
+    NORMAL,
+    /**
+     * Defines an error.
+     */
+    ERROR,
+    /**
+     * Defines a breakline.
+     */
+    BREAKLINE,
+  }
 
   // The error stream which this application should use.
   private final PrintStream err;
@@ -435,51 +439,48 @@
    */
   public final void println(final Message msg, final int indent)
   {
-    println(0, msg, indent);
+    println(Style.NORMAL, msg, indent);
   }
 
   /**
    * Print a line with EOL in the output stream.
    *
-   * @param typeMessage
+   * @param msgStyle
    *          The type of formatted output desired.
    * @param msg
    *          The message to display in normal mode.
    * @param indent
    *          The indentation.
    */
-  public final void println(final int typeMessage, final Message msg,
+  public final void println(final Style msgStyle, final Message msg,
       final int indent)
   {
     if (!isQuiet())
     {
-      if (typeMessage == TITLE)
+      switch (msgStyle)
       {
+      case TITLE:
         out.println();
         out.println(">>>> " + wrapText(msg, MAX_LINE_WIDTH, indent));
         out.println();
-      }
-      else if (typeMessage == SUBTITLE)
-      {
+        break;
+      case SUBTITLE:
         out.println(wrapText(msg, MAX_LINE_WIDTH, indent));
         out.println();
-      }
-      else if (typeMessage == NOTICE)
-      {
+        break;
+      case NOTICE:
         out.println(wrapText(" * " + msg, MAX_LINE_WIDTH, indent));
-      }
-      else if (typeMessage == ERROR)
-      {
+        break;
+      case ERROR:
         out.println();
         out.println(wrapText("** " + msg, MAX_LINE_WIDTH, indent));
-      }
-      else if (typeMessage == BREAKLINE)
-      {
+        break;
+      case BREAKLINE:
         out.println();
-      }
-      else
-      {
+        break;
+      default:
         out.println(wrapText(msg, MAX_LINE_WIDTH, indent));
+        break;
       }
     }
   }
@@ -663,19 +664,20 @@
    *          The message to display.
    * @param defaultValue
    *          The default answer by default.
-   * @param formattedOutput
+   * @param msgStyle
    *          The formatted style chosen.
    * @return The user's input as a string.
    * @throws CLIException
    *           If an Exception occurs during the process.
    */
   public final String readInput(final Message prompt,
-      final String defaultValue, final int formattedOutput) throws CLIException
+      final String defaultValue, final Style msgStyle)
+      throws CLIException
   {
     String answer = null;
     final Message messageToDisplay =
         INFO_PROMPT_SINGLE_DEFAULT.get(prompt.toString(), defaultValue);
-    if (formattedOutput == TITLE)
+    if (msgStyle == Style.TITLE)
     {
       println();
     }
@@ -692,7 +694,8 @@
       throw CLIException.adaptInputException(e);
     }
 
-    if (formattedOutput == TITLE || formattedOutput == SUBTITLE)
+    if (msgStyle == Style.TITLE
+        || msgStyle == Style.SUBTITLE)
     {
       println();
     }

--
Gitblit v1.10.0