From 6f163823cb9ca5e313699c657bb23dc7b39b586f Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Wed, 27 Nov 2013 07:56:03 +0000
Subject: [PATCH] CR-2635 OPENDJ-1196 updateSchemaFile succeeds if it can't find schema in the templates

---
 opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java                  |   11 +++
 opends/src/server/org/opends/server/tools/upgrade/FileManager.java                   |    6 +
 opends/src/messages/messages/tools.properties                                        |    3 
 opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java                  |   14 ++++
 opends/src/server/org/opends/server/util/cli/ConsoleApplication.java                 |   13 +--
 opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java                    |    8 --
 opends/src/server/org/opends/server/tools/upgrade/FormattedNotificationCallback.java |    3 -
 opends/src/server/org/opends/server/tools/upgrade/Upgrade.java                       |  113 ++++++++++++++++++-------------------
 8 files changed, 90 insertions(+), 81 deletions(-)

diff --git a/opends/src/messages/messages/tools.properties b/opends/src/messages/messages/tools.properties
index c34dfd3..53bc168 100644
--- a/opends/src/messages/messages/tools.properties
+++ b/opends/src/messages/messages/tools.properties
@@ -2500,7 +2500,7 @@
 wish to continue?
 INFO_UPGRADE_ABORTED_BY_USER_1815=The upgrade has been canceled
 SEVERE_ERR_UPGRADE_UNKNOWN_OC_ATT_1816=The %s %s doesn't exist \
-in the template configuration
+in the template configuration (possible typo or template corruption)
 SEVERE_ERR_UPGRADE_CONFIG_ERROR_UPGRADE_FOLDER_1817=An error occurred when \
 trying to upgrade the config/upgrade folder: %s
 INFO_UPGRADE_REQUIREMENTS_1818=Preparing to upgrade
@@ -2550,6 +2550,7 @@
 SEVERE_ERR_UPGRADE_INVALID_LOG_FILE_1846=Invalid log file %s
 INFO_UPGRADE_REBUILD_INDEX_ARGUMENTS_1847=The rebuild index tool arguments are %s
 INFO_UPGRADE_REBUILD_ALL_1848=Rebuilding all indexes
+INFO_UPGRADE_PROCESS_END_1849=End of the upgrade process
 
 # 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/upgrade/FileManager.java b/opends/src/server/org/opends/server/tools/upgrade/FileManager.java
index 30bebc3..320049b 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/FileManager.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/FileManager.java
@@ -40,6 +40,7 @@
 import java.io.File;
 import java.io.FileFilter;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.logging.Logger;
@@ -401,11 +402,14 @@
               }
 
             }
+            catch (FileNotFoundException e)
+            {
+              throw new IOException(e.getMessage());
+            }
             catch (Exception e)
             {
               final Message errMsg = INFO_ERROR_COPYING_FILE.get(
                   objectFile.getAbsolutePath(), destination.getAbsolutePath());
-              LOG.log(Level.SEVERE, message.toString());
               throw new IOException(errMsg.toString());
             }
             finally
diff --git a/opends/src/server/org/opends/server/tools/upgrade/FormattedNotificationCallback.java b/opends/src/server/org/opends/server/tools/upgrade/FormattedNotificationCallback.java
index 62ade32..f7dab0d 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/FormattedNotificationCallback.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/FormattedNotificationCallback.java
@@ -52,9 +52,6 @@
   /** Output an error. */
   public static final int ERROR_CALLBACK = 8;
 
-  /** Output a line break. */
-  public static final int BREAKLINE = 9;
-
   /**
    * An integer representing the message's sub-type.
    */
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 8622fdc..13eeac3 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/Upgrade.java
@@ -27,8 +27,6 @@
 
 package org.opends.server.tools.upgrade;
 
-
-
 import static org.opends.messages.ToolMessages.*;
 import static org.opends.server.tools.upgrade.FormattedNotificationCallback.*;
 import static org.opends.server.tools.upgrade.UpgradeTasks.*;
@@ -53,8 +51,6 @@
 import org.opends.server.util.BuildVersion;
 import org.opends.server.util.StaticUtils;
 
-
-
 /**
  * This class contains the table of upgrade tasks that need performing when
  * upgrading from one version to another.
@@ -315,8 +311,6 @@
     // @formatter:on
   }
 
-
-
   /**
    * Returns a list containing all the tasks which are required in order to
    * upgrade from {@code fromVersion} to {@code toVersion}.
@@ -357,14 +351,15 @@
     isVersionCanBeUpdated(context);
 
     // Server must be offline.
-    checkIfServerIsRunning();
+    checkIfServerIsRunning(context);
 
-    context.notify( INFO_UPGRADE_TITLE.get(), TITLE_CALLBACK);
-    context.notify( INFO_UPGRADE_SUMMARY.get(context.getFromVersion()
-        .toString(), context.getToVersion().toString()), NOTICE_CALLBACK);
-    context.notify( INFO_UPGRADE_GENERAL_SEE_FOR_DETAILS
-        .get(UpgradeUtils.getInstallationPath() + File.separator
-            + UpgradeLog.UPGRADELOGNAME), NOTICE_CALLBACK);
+    context.notify(INFO_UPGRADE_TITLE.get(), TITLE_CALLBACK);
+    context.notify(
+        INFO_UPGRADE_SUMMARY.get(context.getFromVersion().toString(), context
+            .getToVersion().toString()), NOTICE_CALLBACK);
+    context.notify(INFO_UPGRADE_GENERAL_SEE_FOR_DETAILS.get(UpgradeUtils
+        .getInstallationPath()
+        + File.separator + UpgradeLog.UPGRADELOGNAME), NOTICE_CALLBACK);
 
     // Checks License.
     checkLicence(context);
@@ -372,8 +367,8 @@
     /*
      * Get the list of required upgrade tasks.
      */
-    final List<UpgradeTask> tasks = getUpgradeTasks(context.getFromVersion(),
-        context.getToVersion());
+    final List<UpgradeTask> tasks =
+        getUpgradeTasks(context.getFromVersion(), context.getToVersion());
     if (tasks.isEmpty())
     {
       changeBuildInfoVersion(context);
@@ -381,10 +376,9 @@
     }
 
     /*
-     * Verify tasks requirements.
-     * E.g. if a task requires mandatory user interaction
-     * and the application is non-interactive then, the process
-     * may abort immediately.
+     * Verify tasks requirements. E.g. if a task requires mandatory user
+     * interaction and the application is non-interactive then, the process may
+     * abort immediately.
      */
     for (final UpgradeTask task : tasks)
     {
@@ -401,12 +395,14 @@
     }
 
     // Starts upgrade
-    final int userResponse = context.confirmYN(
-        INFO_UPGRADE_DISPLAY_CONFIRM_START.get(), ConfirmationCallback.YES);
+    final int userResponse =
+        context.confirmYN(INFO_UPGRADE_DISPLAY_CONFIRM_START.get(),
+            ConfirmationCallback.YES);
     if (userResponse == ConfirmationCallback.NO)
     {
-      throw new ClientException(EXIT_CODE_ERROR,
-          INFO_UPGRADE_ABORTED_BY_USER.get());
+      final Message message = INFO_UPGRADE_ABORTED_BY_USER.get();
+      context.notify(message, WARNING);
+      throw new ClientException(EXIT_CODE_ERROR, message);
     }
 
     try
@@ -414,8 +410,7 @@
       /*
        * Perform the upgrade tasks.
        */
-      context.notify(INFO_UPGRADE_PERFORMING_TASKS.get(),
-          TITLE_CALLBACK);
+      context.notify(INFO_UPGRADE_PERFORMING_TASKS.get(), TITLE_CALLBACK);
       for (final UpgradeTask task : tasks)
       {
         task.perform(context);
@@ -424,9 +419,9 @@
       if (UpgradeTasks.countErrors == 0)
       {
         /*
-         * The end of a successful upgrade is marked up with the build info
-         * file update and the license, if present, requires the creation of
-         * an approval file.
+         * The end of a successful upgrade is marked up with the build info file
+         * update and the license, if present, requires the creation of an
+         * approval file.
          */
         changeBuildInfoVersion(context);
 
@@ -434,8 +429,8 @@
       }
       else
       {
-        context.notify(
-            ERR_UPGRADE_FAILS.get(UpgradeTasks.countErrors), TITLE_CALLBACK);
+        context.notify(ERR_UPGRADE_FAILS.get(UpgradeTasks.countErrors),
+            TITLE_CALLBACK);
       }
 
       /*
@@ -451,27 +446,26 @@
     }
     catch (final ClientException e)
     {
-      LOG.log(Level.SEVERE, e.getMessage());
-      context.notify( e.getMessageObject());
+      context.notify(e.getMessageObject(), ERROR_CALLBACK);
       throw e;
     }
     catch (final Exception e)
     {
-      LOG.log(Level.SEVERE, e.getMessage());
-      context.notify(ERR_UPGRADE_TASKS_FAIL.get(e.getMessage()));
-      throw new ClientException(EXIT_CODE_ERROR, Message.raw(e.getMessage()));
+      final Message message = ERR_UPGRADE_TASKS_FAIL.get(e.getMessage());
+      context.notify(message, ERROR_CALLBACK);
+      throw new ClientException(EXIT_CODE_ERROR, message);
     }
     finally
     {
-      context.notify(INFO_UPGRADE_GENERAL_SEE_FOR_DETAILS
-          .get(UpgradeUtils.getInstallationPath() + File.separator
-              + UpgradeLog.UPGRADELOGNAME), NOTICE_CALLBACK);
+      context.notify(INFO_UPGRADE_GENERAL_SEE_FOR_DETAILS.get(UpgradeUtils
+          .getInstallationPath()
+          + File.separator + UpgradeLog.UPGRADELOGNAME), NOTICE_CALLBACK);
+      LOG.log(Level.CONFIG, INFO_UPGRADE_PROCESS_END.get().toString());
     }
   }
 
   private static void performPostUpgradeTasks(final UpgradeContext context,
-      final List<UpgradeTask> tasks)
-      throws ClientException
+      final List<UpgradeTask> tasks) throws ClientException
   {
     boolean isOk = true;
     for (final UpgradeTask task : tasks)
@@ -518,7 +512,8 @@
    * @throws ClientException
    *           An exception is thrown if the server is currently running.
    */
-  private final static void checkIfServerIsRunning() throws ClientException
+  private final static void checkIfServerIsRunning(final UpgradeContext context)
+      throws ClientException
   {
     final String lockFile = LockFileManager.getServerLockFileName();
 
@@ -529,9 +524,9 @@
       // running.
       if (!LockFileManager.acquireExclusiveLock(lockFile, failureReason))
       {
-        LOG.log(Level.SEVERE, failureReason.toString());
-        throw new ClientException(EXIT_CODE_ERROR,
-            ERR_UPGRADE_REQUIRES_SERVER_OFFLINE.get());
+        final Message message = ERR_UPGRADE_REQUIRES_SERVER_OFFLINE.get();
+        context.notify(message, NOTICE_CALLBACK);
+        throw new ClientException(EXIT_CODE_ERROR, message);
       }
     }
     finally
@@ -540,8 +535,6 @@
     }
   }
 
-
-
   /**
    * Checks if the version can be updated.
    *
@@ -559,22 +552,23 @@
        * If the server is already up to date then treat it as a successful
        * upgrade so that upgrade is idempotent.
        */
-      final Message message = ERR_UPGRADE_VERSION_UP_TO_DATE.get(context
-          .getToVersion().toString());
+      final Message message =
+          ERR_UPGRADE_VERSION_UP_TO_DATE.get(context.getToVersion().toString());
+      context.notify(message, NOTICE_CALLBACK);
       throw new ClientException(EXIT_CODE_SUCCESS, message);
     }
 
     // The upgrade only supports version >= 2.4.5.
     if (context.getFromVersion().compareTo(UPGRADESUPPORTSVERSIONFROM) < 0)
     {
-      throw new ClientException(EXIT_CODE_ERROR,
+      final Message message =
           INFO_UPGRADE_VERSION_IS_NOT_SUPPORTED.get(UPGRADESUPPORTSVERSIONFROM
-              .toString(), UPGRADESUPPORTSVERSIONFROM.toString()));
+              .toString(), UPGRADESUPPORTSVERSIONFROM.toString());
+      context.notify(message, NOTICE_CALLBACK);
+      throw new ClientException(EXIT_CODE_ERROR, message);
     }
   }
 
-
-
   /**
    * Writes the up to date's version number within the build info file.
    *
@@ -591,20 +585,22 @@
     FileWriter buildInfo = null;
     try
     {
-      buildInfo = new FileWriter(new File(UpgradeUtils.configDirectory,
-          Installation.BUILDINFO_RELATIVE_PATH), false);
+      buildInfo =
+          new FileWriter(new File(UpgradeUtils.configDirectory,
+              Installation.BUILDINFO_RELATIVE_PATH), false);
 
       // Write the new version
       buildInfo.write(context.getToVersion().toString());
 
-      context.notify(INFO_UPGRADE_SUCCESSFUL.get(context
-          .getFromVersion().toString(), context.getToVersion().toString()),
-          TITLE_CALLBACK);
+      context.notify(INFO_UPGRADE_SUCCESSFUL.get(context.getFromVersion()
+          .toString(), context.getToVersion().toString()), TITLE_CALLBACK);
 
     }
     catch (IOException e)
     {
-      throw new ClientException(EXIT_CODE_ERROR, Message.raw(e.getMessage()));
+      final Message message = Message.raw(e.getMessage());
+      context.notify(message, ERROR_CALLBACK);
+      throw new ClientException(EXIT_CODE_ERROR, message);
     }
     finally
     {
@@ -612,7 +608,6 @@
     }
   }
 
-
   private static void checkLicence(final UpgradeContext context)
       throws ClientException
   {
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 2dd0f9c..2f195d5 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/UpgradeCli.java
@@ -368,15 +368,10 @@
     }
     catch (ClientException ex)
     {
-      LOG.log(SEVERE, ex.getMessage());
-      println(Style.ERROR, ex.getMessageObject(), 0);
-
       return ex.getExitCode();
     }
     catch (Exception ex)
     {
-      LOG.log(SEVERE, ERR_UPGRADE_MAIN_UPGRADE_PROCESS.get(ex
-          .getMessage()).toString());
       println(Style.ERROR, ERR_UPGRADE_MAIN_UPGRADE_PROCESS.get(ex
           .getMessage()), 0);
 
@@ -425,9 +420,6 @@
           println(Style.ERROR, Message.raw(fnc.getMessage()), 1);
           LOG.log(Level.SEVERE, fnc.getMessage());
           break;
-        case BREAKLINE:
-          println(Style.BREAKLINE, Message.raw(fnc.getMessage()), 1);
-          break;
         case WARNING:
           println(Style.WARNING, Message.raw(fnc.getMessage()), 2);
           LOG.log(Level.WARNING, fnc.getMessage());
diff --git a/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java b/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
index 4c90136..09d7875 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/UpgradeTasks.java
@@ -47,6 +47,7 @@
 import javax.security.auth.callback.ConfirmationCallback;
 import javax.security.auth.callback.TextOutputCallback;
 
+import org.forgerock.opendj.ldap.schema.UnknownSchemaElementException;
 import org.opends.messages.Message;
 import org.opends.server.protocols.ldap.LDAPFilter;
 import org.opends.server.tools.ClientException;
@@ -300,6 +301,11 @@
           manageTaskException(context, ERR_UPGRADE_ADDATTRIBUTE_FAILS.get(
               schemaFileTemplate.getName(), e.getMessage()), pnc);
         }
+        catch (final UnknownSchemaElementException e)
+        {
+          manageTaskException(context, ERR_UPGRADE_ADDATTRIBUTE_FAILS.get(
+              schemaFileTemplate.getName(), e.getMessage()), pnc);
+        }
       }
     };
   }
@@ -356,6 +362,11 @@
           manageTaskException(context, ERR_UPGRADE_ADDOBJECTCLASS_FAILS.get(
               schemaFileTemplate.getName(), e.getMessage()), pnc);
         }
+        catch (final UnknownSchemaElementException e)
+        {
+          manageTaskException(context, ERR_UPGRADE_ADDOBJECTCLASS_FAILS.get(
+              schemaFileTemplate.getName(), e.getMessage()), pnc);
+        }
       }
     };
   }
diff --git a/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java b/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java
index 9d77810..acbddac 100644
--- a/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java
+++ b/opends/src/server/org/opends/server/tools/upgrade/UpgradeUtils.java
@@ -566,10 +566,14 @@
    *         is inserted successfully to the destination file.
    * @throws IOException
    *           If an unexpected IO error occurred while reading the entry.
+   * @throws UnknownSchemaElementException
+   *           Failure to find an attribute in the template schema indicates
+   *           either a programming error (e.g. typo in the attribute name) or
+   *           template corruption. Upgrade should stop.
    */
   static int updateSchemaFile(final File templateFile, final File destination,
       final String[] attributes, final String[] objectClasses)
-      throws IOException
+      throws IOException, UnknownSchemaElementException
   {
     int changeCount = 0;
     LDIFEntryReader reader = null;
@@ -582,6 +586,12 @@
     {
       reader = new LDIFEntryReader(new FileInputStream(templateFile));
 
+      if (!reader.hasNext())
+      {
+        // Unless template are corrupted, this should not happen.
+        throw new IOException(String.format(
+            "'%s' file is empty. Template corrupted.", templateFile.getName()));
+      }
       final LinkedList<String> definitionsList = new LinkedList<String>();
 
       final Entry schemaEntry = reader.readEntry();
@@ -604,6 +614,7 @@
           {
             LOG.log(Level.SEVERE, ERR_UPGRADE_UNKNOWN_OC_ATT.get("attribute",
                 att).toString());
+            throw e;
           }
         }
       }
@@ -623,6 +634,7 @@
           {
             LOG.log(Level.SEVERE, ERR_UPGRADE_UNKNOWN_OC_ATT.get(
                 "object class", oc).toString());
+            throw e;
           }
         }
       }
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 e09a564..d26d78e 100644
--- a/opends/src/server/org/opends/server/util/cli/ConsoleApplication.java
+++ b/opends/src/server/org/opends/server/util/cli/ConsoleApplication.java
@@ -126,10 +126,6 @@
      */
     ERROR,
     /**
-     * Defines a breakline.
-     */
-    BREAKLINE,
-    /**
      * Defines a warning.
      */
     WARNING
@@ -448,8 +444,6 @@
       case ERROR:
         out.println();
         out.println(wrapText("** " + msg, MAX_LINE_WIDTH, indent));
-        break;
-      case BREAKLINE:
         out.println();
         break;
       case WARNING:
@@ -537,9 +531,12 @@
         }
       }
       bar.append(".   ");
-      if(progress >= 0) {
+      if (progress >= 0)
+      {
         bar.append(progress).append("%     ");
-      } else {
+      }
+      else
+      {
         bar.append("FAIL");
       }
       final int endBuilder = linePos + bar.length();

--
Gitblit v1.10.0