use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.
the class StaxJobFactory method createTaskVariables.
/**
* 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, TaskVariable> createTaskVariables(XMLStreamReader cursorVariables, Map<String, String> variables) throws JobCreationException {
Map<String, TaskVariable> variablesMap = new HashMap<>();
try {
int eventType;
while (cursorVariables.hasNext()) {
eventType = cursorVariables.next();
if (eventType == XMLEvent.START_ELEMENT && XMLTags.VARIABLE.matches(cursorVariables.getLocalName())) {
TaskVariable taskVariable = getTaskVariable(cursorVariables, variables);
variablesMap.put(taskVariable.getName(), taskVariable);
} else if (eventType == XMLEvent.END_ELEMENT && XMLTags.VARIABLES.matches(cursorVariables.getLocalName())) {
return variablesMap;
}
}
} 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;
}
use of org.ow2.proactive.scheduler.task.utils.VariablesMap 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;
}
use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.
the class StaxJobFactory method replaceVariablesInJobVariablesMap.
protected Map<String, JobVariable> replaceVariablesInJobVariablesMap(Map<String, JobVariable> variablesMap, Map<String, String> replacementVariables) throws JobCreationException {
HashMap<String, String> updatedReplacementVariables = new HashMap<>();
HashMap<String, JobVariable> updatedVariablesMap = new HashMap<>(variablesMap);
// replacements will include at first variables defined in the job
for (JobVariable variable : updatedVariablesMap.values()) {
updatedReplacementVariables.put(variable.getName(), variable.getValue());
}
if (replacementVariables != null) {
// overwritten by variables used at job submission
updatedReplacementVariables.putAll(replacementVariables);
}
for (Map.Entry<String, String> replacementVariable : updatedReplacementVariables.entrySet()) {
if (updatedVariablesMap.containsKey(replacementVariable.getKey())) {
// if the variable is already defined in the job, overwrite its value by the replacement variable,
// eventually using other variables as pattern replacements
JobVariable jobVariable = updatedVariablesMap.get(replacementVariable.getKey());
jobVariable.setValue(replace(replacementVariable.getValue(), updatedReplacementVariables));
if (jobVariable.getModel() != null) {
// model of an existing variable can use other variables as pattern replacements
jobVariable.setModel(replace(jobVariable.getModel(), updatedReplacementVariables));
}
} else {
// if the variable is not defined in the job, create a new job variable with an empty model
updatedVariablesMap.put(replacementVariable.getKey(), new JobVariable(replacementVariable.getKey(), replace(replacementVariable.getValue(), updatedReplacementVariables), null));
}
}
return updatedVariablesMap;
}
use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.
the class Job2XMLTransformerTest method checkTaskVariables.
@Test
public void checkTaskVariables() throws Exception {
File xmlFile = tmpFolder.newFile();
Map<String, TaskVariable> variablesMap = new HashMap<>();
TaskVariable taskVariable1 = new TaskVariable();
taskVariable1.setName("name1");
taskVariable1.setValue("value1");
taskVariable1.setJobInherited(false);
taskVariable1.setModel("model1");
TaskVariable taskVariable2 = new TaskVariable();
taskVariable2.setName("name2");
taskVariable2.setValue("value2");
taskVariable2.setJobInherited(true);
taskVariable2.setModel("model2");
variablesMap.put(taskVariable1.getName(), taskVariable1);
variablesMap.put(taskVariable2.getName(), taskVariable2);
TaskFlowJob job = new TaskFlowJob();
JavaTask task = new JavaTask();
task.setName("task");
task.setExecutableClassName("oo.Bar");
task.setVariables(variablesMap);
job.addTask(task);
new Job2XMLTransformer().job2xmlFile(job, xmlFile);
TaskFlowJob recreatedJob = (TaskFlowJob) (JobFactory.getFactory().createJob(xmlFile.getAbsolutePath()));
Map<String, TaskVariable> resVariables = recreatedJob.getTask("task").getVariables();
assertEquals(2, resVariables.size());
assertEquals(taskVariable1, resVariables.get("name1"));
assertEquals(taskVariable2, resVariables.get("name2"));
}
use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.
the class TestStaxJobFactory method testCreateJobShouldUseVariableMapParameterToReplaceVariableValue.
@Test
public void testCreateJobShouldUseVariableMapParameterToReplaceVariableValue() throws Exception {
Map<String, String> variablesMap = Maps.newHashMap();
variablesMap.put("from_create_job_parameter", "from_create_job_parameter_value");
TaskFlowJob testJob = (TaskFlowJob) factory.createJob(jobDescriptorUri, variablesMap);
assertEquals("from_create_job_parameter_value", testJob.getVariables().get("from_create_job_parameter").getValue());
}
Aggregations