Search in sources :

Example 6 with GlobalVariablesData

use of org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesData in project scheduling by ow2-proactive.

the class StaxJobFactory method getUnresolvedGenericInformations.

/**
 * Get the defined generic information of the entity.
 * Leave the method at the end of 'ELEMENT_COMMON_GENERIC_INFORMATION' tag.
 *
 * @param cursorInfo the streamReader with the cursor on the 'ELEMENT_COMMON_GENERIC_INFORMATION' tag.
 * @return the list of generic information as a hashMap.
 */
private Map<String, String> getUnresolvedGenericInformations(XMLStreamReader cursorInfo, boolean isTask, GlobalVariablesData globalVariablesData) throws JobCreationException {
    // The following initializaion is to enable overridding of global generic info by workflow gi
    Map<String, String> infos = isTask ? new LinkedHashMap<>() : globalVariablesData.getGenericInformation();
    try {
        int eventType;
        while (cursorInfo.hasNext()) {
            eventType = cursorInfo.next();
            switch(eventType) {
                case XMLEvent.START_ELEMENT:
                    if (XMLTags.COMMON_INFO.matches(cursorInfo.getLocalName())) {
                        Map<String, String> attributesAsMap = getUnresolvedAttributesAsMap(cursorInfo);
                        String name = attributesAsMap.get(XMLAttributes.COMMON_NAME.getXMLName());
                        String value = attributesAsMap.get(XMLAttributes.COMMON_VALUE.getXMLName());
                        infos.put(name, value);
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (XMLTags.COMMON_GENERIC_INFORMATION.matches(cursorInfo.getLocalName())) {
                        return infos;
                    }
                    break;
                default:
            }
        }
        return infos;
    } catch (JobCreationException jce) {
        jce.pushTag(cursorInfo.getLocalName());
        throw jce;
    } catch (Exception e) {
        String attrtmp = null;
        if (cursorInfo.isStartElement() && cursorInfo.getAttributeCount() == 1) {
            attrtmp = cursorInfo.getAttributeLocalName(0);
        }
        throw new JobCreationException(cursorInfo.getLocalName(), attrtmp, e);
    }
}
Also used : 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) IOException(java.io.IOException)

Example 7 with GlobalVariablesData

use of org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesData 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, GlobalVariablesData globalVariablesData) 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, globalVariablesData);
                        // 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:
                }
            } else {
                // Leave the method with the cursor at the end of the taskflow tag
                break;
            }
        }
    } 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) IOException(java.io.IOException)

Example 8 with GlobalVariablesData

use of org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesData in project scheduling by ow2-proactive.

the class StaxJobFactory method createJob.

/**
 * Start parsing and creating the job.
 *
 * @throws JobCreationException if an error occurred during job creation process.
 */
private Job createJob(XMLStreamReader cursorRoot, Map<String, String> replacementVariables, Map<String, String> replacementGenericInfos, Map<String, ArrayList<String>> dependencies, String jobContent) throws JobCreationException {
    String current = null;
    GlobalVariablesData globalVariablesData = getConfiguredGlobalVariablesData(jobContent);
    // start parsing
    try {
        int eventType;
        Job job = null;
        while (cursorRoot.hasNext()) {
            eventType = cursorRoot.next();
            if (eventType == XMLEvent.START_ELEMENT) {
                current = cursorRoot.getLocalName();
                if (XMLTags.JOB.matches(current)) {
                    // first tag of the job.
                    job = createAndFillJob(cursorRoot, replacementVariables, replacementGenericInfos, jobContent, globalVariablesData);
                } else if (XMLTags.TASK.matches(current)) {
                    // once here, the job instance has been created
                    fillJobWithTasks(cursorRoot, job, dependencies, globalVariablesData);
                } else if (XMLTags.METADATA_VISUALIZATION.matches(current) && job != null) {
                    // Add workflow visualization's embedded html
                    job.setVisualization(getJobVisualization(cursorRoot));
                    // Metadata is the last element to parse
                    break;
                }
            }
        }
        if (job != null) {
            resolveCleaningScripts((TaskFlowJob) job, job.getVariablesAsReplacementMap());
        }
        Map<String, JobVariable> globalVariables = new LinkedHashMap<>();
        for (String globalVariable : globalVariablesData.getVariables().keySet()) {
            globalVariables.put(globalVariable, job.getVariables().get(globalVariable));
        }
        job.setGlobalVariables(globalVariables);
        Map<String, String> globalGenericInfoMap = new LinkedHashMap<>();
        for (String globalGenericInfo : globalVariablesData.getGenericInformation().keySet()) {
            globalGenericInfoMap.put(globalGenericInfo, job.getGenericInformation().get(globalGenericInfo));
        }
        job.setGlobalGenericInformation(globalGenericInfoMap);
        return job;
    } catch (JobCreationException jce) {
        if (XMLTags.TASK.matches(current)) {
            jce.pushTag(XMLTags.TASK_FLOW.getXMLName());
        }
        throw jce;
    } catch (Exception e) {
        throw new JobCreationException(current, null, e);
    }
}
Also used : GlobalVariablesData(org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesData) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) Job(org.ow2.proactive.scheduler.common.job.Job) 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) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with GlobalVariablesData

use of org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesData in project scheduling by ow2-proactive.

the class GlobalVariablesParserTest method testParseConfigTwoVars.

@Test
public void testParseConfigTwoVars() throws IOException {
    GlobalVariablesParser.setConfigurationPath(GlobalVariablesParserTest.class.getResource("global_variables_two_vars.xml").toExternalForm());
    GlobalVariablesParser.getInstance().reloadFilters();
    List<Filter> filters = GlobalVariablesParser.getInstance().getLoadedFilters();
    Assert.assertEquals(1, filters.size());
    Assert.assertEquals(1, filters.get(0).getXpath().size());
    Assert.assertEquals(".", filters.get(0).getXpath().get(0));
    Assert.assertEquals(2, filters.get(0).getVariables().size());
    Assert.assertEquals(new JobVariable("var1", "value1", "model1"), filters.get(0).getVariables().get(0));
    Assert.assertEquals(new JobVariable("var2", "value2", "model2"), filters.get(0).getVariables().get(1));
    GlobalVariablesData globalData = GlobalVariablesParser.getInstance().getVariablesFor(IOUtils.toString(GlobalVariablesParserTest.class.getResource("/org/ow2/proactive/scheduler/common/job/factories/job_no_variables.xml"), PASchedulerProperties.FILE_ENCODING.getValueAsString()));
    ;
    Map<String, JobVariable> globalVariables = globalData.getVariables();
    Assert.assertEquals(2, globalVariables.size());
    Assert.assertEquals(new JobVariable("var1", "value1", "model1"), globalVariables.get("var1"));
    Assert.assertEquals(new JobVariable("var2", "value2", "model2"), globalVariables.get("var2"));
    Map<String, String> globalGI = globalData.getGenericInformation();
    Assert.assertEquals(2, globalGI.size());
    Assert.assertEquals("gi_value1", globalGI.get("gi1"));
    Assert.assertEquals("gi_value2", globalGI.get("gi2"));
}
Also used : JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) Test(org.junit.Test)

Example 10 with GlobalVariablesData

use of org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesData in project scheduling by ow2-proactive.

the class GlobalVariablesParserTest method testParseConfigOneFilterOK.

@Test
public void testParseConfigOneFilterOK() throws IOException {
    GlobalVariablesParser.setConfigurationPath(GlobalVariablesParserTest.class.getResource("global_variables_one_filter.xml").toExternalForm());
    GlobalVariablesParser.getInstance().reloadFilters();
    List<Filter> filters = GlobalVariablesParser.getInstance().getLoadedFilters();
    Assert.assertEquals(1, filters.size());
    Assert.assertEquals(2, filters.get(0).getXpath().size());
    Assert.assertEquals(2, filters.get(0).getVariables().size());
    GlobalVariablesData globalData = GlobalVariablesParser.getInstance().getVariablesFor(IOUtils.toString(GlobalVariablesParserTest.class.getResource("/org/ow2/proactive/scheduler/common/job/factories/job_with_global_variables_and_gi.xml"), PASchedulerProperties.FILE_ENCODING.getValueAsString()));
    Map<String, JobVariable> globalVariables = globalData.getVariables();
    // As this job matches the xpath filters, it will return the configured variables
    Assert.assertEquals(2, globalVariables.size());
    Assert.assertEquals(new JobVariable("var1", "value1", "model1"), globalVariables.get("var1"));
    Assert.assertEquals(new JobVariable("var2", "value2", "model2"), globalVariables.get("var2"));
    Map<String, String> globalGI = globalData.getGenericInformation();
    Assert.assertEquals(2, globalGI.size());
    Assert.assertEquals("gi_value1", globalGI.get("gi1"));
    Assert.assertEquals("gi_value2", globalGI.get("gi2"));
}
Also used : JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)8 JobVariable (org.ow2.proactive.scheduler.common.job.JobVariable)7 FileNotFoundException (java.io.FileNotFoundException)6 XMLStreamException (javax.xml.stream.XMLStreamException)6 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)6 JobValidationException (org.ow2.proactive.scheduler.common.exception.JobValidationException)6 LinkedHashMap (java.util.LinkedHashMap)5 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 LRUMap (org.apache.commons.collections4.map.LRUMap)2 Test (org.junit.Test)2 Job (org.ow2.proactive.scheduler.common.job.Job)2 GlobalVariablesData (org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesData)2 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)2 NativeTask (org.ow2.proactive.scheduler.common.task.NativeTask)2 ScriptTask (org.ow2.proactive.scheduler.common.task.ScriptTask)2 Task (org.ow2.proactive.scheduler.common.task.Task)2 StringReader (java.io.StringReader)1