+/* Copyright (c) 2013 Alexander Sherstnev\r
+ * \r
+ * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 \r
+ * \r
+ * This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ * Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ * License for more details.\r
+ * \r
+ * A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+package compbio.stat.collector;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import compbio.engine.client.Executable;\r
+import compbio.engine.client.SkeletalExecutable;\r
+import compbio.metadata.JobStatus;\r
+import compbio.util.FileUtil;\r
+import compbio.ws.client.Services;\r
+import compbio.ws.client.ServicesUtil;\r
+\r
+/**\r
+ * \r
+ * @author Alexander Sherstnev\r
+ * \r
+ */\r
+public class JobDirectory {\r
+\r
+ static final int UNDEFINED = -1;\r
+\r
+ private static final Logger log = Logger.getLogger(JobDirectory.class);\r
+ \r
+ File jobdir;\r
+ Map<String, File> files = new HashMap<String, File>();\r
+\r
+ JobDirectory(File directory) {\r
+ this.jobdir = directory;\r
+ for (File f : jobdir.listFiles()) {\r
+ files.put(f.getName(), f);\r
+ }\r
+ }\r
+\r
+ boolean hasStatus(JobStatus status) {\r
+ return files.containsKey(status.toString());\r
+ }\r
+\r
+ boolean isCollected() {\r
+ return hasStatus(JobStatus.COLLECTED);\r
+ }\r
+\r
+ boolean isCancelled() {\r
+ return hasStatus(JobStatus.CANCELLED);\r
+ }\r
+\r
+ long getStartTime() {\r
+ long starttime = UNDEFINED;\r
+ File startfile = files.get(JobStatus.STARTED.toString());\r
+ if (startfile == null) {\r
+ startfile = files.get(JobStatus.SUBMITTED.toString());\r
+ }\r
+ try {\r
+ if (startfile != null) {\r
+ String start = FileUtil.readFileToString(startfile);\r
+ starttime = Long.parseLong(start.trim());\r
+ }\r
+ } catch (IOException ignore) {\r
+ log.warn("IOException while reading STARTED status file! Ignoring...", ignore);\r
+ // fall back\r
+ starttime = startfile.lastModified();\r
+ } catch (NumberFormatException ignore) {\r
+ log.warn("NumberFormatException while reading STARTED status file! Ignoring...", ignore);\r
+ // fall back\r
+ starttime = startfile.lastModified();\r
+ }\r
+ return starttime;\r
+ }\r
+\r
+ String getClusterJobID() {\r
+ String clustjobId = "";\r
+ File jobid = files.get("JOBID");\r
+ try {\r
+ if (jobid != null) {\r
+ clustjobId = FileUtil.readFileToString(jobid);\r
+ }\r
+ } catch (IOException ioe) {\r
+ log.error(\r
+ "IO Exception while reading the content of JOBID file for job "\r
+ + jobid, ioe);\r
+ }\r
+ return clustjobId.trim();\r
+ }\r
+\r
+ long getFinishedTime() {\r
+ long ftime = UNDEFINED;\r
+ File finished = files.get(JobStatus.FINISHED.toString());\r
+ if (finished != null) {\r
+ try {\r
+ if (finished != null) {\r
+ String start = FileUtil.readFileToString(finished);\r
+ ftime = Long.parseLong(start.trim());\r
+ }\r
+ } catch (IOException ignore) {\r
+ log.warn(\r
+ "IOException while reading FINISHED status file! Ignoring...",\r
+ ignore);\r
+ // fall back\r
+ ftime = finished.lastModified();\r
+ } catch (NumberFormatException ignore) {\r
+ log.warn(\r
+ "NumberFormatException while reading FINISHED status file! Ignoring...",\r
+ ignore);\r
+ // fall back\r
+ ftime = finished.lastModified();\r
+ }\r
+ }\r
+ return ftime;\r
+ }\r
+\r
+ private Services getService() {\r
+ return ServicesUtil.getServiceByJobDirectory(jobdir);\r
+ }\r
+\r
+ long getResultSize() {\r
+ Class<? extends Executable<?>> name = ServicesUtil\r
+ .getRunnerByJobDirectory(jobdir);\r
+\r
+ File f = null;\r
+ if (name.getSimpleName().equalsIgnoreCase("IUPred")) {\r
+ f = files.get("out.glob");\r
+ if (f == null)\r
+ f = files.get("out.short");\r
+ if (f == null)\r
+ f = files.get("out.long");\r
+ } else {\r
+ f = files.get(SkeletalExecutable.OUTPUT);\r
+ }\r
+ if (f != null) {\r
+ return f.length();\r
+ }\r
+ return UNDEFINED;\r
+ }\r
+\r
+ long getInputSize() {\r
+ Class<? extends Executable<?>> name = ServicesUtil\r
+ .getRunnerByJobDirectory(jobdir);\r
+\r
+ File input = files.get(SkeletalExecutable.INPUT);\r
+ if (input != null) {\r
+ return input.length();\r
+ }\r
+ return UNDEFINED;\r
+ }\r
+\r
+ JobStat getJobStat() {\r
+ return JobStat.newInstance(getService(), getClusterJobID(),\r
+ jobdir.getName(), getStartTime(), getFinishedTime(),\r
+ getInputSize(), getResultSize(), isCancelled(),\r
+ isCollected());\r
+ }\r
+\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result\r
+ + ((jobdir == null) ? 0 : jobdir.hashCode());\r
+ return result;\r
+ }\r
+\r
+ public boolean equals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (obj == null)\r
+ return false;\r
+ if (getClass() != obj.getClass())\r
+ return false;\r
+ JobDirectory other = (JobDirectory) obj;\r
+ if (jobdir == null) {\r
+ if (other.jobdir != null)\r
+ return false;\r
+ } else if (!jobdir.equals(other.jobdir))\r
+ return false;\r
+ return true;\r
+ }\r
+}\r
+\r