From b05d71f7c4f7cf048f7604559ddca60e26094be9 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Fri, 20 Nov 2009 10:41:27 +0000
Subject: [PATCH] Fix for issue 4362 (failure to detect properly installed Java) The fix consists on two parts: 1. Give priority to the java binary set in the path (which is usually updated when java is installed). 2. Check that actually the java binary found in the environment variables does exist.

---
 opends/resource/bin/_script-util.sh  |  128 ++++++++++++++++++++++++++++++++----------
 opends/resource/bin/_script-util.bat |   18 +++---
 2 files changed, 106 insertions(+), 40 deletions(-)

diff --git a/opends/resource/bin/_script-util.bat b/opends/resource/bin/_script-util.bat
index 813b37c..635cad8 100644
--- a/opends/resource/bin/_script-util.bat
+++ b/opends/resource/bin/_script-util.bat
@@ -94,11 +94,17 @@
 goto endJavaHomeAndArgs
 
 :checkOpenDSJavaHome
-if "%OPENDS_JAVA_HOME%" == "" goto checkJavaBin
-if not exist "%OPENDS_JAVA_HOME%\bin\java.exe" goto checkJavaBin
+if "%OPENDS_JAVA_HOME%" == "" goto checkJavaPath
+if not exist "%OPENDS_JAVA_HOME%\bin\java.exe" goto checkJavaPath
 set OPENDS_JAVA_BIN=%OPENDS_JAVA_HOME%\bin\java.exe
 goto endJavaHomeAndArgs
 
+:checkJavaPath
+java.exe -version > NUL 2>&1
+if not %errorlevel% == 0 goto checkJavaBin
+set OPENDS_JAVA_BIN=java.exe
+goto endJavaHomeAndArgs
+
 :checkJavaBin
 if "%JAVA_BIN%" == "" goto checkJavaHome
 if not exist "%JAVA_BIN%" goto checkJavaHome
@@ -106,17 +112,11 @@
 goto endJavaHomeAndArgs
 
 :checkJavaHome
-if "%JAVA_HOME%" == "" goto checkJavaPath
+if "%JAVA_HOME%" == "" goto noJavaFound
 if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaFound
 set OPENDS_JAVA_BIN=%JAVA_HOME%\bin\java.exe
 goto endJavaHomeAndArgs
 
-:checkJavaPath
-java.exe -version > NUL 2>&1
-if not %errorlevel% == 0 goto noJavaFound
-set OPENDS_JAVA_BIN=java.exe
-goto endJavaHomeAndArgs
-
 :noJavaFound
 echo ERROR:  Could not find a valid Java binary to be used.
 echo You must specify the path to a valid Java 5.0 or higher version.
diff --git a/opends/resource/bin/_script-util.sh b/opends/resource/bin/_script-util.sh
index d892176..8ed5972 100644
--- a/opends/resource/bin/_script-util.sh
+++ b/opends/resource/bin/_script-util.sh
@@ -26,6 +26,101 @@
 #      Copyright 2008-2009 Sun Microsystems, Inc.
 
 #
+# Display an error message
+#
+display_java_not_found_error() {
+  echo "Please set OPENDS_JAVA_HOME to the root of a Java 5 (or later) installation"
+  echo "or edit the java.properties file and then run the dsjavaproperties script to"
+  echo "specify the Java version to be used"
+}
+
+#
+# function that tests the JAVA_HOME env variable.
+#
+test_java_home() {
+  if test -z "${JAVA_HOME}"
+  then
+    display_java_not_found_error
+    exit 1
+  else
+    OPENDS_JAVA_BIN="${JAVA_HOME}/bin/java"
+    if test -f "${OPENDS_JAVA_BIN}"
+    then
+      export OPENDS_JAVA_BIN
+    else
+      display_java_not_found_error
+      exit 1
+    fi
+  fi
+}
+
+#
+# function that tests the JAVA_BIN env variable.
+#
+test_java_bin() {
+  if test -z "${JAVA_BIN}"
+  then
+    test_java_home
+  else
+    OPENDS_JAVA_BIN="${JAVA_BIN}"
+    if test -f "${OPENDS_JAVA_BIN}"
+    then
+      export OPENDS_JAVA_BIN
+    else
+      test_java_home
+    fi
+  fi
+}
+
+#
+# function that tests the java executable in the PATH env variable.
+#
+test_java_path() {
+  OPENDS_JAVA_BIN=`which java 2> /dev/null`
+  if test ${?} -eq 0
+  then
+    export OPENDS_JAVA_BIN
+  else
+    test_java_bin
+  fi
+}
+
+#
+# function that tests the OPENDS_JAVA_HOME env variable.
+#
+test_opends_java_home() {
+  if test -z "${OPENDS_JAVA_HOME}"
+  then
+    test_java_path
+  else
+    OPENDS_JAVA_BIN="${OPENDS_JAVA_HOME}/bin/java"
+    if test -f "${OPENDS_JAVA_BIN}"
+    then
+      export OPENDS_JAVA_BIN
+    else
+      test_java_path
+    fi
+  fi
+}
+
+#
+# function that tests the OPENDS_JAVA_BIN env variable.
+#
+test_opends_java_bin() {
+  if test -z "${OPENDS_JAVA_BIN}"
+  then
+    test_opends_java_home
+  else
+    if test -f "${OPENDS_JAVA_BIN}"
+    then
+      export OPENDS_JAVA_BIN
+    else
+      test_opends_java_home
+    fi
+  fi
+}
+
+#
 # function that sets the java home
 #
 set_java_home_and_args() {
@@ -33,39 +128,10 @@
   then
     . "${INSTANCE_ROOT}/lib/set-java-home"
   fi
-  if test -z "${OPENDS_JAVA_BIN}"
-  then
-    if test -z "${OPENDS_JAVA_HOME}"
-    then
-      if test -z "${JAVA_BIN}"
-      then
-        if test -z "${JAVA_HOME}"
-        then
-          OPENDS_JAVA_BIN=`which java 2> /dev/null`
-          if test ${?} -eq 0
-          then
-            export OPENDS_JAVA_BIN
-          else
-            echo "Please set OPENDS_JAVA_HOME to the root of a Java 5 (or later) installation"
-            echo "or edit the java.properties file and then run the dsjavaproperties script to"
-            echo "specify the Java version to be used"
-            exit 1
-          fi
-        else
-          OPENDS_JAVA_BIN="${JAVA_HOME}/bin/java"
-          export OPENDS_JAVA_BIN
-        fi
-      else
-        OPENDS_JAVA_BIN="${JAVA_BIN}"
-        export OPENDS_JAVA_BIN
-      fi
-    else
-      OPENDS_JAVA_BIN="${OPENDS_JAVA_HOME}/bin/java"
-      export OPENDS_JAVA_BIN
-    fi
-  fi
+  test_opends_java_bin
 }
 
+
 # Determine whether the detected Java environment is acceptable for use.
 test_java() {
   if test -z "${OPENDS_JAVA_ARGS}"

--
Gitblit v1.10.0