From 73bcbc6c61efa450b12b40598c55f420904b6cea Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 20 Jun 2013 20:16:32 +0000
Subject: [PATCH] Fix OPENDJ-992: JE environment failure (LOG_FILE_NOT_FOUND) after upgrading from OpenDJ 2.4.6 to OpenDJ 2.6.0

---
 opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java |  198 ++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 146 insertions(+), 52 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
index 51b6701..334f8e1 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
@@ -47,6 +47,7 @@
 import org.opends.server.protocols.ldap.LDAPFilter;
 import org.opends.server.tools.ClientException;
 import org.opends.server.tools.upgrade.UpgradeTask.TaskType;
+import org.opends.server.util.BuildVersion;
 
 /**
  * Factory methods for create new upgrade tasks.
@@ -340,6 +341,99 @@
   }
 
   /**
+   * Creates a group of tasks which will only be invoked if the current version
+   * is more recent than the provided version. This may be useful in cases where
+   * a regression was introduced in version X and resolved in a later version Y.
+   * In this case, the provided upgrade tasks will only be invoked if the
+   * current version is between X (inclusive) and Y (exclusive).
+   *
+   * @param versionString
+   *          The lower bound version. The upgrade tasks will not be applied if
+   *          the current version is older than this version.
+   * @param tasks
+   *          The group of tasks to invoke if the current version is equal to or
+   *          more recent than {@code versionString}.
+   * @return An upgrade task which will only be invoked if the current version
+   *         is more recent than the provided version.
+   */
+  public static UpgradeTask regressionInVersion(final String versionString,
+      final UpgradeTask... tasks)
+  {
+    final BuildVersion version = BuildVersion.valueOf(versionString);
+    return new AbstractUpgradeTask()
+    {
+
+      @Override
+      public void verify(UpgradeContext context) throws ClientException
+      {
+        if (currentVersionEqualToOrMoreRecentThan(context, version))
+        {
+          for (UpgradeTask task : tasks)
+          {
+            task.verify(context);
+          }
+        }
+      }
+
+      @Override
+      public void interact(UpgradeContext context) throws ClientException
+      {
+        if (currentVersionEqualToOrMoreRecentThan(context, version))
+        {
+          for (UpgradeTask task : tasks)
+          {
+            task.interact(context);
+          }
+        }
+      }
+
+      @Override
+      public void start(UpgradeContext context) throws ClientException
+      {
+        if (currentVersionEqualToOrMoreRecentThan(context, version))
+        {
+          for (UpgradeTask task : tasks)
+          {
+            task.start(context);
+          }
+        }
+      }
+
+      @Override
+      public void perform(UpgradeContext context) throws ClientException
+      {
+        if (currentVersionEqualToOrMoreRecentThan(context, version))
+        {
+          for (UpgradeTask task : tasks)
+          {
+            task.perform(context);
+          }
+        }
+      }
+
+      @Override
+      public void end(UpgradeContext context) throws ClientException
+      {
+        if (currentVersionEqualToOrMoreRecentThan(context, version))
+        {
+          for (UpgradeTask task : tasks)
+          {
+            task.end(context);
+          }
+        }
+      }
+
+
+
+      private boolean currentVersionEqualToOrMoreRecentThan(
+          UpgradeContext context, final BuildVersion version)
+      {
+        return context.getFromVersion().compareTo(version) >= 0;
+      }
+    };
+  }
+
+  /**
    * Creates a rebuild all indexes task.
    *
    * @param summary
@@ -348,21 +442,8 @@
    */
   public static UpgradeTask rebuildAllIndexes(final Message summary)
   {
-    return new UpgradeTask()
+    return new AbstractUpgradeTask()
     {
-
-      @Override
-      public void end(final UpgradeContext context) throws ClientException
-      {
-        // Nothing to do.
-      }
-
-      @Override
-      public void interact(final UpgradeContext context) throws ClientException
-      {
-        // Nothing to do.
-      }
-
       @Override
       public void perform(final UpgradeContext context) throws ClientException
       {
@@ -383,6 +464,55 @@
     };
   }
 
+
+
+  /**
+   * Creates a rebuild index task for a single index. At the moment this is
+   * implemented as a simple stub which displays a message which should prompt
+   * the user to rebuild the index manually once the upgrade has completed.
+   * <p>
+   * In future this task should register the index to be rebuilt in a table. A
+   * subsequent task executed at the end of the upgrade process will then obtain
+   * the set of indexes to be rebuilt, optimize it (e.g. removing duplicates),
+   * and perform the rebuild.
+   *
+   * @param summary
+   *          A message describing why the index needs to be rebuilt and asking
+   *          them whether or not they wish to continue.
+   * @return The rebuild index task.
+   */
+  public static UpgradeTask rebuildSingleIndex(final Message summary)
+  {
+    return new AbstractUpgradeTask()
+    {
+      @Override
+      public void verify(final UpgradeContext context) throws ClientException
+      {
+        verifyTaskType(TaskType.MANDATORY_USER_INTERACTION, context);
+      }
+
+      @Override
+      public void interact(UpgradeContext context) throws ClientException
+      {
+        // Require acknowledgment from the user.
+        final int answer = context.confirmYN(summary, ConfirmationCallback.NO);
+
+        // The user refused to perform this task.
+        if (answer == ConfirmationCallback.NO)
+        {
+          throw new ClientException(EXIT_CODE_MANUAL_INTERVENTION,
+              INFO_UPGRADE_ABORTED_BY_USER.get());
+        }
+      }
+
+      @Override
+      public void perform(final UpgradeContext context) throws ClientException
+      {
+        // TODO: automatic rebuild is not supported yet.
+      }
+    };
+  }
+
   /**
    * Creates a file object representing config/upgrade/schema.ldif.current which
    * the server creates the first time it starts if there are schema
@@ -481,17 +611,11 @@
       final Message description, final boolean needsUserConfirmation,
       final String... ldif)
   {
-    return new UpgradeTask()
+    return new AbstractUpgradeTask()
     {
       private boolean userConfirmation = true;
 
       @Override
-      public void end(final UpgradeContext context)
-      {
-        // Nothing to do: no cleanup required.
-      }
-
-      @Override
       public void interact(final UpgradeContext context) throws ClientException
       {
         if (needsUserConfirmation)
@@ -544,18 +668,6 @@
           }
         }
       }
-
-      @Override
-      public void start(final UpgradeContext context) throws ClientException
-      {
-        // Nothing to do.
-      }
-
-      @Override
-      public void verify(final UpgradeContext context) throws ClientException
-      {
-        // Nothing to do.
-      }
     };
   }
 
@@ -605,17 +717,11 @@
       final Message description, final boolean needsUserConfirmation,
       final String filter, final String... ldif)
   {
-    return new UpgradeTask()
+    return new AbstractUpgradeTask()
     {
       private boolean userConfirmation = true;
 
       @Override
-      public void end(final UpgradeContext context)
-      {
-        // Nothing to do: no cleanup required.
-      }
-
-      @Override
       public void interact(final UpgradeContext context) throws ClientException
       {
         if (needsUserConfirmation)
@@ -667,18 +773,6 @@
           }
         }
       }
-
-      @Override
-      public void start(final UpgradeContext context) throws ClientException
-      {
-        // Nothing to do.
-      }
-
-      @Override
-      public void verify(final UpgradeContext context) throws ClientException
-      {
-        // Nothing to do.
-      }
     };
   }
 

--
Gitblit v1.10.0