use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.
the class StaxJobFactory method createForkEnvironment.
/**
* Create the forkEnvironment of a java task
* The cursor is currently at the beginning of the 'FORK_ENVIRONMENT' tag.
*
* @param cursorExec the streamReader with the cursor on the 'FORK_ENVIRONMENT' tag.
* @return The created ForkEnvironment
*/
private ForkEnvironment createForkEnvironment(XMLStreamReader cursorExec, Map<String, String> variables) throws JobCreationException {
ForkEnvironment forkEnv = new ForkEnvironment();
int i = 0;
String current = cursorExec.getLocalName();
try {
// parsing executable attributes
int attrCount = cursorExec.getAttributeCount();
for (i = 0; i < attrCount; i++) {
String attrName = cursorExec.getAttributeLocalName(i);
if (XMLAttributes.FORK_JAVA_HOME.matches(attrName)) {
forkEnv.setJavaHome(replace(cursorExec.getAttributeValue(i), variables));
}
if (XMLAttributes.TASK_WORKDING_DIR.matches(attrName)) {
forkEnv.setWorkingDir(replace(cursorExec.getAttributeValue(i), variables));
}
}
// parsing executable tags
int eventType;
while (cursorExec.hasNext()) {
eventType = cursorExec.next();
switch(eventType) {
case XMLEvent.START_ELEMENT:
current = cursorExec.getLocalName();
if (XMLTags.FORK_SYSTEM_PROPERTY.matches(current)) {
attrCount = cursorExec.getAttributeCount();
String name = null, value = null;
for (i = 0; i < attrCount; i++) {
String attrName = cursorExec.getAttributeLocalName(i);
if (XMLAttributes.COMMON_NAME.matches(attrName)) {
name = replace(cursorExec.getAttributeValue(i), variables);
}
if (XMLAttributes.COMMON_VALUE.matches(attrName)) {
value = replace(cursorExec.getAttributeValue(i), variables);
}
}
forkEnv.addSystemEnvironmentVariable(name, value);
} else if (XMLTags.FORK_JVM_ARG.matches(current)) {
forkEnv.addJVMArgument(replace(cursorExec.getAttributeValue(0), variables));
} else if (XMLTags.JOB_PATH_ELEMENT.matches(current)) {
forkEnv.addAdditionalClasspath(replace(cursorExec.getAttributeValue(0), variables));
} else if (XMLTags.SCRIPT_ENV.matches(current)) {
forkEnv.setEnvScript(new ForkEnvironmentScript(createScript(cursorExec, variables)));
}
break;
case XMLEvent.END_ELEMENT:
if (XMLTags.FORK_ENVIRONMENT.matches(cursorExec.getLocalName())) {
return forkEnv;
}
break;
}
}
return forkEnv;
} catch (JobCreationException jce) {
jce.pushTag(current);
throw jce;
} catch (Exception e) {
String attrtmp = null;
if (cursorExec.isStartElement() && cursorExec.getAttributeCount() > 0) {
attrtmp = cursorExec.getAttributeLocalName(i);
}
throw new JobCreationException(current, attrtmp, e);
}
}
use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.
the class StaxJobFactory method setNativeExecutable.
/**
* Add the Native Executable to this native Task.
* The cursor is currently at the beginning of the 'ELEMENT_NATIVE_EXECUTABLE' tag.
*
* @param nativeTask the task in which to add the Native Executable.
* @param cursorExec the streamReader with the cursor on the 'ELEMENT_NATIVE_EXECUTABLE' tag.
*/
private void setNativeExecutable(NativeTask nativeTask, XMLStreamReader cursorExec) throws JobCreationException {
int i = 0;
String current = null;
try {
// one step ahead to go to the command (static or dynamic)
while (cursorExec.next() != XMLEvent.START_ELEMENT) ;
current = cursorExec.getLocalName();
ArrayList<String> command = new ArrayList<>(0);
if (XMLTags.NATIVE_TASK_STATIC_COMMAND.matches(cursorExec.getLocalName())) {
String attr_ = null;
String current_ = null;
try {
for (i = 0; i < cursorExec.getAttributeCount(); i++) {
String attrName = cursorExec.getAttributeLocalName(i);
attr_ = attrName;
if (XMLAttributes.TASK_COMMAND_VALUE.matches(attrName)) {
command.add((cursorExec.getAttributeValue(i)));
}
if (XMLAttributes.TASK_WORKDING_DIR.matches(attrName)) {
logger.warn(XMLAttributes.TASK_WORKDING_DIR.getXMLName() + " attribute no longer supported. Please use a forkEnvironment for defining a working directory.");
}
}
int eventType;
while (cursorExec.hasNext()) {
eventType = cursorExec.next();
switch(eventType) {
case XMLEvent.START_ELEMENT:
current_ = cursorExec.getLocalName();
if (XMLTags.SCRIPT_ARGUMENT.matches(cursorExec.getLocalName())) {
command.add((cursorExec.getAttributeValue(0)));
}
break;
case XMLEvent.END_ELEMENT:
if (XMLTags.NATIVE_EXECUTABLE.matches(cursorExec.getLocalName())) {
nativeTask.setCommandLine(command.toArray(new String[command.size()]));
return;
}
break;
}
}
} catch (Exception e) {
throw new JobCreationException(current_, attr_, e);
}
} else {
throw new RuntimeException("Unknown command type: " + cursorExec.getLocalName());
}
} catch (JobCreationException jce) {
jce.pushTag(current);
throw jce;
} catch (Exception e) {
String temporaryAttribute = null;
if (cursorExec.isStartElement() && cursorExec.getAttributeCount() > 0) {
temporaryAttribute = cursorExec.getAttributeLocalName(i);
}
throw new JobCreationException(current, temporaryAttribute, e);
}
}
use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.
the class SchedulerClientTest method nodeClientJob.
protected Job nodeClientJob(String groovyScript, String forkScript) throws Exception {
URL scriptURL = SchedulerClientTest.class.getResource(groovyScript);
URL forkScriptURL = SchedulerClientTest.class.getResource(forkScript);
TaskFlowJob job = new TaskFlowJob();
job.setName("NodeClientJob");
ScriptTask task = new ScriptTask();
task.setName("NodeClientTask");
ForkEnvironment forkEnvironment = new ForkEnvironment();
forkEnvironment.setEnvScript(new SimpleScript(IOUtils.toString(forkScriptURL.toURI()), "groovy"));
task.setForkEnvironment(forkEnvironment);
task.setScript(new TaskScript(new SimpleScript(IOUtils.toString(scriptURL.toURI()), "groovy")));
// add CleanScript to test external APIs
task.setCleaningScript(new SimpleScript("" + "schedulerapi.connect();\n" + "print(\"SCHEDULERAPI_URI_LIST_NOT_NULL=\"+(schedulerapi.getGlobalSpaceURIs()!=null));\n" + "\n" + "userspaceapi.connect();\n" + "print(\"USERSPACE_FILE_LIST_NOT_NULL=\"+(userspaceapi.listFiles(\".\", \"*\")!=null));\n" + "\n" + "globalspaceapi.connect();\n" + "print(\"GLOBALSPACE_FILE_LIST_NOT_NULL=\"+(globalspaceapi.listFiles(\".\", \"*\")!=null));\n" + "print(\"TEST_CREDS=\"+(credentials.get(\"TEST_CREDS\")));\n", "js"));
job.addTask(task);
return job;
}
use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.
the class Job2XMLTransformerTest method forkEnvironmentIsPreserved.
@Test
public void forkEnvironmentIsPreserved() throws Exception {
File xmlFile = tmpFolder.newFile();
TaskFlowJob job = new TaskFlowJob();
JavaTask task = new JavaTask();
task.setName("forkedTask");
task.setExecutableClassName("oo.Bar");
task.setForkEnvironment(new ForkEnvironment());
job.addTask(task);
new Job2XMLTransformer().job2xmlFile(job, xmlFile);
TaskFlowJob recreatedJob = (TaskFlowJob) (JobFactory.getFactory().createJob(xmlFile.getAbsolutePath()));
assertNotNull(recreatedJob.getTask("forkedTask").getForkEnvironment());
}
use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.
the class TaskContextVariableExtractor method extractVariablesThirdPartyCredentialsAndSystemEnvironmentVariables.
/**
* Retrieve all third party credential variables in a map.
*
* @param taskContext all information to extract third party credentials is here.
*
* @return map containing thirdPartyCredentials
*/
public Map<String, String> extractVariablesThirdPartyCredentialsAndSystemEnvironmentVariables(TaskContext taskContext) {
ForkEnvironment forkEnvironment = taskContext.getInitializer().getForkEnvironment();
Map<String, Serializable> variables = new HashMap<>();
try {
variables = extractAllVariables(taskContext, null, "");
} catch (IOException | ClassNotFoundException e) {
logger.error(ERROR_READING_VARIABLES, e);
}
Map<String, String> thirdPartyCredentials = new HashMap<>();
try {
thirdPartyCredentials = forkedTaskVariablesManager.extractThirdPartyCredentials(taskContext);
} catch (Exception e) {
logger.error(ERROR_READING_VARIABLES, e);
}
HashMap<String, Serializable> systemEnvironmentVariables = new HashMap<String, Serializable>(System.getenv());
systemEnvironmentVariables.putAll(variables);
systemEnvironmentVariables.putAll(thirdPartyCredentials);
return VariableSubstitutor.filterAndUpdate(forkEnvironment.getSystemEnvironment(), systemEnvironmentVariables);
}
Aggregations