necessary changes for my Eclipse
[jabaws.git] / engine / compbio / engine / client / Util.java
index 4d0b8a1..5eda4b6 100644 (file)
@@ -32,6 +32,7 @@ import compbio.engine.client.Executable.ExecProvider;
 import compbio.engine.conf.DirectoryManager;\r
 import compbio.engine.conf.PropertyHelperManager;\r
 import compbio.metadata.JobStatus;\r
+import compbio.metadata.LimitsManager;\r
 import compbio.metadata.ResultNotAvailableException;\r
 import compbio.util.FileUtil;\r
 import compbio.util.PropertyHelper;\r
@@ -68,8 +69,8 @@ public final class Util {
                // Work directory could be null for cancelled or incomplete jobs, just\r
                // ignore\r
                if (!compbio.util.Util.isEmpty(workDirectory)) {\r
-                       writeFile(workDirectory, fileAndEventName, new Long(System\r
-                                       .nanoTime()).toString(), false);\r
+                       writeFile(workDirectory, fileAndEventName,\r
+                                       new Long(System.currentTimeMillis()).toString(), false);\r
                }\r
        }\r
 \r
@@ -77,9 +78,8 @@ public final class Util {
                        String content, boolean override) {\r
                File file = null;\r
                if (compbio.util.Util.isEmpty(workDirectory)) {\r
-                       log\r
-                                       .debug("Calling compbio.engine.Util.writeFile() with not work directory."\r
-                                                       + " Skipping writing statistics!");\r
+                       log.debug("Calling compbio.engine.Util.writeFile() with not work directory."\r
+                                       + " Skipping writing statistics!");\r
                        return;\r
                }\r
                assert !compbio.util.Util.isEmpty(content) : "Content expected!";\r
@@ -127,9 +127,10 @@ public final class Util {
                                return sfile.createNewFile();\r
                        }\r
                } catch (IOException e) {\r
-                       log.error("Could not record stat marker file " + fileType\r
-                                       + " into the directory " + workDirectory + " ! "\r
-                                       + e.getMessage(), e.getCause());\r
+                       log.error(\r
+                                       "Could not record stat marker file " + fileType\r
+                                                       + " into the directory " + workDirectory + " ! "\r
+                                                       + e.getMessage(), e.getCause());\r
                }\r
                return false;\r
        }\r
@@ -183,9 +184,8 @@ public final class Util {
                String absolute = relativePath;\r
                if (!PathValidator.isAbsolutePath(relativePath)) {\r
                        absolute = PropertyHelperManager.getLocalPath() + relativePath;\r
-                       Util.log\r
-                                       .trace("Changing local path in enviromental variable to absolute: FROM "\r
-                                                       + relativePath + " TO " + absolute);\r
+                       Util.log.trace("Changing local path in enviromental variable to absolute: FROM "\r
+                                       + relativePath + " TO " + absolute);\r
                }\r
                return absolute;\r
        }\r
@@ -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,80 @@ 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
+               // Could have done: Set executable flag if not set\r
+               // but - do not because in some cases more than one file must be made\r
+               // executable!\r
+               /*\r
+                * if (!compbio.util.Util.isEmpty(bin)) { File command = new File(bin);\r
+                * if (!command.canExecute()) { log.debug(\r
+                * "The command line binary is not executable! (just unpacked from war file? )"\r
+                * ); log.debug("Attempting to set executable flag for command: " +\r
+                * bin); command.setExecutable(true, false); } }\r
+                */\r
+               log.debug("Using executable: " + bin);\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
+               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 returns the absolute path to the Java executable from JAVA_HOME\r
+        */\r
+       public static String getJava() {\r
+               String javahome = System.getProperty("java.home");\r
+               if (compbio.util.Util.isEmpty(javahome)) {\r
+                       javahome = System.getenv("JAVA_HOME");\r
+               }\r
+               if (compbio.util.Util.isEmpty(javahome)) {\r
+                       log.warn("Cannot find Java in java.home system property "\r
+                                       + "or JAVA_HOME environment variable! ");\r
+                       return null;\r
+               }\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 +326,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 +347,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
@@ -286,21 +363,55 @@ public final class Util {
                        exec = ConfExecutable.newConfExecutable(rconf);\r
                        fileInStream.close();\r
                } catch (FileNotFoundException e) {\r
-                       log.error("Could not find run configuration to load!"\r
-                                       + e.getLocalizedMessage(), e.getCause());\r
+                       log.error(\r
+                                       "Could not find run configuration to load!"\r
+                                                       + e.getLocalizedMessage(), e.getCause());\r
                        throw new ResultNotAvailableException(\r
                                        "Could not find run configuration to load!"\r
                                                        + e.getMessage(), e.getCause());\r
                } catch (IOException e) {\r
-                       log.error("IO Exception while reading run configuration file!"\r
-                                       + e.getLocalizedMessage(), e.getCause());\r
+                       log.error(\r
+                                       "IO Exception while reading run configuration file!"\r
+                                                       + e.getLocalizedMessage(), e.getCause());\r
                        throw new ResultNotAvailableException(\r
-                                       "Could not load run configuration!" + e.getMessage(), e\r
-                                                       .getCause());\r
+                                       "Could not load run configuration!" + e.getMessage(),\r
+                                       e.getCause());\r
                } finally {\r
                        FileUtil.closeSilently(log, fileInStream);\r
                }\r
                return exec;\r
        }\r
 \r
+       /**\r
+        * For now just assume that all parameters which came in needs setting it\r
+        * will be a client responsibility to prepare RunnerConfig object then\r
+        * \r
+        * @param rconfig\r
+        * @return\r
+        * \r
+        *         public static List<String> toOptionString(RunnerConfig<?>\r
+        *         rconfig) { String option = ""; List<String> options = new\r
+        *         ArrayList<String>(); for (Parameter<?> par :\r
+        *         rconfig.getParameters()) { if (par.getPossibleValues().isEmpty())\r
+        *         { option = par.getOptionName(); } else { option =\r
+        *         par.getOptionName() + "=" + par.getPossibleValues().get(0); } //\r
+        *         separate options options.add(option); } return options; }\r
+        */\r
+\r
+       public static <T> LimitsManager<T> getLimits(Class<T> clazz) {\r
+               LimitsManager<T> limits = null;\r
+               try {\r
+                       limits = ConfExecutable.getRunnerLimits(clazz);\r
+               } catch (FileNotFoundException e) {\r
+                       Util.log.warn("No limits are found for " + clazz + " executable! "\r
+                                       + e.getLocalizedMessage(), e.getCause());\r
+                       // its ok, limit may not be initialized\r
+               } catch (IOException e) {\r
+                       Util.log.warn("IO exception while attempting to read limits for "\r
+                                       + clazz + " executable! " + e.getLocalizedMessage(),\r
+                                       e.getCause());\r
+               }\r
+               return limits;\r
+       }\r
+\r
 }\r