use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.
the class StaxJobFactory method createScript.
/**
* Get the script defined at the specified cursor.
* Leave the method with cursor at the end of the corresponding script.
*
* @param cursorScript the streamReader with the cursor on the corresponding script tag (pre, post, cleaning, selection, generation).
* @param type nature of the script
* @return the script defined at the specified cursor.
*/
private Script<?> createScript(XMLStreamReader cursorScript, ScriptType type, Map<String, String> variables) throws JobCreationException {
String attrtmp = null;
String currentScriptTag = cursorScript.getLocalName();
String current = null;
try {
boolean isDynamic = true;
Script<?> toReturn = null;
int eventType = -1;
while (cursorScript.hasNext()) {
if (type == ScriptType.SELECTION && eventType == -1) {
eventType = cursorScript.getEventType();
} else {
eventType = cursorScript.next();
}
switch(eventType) {
case XMLEvent.START_ELEMENT:
current = cursorScript.getLocalName();
if (XMLTags.SCRIPT_CODE.matches(current)) {
String language = null;
String content = "";
if (cursorScript.getAttributeCount() > 0) {
language = cursorScript.getAttributeValue(0);
attrtmp = cursorScript.getAttributeLocalName(0);
}
// goto script content
if (cursorScript.next() == XMLEvent.CHARACTERS) {
content = cursorScript.getText();
}
toReturn = new SimpleScript(content, language);
// fast forward to the end of tag
while (true) {
int ev = cursorScript.next();
if (XMLTags.SCRIPT_CODE.matches(current) && ev == XMLEvent.END_ELEMENT) {
break;
}
}
} else if (XMLTags.SCRIPT_FILE.matches(current)) {
String path = null;
String url = null;
String language = null;
for (int i = 0; i < cursorScript.getAttributeCount(); i++) {
attrtmp = cursorScript.getAttributeLocalName(i);
if (XMLAttributes.SCRIPT_URL.matches(attrtmp)) {
url = replace(cursorScript.getAttributeValue(i), variables);
} else if (XMLAttributes.LANGUAGE.matches(attrtmp)) {
language = replace(cursorScript.getAttributeValue(i), variables);
} else if (XMLAttributes.PATH.matches(attrtmp)) {
path = checkPath(cursorScript.getAttributeValue(i), variables);
} else {
throw new JobCreationException("Unrecognized attribute : " + attrtmp);
}
}
// go to the next 'arguments' start element or the 'file' end element
while (true) {
int ev = cursorScript.next();
if (((ev == XMLEvent.START_ELEMENT) && XMLTags.SCRIPT_ARGUMENTS.matches(cursorScript.getLocalName())) || (ev == XMLEvent.END_ELEMENT)) {
break;
}
}
if (url != null) {
if (language != null) {
toReturn = new SimpleScript(new URL(url), language, getArguments(cursorScript));
} else {
toReturn = new SimpleScript(new URL(url), getArguments(cursorScript));
}
} else if (path != null) {
// language is ignored if a File is provided, the script language will be determined based on the file extension
toReturn = new SimpleScript(new File(path), getArguments(cursorScript));
} else {
attrtmp = null;
throw new JobCreationException("Invalid script file definition, one of path/url attributes must be declared");
}
} else if (XMLTags.SCRIPT_ARGUMENTS.matches(current)) {
toReturn = new SimpleScript(toReturn.getScript(), toReturn.getEngineName(), getArguments(cursorScript));
} else if (XMLTags.SCRIPT_SCRIPT.matches(current)) {
if (cursorScript.getAttributeCount() > 0) {
isDynamic = !"static".equals(cursorScript.getAttributeValue(0));
}
}
break;
case XMLEvent.END_ELEMENT:
if (cursorScript.getLocalName().equals(currentScriptTag)) {
if (type == ScriptType.SELECTION) {
return new SelectionScript(toReturn, isDynamic);
} else {
return toReturn;
}
}
break;
}
}
return toReturn;
} catch (JobCreationException jce) {
jce.pushTag(current);
throw jce;
} catch (Exception e) {
throw new JobCreationException(current, attrtmp, e);
}
}
use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.
the class StaxJobFactory method getGenericInformation.
/**
* 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 HashMap<String, String> getGenericInformation(XMLStreamReader cursorInfo, Map<String, String> variables) throws JobCreationException {
HashMap<String, String> infos = new HashMap<>();
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 = getAttributesAsMap(cursorInfo, variables);
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;
}
}
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.exception.JobCreationException in project scheduling by ow2-proactive.
the class StaxJobFactory method createControlFlowScript.
private FlowScript createControlFlowScript(XMLStreamReader cursorTask, Task tmpTask, Map<String, String> variables) throws JobCreationException {
String type = null;
String target = null;
String targetElse = null;
String targetJoin = null;
int event = -1;
for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
String attrName = cursorTask.getAttributeLocalName(i);
if (XMLAttributes.FLOW_BLOCK.matches(attrName)) {
tmpTask.setFlowBlock(FlowBlock.parse(replace(cursorTask.getAttributeValue(i), variables)));
}
}
// <control> => <if> | <loop> | <replicate>
try {
while (cursorTask.hasNext()) {
event = cursorTask.next();
if (event == XMLEvent.START_ELEMENT) {
break;
} else if (event == XMLEvent.END_ELEMENT && XMLTags.FLOW.matches(cursorTask.getLocalName())) {
return null;
}
}
} catch (Exception e) {
throw new JobCreationException(XMLTags.FLOW.getXMLName(), null, e);
}
if (event != XMLEvent.START_ELEMENT) {
throw new JobCreationException(XMLTags.FLOW.getXMLName(), null, null);
}
String tag = null;
// REPLICATE: no attribute
if (XMLTags.FLOW_REPLICATE.matches(cursorTask.getLocalName())) {
type = FlowActionType.REPLICATE.toString();
tag = XMLTags.FLOW_REPLICATE.getXMLName();
} else // IF: attributes TARGET_IF and TARGET_ELSE and TARGET_JOIN
if (XMLTags.FLOW_IF.matches(cursorTask.getLocalName())) {
type = FlowActionType.IF.toString();
tag = XMLTags.FLOW_IF.getXMLName();
for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
String attrName = cursorTask.getAttributeLocalName(i);
if (XMLAttributes.FLOW_TARGET.matches(attrName)) {
target = cursorTask.getAttributeValue(i);
} else if (XMLAttributes.FLOW_ELSE.matches(attrName)) {
targetElse = cursorTask.getAttributeValue(i);
} else if (XMLAttributes.FLOW_CONTINUATION.matches(attrName)) {
targetJoin = cursorTask.getAttributeValue(i);
}
}
} else // LOOP: attribute TARGET
if (XMLTags.FLOW_LOOP.matches(cursorTask.getLocalName())) {
type = FlowActionType.LOOP.toString();
tag = XMLTags.FLOW_LOOP.getXMLName();
for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
String attrName = cursorTask.getAttributeLocalName(i);
if (XMLAttributes.FLOW_TARGET.matches(attrName)) {
target = cursorTask.getAttributeValue(i);
}
}
}
FlowScript sc = null;
Script<?> internalScript;
try {
internalScript = createScript(cursorTask, ScriptType.FLOW, variables);
switch(FlowActionType.parse(type)) {
case IF:
sc = FlowScript.createIfFlowScript(internalScript, target, targetElse, targetJoin);
break;
case REPLICATE:
sc = FlowScript.createReplicateFlowScript(internalScript);
break;
case LOOP:
sc = FlowScript.createLoopFlowScript(internalScript, target);
break;
default:
break;
}
} catch (Exception e) {
throw new JobCreationException(tag, null, e);
}
// </script> --> </if> | </replicate> | </loop>
try {
while (cursorTask.hasNext()) {
event = cursorTask.next();
if (event == XMLEvent.END_ELEMENT) {
break;
}
}
} catch (XMLStreamException e) {
throw new JobCreationException(tag, null, e);
}
if (event != XMLEvent.END_ELEMENT) {
throw new JobCreationException(tag, null, null);
}
return sc;
}
use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.
the class StaxJobFactory method getTaskVariable.
private TaskVariable getTaskVariable(XMLStreamReader cursorVariables, Map<String, String> variables) throws JobCreationException {
TaskVariable taskVariable = new TaskVariable();
Map<String, String> attributesAsMap = getAttributesAsMap(cursorVariables, variables);
taskVariable.setName(attributesAsMap.get(XMLAttributes.VARIABLE_NAME.getXMLName()));
taskVariable.setValue(attributesAsMap.get(XMLAttributes.VARIABLE_VALUE.getXMLName()));
taskVariable.setModel(attributesAsMap.get(XMLAttributes.VARIABLE_MODEL.getXMLName()));
if (attributesAsMap.containsKey(XMLAttributes.VARIABLE_JOB_INHERITED.getXMLName())) {
taskVariable.setJobInherited(Boolean.valueOf(attributesAsMap.get(XMLAttributes.VARIABLE_JOB_INHERITED.getXMLName())));
}
return taskVariable;
}
use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.
the class TestStaxJobFactory method testJobCreationAttributeOrderDefinitionVariableXmlElement.
@Test
public void testJobCreationAttributeOrderDefinitionVariableXmlElement() throws URISyntaxException, JobCreationException {
Job job = factory.createJob(getResource("job_attr_def_variable_xml_element.xml"));
Map<String, JobVariable> jobVariables = job.getVariables();
assertEquals(2, jobVariables.size());
JobVariable jobVariable = jobVariables.get("name1");
assertNotNull(jobVariable);
assertEquals("name1", jobVariable.getName());
assertEquals("value1", jobVariable.getValue());
assertEquals("model1", jobVariable.getModel());
jobVariable = jobVariables.get("name2");
assertNotNull(jobVariable);
assertEquals("name2", jobVariable.getName());
assertEquals("value2", jobVariable.getValue());
assertEquals("model2", jobVariable.getModel());
}
Aggregations