Search in sources :

Example 1 with VariablesMap

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TaskVariable(org.ow2.proactive.scheduler.common.task.TaskVariable) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException)

Example 2 with 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;
}
Also used : JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with 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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with VariablesMap

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"));
}
Also used : HashMap(java.util.HashMap) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) TaskVariable(org.ow2.proactive.scheduler.common.task.TaskVariable) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) File(java.io.File) Test(org.junit.Test)

Example 5 with VariablesMap

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());
}
Also used : TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) Test(org.junit.Test)

Aggregations

VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)22 Test (org.junit.Test)20 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)17 HashMap (java.util.HashMap)13 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)10 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)9 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)9 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)8 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)8 Serializable (java.io.Serializable)7 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)4 SchedulerNodeClient (org.ow2.proactive.scheduler.task.client.SchedulerNodeClient)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 JobVariable (org.ow2.proactive.scheduler.common.job.JobVariable)3 RemoteSpace (org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace)3 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 LoginException (javax.security.auth.login.LoginException)2