GridJobInfo



GridJobInfo.java




GridJobInfo.java

package plgrid;

import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.Node;

/* 
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6. 
 */
public class GridJobInfo {
    private int state;
    private final String jobId;
    private String queue;
    private String cpu;
    private String mem;
    private String io;
    private String username;
    private String jobname;
    private long startTime;
    private long queuedTime;
    private long finishTime;
    private Integer exitStatus;
    private Map<String, String> complexVariables;
    public static final int STATE_UNKNOWN = 0;
    public static final int STATE_QUEUED = 1;
    public static final int STATE_RUNNING = 2;
    public static final int STATE_FINISHED = 3;
    public static final int STATE_NOT_FOUND = 4;
    public static final int STATE_ERRORED = 5;

    public GridJobInfo(String jobId) {
        this.jobId = jobId;
        this.complexVariables = new HashMap<String, String>();
    }

    public void addComplexVariable(String name, String value) {
        this.complexVariables.put(name, value);
    }

    public void removeComplexVariable(String name) {
        this.complexVariables.remove(name);
    }

    public Map<String, String> getComplexVariables() {
        return this.complexVariables;
    }

    public void setState(int state) {
        this.state = state;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public void setQueuedTime(long queuedTime) {
        this.queuedTime = queuedTime;
    }

    public void setFinishTime(long finishTime) {
        this.finishTime = finishTime;
    }

    public void setExitStatus(int exitStatus) {
        this.exitStatus = exitStatus;
    }

    public void setQueue(String queue) {
        this.queue = queue;
    }

    public void setCPU(String cpu) {
        this.cpu = cpu;
    }

    public void setMemory(String mem) {
        this.mem = mem;
    }

    public void setIO(String io) {
        this.io = io;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setJobname(String name) {
        this.jobname = name;
    }

    public void setExitStatus(Integer exitStatus) {
        this.exitStatus = exitStatus;
    }

    public int getState() {
        return this.state;
    }

    public String getJobId() {
        return this.jobId;
    }

    public long getStartTime() {
        return this.startTime;
    }

    public long getQueuedTime() {
        return this.queuedTime;
    }

    public long getFinishTime() {
        return this.finishTime;
    }

    public int getExitStatus() {
        return this.exitStatus;
    }

    public String getQueue() {
        return this.queue;
    }

    public String getCPU() {
        return this.cpu;
    }

    public String getMemory() {
        return this.mem;
    }

    public String getIO() {
        return this.io;
    }

    public String getUsername() {
        return this.username;
    }

    public String getJobname() {
        return this.jobname;
    }

    public Element toXMLElement() {
        Element gjiElement = new Element("gridJobInfo");
        if (this.jobId != null && this.jobId.length() > 0) {
            gjiElement.addAttribute(new Attribute("jobId", this.jobId));
        }
        if (this.queue != null) {
            gjiElement.addAttribute(new Attribute("queue", this.queue));
        }
        if (this.cpu != null) {
            gjiElement.addAttribute(new Attribute("cpu", this.cpu));
        }
        if (this.mem != null) {
            gjiElement.addAttribute(new Attribute("mem", this.mem));
        }
        if (this.io != null) {
            gjiElement.addAttribute(new Attribute("io", this.io));
        }
        if (this.username != null) {
            gjiElement.addAttribute(new Attribute("username", this.username));
        }
        if (this.jobname != null) {
            gjiElement.addAttribute(new Attribute("jobname", this.jobname));
        }
        if (this.startTime > 0) {
            gjiElement.addAttribute(new Attribute("startTime", Long.toString(this.startTime)));
        }
        if (this.queuedTime > 0) {
            gjiElement.addAttribute(new Attribute("queuedTime", Long.toString(this.queuedTime)));
        }
        if (this.finishTime > 0) {
            gjiElement.addAttribute(new Attribute("finishTime", Long.toString(this.finishTime)));
        }
        if (this.exitStatus != null) {
            gjiElement.addAttribute(new Attribute("exitStatus", Integer.toString(this.exitStatus)));
        }
        gjiElement.addAttribute(new Attribute("state", Integer.toString(this.state)));
        if (this.complexVariables != null && this.complexVariables.size() > 0) {
            Element complexVarsElement = new Element("complexVariables");
            for (String complexVarName : this.complexVariables.keySet()) {
                String complexVarValue = this.complexVariables.get(complexVarName);
                Element complexVarElement = new Element("complexVar");
                complexVarElement.addAttribute(new Attribute("name", complexVarName));
                complexVarElement.addAttribute(new Attribute("value", complexVarValue));
                complexVarsElement.appendChild((Node)complexVarElement);
            }
            if (complexVarsElement.getChildCount() > 0) {
                gjiElement.appendChild((Node)complexVarsElement);
            }
        }
        return gjiElement;
    }

    public String toXML() {
        return new Document(this.toXMLElement()).toXML();
    }

    public static GridJobInfo fromXMLElement(Element gjiElement) {
        Element complexVariablesElement;
        String str = null;
        str = gjiElement.getAttributeValue("jobId");
        if (str == null) {
            return null;
        }
        GridJobInfo gji = new GridJobInfo(str);
        str = gjiElement.getAttributeValue("queue");
        if (str != null) {
            gji.setQueue(str);
        }
        if ((str = gjiElement.getAttributeValue("cpu")) != null) {
            gji.setCPU(str);
        }
        if ((str = gjiElement.getAttributeValue("mem")) != null) {
            gji.setMemory(str);
        }
        if ((str = gjiElement.getAttributeValue("io")) != null) {
            gji.setIO(str);
        }
        if ((str = gjiElement.getAttributeValue("username")) != null) {
            gji.setUsername(str);
        }
        if ((str = gjiElement.getAttributeValue("jobname")) != null) {
            gji.setJobname(str);
        }
        if ((str = gjiElement.getAttributeValue("startTime")) != null) {
            gji.setStartTime(Long.parseLong(str));
        }
        if ((str = gjiElement.getAttributeValue("queuedTime")) != null) {
            gji.setQueuedTime(Long.parseLong(str));
        }
        if ((str = gjiElement.getAttributeValue("finishTime")) != null) {
            gji.setFinishTime(Long.parseLong(str));
        }
        if ((str = gjiElement.getAttributeValue("exitStatus")) != null) {
            gji.setExitStatus(Integer.parseInt(str));
        }
        if ((str = gjiElement.getAttributeValue("state")) != null) {
            gji.setState(Integer.parseInt(str));
        }
        if ((complexVariablesElement = gjiElement.getFirstChildElement("complexVariables")) != null) {
            Elements complexVarElements = complexVariablesElement.getChildElements("complexVar");
            int numChildren = complexVarElements.size();
            for (int i = 0; i < numChildren; ++i) {
                Element complexVarElement = complexVarElements.get(i);
                String name = complexVarElement.getAttributeValue("name");
                String value = complexVarElement.getAttributeValue("value");
                if (name == null || value == null) continue;
                gji.addComplexVariable(name, value);
            }
        }
        return gji;
    }

    public static GridJobInfo fromXML(String xml) {
        StringReader sr = new StringReader(xml);
        Builder builder = new Builder();
        Document doc = null;
        try {
            doc = builder.build((Reader)sr);
        }
        catch (Exception ex) {
            return null;
        }
        return GridJobInfo.fromXMLElement(doc.getRootElement());
    }
}