use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.
the class StaxJobFactory method getJobVisualization.
/**
* Get job's SVG visualization as HTML String from Workflows's Metadata.
* Leave the method with the cursor at the end of 'VISUALIZATION' tag.
*
* @param cursorMetadata the streamReader with the cursor on the 'VISUALIZATION' tag.
* @return the Workflow's visualization HTML String between the tags.
*/
private String getJobVisualization(XMLStreamReader cursorMetadata) throws JobCreationException {
try {
String visualization = "";
// if visualization tag exists, then we have a characters event next.
int eventType = cursorMetadata.next();
if (eventType == XMLEvent.CHARACTERS) {
visualization = cursorMetadata.getText();
} else if (eventType == XMLEvent.END_ELEMENT) {
return visualization;
}
// go to the description END_ELEMENT
while (cursorMetadata.next() != XMLEvent.END_ELEMENT) ;
return visualization;
} catch (Exception e) {
throw new JobCreationException((String) null, null, e);
}
}
use of org.ow2.proactive.scheduler.common.exception.JobCreationException in project scheduling by ow2-proactive.
the class StaxJobFactory method validate.
/*
* Validate the given job descriptor
*/
private TaskFlowJob validate(TaskFlowJob job, Scheduler scheduler, SchedulerSpaceInterface space, String sessionId) throws JobCreationException {
Map<String, JobValidatorService> factories;
try {
factories = JobValidatorRegistry.getInstance().getRegisteredFactories();
} catch (Exception e) {
throw new JobCreationException(MSG_UNABLE_TO_INSTANCIATE_JOB_VALIDATION_FACTORIES, e);
}
TaskFlowJob updatedJob = job;
try {
for (JobValidatorService factory : factories.values()) {
updatedJob = factory.validateJob(updatedJob, scheduler, space, sessionId);
}
} catch (JobValidationException e) {
fillUpdatedVariables(e, job);
throw e;
} catch (Exception e) {
JobValidationException validationException = new JobValidationException(e);
fillUpdatedVariables(validationException, job);
throw validationException;
}
return updatedJob;
}
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) && 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;
default:
}
}
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 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.exception.JobCreationException 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);
}
}
Aggregations