From cb4fffb7bf1f9de28183fe58376f5762b1e45cf9 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Fri, 20 Apr 2007 15:09:47 +0000
Subject: [PATCH] Fix for issue 1494: web start installer provides the option to create non-existent install path. 

---
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java                  |   68 ++++++++++++++++++++--
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java |   33 +++++++++++
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java                        |   25 ++++----
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/UserDataException.java                    |    2 
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/resources/Resources.properties            |   11 +++
 5 files changed, 118 insertions(+), 21 deletions(-)

diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/UserDataException.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/UserDataException.java
index 26df344..a02edd0 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/UserDataException.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/UserDataException.java
@@ -30,7 +30,7 @@
 /**
  * This exception is used when there is an error with the data provided by
  * the user.  It will be thrown by the class that is in charge of validating
- * the user data (QuickSetup).
+ * the user data (the Application class).
  *
  */
 public class UserDataException extends Exception
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
index 3a5a4c9..7ea13ae 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
@@ -906,8 +906,10 @@
    *
    */
   private void updateUserDataForServerSettingsPanel(QuickSetup qs)
-      throws UserDataException {
+      throws UserDataException
+  {
     ArrayList<String> errorMsgs = new ArrayList<String>();
+    String confirmationMsg = null;
 
     if (Utils.isWebStart())
     {
@@ -918,12 +920,61 @@
       {
         errorMsgs.add(getMsg("empty-server-location"));
         qs.displayFieldInvalid(FieldName.SERVER_LOCATION, true);
-      } else if (!Utils.parentDirectoryExists(serverLocation))
+      }
+      else if (!Utils.parentDirectoryExists(serverLocation))
       {
-        String[] arg =
-          { serverLocation };
-        errorMsgs.add(getMsg("parent-directory-does-not-exist", arg));
-        qs.displayFieldInvalid(FieldName.SERVER_LOCATION, true);
+        String existingParentDirectory = null;
+        File f = new File(serverLocation);
+        while ((existingParentDirectory == null) && (f != null))
+        {
+          f = f.getParentFile();
+          if ((f != null) && f.exists())
+          {
+            if (f.isDirectory())
+            {
+              existingParentDirectory = f.getAbsolutePath();
+            }
+            else
+            {
+              // The parent path is a file!
+              f = null;
+            }
+          }
+        }
+        if (existingParentDirectory == null)
+        {
+          String[] arg =
+            { serverLocation };
+          errorMsgs.add(getMsg("parent-directory-could-not-be-found", arg));
+          qs.displayFieldInvalid(FieldName.SERVER_LOCATION, true);
+        }
+        else
+        {
+          if (!Utils.canWrite(existingParentDirectory))
+          {
+            String[] arg =
+              { existingParentDirectory };
+            errorMsgs.add(getMsg("directory-not-writable", arg));
+            qs.displayFieldInvalid(FieldName.SERVER_LOCATION, true);
+          }
+          else if (!Utils.hasEnoughSpace(existingParentDirectory,
+              getRequiredInstallSpace()))
+          {
+            long requiredInMb = getRequiredInstallSpace() / (1024 * 1024);
+            String[] args =
+              { existingParentDirectory, String.valueOf(requiredInMb) };
+            errorMsgs.add(getMsg("not-enough-disk-space", args));
+            qs.displayFieldInvalid(FieldName.SERVER_LOCATION, true);
+          }
+          else
+          {
+            String[] arg =
+            { serverLocation };
+            confirmationMsg =
+              getMsg("parent-directory-does-not-exist-confirmation", arg);
+            getUserData().setServerLocation(serverLocation);
+          }
+        }
       } else if (Utils.fileExists(serverLocation))
       {
         String[] arg =
@@ -1113,6 +1164,11 @@
       throw new UserDataException(Step.SERVER_SETTINGS,
           Utils.getStringFromCollection(errorMsgs, "\n"));
     }
+    if (confirmationMsg != null)
+    {
+      throw new UserDataConfirmationException(Step.SERVER_SETTINGS,
+          confirmationMsg);
+    }
   }
 
   /**
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
index 8d16069..f1901dc 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
@@ -28,6 +28,7 @@
 package org.opends.quicksetup.installer.webstart;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
 import java.util.ArrayList;
@@ -111,6 +112,7 @@
       notifyListeners(getTaskSeparator());
 
       status = InstallProgressStep.EXTRACTING;
+      createParentDirectoryIfRequired();
       extractZipFiles(in, getRatio(InstallProgressStep.EXTRACTING),
           getRatio(InstallProgressStep.CONFIGURING_SERVER));
       notifyListeners(getTaskSeparator());
@@ -360,6 +362,37 @@
   }
 
   /**
+   * Creates the parent Directory for the server location if it does not exist.
+   * @throws QuickSetupException if something goes wrong.
+   */
+  private void createParentDirectoryIfRequired() throws QuickSetupException
+  {
+    String serverLocation = getUserData().getServerLocation();
+    if (!Utils.parentDirectoryExists(serverLocation))
+    {
+      File f = new File(serverLocation);
+      String parent = f.getParent();
+      try
+      {
+        if (!Utils.createDirectory(parent))
+        {
+          throw new QuickSetupException(
+              QuickSetupException.Type.FILE_SYSTEM_ERROR,
+              getMsg("error-could-not-create-parent-dir",
+                  new String[] {parent}), null);
+        }
+      }
+      catch (IOException ioe)
+      {
+        throw new QuickSetupException(
+            QuickSetupException.Type.FILE_SYSTEM_ERROR,
+            getMsg("error-could-not-create-parent-dir", new String[] {parent}),
+            ioe);
+      }
+    }
+  }
+
+  /**
    * This method extracts the zip file.
    * @param is the inputstream with the contents of the zip file.
    * @param minRatio the value of the ratio in the install that corresponds to
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/resources/Resources.properties b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/resources/Resources.properties
index 12008b6..4f0bdfe 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/resources/Resources.properties
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/resources/Resources.properties
@@ -235,6 +235,11 @@
 error-title=Error
 
 #
+# Confirmation dialog title
+#
+confirmation-title=Confirmation Required
+
+#
 # Error when we cannot launch the status panel
 #
 could-not-launch-status-panel-msg=An unexpected error occurred launching the \
@@ -275,7 +280,9 @@
 #
 # Server Settings
 empty-server-location=You must provide the installation path.
-parent-directory-does-not-exist=The parent directory of {0} does not exist.
+parent-directory-could-not-be-found=Could not find a parent directory for {0}.
+parent-directory-does-not-exist-confirmation=The parent directory of {0} does \
+not exist.\nWould you like to create this directory?
 file-exists=The file {0} already exists.
 directory-exists-not-empty=The directory {0} is not empty.
 directory-not-writable=You do not have write access on the directory {0}. \
@@ -737,6 +744,8 @@
 exception-details=Details: {0}
 exception-out-of-memory-details=Not enough memory to perform the operation.  \
 Details: {0}
+error-could-not-create-parent-dir=Could not create parent directory {0}.  \
+Check that you have file system access rights.  
 downloading-error=An error occurred downloading remote file(s) {0}.
 error-zipinputstreamnull=Could not retrieve zip file {0}.  The input stream \
 is null.
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java
index 1f198c9..4da9398 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/ui/QuickSetup.java
@@ -278,7 +278,18 @@
 
         if (throwable != null) {
           UserDataException ude = (UserDataException) throwable;
-          displayError(ude.getLocalizedMessage(), getMsg("error-title"));
+          if (ude instanceof UserDataConfirmationException)
+          {
+            if (displayConfirmation(ude.getLocalizedMessage(),
+                getMsg("confirmation-title")))
+            {
+              setCurrentStep(application.getNextWizardStep(cStep));
+            }
+          }
+          else
+          {
+            displayError(ude.getLocalizedMessage(), getMsg("error-title"));
+          }
         } else {
           setCurrentStep(application.getNextWizardStep(cStep));
         }
@@ -448,18 +459,6 @@
    * The following three methods are just commodity methods to get localized
    * messages.
    * @param key String key
-   * @param args String[] args
-   * @return String message
-   */
-  private String getMsg(String key, String[] args)
-  {
-    return getI18n().getMsg(key, args);
-  }
-
-  /**
-   * The following three methods are just commodity methods to get localized
-   * messages.
-   * @param key String key
    * @param t Throwable throwable
    * @return String message
    */

--
Gitblit v1.10.0