1 /* Copyright (c) 2013 Alexander Sherstnev
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.stat.collector;
\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
25 import org.apache.log4j.Logger;
\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
36 * @author Alexander Sherstnev
\r
39 public class JobDirectory {
\r
41 static final int UNDEFINED = -1;
\r
43 private static final Logger log = Logger.getLogger(JobDirectory.class);
\r
46 Map<String, File> files = new HashMap<String, File>();
\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
55 boolean hasStatus(JobStatus status) {
\r
56 return files.containsKey(status.toString());
\r
59 boolean isCollected() {
\r
60 return hasStatus(JobStatus.COLLECTED);
\r
63 boolean isCancelled() {
\r
64 return hasStatus(JobStatus.CANCELLED);
\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
74 if (startfile != null) {
\r
75 String start = FileUtil.readFileToString(startfile);
\r
76 starttime = Long.parseLong(start.trim());
\r
78 } catch (IOException ignore) {
\r
79 log.warn("IOException while reading STARTED status file! Ignoring...", ignore);
\r
81 starttime = startfile.lastModified();
\r
82 } catch (NumberFormatException ignore) {
\r
83 log.warn("NumberFormatException while reading STARTED status file! Ignoring...", ignore);
\r
85 starttime = startfile.lastModified();
\r
90 String getClusterJobID() {
\r
91 String clustjobId = "";
\r
92 File jobid = files.get("JOBID");
\r
94 if (jobid != null) {
\r
95 clustjobId = FileUtil.readFileToString(jobid);
\r
97 } catch (IOException ioe) {
\r
99 "IO Exception while reading the content of JOBID file for job "
\r
102 return clustjobId.trim();
\r
105 long getFinishedTime() {
\r
106 long ftime = UNDEFINED;
\r
107 File finished = files.get(JobStatus.FINISHED.toString());
\r
108 if (finished != null) {
\r
110 if (finished != null) {
\r
111 String start = FileUtil.readFileToString(finished);
\r
112 ftime = Long.parseLong(start.trim());
\r
114 } catch (IOException ignore) {
\r
116 "IOException while reading FINISHED status file! Ignoring...",
\r
119 ftime = finished.lastModified();
\r
120 } catch (NumberFormatException ignore) {
\r
122 "NumberFormatException while reading FINISHED status file! Ignoring...",
\r
125 ftime = finished.lastModified();
\r
131 private Services getService() {
\r
132 return ServicesUtil.getServiceByJobDirectory(jobdir);
\r
135 long getResultSize() {
\r
136 Class<? extends Executable<?>> name = ServicesUtil
\r
137 .getRunnerByJobDirectory(jobdir);
\r
140 if (name.getSimpleName().equalsIgnoreCase("IUPred")) {
\r
141 f = files.get("out.glob");
\r
143 f = files.get("out.short");
\r
145 f = files.get("out.long");
\r
147 f = files.get(SkeletalExecutable.OUTPUT);
\r
155 long getInputSize() {
\r
156 Class<? extends Executable<?>> name = ServicesUtil
\r
157 .getRunnerByJobDirectory(jobdir);
\r
159 File input = files.get(SkeletalExecutable.INPUT);
\r
160 if (input != null) {
\r
161 return input.length();
\r
166 JobStat getJobStat() {
\r
167 return JobStat.newInstance(getService(), getClusterJobID(),
\r
168 jobdir.getName(), getStartTime(), getFinishedTime(),
\r
169 getInputSize(), getResultSize(), isCancelled(),
\r
173 public int hashCode() {
\r
174 final int prime = 31;
\r
176 result = prime * result
\r
177 + ((jobdir == null) ? 0 : jobdir.hashCode());
\r
181 public boolean equals(Object obj) {
\r
186 if (getClass() != obj.getClass())
\r
188 JobDirectory other = (JobDirectory) obj;
\r
189 if (jobdir == null) {
\r
190 if (other.jobdir != null)
\r
192 } else if (!jobdir.equals(other.jobdir))
\r