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