From 7799753ee3631b6ac9f3ade5205da789455bc4fd Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 01 Aug 2013 15:38:23 +0000
Subject: [PATCH] More minor enhancements to the upgrader so that it can be used for embedded upgrades.

---
 opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java     |   42 +++++++++++++-------
 opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java |   60 +++++++++++++++---------------
 2 files changed, 57 insertions(+), 45 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java
index d770642..da69d34 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java
@@ -52,7 +52,6 @@
 import org.opends.messages.Message;
 import org.opends.server.extensions.ConfigFileHandler;
 import org.opends.server.tools.ClientException;
-import org.opends.server.util.BuildVersion;
 import org.opends.server.util.ServerConstants;
 import org.opends.server.util.StaticUtils;
 import org.opends.server.util.args.ArgumentException;
@@ -357,14 +356,11 @@
       UpgradeLog.initLogFileHandler();
 
       // Upgrade's context.
-      UpgradeContext context =
-          new UpgradeContext(BuildVersion.instanceVersion(), BuildVersion
-              .binaryVersion(), this);
-
-      context.setIgnoreErrorsMode(isIgnoreErrors());
-      context.setAcceptLicenseMode(isAcceptLicense());
-      context.setInteractiveMode(isInteractive());
-      context.setForceUpgradeMode(isForceUpgrade());
+      UpgradeContext context = new UpgradeContext(this)
+          .setIgnoreErrorsMode(isIgnoreErrors())
+          .setAcceptLicenseMode(isAcceptLicense())
+          .setInteractiveMode(isInteractive())
+          .setForceUpgradeMode(isForceUpgrade());
 
       // Starts upgrade.
       Upgrade.upgrade(context);
@@ -452,9 +448,7 @@
         final ConfirmationCallback cc = (ConfirmationCallback) c;
         List<String> choices = new ArrayList<String>();
 
-        final String defaultOption =
-            UpgradeContext.getDefaultOption(cc.getDefaultOption());
-
+        final String defaultOption = getDefaultOption(cc.getDefaultOption());
         StringBuilder prompt =
             new StringBuilder(StaticUtils.wrapText(cc.getPrompt(),
                 ServerConstants.MAX_LINE_WIDTH, 2));
@@ -553,10 +547,9 @@
             cc.setSelectedIndex(cc.getDefaultOption());
           }
           // Displays the prompt
-          prompt.append(" ").append(
-              UpgradeContext.getDefaultOption(cc.getSelectedIndex()));
+          prompt.append(" ").append(getDefaultOption(cc.getSelectedIndex()));
           println(Style.SUBTITLE, Message.raw(prompt), 0);
-          LOG.log(INFO, UpgradeContext.getDefaultOption(cc.getSelectedIndex()));
+          LOG.log(INFO, getDefaultOption(cc.getSelectedIndex()));
         }
       }
       else
@@ -566,4 +559,23 @@
       }
     }
   }
+
+
+
+  private static String getDefaultOption(final int defaultOption)
+  {
+    if (defaultOption == ConfirmationCallback.YES)
+    {
+      return INFO_PROMPT_YES_COMPLETE_ANSWER.get().toString();
+    }
+    else if (defaultOption == ConfirmationCallback.NO)
+    {
+      return INFO_PROMPT_NO_COMPLETE_ANSWER.get().toString();
+    }
+    else if (defaultOption == ConfirmationCallback.CANCEL)
+    {
+      return INFO_TASKINFO_CMD_CANCEL_CHAR.get().toString();
+    }
+    return null;
+  }
 }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java
index 4c76d24..ad01a0e 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeContext.java
@@ -39,6 +39,7 @@
 
 import org.opends.messages.Message;
 import org.opends.server.tools.ClientException;
+import org.opends.server.types.InitializationException;
 import org.opends.server.util.BuildVersion;
 
 
@@ -88,6 +89,23 @@
 
 
   /**
+   * Creates a new upgrade context for upgrading from the instance version (as
+   * obtained from config/buildinfo) to the binary version.
+   *
+   * @param handler
+   *          The call-back handler for interacting with the upgrade
+   *          application.
+   * @throws InitializationException
+   *           If an error occurred while reading or parsing the version.
+   */
+  public UpgradeContext(CallbackHandler handler) throws InitializationException
+  {
+    this(BuildVersion.instanceVersion(), BuildVersion.binaryVersion(), handler);
+  }
+
+
+
+  /**
    * Constructor for the upgrade context.
    *
    * @param fromVersion
@@ -149,10 +167,12 @@
    *
    * @param isIgnoreErrorsMode
    *          {@code true} if ignore error mode is activated.
+   * @return This upgrade context.
    */
-  public void setIgnoreErrorsMode(boolean isIgnoreErrorsMode)
+  public UpgradeContext setIgnoreErrorsMode(boolean isIgnoreErrorsMode)
   {
     this.isIgnoreErrorsMode = isIgnoreErrorsMode;
+    return this;
   }
 
 
@@ -174,10 +194,12 @@
    *
    * @param isAcceptLicenseMode
    *          {@code true} if the accept license mode is activated.
+   * @return This upgrade context.
    */
-  public void setAcceptLicenseMode(boolean isAcceptLicenseMode)
+  public UpgradeContext setAcceptLicenseMode(boolean isAcceptLicenseMode)
   {
     this.isAcceptLicenseMode = isAcceptLicenseMode;
+    return this;
   }
 
 
@@ -211,10 +233,12 @@
    *
    * @param isInteractiveMode
    *          {@code true} if the interactive mode is activated.
+   * @return This upgrade context.
    */
-  public void setInteractiveMode(boolean isInteractiveMode)
+  public UpgradeContext setInteractiveMode(boolean isInteractiveMode)
   {
     this.isInteractiveMode = isInteractiveMode;
+    return this;
   }
 
 
@@ -236,10 +260,12 @@
    *
    * @param isForceUpgradeMode
    *          {@code true} if the force upgrade mode is activated.
+   * @return This upgrade context.
    */
-  public void setForceUpgradeMode(boolean isForceUpgradeMode)
+  public UpgradeContext setForceUpgradeMode(boolean isForceUpgradeMode)
   {
     this.isForceUpgradeMode = isForceUpgradeMode;
+    return this;
   }
 
 
@@ -347,30 +373,4 @@
     }
     return confirmYNCallback.getSelectedIndex();
   }
-
-
-
-  /**
-   * Returns the default option string.
-   *
-   * @param defaultOption
-   *          The default option int value.
-   * @return The default option string.
-   */
-  public static String getDefaultOption(final int defaultOption)
-  {
-    if (defaultOption == ConfirmationCallback.YES)
-    {
-      return INFO_PROMPT_YES_COMPLETE_ANSWER.get().toString();
-    }
-    else if (defaultOption == ConfirmationCallback.NO)
-    {
-      return INFO_PROMPT_NO_COMPLETE_ANSWER.get().toString();
-    }
-    else if (defaultOption == ConfirmationCallback.CANCEL)
-    {
-      return INFO_TASKINFO_CMD_CANCEL_CHAR.get().toString();
-    }
-    return null;
-  }
 }

--
Gitblit v1.10.0