From d48536f1cf5e4b6714122fc0d85a5e77bf731346 Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Wed, 16 Mar 2016 11:41:09 +0000
Subject: [PATCH] OPENDJ-2730 Fix upgrade on case insensitive file systems

---
 opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeCli.java   |    2 
 opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java      |   41 +++++++++-----------
 opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java |   48 +++++++++++++++++++----
 opendj-server-legacy/src/messages/org/opends/messages/tool.properties                |    2 +
 4 files changed, 61 insertions(+), 32 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java
index df06213..f70eea9 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/Upgrade.java
@@ -56,21 +56,18 @@
   private static LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
   /** Upgrade supports version from 2.4.5. */
-  private static BuildVersion UPGRADESUPPORTSVERSIONFROM = BuildVersion.valueOf("2.4.5.0000");
+  private static BuildVersion UPGRADE_SUPPORTS_VERSION_FROM = BuildVersion.valueOf("2.4.5.0000");
 
   /** The success exit code value. */
   static final int EXIT_CODE_SUCCESS = 0;
   /** The error exit code value. */
   static final int EXIT_CODE_ERROR = 1;
 
-  /**
-   * The exit code value that will be used if upgrade requires manual
-   * intervention.
-   */
-  static final int EXIT_CODE_MANUAL_INTERVENTION = 2;
-
   /** If the upgrade contains some post upgrade tasks to do. */
-  static boolean hasPostUpgradeTask;
+  private static boolean hasPostUpgradeTask;
+
+  /** If the upgrade script should exit with error code (useful for warnings) */
+  private static boolean exitWithErrorCode;
 
   /** Developers should register upgrade tasks below. */
   private static final NavigableMap<BuildVersion, List<UpgradeTask>> TASKS = new TreeMap<>();
@@ -863,10 +860,10 @@
     }
 
     // The upgrade only supports version >= 2.4.5.
-    if (context.getFromVersion().compareTo(UPGRADESUPPORTSVERSIONFROM) < 0)
+    if (context.getFromVersion().compareTo(UPGRADE_SUPPORTS_VERSION_FROM) < 0)
     {
       final LocalizableMessage message =
-          INFO_UPGRADE_VERSION_IS_NOT_SUPPORTED.get(UPGRADESUPPORTSVERSIONFROM, UPGRADESUPPORTSVERSIONFROM);
+          INFO_UPGRADE_VERSION_IS_NOT_SUPPORTED.get(UPGRADE_SUPPORTS_VERSION_FROM, UPGRADE_SUPPORTS_VERSION_FROM);
       context.notify(message, NOTICE_CALLBACK);
       throw new ClientException(ReturnCode.ERROR_UNEXPECTED, message);
     }
@@ -970,25 +967,25 @@
     }
   }
 
-  /**
-   * Returns {@code true} if the current upgrade contains post upgrade tasks.
-   *
-   * @return {@code true} if the current upgrade contains post upgrade tasks.
-   */
-  static boolean hasPostUpgradeTask()
+  static void needToRunPostUpgradePhase()
   {
-    return hasPostUpgradeTask;
+    Upgrade.hasPostUpgradeTask = true;
+  }
+
+  /** This method should be used when the upgrade tool has issued a warning. */
+  static void needToExitWithErrorCode()
+  {
+    Upgrade.exitWithErrorCode = true;
   }
 
   /**
-   * Sets {@code true} if the current upgrade contains post upgrade tasks.
+   * {@code true} if the upgrade succeeded.
    *
-   * @param hasPostUpgradeTask
-   *          {@code true} if the current upgrade contains post upgrade tasks.
+   * @return {@code true} if the upgrade succeeded.
    */
-  static void setHasPostUpgradeTask(boolean hasPostUpgradeTask)
+  static boolean isSuccess()
   {
-    Upgrade.hasPostUpgradeTask = hasPostUpgradeTask;
+    return !exitWithErrorCode;
   }
 
   /** Prevent instantiation. */
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeCli.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeCli.java
index ee0c4c8..569c5cb 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeCli.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeCli.java
@@ -330,7 +330,7 @@
 
       return EXIT_CODE_ERROR;
     }
-    return EXIT_CODE_SUCCESS;
+    return Upgrade.isSuccess() ? EXIT_CODE_SUCCESS : EXIT_CODE_ERROR;
   }
 
   /** {@inheritDoc} */
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java
index 53bd46c..8761918 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/tools/upgrade/UpgradeTasks.java
@@ -563,7 +563,7 @@
       @Override
       public void prepare(UpgradeContext context) throws ClientException
       {
-        Upgrade.setHasPostUpgradeTask(true);
+        Upgrade.needToRunPostUpgradePhase();
         // Requires answer from the user.
         isATaskToPerform = context.confirmYN(summary, NO) == YES;
         isRebuildAllIndexesIsPresent = true;
@@ -618,7 +618,7 @@
       @Override
       public void prepare(UpgradeContext context) throws ClientException
       {
-        Upgrade.setHasPostUpgradeTask(true);
+        Upgrade.needToRunPostUpgradePhase();
         // Requires answer from the user.
         isATaskToPerform = context.confirmYN(summary, NO) == YES;
       }
@@ -1059,7 +1059,7 @@
           {
             while (jeBackends.hasNext())
             {
-              Upgrade.setHasPostUpgradeTask(true);
+              Upgrade.needToRunPostUpgradePhase();
               reimportRequired = true;
 
               Entry jeBackend = jeBackends.readEntry();
@@ -1322,6 +1322,7 @@
   {
     return new AbstractUpgradeTask()
     {
+
       @Override
       public void perform(final UpgradeContext context) throws ClientException
       {
@@ -1329,19 +1330,30 @@
             INFORMATION, INFO_UPGRADE_TASK_REMOVE_OLD_JARS.get(), 0);
         context.notifyProgress(pnc);
 
+        final boolean fileSystemIsCaseSensitive = fileSystemIsCaseSensitive(context);
+
+        deleteJarFilesIfFileSystemIsCaseSensitive(fileSystemIsCaseSensitive, "OpenDJ");
         for (final String locale : SUPPORTED_LOCALES_FOR_3_0_0)
         {
-          deleteJarFileIfExists("OpenDJ_" + locale);
-          deleteJarFileIfExists("OpenDJ-" + locale);
+          deleteJarFiles("OpenDJ-" + locale);
+          deleteJarFilesIfFileSystemIsCaseSensitive(fileSystemIsCaseSensitive, "OpenDJ_" + locale);
         }
-        deleteJarFileIfExists("OpenDJ",
-            // Jar files from 2.6.x
-            "jackson-core-asl", "jackson-mapper-asl", "json-fluent", "json-resource-servlet",
+        // Jar files from 2.6.x
+        deleteJarFiles("jackson-core-asl", "jackson-mapper-asl", "json-fluent", "json-resource-servlet",
             "mail", "opendj-ldap-sdk", "opendj-rest2ldap-servlet", "opendj-server2x-adapter");
         context.notifyProgress(pnc.setProgress(100));
       }
 
-      private void deleteJarFileIfExists(final String... jarFileNames)
+      private void deleteJarFilesIfFileSystemIsCaseSensitive(
+          final boolean fileSystemIsCaseSensitive, final String... jarFileNames)
+      {
+        if (fileSystemIsCaseSensitive)
+        {
+          deleteJarFiles(jarFileNames);
+        }
+      }
+
+      private void deleteJarFiles(final String... jarFileNames)
       {
         for (final String jarFileName : jarFileNames)
         {
@@ -1352,6 +1364,24 @@
           }
         }
       }
+
+      /** Used to know if we have to remove old "camel case" OpenDJ[_]*.jar(see OPENDJ-2692). */
+      private boolean fileSystemIsCaseSensitive(final UpgradeContext context) throws ClientException
+      {
+        final File openDJCamelCaseJar = new File(libDirectory, "OpenDJ.jar");
+        try
+        {
+          // getCanonicalPath() will return the new "opendj.jar" on case insensitive file systems
+          return openDJCamelCaseJar.getCanonicalPath().equals(openDJCamelCaseJar.getAbsolutePath());
+        }
+        catch (final IOException unlikely)
+        {
+          // Warn the user that he may have some old camel case jars to remove
+          context.notify(INFO_UPGRADE_TASK_UNABLE_TO_REMOVE_OLD_JARS.get());
+          Upgrade.needToExitWithErrorCode();
+          return false;
+        }
+      }
     };
 
   }
diff --git a/opendj-server-legacy/src/messages/org/opends/messages/tool.properties b/opendj-server-legacy/src/messages/org/opends/messages/tool.properties
index 70613b5..2afca86 100644
--- a/opendj-server-legacy/src/messages/org/opends/messages/tool.properties
+++ b/opendj-server-legacy/src/messages/org/opends/messages/tool.properties
@@ -2623,6 +2623,8 @@
 based on "distinguishedName" attribute type, like "member", "owner", "roleOccupant" and "seeAlso". \
 They all have to be rebuilt if they exist on your server and this could take a long time to proceed. \
 Do you want to launch this process automatically at the end of the upgrade?
+INFO_UPGRADE_TASK_UNABLE_TO_REMOVE_OLD_JARS_20029=Unable to determine whether the file system is case sensitive. \
+  If the file system is case sensitive, then remove OpenDJ.jar and all OpenDJ_*.jar before starting the upgraded server.
 
 INFO_LDAP_CONN_PROMPT_SECURITY_LDAP=LDAP
 INFO_LDAP_CONN_PROMPT_SECURITY_USE_SSL=LDAP with SSL

--
Gitblit v1.10.0