GridJobSubmitInfo



GridJobSubmitInfo.java




GridJobSubmitInfo.java

package plgrid;

import java.io.Reader;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
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 GridJobSubmitInfo {
    private int submissionType = SUBMISSION_SINGLE;
    private int beginIndex;
    private int endIndex;
    private String outputPath;
    private String errorPath;
    private String username;
    private String nativeSpecification;
    private Properties environmentProperties;
    private String command;
    private List<GridJobArgument> arguments;
    private int numJobs = 1;
    private boolean privilegeEscalation;
    public static int SUBMISSION_SINGLE = 0;
    public static int SUBMISSION_ARRAY = 1;

    public void setOutputPath(String outputPath) {
        this.outputPath = outputPath;
    }

    public void setErrorPath(String errorPath) {
        this.errorPath = errorPath;
    }

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

    public void setNativeSpecification(String nativeSpecification) {
        this.nativeSpecification = nativeSpecification;
    }

    public void setEnvironmentProperties(Properties environmentProperties) {
        this.environmentProperties = environmentProperties;
    }

    public void setSubmissionType(int submissionType) {
        this.submissionType = submissionType;
    }

    public void setBeginIndex(int index) {
        this.beginIndex = index;
    }

    public void setEndIndex(int index) {
        this.endIndex = index;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public void setArguments(List<GridJobArgument> arguments) {
        this.arguments = arguments;
    }

    public void setPrivilegeEscalation(boolean privilegeEscalation) {
        this.privilegeEscalation = privilegeEscalation;
    }

    public void setNumJobs(int numJobs) {
        this.numJobs = numJobs;
    }

    public String getOutputPath() {
        return this.outputPath;
    }

    public String getErrorPath() {
        return this.errorPath;
    }

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

    public String getNativeSpecification() {
        return this.nativeSpecification;
    }

    public Properties getEnvironmentProperties() {
        return this.environmentProperties;
    }

    public int getSubmissionType() {
        return this.submissionType;
    }

    public int getBeginIndex() {
        return this.beginIndex;
    }

    public int getEndIndex() {
        return this.endIndex;
    }

    public String getCommand() {
        return this.command;
    }

    public List<GridJobArgument> getArguments() {
        return this.arguments;
    }

    public int getNumJobs() {
        return this.numJobs;
    }

    public boolean getPrivilegeEscalation() {
        return this.privilegeEscalation;
    }

    public Element toXMLElement() {
        Element gjsiElement = new Element("gridJobSubmitInfo");
        gjsiElement.addAttribute(new Attribute("submissionType", Integer.toString(this.submissionType)));
        gjsiElement.addAttribute(new Attribute("beginIndex", Integer.toString(this.beginIndex)));
        gjsiElement.addAttribute(new Attribute("endIndex", Integer.toString(this.endIndex)));
        if (this.username != null && this.username.length() > 0) {
            gjsiElement.addAttribute(new Attribute("username", this.username));
        }
        if (this.command != null && this.command.length() > 0) {
            gjsiElement.addAttribute(new Attribute("command", this.command));
        }
        if (this.arguments != null && this.arguments.size() > 0) {
            Element argsElement = new Element("arguments");
            for (GridJobArgument gja : this.arguments) {
                argsElement.appendChild((Node)gja.toXMLElement());
            }
            gjsiElement.appendChild((Node)argsElement);
        }
        gjsiElement.addAttribute(new Attribute("numJobs", Integer.toString(this.numJobs)));
        gjsiElement.addAttribute(new Attribute("privilegeEscalation", Boolean.toString(this.privilegeEscalation)));
        if (this.outputPath != null && this.outputPath.length() > 0) {
            gjsiElement.addAttribute(new Attribute("outputPath", this.outputPath));
        }
        if (this.errorPath != null && this.errorPath.length() > 0) {
            gjsiElement.addAttribute(new Attribute("errorPath", this.errorPath));
        }
        if (this.nativeSpecification != null && this.nativeSpecification.length() > 0) {
            gjsiElement.addAttribute(new Attribute("nativeSpecification", this.nativeSpecification));
        }
        if (this.environmentProperties != null && this.environmentProperties.size() > 0) {
            Element envPropertiesElement = new Element("envProperties");
            Enumeration propNames = this.environmentProperties.propertyNames();
            while (propNames.hasMoreElements()) {
                Element propElement = new Element("property");
                String propName = (String)propNames.nextElement();
                String propValue = this.environmentProperties.getProperty(propName);
                propElement.addAttribute(new Attribute("name", propName));
                propElement.addAttribute(new Attribute("value", propValue));
                envPropertiesElement.appendChild((Node)propElement);
            }
            if (envPropertiesElement.getChildCount() > 0) {
                gjsiElement.appendChild((Node)envPropertiesElement);
            }
        }
        return gjsiElement;
    }

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

    public static GridJobSubmitInfo fromXMLElement(Element gjsiElement) {
        Element envPropertiesElement;
        GridJobSubmitInfo gjsi = new GridJobSubmitInfo();
        int submissionType = Integer.parseInt(gjsiElement.getAttributeValue("submissionType"));
        int beginIndex = Integer.parseInt(gjsiElement.getAttributeValue("beginIndex"));
        int endIndex = Integer.parseInt(gjsiElement.getAttributeValue("endIndex"));
        gjsi.setSubmissionType(submissionType);
        gjsi.setBeginIndex(beginIndex);
        gjsi.setEndIndex(endIndex);
        String str = gjsiElement.getAttributeValue("outputPath");
        if (str != null) {
            gjsi.setOutputPath(str);
        }
        if ((str = gjsiElement.getAttributeValue("errorPath")) != null) {
            gjsi.setErrorPath(str);
        }
        if ((str = gjsiElement.getAttributeValue("nativeSpecification")) != null) {
            gjsi.setNativeSpecification(str);
        }
        if ((envPropertiesElement = gjsiElement.getFirstChildElement("envProperties")) != null) {
            Properties properties = new Properties();
            Elements propElements = envPropertiesElement.getChildElements("property");
            int numChildren = propElements.size();
            for (int i = 0; i < numChildren; ++i) {
                Element propElement = propElements.get(i);
                String propName = propElement.getAttributeValue("name");
                String propValue = propElement.getAttributeValue("value");
                if (propName == null || propValue == null) continue;
                properties.put(propName, propValue);
            }
            gjsi.setEnvironmentProperties(properties);
        }
        if ((str = gjsiElement.getAttributeValue("username")) != null) {
            gjsi.setUsername(str);
        }
        if ((str = gjsiElement.getAttributeValue("command")) != null) {
            gjsi.setCommand(str);
        }
        if ((str = gjsiElement.getAttributeValue("privilegeEscalation")) != null) {
            gjsi.setPrivilegeEscalation(Boolean.parseBoolean(str));
        }
        Element argsElement = gjsiElement.getFirstChildElement("arguments");
        LinkedList<GridJobArgument> args = new LinkedList<GridJobArgument>();
        if (argsElement != null) {
            Elements argElements = argsElement.getChildElements("argument");
            int numChildren = argElements.size();
            for (int i = 0; i < numChildren; ++i) {
                Element argElement = argElements.get(i);
                args.add(GridJobArgument.fromXMLElement(argElement));
            }
        }
        gjsi.setArguments(args);
        return gjsi;
    }

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