From ecd6711981fbd48a4a9bc376377ba6cc86c0c3fb Mon Sep 17 00:00:00 2001
From: kenneth_suter <kenneth_suter@localhost>
Date: Fri, 13 Apr 2007 15:36:41 +0000
Subject: [PATCH] This commit addresses addresses problems with the file system locking files that arose from testing the upgrader on Windows.

---
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/upgrader/Upgrader.java |   18 +++++++--
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/FileManager.java  |   27 ++++++++++---
 opendj-sdk/opends/resource/upgrade.bat                                        |   24 ++++++++----
 3 files changed, 50 insertions(+), 19 deletions(-)

diff --git a/opendj-sdk/opends/resource/upgrade.bat b/opendj-sdk/opends/resource/upgrade.bat
index 1e0bd31..ea1e591 100644
--- a/opendj-sdk/opends/resource/upgrade.bat
+++ b/opendj-sdk/opends/resource/upgrade.bat
@@ -32,28 +32,25 @@
 
 :checkJavaBin
 if "%JAVA_BIN%" == "" goto noJavaBin
-goto setClassPath
+goto callExtractor
 
 :noJavaBin
 if "%JAVA_HOME%" == "" goto noJavaHome
 if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
 set JAVA_BIN=%JAVA_HOME%\bin\java.exe
-goto setClassPath
+goto callExtractor
 
 :noJavaHome
 if not exist "%DIR_HOME%\bat\set-java-home.bat" goto noSetJavaHome
 call "%DIR_HOME%\bat\set-java-home.bat"
 set JAVA_BIN=%JAVA_HOME%\bin\java.exe
-goto setClassPath
+goto callExtractor
 
 :noSetJavaHome
 echo Error: JAVA_HOME environment variable is not set.
 echo        Please set it to a valid Java 5 installation.
 goto end
 
-:setClassPath
-FOR %%x in ("%DIR_HOME%\lib\*.jar") DO call "%DIR_HOME%\bat\setcp.bat" %%x
-
 set PATH=%SystemRoot%
 
 if "%*" == "" goto callLaunch
@@ -63,8 +60,19 @@
 "%DIR_HOME%\lib\winlauncher.exe" launch "%DIR_HOME%" "%JAVA_BIN%" %JAVA_ARGS% org.opends.quicksetup.upgrader.UpgradeLauncher
 goto end
 
-:callJava
-"%JAVA_BIN%" %JAVA_ARGS% org.opends.quicksetup.upgrader.UpgradeLauncher %*
+:callExtractor
+if EXIST .\tmp\upgrade rd .\tmp\upgrade /s /q
+set CLASSPATH=""
+FOR %%x in ("%DIR_HOME%\lib\*.jar") DO call "%DIR_HOME%\bat\setcp.bat" %%x
+"%JAVA_BIN%" org.opends.quicksetup.upgrader.BuildExtractor %*
+if %ERRORLEVEL%==0 goto callUpgrader
+goto end
+
+:callUpgrader
+set CLASSPATH=""
+FOR %%x in ("%DIR_HOME%\tmp\upgrade\lib\*.jar") DO call "%DIR_HOME%\bat\setcp.bat" %%x
+"%JAVA_BIN%" org.opends.quicksetup.upgrader.UpgradeLauncher %*
+if EXIST .\tmp\upgrade rd .\tmp\upgrade /s /q
 goto end
 
 :end
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/upgrader/Upgrader.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/upgrader/Upgrader.java
index fb75387..7c55a5c 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/upgrader/Upgrader.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/upgrader/Upgrader.java
@@ -233,7 +233,15 @@
    * {@inheritDoc}
    */
   protected String getInstallationPath() {
-    return Utils.getInstallPathFromClasspath();
+    // The upgrader runs from the bits extracted by BuildExtractor
+    // in the staging directory.  So 'stagePath' below will point
+    // to the staging directory [installroot]/tmp/upgrade.  However
+    // we still want the Installation to point at the build being
+    // upgraded so the install path reported in [installroot].
+
+    String stagePath = Utils.getInstallPathFromClasspath();
+    File f = new File(stagePath);
+    return Utils.getPath(f.getParentFile().getParentFile());
   }
 
   /**
@@ -924,9 +932,11 @@
       stagingDir = getStageDirectory();
       FileManager fm = new FileManager(this);
 
-      // doing this seems to work betterh than just plain
-      // old delete
-      fm.deleteRecursively(stagingDir);
+      // Doing this seems to work better than just plain
+      // old delete.  Note that on Windows there are file
+      // locking issues to we mark files for deletion after
+      // this JVM exits
+      fm.deleteRecursively(stagingDir, null, /*onExit=*/true);
 
     } catch (IOException e) {
       // TODO i18n
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/FileManager.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/FileManager.java
index fa1d269..6f8da78 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/FileManager.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/FileManager.java
@@ -77,7 +77,7 @@
           throws ApplicationException
   {
     if (filter == null || filter.accept(object)) {
-      new DeleteOperation(object).apply();
+      new DeleteOperation(object, false).apply();
     }
   }
 
@@ -88,7 +88,7 @@
    * @throws org.opends.quicksetup.ApplicationException if something goes wrong.
    */
   public void deleteRecursively(File file) throws ApplicationException {
-    deleteRecursively(file, null);
+    deleteRecursively(file, null, false);
   }
 
   /**
@@ -97,11 +97,13 @@
    * @param file   the path to be deleted.
    * @param filter the filter of the files to know if the file can be deleted
    *               directly or not.
+   * @param onExit when true just marks the files for deletion after the
+   *        JVM exits rather than deleting the files immediately.
    * @throws ApplicationException if something goes wrong.
    */
-  public void deleteRecursively(File file, FileFilter filter)
+  public void deleteRecursively(File file, FileFilter filter, boolean onExit)
           throws ApplicationException {
-    operateRecursively(new DeleteOperation(file), filter);
+    operateRecursively(new DeleteOperation(file, onExit), filter);
   }
 
   /**
@@ -351,19 +353,25 @@
    */
   private class DeleteOperation extends FileOperation {
 
+    private boolean afterExit;
+
     /**
      * Creates a delete operation.
      * @param objectFile to delete
+     * @param afterExit boolean indicates that the actual delete
+     * is to take place after this program exists.  This is useful
+     * for cleaning up files that are currently in use.
      */
-    public DeleteOperation(File objectFile) {
+    public DeleteOperation(File objectFile, boolean afterExit) {
       super(objectFile);
+      this.afterExit = afterExit;
     }
 
     /**
      * {@inheritDoc}
      */
     public FileOperation copyForChild(File child) {
-      return new DeleteOperation(child);
+      return new DeleteOperation(child, afterExit);
     }
 
     /**
@@ -390,7 +398,12 @@
        */
       int nTries = 5;
       for (int i = 0; i < nTries && !delete; i++) {
-        delete = file.delete();
+        if (afterExit) {
+          file.deleteOnExit();
+          delete = true;
+        } else {
+          delete = file.delete();
+        }
         if (!delete) {
           try {
             Thread.sleep(1000);

--
Gitblit v1.10.0