Search in sources :

Example 11 with JobVariable

use of org.ow2.proactive.scheduler.common.job.JobVariable in project scheduling by ow2-proactive.

the class StaxJobFactory method createUnresolvedJobVariables.

/**
 * 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.
 * @return the map in which the variables were added.
 * @throws JobCreationException
 */
private Map<String, JobVariable> createUnresolvedJobVariables(XMLStreamReader cursorVariables, GlobalVariablesData globalVariablesData) throws JobCreationException {
    // The following initializaion is to enable overridding of global variables by workflow variables
    Map<String, JobVariable> unresolvedVariablesMap = globalVariablesData.getVariables();
    try {
        int eventType;
        while (cursorVariables.hasNext()) {
            eventType = cursorVariables.next();
            switch(eventType) {
                case XMLEvent.START_ELEMENT:
                    if (XMLTags.VARIABLE.matches(cursorVariables.getLocalName())) {
                        Map<String, String> unresolvedAttributesAsMap = getUnresolvedAttributesAsMap(cursorVariables);
                        String name = unresolvedAttributesAsMap.get(XMLAttributes.VARIABLE_NAME.getXMLName());
                        String value = unresolvedAttributesAsMap.get(XMLAttributes.VARIABLE_VALUE.getXMLName());
                        String model = unresolvedAttributesAsMap.get(XMLAttributes.VARIABLE_MODEL.getXMLName());
                        String description = unresolvedAttributesAsMap.get(XMLAttributes.VARIABLE_DESCRIPTION.getXMLName());
                        String group = unresolvedAttributesAsMap.get(XMLAttributes.VARIABLE_GROUP.getXMLName());
                        boolean advanced = false;
                        if (unresolvedAttributesAsMap.containsKey(XMLAttributes.VARIABLE_ADVANCED.getXMLName())) {
                            advanced = Boolean.parseBoolean(unresolvedAttributesAsMap.get(XMLAttributes.VARIABLE_ADVANCED.getXMLName()));
                        }
                        boolean hidden = false;
                        if (unresolvedAttributesAsMap.containsKey(XMLAttributes.VARIABLE_HIDDEN.getXMLName())) {
                            hidden = Boolean.parseBoolean(unresolvedAttributesAsMap.get(XMLAttributes.VARIABLE_HIDDEN.getXMLName()));
                        }
                        unresolvedVariablesMap.put(name, new JobVariable(name, value, model, description, group, advanced, hidden));
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (XMLTags.VARIABLES.matches(cursorVariables.getLocalName())) {
                        return unresolvedVariablesMap;
                    }
                    break;
                default:
            }
        }
    } 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 unresolvedVariablesMap;
}
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) IOException(java.io.IOException)

Example 12 with JobVariable

use of org.ow2.proactive.scheduler.common.job.JobVariable in project scheduling by ow2-proactive.

the class GlobalVariablesParser method getVariablesFor.

/**
 * Return the global variables and generic information configured for the given workflow
 * @param jobContent xml workflow as string
 * @return global data containing variables and generic information
 */
public synchronized GlobalVariablesData getVariablesFor(String jobContent) {
    GlobalVariablesData answer = new GlobalVariablesData();
    Map<String, JobVariable> configuredVariables = new LinkedHashMap<>();
    Map<String, String> configuredGenericInfo = new LinkedHashMap<>();
    answer.setVariables(configuredVariables);
    answer.setGenericInformation(configuredGenericInfo);
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = builderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        logger.error("Error when configuring DocumentBuilder", e);
        return answer;
    }
    try (StringReader reader = new StringReader(jobContent)) {
        InputSource inputSource = new InputSource(reader);
        Document xmlDocument = builder.parse(inputSource);
        XPath xPath = XPathFactory.newInstance().newXPath();
        for (Filter filter : loadedFilters) {
            boolean allMatch = true;
            for (String xpathExpression : filter.getXpath()) {
                NodeList nodeList = (NodeList) xPath.compile(xpathExpression).evaluate(xmlDocument, XPathConstants.NODESET);
                allMatch = allMatch && (nodeList.getLength() > 0);
            }
            if (allMatch) {
                for (JobVariable variable : filter.getVariables()) {
                    configuredVariables.put(variable.getName(), variable);
                }
                for (GenericInformation info : filter.getGenericInformation()) {
                    configuredGenericInfo.put(info.getName(), info.getValue());
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error analysing workflow global variables", e);
    }
    return answer;
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LinkedHashMap(java.util.LinkedHashMap) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable)

Example 13 with JobVariable

use of org.ow2.proactive.scheduler.common.job.JobVariable in project scheduling by ow2-proactive.

the class GetJobContentGeneratorTest method testReplaceVarsWithParams.

@Test
public void testReplaceVarsWithParams() throws IOException {
    URL url = Resources.getResource("org/ow2/proactive/scheduler/common/job/factories/job_with_vars_and_info.xml");
    String jobContent = Resources.toString(url, Charsets.UTF_8);
    Map<String, JobVariable> vars = new HashMap<>();
    vars.put("var", new JobVariable("var", "myvalue", "pa:model", "my var description", "varGroup", true, true));
    final String newJobContent = generator.replaceVarsAndGenericInfo(jobContent, vars, Collections.emptyMap());
    assertTrue(newJobContent.contains("<variables>"));
    assertTrue(newJobContent.contains("<variable name=\"var\" value=\"myvalue\" model=\"pa:model\" description=\"my var description\" group=\"varGroup\" advanced=\"true\" hidden=\"true\" />"));
    assertFalse(newJobContent.contains("<genericInformation>"));
    assertNotEquals(jobContent, newJobContent);
}
Also used : HashMap(java.util.HashMap) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) URL(java.net.URL) Test(org.junit.Test)

Example 14 with JobVariable

use of org.ow2.proactive.scheduler.common.job.JobVariable in project scheduling by ow2-proactive.

the class TestStaxJobFactory method testHandleVariablesReplacements.

@Test
public void testHandleVariablesReplacements() throws JobCreationException {
    // null replacement
    Assert.assertEquals(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1", "m1")), factory.replaceVariablesInJobVariablesMap(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1", "m1")), null));
    // replace existing variable, leave the model intact
    Assert.assertEquals(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v2", "m1")), factory.replaceVariablesInJobVariablesMap(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1", "m1")), ImmutableMap.<String, String>of("a1", "v2")));
    // add new variable
    Assert.assertEquals(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1", "m1"), "a2", new JobVariable("a2", "v2")), factory.replaceVariablesInJobVariablesMap(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1", "m1")), ImmutableMap.<String, String>of("a2", "v2")));
    // reuse replacement variable in pattern and add new variable
    Assert.assertEquals(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1v2", "m1v2"), "a2", new JobVariable("a2", "v2")), factory.replaceVariablesInJobVariablesMap(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1${a2}", "m1${a2}")), ImmutableMap.<String, String>of("a2", "v2")));
    // existing variable uses another existing variable in a pattern, null replacements
    Assert.assertEquals(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1v2", "m1v2"), "a2", new JobVariable("a2", "v2")), factory.replaceVariablesInJobVariablesMap(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1${a2}", "m1${a2}"), "a2", new JobVariable("a2", "v2")), null));
    // existing variable uses another existing variable in a pattern, this other existing variable uses itself a replacement variable
    Assert.assertEquals(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1v3", "m1v3"), "a2", new JobVariable("a2", "v3", "m2v3"), "a3", new JobVariable("a3", "v3")), factory.replaceVariablesInJobVariablesMap(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1${a2}", "m1${a2}"), "a2", new JobVariable("a2", "${a3}", "m2${a3}")), ImmutableMap.<String, String>of("a3", "v3")));
    // existing variable uses a replacement variable in a pattern, this replacement variable uses itself a job variable from the workflow
    Assert.assertEquals(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1v3", "m1v3"), "a2", new JobVariable("a2", "v3"), "a3", new JobVariable("a3", "v3", "m3v3")), factory.replaceVariablesInJobVariablesMap(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1${a2}", "m1${a2}"), "a3", new JobVariable("a3", "v3", "m3${a2}")), ImmutableMap.<String, String>of("a2", "${a3}")));
    // existing variable uses a replacement variable in a pattern, but is overwritten by another replacement variable
    Assert.assertEquals(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1", "m1v2"), "a2", new JobVariable("a2", "v2")), factory.replaceVariablesInJobVariablesMap(ImmutableMap.<String, JobVariable>of("a1", new JobVariable("a1", "v1${a2}", "m1${a2}")), ImmutableMap.<String, String>of("a2", "v2", "a1", "v1")));
}
Also used : JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) GlobalVariablesParserTest(org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesParserTest) Test(org.junit.Test)

Example 15 with JobVariable

use of org.ow2.proactive.scheduler.common.job.JobVariable in project scheduling by ow2-proactive.

the class TestStaxJobFactory method testUnresolvedJobVariables.

@Test
public void testUnresolvedJobVariables() throws URISyntaxException, JobCreationException {
    TaskFlowJob job = (TaskFlowJob) factory.createJob(jobDescriptorWithUnresolvedGenericInfoAndVariables);
    Map<String, JobVariable> unresolvedVariables = job.getUnresolvedVariables();
    Map<String, JobVariable> variables = job.getVariables();
    assertEquals("value1", unresolvedVariables.get("variable1").getValue());
    assertEquals("value1", variables.get("variable1").getValue());
    assertEquals("${variable1}", unresolvedVariables.get("variable2").getValue());
    assertEquals("value1", variables.get("variable2").getValue());
}
Also used : TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) GlobalVariablesParserTest(org.ow2.proactive.scheduler.common.job.factories.globalvariables.GlobalVariablesParserTest) Test(org.junit.Test)

Aggregations

JobVariable (org.ow2.proactive.scheduler.common.job.JobVariable)51 Test (org.junit.Test)19 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)17 HashMap (java.util.HashMap)15 LinkedHashMap (java.util.LinkedHashMap)9 Job (org.ow2.proactive.scheduler.common.job.Job)8 JobId (org.ow2.proactive.scheduler.common.job.JobId)8 InternalJob (org.ow2.proactive.scheduler.job.InternalJob)7 JobValidationException (org.ow2.proactive.scheduler.common.exception.JobValidationException)6 Task (org.ow2.proactive.scheduler.common.task.Task)6 TaskVariable (org.ow2.proactive.scheduler.common.task.TaskVariable)6 IOException (java.io.IOException)5 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)5 SimpleScript (org.ow2.proactive.scripting.SimpleScript)5 TaskScript (org.ow2.proactive.scripting.TaskScript)5 NonTerminatingJob (functionaltests.jobs.NonTerminatingJob)4 SimpleJob (functionaltests.jobs.SimpleJob)4 FileNotFoundException (java.io.FileNotFoundException)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)4