From 178d9f0be9ba1b06ac124aa71cbaec625e24ebb3 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Mon, 11 Dec 2006 03:15:28 +0000
Subject: [PATCH] Update the Java Web Start, GUI, and CLI setup modes to write a shell script or batch file that can be used to set JAVA_HOME.  Update all of the tools and administrative scripts provided with OpenDS so that they will use this to set JAVA_HOME unless it's already set in the user's environment.

---
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java                  |    4 
 opendj-sdk/opends/resource/bin/_server-script.bat                                                |   11 
 opendj-sdk/opends/resource/bin/stop-ds                                                           |   47 ++-
 opendj-sdk/opends/build.xml                                                                      |    4 
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java   |    8 
 opendj-sdk/opends/resource/bin/start-ds                                                          |   47 ++-
 opendj-sdk/opends/resource/bin/start-ds.bat                                                      |   10 
 opendj-sdk/opends/resource/setup.bat                                                             |    4 
 /dev/null                                                                                        |  119 ----------
 opendj-sdk/opends/resource/bin/_server-script.sh                                                 |   47 ++-
 opendj-sdk/opends/resource/bin/_client-script.bat                                                |   10 
 opendj-sdk/opends/resource/bin/_client-script.sh                                                 |   47 ++-
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java |    8 
 opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java                              |  204 ++++++++++++++++++
 opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java                              |   32 +-
 15 files changed, 379 insertions(+), 223 deletions(-)

diff --git a/opendj-sdk/opends/build.xml b/opendj-sdk/opends/build.xml
index ada4174..10c2524 100644
--- a/opendj-sdk/opends/build.xml
+++ b/opendj-sdk/opends/build.xml
@@ -319,7 +319,7 @@
     </copy>
     <copy todir="${quicksetup.classes.dir}">
       <fileset dir="${classes.dir}"
-               includes="**/DynamicConstants.class **/CreateTemplate.class" />
+               includes="**/DynamicConstants.class **/SetupUtils.class" />
     </copy>
   </target>
 
@@ -395,7 +395,7 @@
 
     <copy todir="${quicksetup.classes.dir}">
         <fileset dir="${classes.dir}"
-            includes="**/DynamicConstants.class **/CreateTemplate.class"/>
+            includes="**/DynamicConstants.class **/SetupUtils.class"/>
     </copy>
 
     <!-- Generate the quicksetup.jar file -->
diff --git a/opendj-sdk/opends/resource/bin/_client-script.bat b/opendj-sdk/opends/resource/bin/_client-script.bat
index b9c04e9..9ac82e1 100644
--- a/opendj-sdk/opends/resource/bin/_client-script.bat
+++ b/opendj-sdk/opends/resource/bin/_client-script.bat
@@ -47,10 +47,16 @@
 :noJavaBin
 if "%JAVA_HOME%" == "" goto noJavaHome
 if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
-set JAVA_BIN="%JAVA_HOME%\bin\java.exe"
+set JAVA_BIN=%JAVA_HOME%\bin\java.exe
 goto setClassPath
 
 :noJavaHome
+if not exist "%DIR_HOME%\bin\set-java-home.bat" goto noSetJavaHome
+call "%DIR_HOME%\bin\set-java-home.bat"
+set JAVA_BIN=%JAVA_HOME%\bin\java.exe
+goto setClassPath
+
+:noSetJavaHome
 echo Error: JAVA_HOME environment variable is not set.
 echo        Please set it to a valid Java 5 installation.
 goto end
@@ -61,7 +67,7 @@
 
 set PATH=%SystemRoot%
 
-%JAVA_BIN% %JAVA_ARGS% %SCRIPT_NAME_ARG% %OPENDS_INVOKE_CLASS% %*
+"%JAVA_BIN%" %JAVA_ARGS% %SCRIPT_NAME_ARG% %OPENDS_INVOKE_CLASS% %*
 
 
 :end
diff --git a/opendj-sdk/opends/resource/bin/_client-script.sh b/opendj-sdk/opends/resource/bin/_client-script.sh
index b4723f4..2d95471 100755
--- a/opendj-sdk/opends/resource/bin/_client-script.sh
+++ b/opendj-sdk/opends/resource/bin/_client-script.sh
@@ -35,22 +35,44 @@
 fi
 
 
+# Capture the current working directory so that we can change to it later.
+# Then capture the location of this script and the Directory Server instance
+# root so that we can use them to create appropriate paths.
+WORKING_DIR=`pwd`
+
+cd `dirname "${0}"`
+SCRIPT_DIR=`pwd`
+
+cd ..
+INSTANCE_ROOT=`pwd`
+export INSTANCE_ROOT
+
+cd "${WORKING_DIR}"
+
+
 # See if JAVA_HOME is set.  If not, then see if there is a java executable in
 # the path and try to figure it out.
 if test -z "${JAVA_BIN}"
 then
   if test -z "${JAVA_HOME}"
   then
-    JAVA_BIN=`which java 2> /dev/null`
-    if test ${?} -eq 0
+    if test -f "${INSTANCE_ROOT}/bin/set-java-home"
     then
+      . "${INSTANCE_ROOT}/bin/set-java-home"
+      JAVA_BIN="${JAVA_HOME}/bin/java"
       export JAVA_BIN
     else
-      echo "Please set JAVA_HOME to the root of a Java 5.0 installation."
-      exit 1
+      JAVA_BIN=`which java 2> /dev/null`
+      if test ${?} -eq 0
+      then
+        export JAVA_BIN
+      else
+        echo "Please set JAVA_HOME to the root of a Java 5.0 installation."
+        exit 1
+      fi
     fi
   else
-    JAVA_BIN=${JAVA_HOME}/bin/java
+    JAVA_BIN="${JAVA_HOME}/bin/java"
     export JAVA_BIN
   fi
 fi
@@ -69,21 +91,6 @@
        LD_PRELOAD LD_PRELOAD_32 LD_PRELOAD_34
 
 
-# Capture the current working directory so that we can change to it later.
-# Then capture the location of this script and the Directory Server instance
-# root so that we can use them to create appropriate paths.
-WORKING_DIR=`pwd`
-
-cd `dirname "${0}"`
-SCRIPT_DIR=`pwd`
-
-cd ..
-INSTANCE_ROOT=`pwd`
-export INSTANCE_ROOT
-
-cd "${WORKING_DIR}"
-
-
 # Configure the appropriate CLASSPATH.
 CLASSPATH=${INSTANCE_ROOT}/classes
 for JAR in ${INSTANCE_ROOT}/lib/*.jar
diff --git a/opendj-sdk/opends/resource/bin/_server-script.bat b/opendj-sdk/opends/resource/bin/_server-script.bat
index 908963e..7905601 100644
--- a/opendj-sdk/opends/resource/bin/_server-script.bat
+++ b/opendj-sdk/opends/resource/bin/_server-script.bat
@@ -47,21 +47,26 @@
 :noJavaBin
 if "%JAVA_HOME%" == "" goto noJavaHome
 if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
-set JAVA_BIN="%JAVA_HOME%\bin\java.exe"
+set JAVA_BIN=%JAVA_HOME%\bin\java.exe
 goto setClassPath
 
 :noJavaHome
+if not exist "%DIR_HOME%\bin\set-java-home.bat" goto noSetJavaHome
+call "%DIR_HOME%\bin\set-java-home.bat"
+set JAVA_BIN=%JAVA_HOME%\bin\java.exe
+goto setClassPath
+
+: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%\bin\setcp.bat" %%x
 
 set PATH=%SystemRoot%
 
-%JAVA_BIN% %JAVA_ARGS% %SCRIPT_NAME_ARG% %OPENDS_INVOKE_CLASS% --configClass org.opends.server.extensions.ConfigFileHandler --configFile "%DIR_HOME%\config\config.ldif" %*
+"%JAVA_BIN%" %JAVA_ARGS% %SCRIPT_NAME_ARG% %OPENDS_INVOKE_CLASS% --configClass org.opends.server.extensions.ConfigFileHandler --configFile "%DIR_HOME%\config\config.ldif" %*
 
 
 :end
diff --git a/opendj-sdk/opends/resource/bin/_server-script.sh b/opendj-sdk/opends/resource/bin/_server-script.sh
index 0629d88..8c42d8e 100755
--- a/opendj-sdk/opends/resource/bin/_server-script.sh
+++ b/opendj-sdk/opends/resource/bin/_server-script.sh
@@ -35,22 +35,44 @@
 fi
 
 
+# Capture the current working directory so that we can change to it later.
+# Then capture the location of this script and the Directory Server instance
+# root so that we can use them to create appropriate paths.
+WORKING_DIR=`pwd`
+
+cd `dirname "${0}"`
+SCRIPT_DIR=`pwd`
+
+cd ..
+INSTANCE_ROOT=`pwd`
+export INSTANCE_ROOT
+
+cd "${WORKING_DIR}"
+
+
 # See if JAVA_HOME is set.  If not, then see if there is a java executable in
 # the path and try to figure it out.
 if test -z "${JAVA_BIN}"
 then
   if test -z "${JAVA_HOME}"
   then
-    JAVA_BIN=`which java 2> /dev/null`
-    if test ${?} -eq 0
+    if test -f "${INSTANCE_ROOT}/bin/set-java-home"
     then
+      . "${INSTANCE_ROOT}/bin/set-java-home"
+      JAVA_BIN="${JAVA_HOME}/bin/java"
       export JAVA_BIN
     else
-      echo "Please set JAVA_HOME to the root of a Java 5.0 installation."
-      exit 1
+      JAVA_BIN=`which java 2> /dev/null`
+      if test ${?} -eq 0
+      then
+        export JAVA_BIN
+      else
+        echo "Please set JAVA_HOME to the root of a Java 5.0 installation."
+        exit 1
+      fi
     fi
   else
-    JAVA_BIN=${JAVA_HOME}/bin/java
+    JAVA_BIN="${JAVA_HOME}/bin/java"
     export JAVA_BIN
   fi
 fi
@@ -69,21 +91,6 @@
        LD_PRELOAD LD_PRELOAD_32 LD_PRELOAD_34
 
 
-# Capture the current working directory so that we can change to it later.
-# Then capture the location of this script and the Directory Server instance
-# root so that we can use them to create appropriate paths.
-WORKING_DIR=`pwd`
-
-cd `dirname "${0}"`
-SCRIPT_DIR=`pwd`
-
-cd ..
-INSTANCE_ROOT=`pwd`
-export INSTANCE_ROOT
-
-cd "${WORKING_DIR}"
-
-
 # Configure the appropriate CLASSPATH.
 CLASSPATH=${INSTANCE_ROOT}/classes
 for JAR in ${INSTANCE_ROOT}/lib/*.jar
diff --git a/opendj-sdk/opends/resource/bin/start-ds b/opendj-sdk/opends/resource/bin/start-ds
index f2bee71..9921cd0 100755
--- a/opendj-sdk/opends/resource/bin/start-ds
+++ b/opendj-sdk/opends/resource/bin/start-ds
@@ -26,22 +26,44 @@
 #      Portions Copyright 2006 Sun Microsystems, Inc.
 
 
+# Capture the current working directory so that we can change to it later.
+# Then capture the location of this script and the Directory Server instance
+# root so that we can use them to create appropriate paths.
+WORKING_DIR=`pwd`
+
+cd `dirname "${0}"`
+SCRIPT_DIR=`pwd`
+
+cd ..
+INSTANCE_ROOT=`pwd`
+export INSTANCE_ROOT
+
+cd "${WORKING_DIR}"
+
+
 # See if JAVA_HOME is set.  If not, then see if there is a java executable in
 # the path and try to figure it out.
 if test -z "${JAVA_BIN}"
 then
   if test -z "${JAVA_HOME}"
   then
-    JAVA_BIN=`which java 2> /dev/null`
-    if test ${?} -eq 0
+    if test -f "${INSTANCE_ROOT}/bin/set-java-home"
     then
+      . "${INSTANCE_ROOT}/bin/set-java-home"
+      JAVA_BIN="${JAVA_HOME}/bin/java"
       export JAVA_BIN
     else
-      echo "Please set JAVA_HOME to the root of a Java 5.0 installation."
-      exit 1
+      JAVA_BIN=`which java 2> /dev/null`
+      if test ${?} -eq 0
+      then
+        export JAVA_BIN
+      else
+        echo "Please set JAVA_HOME to the root of a Java 5.0 installation."
+        exit 1
+      fi
     fi
   else
-    JAVA_BIN=${JAVA_HOME}/bin/java
+    JAVA_BIN="${JAVA_HOME}/bin/java"
     export JAVA_BIN
   fi
 fi
@@ -60,21 +82,6 @@
        LD_PRELOAD LD_PRELOAD_32 LD_PRELOAD_34
 
 
-# Capture the current working directory so that we can change to it later.
-# Then capture the location of this script and the Directory Server instance
-# root so that we can use them to create appropriate paths.
-WORKING_DIR=`pwd`
-
-cd `dirname "${0}"`
-SCRIPT_DIR=`pwd`
-
-cd ..
-INSTANCE_ROOT=`pwd`
-export INSTANCE_ROOT
-
-cd "${WORKING_DIR}"
-
-
 # Configure the appropriate CLASSPATH.
 CLASSPATH=${INSTANCE_ROOT}/classes
 for JAR in ${INSTANCE_ROOT}/lib/*.jar
diff --git a/opendj-sdk/opends/resource/bin/start-ds.bat b/opendj-sdk/opends/resource/bin/start-ds.bat
index 11ec3e7..429cca9 100644
--- a/opendj-sdk/opends/resource/bin/start-ds.bat
+++ b/opendj-sdk/opends/resource/bin/start-ds.bat
@@ -30,6 +30,7 @@
 set DIR_HOME=%~dP0..
 set INSTANCE_ROOT=%DIR_HOME%
 
+:checkJavaBin
 if "%JAVA_BIN%" == "" goto noJavaBin
 goto setClassPath
 
@@ -40,17 +41,22 @@
 goto setClassPath
 
 :noJavaHome
+if not exist "%DIR_HOME%\bin\set-java-home.bat" goto noSetJavaHome
+call "%DIR_HOME%\bin\set-java-home.bat"
+set JAVA_BIN=%JAVA_HOME%\bin\java.exe
+goto setClassPath
+
+: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%\bin\setcp.bat" %%x
 
 set PATH=%SystemRoot%
 
-set SCRIPT_NAME_ARG="-Dorg.opends.server.scriptName=start-ds"
+set SCRIPT_NAME_ARG=-Dorg.opends.server.scriptName=start-ds
 
 set NODETACH=0
 for %%x in (%*) DO if "%%x" == "-N" set NODETACH=1
diff --git a/opendj-sdk/opends/resource/bin/stop-ds b/opendj-sdk/opends/resource/bin/stop-ds
index ea29e5b..117ef72 100755
--- a/opendj-sdk/opends/resource/bin/stop-ds
+++ b/opendj-sdk/opends/resource/bin/stop-ds
@@ -35,22 +35,44 @@
 # to stop the server using an LDAP request.
 
 
+# Capture the current working directory so that we can change to it later.
+# Then capture the location of this script and the Directory Server instance
+# root so that we can use them to create appropriate paths.
+WORKING_DIR=`pwd`
+
+cd `dirname "${0}"`
+SCRIPT_DIR=`pwd`
+
+cd ..
+INSTANCE_ROOT=`pwd`
+export INSTANCE_ROOT
+
+cd "${WORKING_DIR}"
+
+
 # See if JAVA_HOME is set.  If not, then see if there is a java executable in
 # the path and try to figure it out.
 if test -z "${JAVA_BIN}"
 then
   if test -z "${JAVA_HOME}"
   then
-    JAVA_BIN=`which java 2> /dev/null`
-    if test ${?} -eq 0
+    if test -f "${INSTANCE_ROOT}/bin/set-java-home"
     then
+      . "${INSTANCE_ROOT}/bin/set-java-home"
+      JAVA_BIN="${JAVA_HOME}/bin/java"
       export JAVA_BIN
     else
-      echo "Please set JAVA_HOME to the root of a Java 5.0 installation."
-      exit 1
+      JAVA_BIN=`which java 2> /dev/null`
+      if test ${?} -eq 0
+      then
+        export JAVA_BIN
+      else
+        echo "Please set JAVA_HOME to the root of a Java 5.0 installation."
+        exit 1
+      fi
     fi
   else
-    JAVA_BIN=${JAVA_HOME}/bin/java
+    JAVA_BIN="${JAVA_HOME}/bin/java"
     export JAVA_BIN
   fi
 fi
@@ -69,21 +91,6 @@
        LD_PRELOAD LD_PRELOAD_32 LD_PRELOAD_34
 
 
-# Capture the current working directory so that we can change to it later.
-# Then capture the location of this script and the Directory Server instance
-# root so that we can use them to create appropriate paths.
-WORKING_DIR=`pwd`
-
-cd `dirname "${0}"`
-SCRIPT_DIR=`pwd`
-
-cd ..
-INSTANCE_ROOT=`pwd`
-export INSTANCE_ROOT
-
-cd "${WORKING_DIR}"
-
-
 # Configure the appropriate CLASSPATH.
 CLASSPATH=${INSTANCE_ROOT}/classes
 for JAR in ${INSTANCE_ROOT}/lib/*.jar
diff --git a/opendj-sdk/opends/resource/setup.bat b/opendj-sdk/opends/resource/setup.bat
index 912385c..7806468 100644
--- a/opendj-sdk/opends/resource/setup.bat
+++ b/opendj-sdk/opends/resource/setup.bat
@@ -36,7 +36,7 @@
 :noJavaBin
 if "%JAVA_HOME%" == "" goto noJavaHome
 if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
-set JAVA_BIN="%JAVA_HOME%\bin\java.exe"
+set JAVA_BIN=%JAVA_HOME%\bin\java.exe
 goto setClassPath
 :noJavaHome
 echo Error: JAVA_HOME environment variable is not set.
@@ -49,7 +49,7 @@
 
 set PATH=%SystemRoot%
 
-%JAVA_BIN% %JAVA_ARGS% org.opends.quicksetup.SetupLauncher -P setup.bat %*
+"%JAVA_BIN%" %JAVA_ARGS% org.opends.quicksetup.SetupLauncher -P setup.bat %*
 
 
 :end
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 046804c..a5e5142 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
@@ -41,7 +41,7 @@
 import org.opends.quicksetup.i18n.ResourceProvider;
 import org.opends.quicksetup.util.ProgressMessageFormatter;
 import org.opends.quicksetup.util.Utils;
-import org.opends.server.util.CreateTemplate;
+import org.opends.server.util.SetupUtils;
 
 /**
  * This is an abstract class that is in charge of actually performing the
@@ -391,7 +391,7 @@
   {
     try
     {
-      return CreateTemplate.createTemplateFile(
+      return SetupUtils.createTemplateFile(
                   getUserData().getDataOptions().getBaseDn(),
                   getUserData().getDataOptions().getNumberEntries());
     }
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java
index 8582eb8..e6bbdf7 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/offline/OfflineInstaller.java
@@ -38,6 +38,7 @@
 import org.opends.quicksetup.installer.UserInstallData;
 import org.opends.quicksetup.util.ProgressMessageFormatter;
 import org.opends.quicksetup.util.Utils;
+import org.opends.server.util.SetupUtils;
 
 /**
  * This is an implementation of the Installer class that is used to install
@@ -174,6 +175,13 @@
         break;
       }
 
+      try
+      {
+        // This isn't likely to happen, and it's not a serious problem even if
+        // it does.
+        SetupUtils.writeSetJavaHome(FULL_INSTALL_PATH);
+      } catch (Exception e) {}
+
       if (getUserData().getStartServer())
       {
         notifyListeners(getTaskSeparator());
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 1e69c29..061f795 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
@@ -43,6 +43,7 @@
 import org.opends.quicksetup.installer.UserInstallData;
 import org.opends.quicksetup.util.ProgressMessageFormatter;
 import org.opends.quicksetup.util.Utils;
+import org.opends.server.util.SetupUtils;
 
 /**
  * This is an implementation of the Installer class that is used to install
@@ -169,6 +170,13 @@
         break;
       }
 
+      try
+      {
+        // This isn't likely to happen, and it's not a serious problem even if
+        // it does.
+        SetupUtils.writeSetJavaHome(getUserData().getServerLocation());
+      } catch (Exception e) {}
+
       if (getUserData().getStartServer())
       {
         notifyListeners(getTaskSeparator());
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 9358d71..a61fdab 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
@@ -41,7 +41,7 @@
 import org.opends.server.types.DN;
 import org.opends.server.types.ExistingFileBehavior;
 import org.opends.server.types.LDIFExportConfig;
-import org.opends.server.util.CreateTemplate;
+import org.opends.server.util.SetupUtils;
 import org.opends.server.util.LDIFWriter;
 import org.opends.server.util.PasswordReader;
 import org.opends.server.util.args.ArgumentException;
@@ -81,7 +81,7 @@
   /**
    * Indicates whether we think we're running on a Windows system.
    */
-  private static boolean isWindows = false;
+  private static final boolean isWindows = SetupUtils.isWindows();
 
 
 
@@ -156,14 +156,6 @@
    */
   public static int installMain(String[] args)
   {
-    // Determine whether we think we're running on Windows.
-    String osName = System.getProperty("os.name");
-    if ((osName != null) && (osName.toLowerCase().indexOf("windows") >= 0))
-    {
-      isWindows = true;
-    }
-
-
     // Construct the product version string and the setup filename.
     versionString = DirectoryServer.getVersionString();
 
@@ -784,7 +776,7 @@
     {
       try
       {
-        File templateFile = CreateTemplate.createTemplateFile(
+        File templateFile = SetupUtils.createTemplateFile(
                                  baseDNs.getFirst().toString(), numUsers);
         if (ldifFiles == null)
         {
@@ -861,6 +853,24 @@
     }
 
 
+    // Try to write a file that can be used to set the JAVA_HOME environment
+    // variable for the administrative scripts and client tools provided with
+    // the server.  If this fails, then it's not a big deal.
+    try
+    {
+      String serverRoot = System.getenv("INSTANCE_ROOT");
+      if ((serverRoot == null) || (serverRoot.length() == 0))
+      {
+        File f = new File(configFileName);
+        serverRoot = f.getParentFile().getParentFile().getAbsolutePath();
+      }
+
+      // This isn't likely to happen, and it's not a serious problem even if it
+      // does.
+      SetupUtils.writeSetJavaHome(serverRoot);
+    } catch (Exception e) {}
+
+
     // If we've gotten here, then everything seems to have gone smoothly.
     if (! silentInstall.isPresent())
     {
diff --git a/opendj-sdk/opends/src/server/org/opends/server/util/CreateTemplate.java b/opendj-sdk/opends/src/server/org/opends/server/util/CreateTemplate.java
deleted file mode 100644
index dfb05a7..0000000
--- a/opendj-sdk/opends/src/server/org/opends/server/util/CreateTemplate.java
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * CDDL HEADER START
- *
- * The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License").  You may not use this file except in compliance
- * with the License.
- *
- * You can obtain a copy of the license at
- * trunk/opends/resource/legal-notices/OpenDS.LICENSE
- * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
- * See the License for the specific language governing permissions
- * and limitations under the License.
- *
- * When distributing Covered Code, include this CDDL HEADER in each
- * file and include the License file at
- * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
- * add the following below this CDDL HEADER, with the fields enclosed
- * by brackets "[]" replaced with your own identifying * information:
- *      Portions Copyright [yyyy] [name of copyright owner]
- *
- * CDDL HEADER END
- *
- *
- *      Portions Copyright 2006 Sun Microsystems, Inc.
- */
-package org.opends.server.util;
-
-
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.LinkedList;
-
-
-
-/**
- * This class may be used to generate a MakeLDIF template with a specified base
- * DN and number of user entries.
- */
-public class CreateTemplate
-{
-  /**
-   * Creates a MakeLDIF template file using the provided information.
-   *
-   * @param  baseDN      The base DN for the data in the template file.
-   * @param  numEntries  The number of user entries the template file should
-   *                     create.
-   *
-   * @return  The {@code File} object that references the created template file.
-   *
-   * @throws  IOException  If a problem occurs while writing the template file.
-   */
-  public static File createTemplateFile(String baseDN, int numEntries)
-         throws IOException
-  {
-    File templateFile = File.createTempFile("opends-install", ".template");
-    templateFile.deleteOnExit();
-
-    LinkedList<String> lines = new LinkedList<String>();
-    lines.add("define suffix=" + baseDN);
-
-    if (numEntries > 0)
-    {
-      lines.add("define numusers=" + numEntries);
-    }
-
-    lines.add("");
-    lines.add("branch: [suffix]");
-    lines.add("");
-    lines.add("branch: ou=People,[suffix]");
-
-    if (numEntries > 0)
-    {
-      lines.add("subordinateTemplate: person:[numusers]");
-      lines.add("");
-      lines.add("template: person");
-      lines.add("rdnAttr: uid");
-      lines.add("objectClass: top");
-      lines.add("objectClass: person");
-      lines.add("objectClass: organizationalPerson");
-      lines.add("objectClass: inetOrgPerson");
-      lines.add("givenName: <first>");
-      lines.add("sn: <last>");
-      lines.add("cn: {givenName} {sn}");
-      lines.add("initials: {givenName:1}" +
-                "<random:chars:ABCDEFGHIJKLMNOPQRSTUVWXYZ:1>{sn:1}");
-      lines.add("employeeNumber: <sequential:0>");
-      lines.add("uid: user.{employeeNumber}");
-      lines.add("mail: {uid}@maildomain.net");
-      lines.add("userPassword: password");
-      lines.add("telephoneNumber: <random:telephone>");
-      lines.add("homePhone: <random:telephone>");
-      lines.add("pager: <random:telephone>");
-      lines.add("mobile: <random:telephone>");
-      lines.add("street: <random:numeric:5> <file:streets> Street");
-      lines.add("l: <file:cities>");
-      lines.add("st: <file:states>");
-      lines.add("postalCode: <random:numeric:5>");
-      lines.add("postalAddress: {cn}${street}${l}, {st}  {postalCode}");
-      lines.add("description: This is the description for {cn}.");
-    }
-
-    BufferedWriter writer = new BufferedWriter(new FileWriter(templateFile));
-    for (String line : lines)
-    {
-      writer.write(line);
-      writer.newLine();
-    }
-
-    writer.flush();
-    writer.close();
-
-    return templateFile;
-  }
-}
-
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
new file mode 100644
index 0000000..b954cc1
--- /dev/null
+++ b/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java
@@ -0,0 +1,204 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying * information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Portions Copyright 2006 Sun Microsystems, Inc.
+ */
+package org.opends.server.util;
+
+
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.LinkedList;
+
+
+
+/**
+ * This class provides a number of utility methods that may be used during the
+ * graphical or command-line setup process.
+ */
+public class SetupUtils
+{
+  /**
+   * Creates a MakeLDIF template file using the provided information.
+   *
+   * @param  baseDN      The base DN for the data in the template file.
+   * @param  numEntries  The number of user entries the template file should
+   *                     create.
+   *
+   * @return  The {@code File} object that references the created template file.
+   *
+   * @throws  IOException  If a problem occurs while writing the template file.
+   */
+  public static File createTemplateFile(String baseDN, int numEntries)
+         throws IOException
+  {
+    File templateFile = File.createTempFile("opends-install", ".template");
+    templateFile.deleteOnExit();
+
+    LinkedList<String> lines = new LinkedList<String>();
+    lines.add("define suffix=" + baseDN);
+
+    if (numEntries > 0)
+    {
+      lines.add("define numusers=" + numEntries);
+    }
+
+    lines.add("");
+    lines.add("branch: [suffix]");
+    lines.add("");
+    lines.add("branch: ou=People,[suffix]");
+
+    if (numEntries > 0)
+    {
+      lines.add("subordinateTemplate: person:[numusers]");
+      lines.add("");
+      lines.add("template: person");
+      lines.add("rdnAttr: uid");
+      lines.add("objectClass: top");
+      lines.add("objectClass: person");
+      lines.add("objectClass: organizationalPerson");
+      lines.add("objectClass: inetOrgPerson");
+      lines.add("givenName: <first>");
+      lines.add("sn: <last>");
+      lines.add("cn: {givenName} {sn}");
+      lines.add("initials: {givenName:1}" +
+                "<random:chars:ABCDEFGHIJKLMNOPQRSTUVWXYZ:1>{sn:1}");
+      lines.add("employeeNumber: <sequential:0>");
+      lines.add("uid: user.{employeeNumber}");
+      lines.add("mail: {uid}@maildomain.net");
+      lines.add("userPassword: password");
+      lines.add("telephoneNumber: <random:telephone>");
+      lines.add("homePhone: <random:telephone>");
+      lines.add("pager: <random:telephone>");
+      lines.add("mobile: <random:telephone>");
+      lines.add("street: <random:numeric:5> <file:streets> Street");
+      lines.add("l: <file:cities>");
+      lines.add("st: <file:states>");
+      lines.add("postalCode: <random:numeric:5>");
+      lines.add("postalAddress: {cn}${street}${l}, {st}  {postalCode}");
+      lines.add("description: This is the description for {cn}.");
+    }
+
+    BufferedWriter writer = new BufferedWriter(new FileWriter(templateFile));
+    for (String line : lines)
+    {
+      writer.write(line);
+      writer.newLine();
+    }
+
+    writer.flush();
+    writer.close();
+
+    return templateFile;
+  }
+
+
+
+  /**
+   * Indicates whether the underlying operating system is a Windows variant.
+   *
+   * @return  {@code true} if the underlying operating system is a Windows
+   *          variant, or {@code false} if not.
+   */
+  public static boolean isWindows()
+  {
+    String osName = System.getProperty("os.name");
+    return ((osName != null) && (osName.toLowerCase().indexOf("windows") >= 0));
+  }
+
+
+
+  /**
+   * Write a set-java-home file appropriate for the underlying platform that may
+   * be used to set the JAVA_HOME environment variable in a form suitable for
+   * the underlying operating system.  If a JAVA_HOME environment variable is
+   * currently set, then its value will be used.  Otherwise, it will be
+   * dynamically determined from the JVM properties.
+   * <BR><BR>
+   * Note that if the target file that would be written already exists, then
+   * this method will exit without doing anything and leaving the existing file
+   * intact.
+   *
+   *
+   * @param  serverRoot  The path to the root of the Directory Server instance
+   *                     for which the file will be written.
+   *
+   * @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 writeSetJavaHome(String serverRoot)
+         throws IOException
+  {
+    String javaHome = System.getenv("JAVA_HOME");
+    if ((javaHome == null) || (javaHome.length() == 0))
+    {
+      javaHome = System.getProperty("java.home");
+    }
+
+
+    File binDirectory = new File(serverRoot, "bin");
+    File setJavaHomeFile;
+    if (isWindows())
+    {
+      setJavaHomeFile = new File(binDirectory, "set-java-home.bat");
+      if (setJavaHomeFile.exists())
+      {
+        return setJavaHomeFile;
+      }
+
+      BufferedWriter writer =
+           new BufferedWriter(new FileWriter(setJavaHomeFile));
+      writer.write("set JAVA_HOME=" + javaHome);
+      writer.newLine();
+      writer.close();
+    }
+    else
+    {
+      setJavaHomeFile = new File(binDirectory, "set-java-home");
+      if (setJavaHomeFile.exists())
+      {
+        return setJavaHomeFile;
+      }
+
+      BufferedWriter writer =
+           new BufferedWriter(new FileWriter(setJavaHomeFile));
+      writer.write("#!/bin/sh");
+      writer.newLine();
+      writer.newLine();
+      writer.write("JAVA_HOME=" + javaHome);
+      writer.newLine();
+      writer.write("export JAVA_HOME");
+      writer.newLine();
+      writer.close();
+    }
+
+    return setJavaHomeFile;
+  }
+}
+

--
Gitblit v1.10.0