Re-formatting the file
[jabaws.git] / webservices / compbio / stat / collector / JobDirectory.java
1 /* Copyright (c) 2013 Alexander Sherstnev\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.stat.collector;\r
19 \r
20 import java.io.File;\r
21 import java.io.IOException;\r
22 import java.util.HashMap;\r
23 import java.util.Map;\r
24 \r
25 import org.apache.log4j.Logger;\r
26 \r
27 import compbio.engine.client.Executable;\r
28 import compbio.engine.client.SkeletalExecutable;\r
29 import compbio.metadata.JobStatus;\r
30 import compbio.util.FileUtil;\r
31 import compbio.ws.client.Services;\r
32 import compbio.ws.client.ServicesUtil;\r
33 \r
34 /**\r
35  * \r
36  * @author Alexander Sherstnev\r
37  * \r
38  */\r
39 public class JobDirectory {\r
40 \r
41         static final int UNDEFINED = -1;\r
42 \r
43         private static final Logger log = Logger.getLogger(JobDirectory.class);\r
44         \r
45         File jobdir;\r
46         Map<String, File> files = new HashMap<String, File>();\r
47 \r
48         JobDirectory(File directory) {\r
49                 this.jobdir = directory;\r
50                 for (File f : jobdir.listFiles()) {\r
51                 files.put(f.getName(), f);\r
52                 }\r
53         }\r
54 \r
55         boolean hasStatus(JobStatus status) {\r
56                 return files.containsKey(status.toString());\r
57         }\r
58 \r
59         boolean isCollected() {\r
60                 return hasStatus(JobStatus.COLLECTED);\r
61         }\r
62 \r
63         boolean isCancelled() {\r
64                 return hasStatus(JobStatus.CANCELLED);\r
65         }\r
66 \r
67         long getStartTime() {\r
68                 long starttime = UNDEFINED;\r
69                 File startfile = files.get(JobStatus.STARTED.toString());\r
70                 if (startfile == null) {\r
71                         startfile = files.get(JobStatus.SUBMITTED.toString());\r
72                 }\r
73                 try {\r
74                         if (startfile != null) {\r
75                                 String start = FileUtil.readFileToString(startfile);\r
76                                 starttime = Long.parseLong(start.trim());\r
77                         }\r
78                 } catch (IOException ignore) {\r
79                         log.warn("IOException while reading STARTED status file! Ignoring...", ignore);\r
80                         // fall back\r
81                         starttime = startfile.lastModified();\r
82                 } catch (NumberFormatException ignore) {\r
83                         log.warn("NumberFormatException while reading STARTED status file! Ignoring...", ignore);\r
84                         // fall back\r
85                         starttime = startfile.lastModified();\r
86                 }\r
87                 return starttime;\r
88         }\r
89 \r
90         String getClusterJobID() {\r
91                 String clustjobId = "";\r
92                 File jobid = files.get("JOBID");\r
93                 try {\r
94                 if (jobid != null) {\r
95                         clustjobId = FileUtil.readFileToString(jobid);\r
96                 }\r
97                 } catch (IOException ioe) {\r
98                 log.error(\r
99                         "IO Exception while reading the content of JOBID file for job "\r
100                                 + jobid, ioe);\r
101                 }\r
102                 return clustjobId.trim();\r
103         }\r
104 \r
105         long getFinishedTime() {\r
106                 long ftime = UNDEFINED;\r
107                 File finished = files.get(JobStatus.FINISHED.toString());\r
108                 if (finished != null) {\r
109                 try {\r
110                         if (finished != null) {\r
111                         String start = FileUtil.readFileToString(finished);\r
112                         ftime = Long.parseLong(start.trim());\r
113                         }\r
114                 } catch (IOException ignore) {\r
115                         log.warn(\r
116                                 "IOException while reading FINISHED status file! Ignoring...",\r
117                                 ignore);\r
118                         // fall back\r
119                         ftime = finished.lastModified();\r
120                 } catch (NumberFormatException ignore) {\r
121                         log.warn(\r
122                                 "NumberFormatException while reading FINISHED status file! Ignoring...",\r
123                                 ignore);\r
124                         // fall back\r
125                         ftime = finished.lastModified();\r
126                 }\r
127                 }\r
128                 return ftime;\r
129         }\r
130 \r
131         private Services getService() {\r
132                 return ServicesUtil.getServiceByJobDirectory(jobdir);\r
133         }\r
134 \r
135         long getResultSize() {\r
136                 Class<? extends Executable<?>> name = ServicesUtil\r
137                         .getRunnerByJobDirectory(jobdir);\r
138 \r
139                 File f = null;\r
140                 if (name.getSimpleName().equalsIgnoreCase("IUPred")) {\r
141                 f = files.get("out.glob");\r
142                 if (f == null)\r
143                         f = files.get("out.short");\r
144                 if (f == null)\r
145                         f = files.get("out.long");\r
146                 } else {\r
147                 f = files.get(SkeletalExecutable.OUTPUT);\r
148                 }\r
149                 if (f != null) {\r
150                 return f.length();\r
151                 }\r
152                 return UNDEFINED;\r
153         }\r
154 \r
155         long getInputSize() {\r
156                 Class<? extends Executable<?>> name = ServicesUtil\r
157                         .getRunnerByJobDirectory(jobdir);\r
158 \r
159                 File input = files.get(SkeletalExecutable.INPUT);\r
160                 if (input != null) {\r
161                 return input.length();\r
162                 }\r
163                 return UNDEFINED;\r
164         }\r
165 \r
166         JobStat getJobStat() {\r
167                 return JobStat.newInstance(getService(), getClusterJobID(),\r
168                         jobdir.getName(), getStartTime(), getFinishedTime(),\r
169                         getInputSize(), getResultSize(), isCancelled(),\r
170                         isCollected());\r
171         }\r
172 \r
173         public int hashCode() {\r
174                 final int prime = 31;\r
175                 int result = 1;\r
176                 result = prime * result\r
177                         + ((jobdir == null) ? 0 : jobdir.hashCode());\r
178                 return result;\r
179         }\r
180 \r
181         public boolean equals(Object obj) {\r
182                 if (this == obj)\r
183                 return true;\r
184                 if (obj == null)\r
185                 return false;\r
186                 if (getClass() != obj.getClass())\r
187                 return false;\r
188                 JobDirectory other = (JobDirectory) obj;\r
189                 if (jobdir == null) {\r
190                 if (other.jobdir != null)\r
191                         return false;\r
192                 } else if (!jobdir.equals(other.jobdir))\r
193                 return false;\r
194                 return true;\r
195         }\r
196 }\r
197 \r