Search in sources :

Example 6 with JobCreationException

use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.

the class StaxJobFactory method createForkEnvironment.

/**
 * Create the forkEnvironment of a java task
 * The cursor is currently at the beginning of the 'FORK_ENVIRONMENT' tag.
 *
 * @param cursorExec the streamReader with the cursor on the 'FORK_ENVIRONMENT' tag.
 * @return The created ForkEnvironment
 */
private ForkEnvironment createForkEnvironment(XMLStreamReader cursorExec, Map<String, String> variables) throws JobCreationException {
    ForkEnvironment forkEnv = new ForkEnvironment();
    int i = 0;
    String current = cursorExec.getLocalName();
    try {
        // parsing executable attributes
        int attrCount = cursorExec.getAttributeCount();
        for (i = 0; i < attrCount; i++) {
            String attrName = cursorExec.getAttributeLocalName(i);
            if (XMLAttributes.FORK_JAVA_HOME.matches(attrName)) {
                forkEnv.setJavaHome(replace(cursorExec.getAttributeValue(i), variables));
            }
            if (XMLAttributes.TASK_WORKDING_DIR.matches(attrName)) {
                forkEnv.setWorkingDir(replace(cursorExec.getAttributeValue(i), variables));
            }
        }
        // parsing executable tags
        int eventType;
        while (cursorExec.hasNext()) {
            eventType = cursorExec.next();
            switch(eventType) {
                case XMLEvent.START_ELEMENT:
                    current = cursorExec.getLocalName();
                    if (XMLTags.FORK_SYSTEM_PROPERTY.matches(current)) {
                        attrCount = cursorExec.getAttributeCount();
                        String name = null, value = null;
                        for (i = 0; i < attrCount; i++) {
                            String attrName = cursorExec.getAttributeLocalName(i);
                            if (XMLAttributes.COMMON_NAME.matches(attrName)) {
                                name = replace(cursorExec.getAttributeValue(i), variables);
                            }
                            if (XMLAttributes.COMMON_VALUE.matches(attrName)) {
                                value = replace(cursorExec.getAttributeValue(i), variables);
                            }
                        }
                        forkEnv.addSystemEnvironmentVariable(name, value);
                    } else if (XMLTags.FORK_JVM_ARG.matches(current)) {
                        forkEnv.addJVMArgument(replace(cursorExec.getAttributeValue(0), variables));
                    } else if (XMLTags.JOB_PATH_ELEMENT.matches(current)) {
                        forkEnv.addAdditionalClasspath(replace(cursorExec.getAttributeValue(0), variables));
                    } else if (XMLTags.SCRIPT_ENV.matches(current)) {
                        forkEnv.setEnvScript(new ForkEnvironmentScript(createScript(cursorExec, variables)));
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (XMLTags.FORK_ENVIRONMENT.matches(cursorExec.getLocalName())) {
                        return forkEnv;
                    }
                    break;
            }
        }
        return forkEnv;
    } catch (JobCreationException jce) {
        jce.pushTag(current);
        throw jce;
    } catch (Exception e) {
        String attrtmp = null;
        if (cursorExec.isStartElement() && cursorExec.getAttributeCount() > 0) {
            attrtmp = cursorExec.getAttributeLocalName(i);
        }
        throw new JobCreationException(current, attrtmp, e);
    }
}
Also used : JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) ForkEnvironmentScript(org.ow2.proactive.scripting.ForkEnvironmentScript) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException)

Example 7 with JobCreationException

use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.

the class StaxJobFactory method fillJobWithTasks.

/**
 * Fill the created Job with coming tasks..
 * Leave the method with the cursor at the end of the file (nothing more has to be parsed).
 *
 * @param cursorTask the streamReader with the cursor on the first 'ELEMENT_TASK' tag.
 */
private void fillJobWithTasks(XMLStreamReader cursorTask, Job job, Map<String, ArrayList<String>> dependencies) throws JobCreationException {
    if (job == null) {
        throw new JobCreationException(XMLTags.JOB.getXMLName(), null, null);
    }
    XMLTags current = null;
    try {
        int eventType = -1;
        while (cursorTask.hasNext()) {
            // if use to keep the cursor on the task tag for the first loop
            if (eventType == -1) {
                eventType = cursorTask.getEventType();
            } else {
                eventType = cursorTask.next();
            }
            if (eventType == XMLEvent.START_ELEMENT && XMLTags.TASK.matches(cursorTask.getLocalName())) {
                Task t;
                switch(job.getType()) {
                    case TASKSFLOW:
                        current = XMLTags.TASK;
                        // create new task
                        t = createTask(cursorTask, job, dependencies);
                        // add task to the job
                        ((TaskFlowJob) job).addTask(t);
                        break;
                    case PARAMETER_SWEEPING:
                        current = XMLTags.TASK;
                        throw new RuntimeException("Job parameter sweeping is not yet implemented!");
                    default:
                }
            }
        }
    } catch (JobCreationException jce) {
        jce.pushTag(current);
        throw jce;
    } catch (Exception e) {
        throw new JobCreationException(current, null, e);
    }
}
Also used : Task(org.ow2.proactive.scheduler.common.task.Task) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) ScriptTask(org.ow2.proactive.scheduler.common.task.ScriptTask) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException)

Example 8 with JobCreationException

use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.

the class StaxJobFactory method createJob.

private Job createJob(File file, Map<String, String> replacementVariables) throws JobCreationException {
    try {
        // Check if the file exist
        if (!file.exists()) {
            throw new FileNotFoundException("This file has not been found: " + file.getAbsolutePath());
        }
        // validate content using the proper XML schema
        File updatedFile = validate(file);
        // set relative path
        relativePathRoot = updatedFile.getParentFile().getAbsolutePath();
        // create and get XML STAX reader
        XMLStreamReader xmlsr;
        Map<String, ArrayList<String>> dependencies = new HashMap<>();
        Job job;
        try (InputStream inputStream = new FileInputStream(updatedFile)) {
            // use the server side property to accept encoding
            if (PASchedulerProperties.FILE_ENCODING.isSet()) {
                xmlsr = xmlInputFactory.createXMLStreamReader(inputStream, PASchedulerProperties.FILE_ENCODING.getValueAsString());
            } else {
                xmlsr = xmlInputFactory.createXMLStreamReader(inputStream);
            }
            // Create the job starting at the first cursor position of the XML Stream reader
            job = createJob(xmlsr, replacementVariables, dependencies);
            // Close the stream
            xmlsr.close();
        }
        // make dependencies
        makeDependences(job, dependencies);
        validate((TaskFlowJob) job);
        logger.debug("Job successfully created!");
        // debug mode only
        displayJobInfo(job);
        return job;
    } catch (JobCreationException jce) {
        jce.pushTag(XMLTags.JOB.getXMLName());
        throw jce;
    } catch (Exception e) {
        throw new JobCreationException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) Job(org.ow2.proactive.scheduler.common.job.Job) File(java.io.File) FileInputStream(java.io.FileInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException)

Example 9 with JobCreationException

use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.

the class StaxJobFactory method createJobVariables.

/**
 * Create a map of variables from XML variables.
 * Leave the method with the cursor at the end of 'ELEMENT_VARIABLES' tag
 *
 * @param cursorVariables the streamReader with the cursor on the 'ELEMENT_VARIABLES' tag.
 * @param replacementVariables variables which have precedence over the one defined in the job
 * @return the map in which the variables were added.
 * @throws JobCreationException
 */
private Map<String, JobVariable> createJobVariables(XMLStreamReader cursorVariables, Map<String, String> replacementVariables) throws JobCreationException {
    HashMap<String, JobVariable> variablesMap = new LinkedHashMap<>();
    try {
        int eventType;
        while (cursorVariables.hasNext()) {
            eventType = cursorVariables.next();
            switch(eventType) {
                case XMLEvent.START_ELEMENT:
                    if (XMLTags.VARIABLE.matches(cursorVariables.getLocalName())) {
                        Map<String, String> attributesAsMap = getAttributesAsMap(cursorVariables, null);
                        String name = attributesAsMap.get(XMLAttributes.VARIABLE_NAME.getXMLName());
                        String value = attributesAsMap.get(XMLAttributes.VARIABLE_VALUE.getXMLName());
                        String model = attributesAsMap.get(XMLAttributes.VARIABLE_MODEL.getXMLName());
                        variablesMap.put(name, new JobVariable(name, value, model));
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (XMLTags.VARIABLES.matches(cursorVariables.getLocalName())) {
                        return replaceVariablesInJobVariablesMap(variablesMap, replacementVariables);
                    }
                    break;
            }
        }
    } catch (JobCreationException jce) {
        jce.pushTag(cursorVariables.getLocalName());
        throw jce;
    } catch (Exception e) {
        String attrtmp = null;
        if (cursorVariables.isStartElement() && cursorVariables.getAttributeCount() == 1) {
            attrtmp = cursorVariables.getAttributeLocalName(0);
        }
        throw new JobCreationException(cursorVariables.getLocalName(), attrtmp, e);
    }
    return variablesMap;
}
Also used : JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with JobCreationException

use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.

the class StaxJobFactory method setNativeExecutable.

/**
 * Add the Native Executable to this native Task.
 * The cursor is currently at the beginning of the 'ELEMENT_NATIVE_EXECUTABLE' tag.
 *
 * @param nativeTask the task in which to add the Native Executable.
 * @param cursorExec the streamReader with the cursor on the 'ELEMENT_NATIVE_EXECUTABLE' tag.
 */
private void setNativeExecutable(NativeTask nativeTask, XMLStreamReader cursorExec) throws JobCreationException {
    int i = 0;
    String current = null;
    try {
        // one step ahead to go to the command (static or dynamic)
        while (cursorExec.next() != XMLEvent.START_ELEMENT) ;
        current = cursorExec.getLocalName();
        ArrayList<String> command = new ArrayList<>(0);
        if (XMLTags.NATIVE_TASK_STATIC_COMMAND.matches(cursorExec.getLocalName())) {
            String attr_ = null;
            String current_ = null;
            try {
                for (i = 0; i < cursorExec.getAttributeCount(); i++) {
                    String attrName = cursorExec.getAttributeLocalName(i);
                    attr_ = attrName;
                    if (XMLAttributes.TASK_COMMAND_VALUE.matches(attrName)) {
                        command.add((cursorExec.getAttributeValue(i)));
                    }
                    if (XMLAttributes.TASK_WORKDING_DIR.matches(attrName)) {
                        logger.warn(XMLAttributes.TASK_WORKDING_DIR.getXMLName() + " attribute no longer supported. Please use a forkEnvironment for defining a working directory.");
                    }
                }
                int eventType;
                while (cursorExec.hasNext()) {
                    eventType = cursorExec.next();
                    switch(eventType) {
                        case XMLEvent.START_ELEMENT:
                            current_ = cursorExec.getLocalName();
                            if (XMLTags.SCRIPT_ARGUMENT.matches(cursorExec.getLocalName())) {
                                command.add((cursorExec.getAttributeValue(0)));
                            }
                            break;
                        case XMLEvent.END_ELEMENT:
                            if (XMLTags.NATIVE_EXECUTABLE.matches(cursorExec.getLocalName())) {
                                nativeTask.setCommandLine(command.toArray(new String[command.size()]));
                                return;
                            }
                            break;
                    }
                }
            } catch (Exception e) {
                throw new JobCreationException(current_, attr_, e);
            }
        } else {
            throw new RuntimeException("Unknown command type: " + cursorExec.getLocalName());
        }
    } catch (JobCreationException jce) {
        jce.pushTag(current);
        throw jce;
    } catch (Exception e) {
        String temporaryAttribute = null;
        if (cursorExec.isStartElement() && cursorExec.getAttributeCount() > 0) {
            temporaryAttribute = cursorExec.getAttributeLocalName(i);
        }
        throw new JobCreationException(current, temporaryAttribute, e);
    }
}
Also used : ArrayList(java.util.ArrayList) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException)

Aggregations

JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)37 VerifierConfigurationException (org.iso_relax.verifier.VerifierConfigurationException)21 FileNotFoundException (java.io.FileNotFoundException)20 XMLStreamException (javax.xml.stream.XMLStreamException)20 JobValidationException (org.ow2.proactive.scheduler.common.exception.JobValidationException)20 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)13 LinkedHashMap (java.util.LinkedHashMap)10 HashMap (java.util.HashMap)9 Job (org.ow2.proactive.scheduler.common.job.Job)9 PermissionException (org.ow2.proactive.scheduler.common.exception.PermissionException)8 NotConnectedException (org.ow2.proactive.scheduler.common.exception.NotConnectedException)7 SubmissionClosedException (org.ow2.proactive.scheduler.common.exception.SubmissionClosedException)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 JobAlreadyFinishedException (org.ow2.proactive.scheduler.common.exception.JobAlreadyFinishedException)6 UnknownJobException (org.ow2.proactive.scheduler.common.exception.UnknownJobException)6 UnknownTaskException (org.ow2.proactive.scheduler.common.exception.UnknownTaskException)6 InvalidScriptException (org.ow2.proactive.scripting.InvalidScriptException)6 File (java.io.File)5 IOException (java.io.IOException)5