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;
}
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;
}
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);
}
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")));
}
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());
}
Aggregations