obtain JAVA executable path from the JAVA_HOME variable. Does not require path to...
[jabaws.git] / engine / compbio / engine / client / Util.java
index 9d0727b..30ac944 100644 (file)
@@ -225,6 +225,14 @@ public final class Util {
                        } else {\r
                                bin = ph.getProperty("local." + execCommandName + ".bin");\r
                        }\r
+                       // For executable Jar files the location of Java executable is not\r
+                       // required for local execution. If it is not provided, JABAWS will\r
+                       // attempt to use Java from JAVA_HOME env variable\r
+                       if (isJavaLibrary(clazz)) {\r
+                               if (compbio.util.Util.isEmpty(bin)) {\r
+                                       bin = getJava();\r
+                               }\r
+                       }\r
                        // If path to executable defined in the properties is not absolute,\r
                        // then make it so\r
                        // as setting working directory of ProcessBuilder will make it\r
@@ -232,13 +240,62 @@ public final class Util {
                        // to find an executable otherwise\r
                        if (!compbio.util.Util.isEmpty(bin)\r
                                        && !PathValidator.isAbsolutePath(bin)) {\r
-                               bin = PropertyHelperManager.getLocalPath() + bin;\r
+                               bin = bin.trim();\r
+                               if (bin.equalsIgnoreCase("java")\r
+                                               || bin.equalsIgnoreCase("java.exe")) {\r
+                                       // do not make path absolute to the java executable if\r
+                                       // relative path is provided. Java executable is not a part\r
+                                       // of JABAWS distribution!\r
+                               } else {\r
+                                       bin = PropertyHelperManager.getLocalPath() + bin;\r
+                               }\r
                        }\r
                } else {\r
                        bin = ph.getProperty("cluster." + execCommandName + ".bin");\r
                }\r
                return bin; // File.separator\r
        }\r
+       /**\r
+        * Returns true of executableName.jar.file property has some value in the\r
+        * Executable.properties file, false otherwise.\r
+        * \r
+        * @param clazz\r
+        * @return\r
+        */\r
+       public static boolean isJavaLibrary(Class<?> clazz) {\r
+               String execCommandName = clazz.getSimpleName().toLowerCase();\r
+               String java_lib = ph.getProperty(execCommandName + ".jar.file");\r
+               assert FileUtil.exist(java_lib);\r
+               return !compbio.util.Util.isEmpty(java_lib);\r
+       }\r
+\r
+       /**\r
+        * Returns the absolute path to the Java executable from JAVA_HOME\r
+        * \r
+        * @return\r
+        */\r
+       public static String getJava() {\r
+               String javahome = System.getenv("JAVA_HOME");\r
+               File jh = new File(javahome);\r
+               if (jh.exists() && jh.isDirectory()) {\r
+                       String java = javahome + File.separator + "bin" + File.separator\r
+                                       + "java";\r
+                       if (SysPrefs.isWindows) {\r
+                               java += ".exe";\r
+                       }\r
+                       File jexe = new File(java);\r
+                       if (jexe.exists() && jexe.isFile() && jexe.canExecute()) {\r
+                               log.info("Using Java from: " + jexe.getAbsolutePath());\r
+                               return jexe.getAbsolutePath();\r
+                       } else {\r
+                               log.warn("Cannot find java executable in the JAVA_HOME!");\r
+                       }\r
+               } else {\r
+                       log.warn("JAVA_HOME does not seems to point to a valid directory! Value: "\r
+                                       + javahome);\r
+               }\r
+               return null;\r
+       }\r
 \r
        public static ExecProvider getSupportedRuntimes(Class<?> clazz) {\r
                boolean localRuntimeSupport = false;\r
@@ -251,7 +308,10 @@ public final class Util {
                if (!compbio.util.Util.isEmpty(localRuntime1)\r
                                || !compbio.util.Util.isEmpty(localRuntime2)) {\r
                        localRuntimeSupport = true;\r
+               } else {\r
+                       localRuntimeSupport = isJavaLibrary(clazz) && getJava() != null;\r
                }\r
+\r
                String clusterRuntime = ph.getProperty("cluster." + executableName\r
                                + ".bin");\r
                if (!compbio.util.Util.isEmpty(clusterRuntime)) {\r
@@ -269,7 +329,6 @@ public final class Util {
                throw new InvalidParameterException(\r
                                "Executable is not provided for any runtime environments");\r
        }\r
-\r
        public static ConfiguredExecutable<?> loadExecutable(String taskId)\r
                        throws ResultNotAvailableException {\r
                String workDir = compbio.engine.Configurator.getWorkDirectory(taskId);\r