From bb15aa09f9dd807bb3f5710c53b78062befa35b0 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Mon, 22 Oct 2007 15:48:15 +0000
Subject: [PATCH] Fix for issue 2483: Bad management of lib/set-java-home when java path changed

---
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java |    6 +
 opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java             |  103 ++++++++++++++++++++++++++++++----
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/Installation.java        |    4 +
 opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java             |   42 ++++++++++++--
 4 files changed, 133 insertions(+), 22 deletions(-)

diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/Installation.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/Installation.java
index 742d070..ac65361 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/Installation.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/Installation.java
@@ -39,6 +39,7 @@
 import java.util.concurrent.ExecutionException;
 
 import org.opends.quicksetup.util.Utils;
+import org.opends.server.util.SetupUtils;
 
 /**
  * This class represents the physical state of an OpenDS installation.
@@ -71,7 +72,8 @@
   /**
    * The relative path where all the libraries (jar files) are.
    */
-  public static final String LIBRARIES_PATH_RELATIVE = "lib";
+  public static final String LIBRARIES_PATH_RELATIVE =
+    SetupUtils.LIBRARIES_PATH_RELATIVE;
 
   /**
    * The relative path where the database files are.
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 116ddc4..812d1c5 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
@@ -1758,8 +1758,10 @@
     {
       // This isn't likely to happen, and it's not a serious problem even if
       // it does.
-      SetupUtils.writeSetOpenDSJavaHome(getInstallationPath());
-    } catch (Exception e) {}
+      SetupUtils.writeSetOpenDSJavaHome(getInstallationPath(), false);
+    } catch (Exception e) {
+      LOG.log(Level.WARNING, "Error writing OpenDS Java Home file: "+e, e);
+    }
   }
 
   /**
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java b/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java
index 121a1ad..04be7cc 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java
@@ -38,6 +38,7 @@
 import java.security.KeyStoreException;
 import java.util.Collection;
 import java.util.LinkedList;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.opends.messages.Message;
@@ -282,12 +283,42 @@
       return ErrorReturnCode.ERROR_USER_DATA.getReturnCode();
     }
 
-    // If we are on test only mode, delete the log file that does not contain
-    // any information.  The test only mode is called several times by the setup
-    // script and if we do not remove it we have a lot of empty log files.
+    // If we are on test only mode, try to see if the contents of the
+    // set-java-home file are valid.  If they are not try to update them, if no
+    // problem occurred while doing this delete the log file that does not
+    // contain any information.  The test only mode is called several times by
+    // the setup script and if we do not remove it we have a lot of empty log
+    // files.
     if (argParser.testOnlyArg.isPresent())
     {
-      QuickSetupLog.getLogFile().deleteOnExit();
+      try
+      {
+        String serverRoot = Utils.getInstallPathFromClasspath();
+        String javaHome = SetupUtils.getOpenDSJavaHome(serverRoot);
+        boolean writeJavaHome = false;
+        if (javaHome != null)
+        {
+          File f = new File(javaHome);
+          if (!f.exists())
+          {
+            writeJavaHome = true;
+          }
+        }
+        else
+        {
+          writeJavaHome = true;
+        }
+        if (writeJavaHome)
+        {
+          SetupUtils.writeSetOpenDSJavaHome(serverRoot, true);
+        }
+        QuickSetupLog.getLogFile().deleteOnExit();
+      }
+      catch (Throwable t)
+      {
+        LOG.log(Level.WARNING, "Error while trying to update the contents of "+
+            "the set-java-home file in test only mode: "+t, t);
+      }
     }
 
     // If either the showUsage or testOnly or version arguments were provided,
@@ -340,10 +371,9 @@
         return ErrorReturnCode.ERROR_USER_DATA.getReturnCode();
       }
     }
-
+    System.setProperty(Constants.CLI_JAVA_PROPERTY, "true");
     OfflineInstaller installer = new OfflineInstaller();
     installer.setUserData(uData);
-    System.setProperty(Constants.CLI_JAVA_PROPERTY, "true");
     installer.setProgressMessageFormatter(formatter);
     installer.addProgressUpdateListener(
         new ProgressUpdateListener() {
diff --git a/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java b/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java
index 8351fb5..963d434 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java
@@ -28,8 +28,10 @@
 
 
 
+import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.net.InetSocketAddress;
@@ -78,6 +80,14 @@
       "org.opends.quicksetup.zipfilename";
 
   /**
+   * The relative path where all the libraries (jar files) are.
+   */
+  public static final String LIBRARIES_PATH_RELATIVE = "lib";
+
+  private static final String SET_JAVA_HOME_UNIX = "set-java-home";
+  private static final String SET_JAVA_HOME_WINDOWS = "set-java-home.bat";
+
+  /**
    * Creates a MakeLDIF template file using the provided information.
    *
    * @param  baseDN      The base DN for the data in the template file.
@@ -239,54 +249,78 @@
    * @param  serverRoot  The path to the root of the Directory Server instance
    *                     for which the file will be written.
    *
+   * @param overWrite when the set-java-home file exists whether to overwrite it
+   *        or not.
    * @return  A handle to the {@code File} object that has been written.
    *
    * @throws  IOException  If a problem occurs while creating or writing to the
    *                       specified file.
    */
-  public static File writeSetOpenDSJavaHome(String serverRoot)
-         throws IOException
+  public static File writeSetOpenDSJavaHome(String serverRoot,
+      boolean overWrite) throws IOException
   {
-    String javaHome = System.getenv("OPENDS_JAVA_HOME");
+    String javaHome = System.getenv(OPENDS_JAVA_HOME);
     if ((javaHome == null) || (javaHome.length() == 0))
     {
       javaHome = System.getProperty("java.home");
     }
 
 
-    File libDirectory = new File(serverRoot, "lib");
+    File libDirectory = new File(serverRoot, LIBRARIES_PATH_RELATIVE);
 
     File setJavaHomeFile;
     if (isWindows())
     {
-      setJavaHomeFile = new File(libDirectory, "set-java-home.bat");
+      setJavaHomeFile = new File(libDirectory, SET_JAVA_HOME_WINDOWS);
       if (setJavaHomeFile.exists())
       {
-        return setJavaHomeFile;
+        if (!overWrite)
+        {
+          return setJavaHomeFile;
+        }
+        else
+        {
+          File f1 = new File(javaHome);
+          File f2 = new File(getOpenDSJavaHome(serverRoot));
+          if (f1.equals(f2))
+          {
+            return setJavaHomeFile;
+          }
+        }
       }
-
       BufferedWriter writer =
            new BufferedWriter(new FileWriter(setJavaHomeFile));
-      writer.write("set OPENDS_JAVA_HOME=" + javaHome);
+      writer.write("set "+OPENDS_JAVA_HOME+"=" + javaHome);
       writer.newLine();
       writer.close();
     }
     else
     {
-      setJavaHomeFile = new File(libDirectory, "set-java-home");
+      setJavaHomeFile = new File(libDirectory, SET_JAVA_HOME_UNIX);
       if (setJavaHomeFile.exists())
       {
-        return setJavaHomeFile;
+        if (!overWrite)
+        {
+          return setJavaHomeFile;
+        }
+        else
+        {
+          File f1 = new File(javaHome);
+          File f2 = new File(getOpenDSJavaHome(serverRoot));
+          if (f1.equals(f2))
+          {
+            return setJavaHomeFile;
+          }
+        }
       }
-
       BufferedWriter writer =
            new BufferedWriter(new FileWriter(setJavaHomeFile));
       writer.write("#!/bin/sh");
       writer.newLine();
       writer.newLine();
-      writer.write("OPENDS_JAVA_HOME=" + javaHome);
+      writer.write(OPENDS_JAVA_HOME+"=" + javaHome);
       writer.newLine();
-      writer.write("export OPENDS_JAVA_HOME");
+      writer.write("export "+OPENDS_JAVA_HOME);
       writer.newLine();
       writer.close();
     }
@@ -295,6 +329,49 @@
   }
 
   /**
+   * Returns the java home value as it is specified in the set-java-home file.
+   * It returns <CODE>null</CODE> if the contents of the file are not valid, the
+   * file could not be read or if the file does not exist.
+   * @param  serverRoot  The path to the root of the Directory Server instance
+   *                     in which the set-java-home file is located.
+   * @return the java home value as it is specified in the set-java-home file.
+   * @throws  IOException  If a problem occurs while reading the file.
+   */
+  public static String getOpenDSJavaHome(String serverRoot) throws IOException
+  {
+    String javaHome = null;
+
+    File libDirectory = new File(serverRoot, LIBRARIES_PATH_RELATIVE);
+
+    File setJavaHomeFile;
+    if (isWindows())
+    {
+      setJavaHomeFile = new File(libDirectory, SET_JAVA_HOME_WINDOWS);
+    }
+    else
+    {
+      setJavaHomeFile = new File(libDirectory, SET_JAVA_HOME_UNIX);
+    }
+    if (setJavaHomeFile.exists())
+    {
+      BufferedReader reader =
+        new BufferedReader(new FileReader(setJavaHomeFile));
+      String line = reader.readLine();
+      String tag = OPENDS_JAVA_HOME+"=";
+      while ((line != null) && (javaHome == null))
+      {
+        int index = line.indexOf(tag);
+        if (index != -1)
+        {
+          javaHome = line.substring(index + tag.length());
+        }
+        line = reader.readLine();
+      }
+    }
+    return javaHome;
+  }
+
+  /**
    * Returns {@code true} if the provided port is free and we can use it,
    * {@code false} otherwise.
    * @param hostname the host name we are analyzing.

--
Gitblit v1.10.0