Search in sources :

Example 1 with JavaStandaloneExecutableInitializer

use of org.ow2.proactive.scheduler.common.task.executable.internal.JavaStandaloneExecutableInitializer in project scheduling by ow2-proactive.

the class JavaClassScriptEngine method eval.

@Override
public Object eval(String userExecutableClassName, ScriptContext context) throws ScriptException {
    try {
        JavaExecutable javaExecutable = getExecutable(userExecutableClassName);
        JavaStandaloneExecutableInitializer execInitializer = new JavaStandaloneExecutableInitializer();
        PrintStream output = new PrintStream(new WriterOutputStream(context.getWriter()), true);
        execInitializer.setOutputSink(output);
        PrintStream error = new PrintStream(new WriterOutputStream(context.getErrorWriter()), true);
        execInitializer.setErrorSink(error);
        Map<String, byte[]> propagatedVariables = null;
        if (context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME) != null) {
            propagatedVariables = SerializationUtil.serializeVariableMap(((VariablesMap) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).getPropagatedVariables());
            execInitializer.setPropagatedVariables(propagatedVariables);
        } else {
            execInitializer.setPropagatedVariables(Collections.<String, byte[]>emptyMap());
        }
        if (context.getAttribute(Script.ARGUMENTS_NAME) != null) {
            execInitializer.setSerializedArguments((Map<String, byte[]>) ((Serializable[]) context.getAttribute(Script.ARGUMENTS_NAME))[0]);
        } else {
            execInitializer.setSerializedArguments(Collections.<String, byte[]>emptyMap());
        }
        if (context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE) != null) {
            execInitializer.setThirdPartyCredentials((Map<String, String>) context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE));
        } else {
            execInitializer.setThirdPartyCredentials(Collections.<String, String>emptyMap());
        }
        if (context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME) != null) {
            List<String> nodesURLs = (List<String>) context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME);
            execInitializer.setNodesURL(nodesURLs);
        } else {
            execInitializer.setNodesURL(Collections.<String>emptyList());
        }
        javaExecutable.internalInit(execInitializer, context);
        Serializable execute = javaExecutable.execute((TaskResult[]) context.getAttribute(SchedulerConstants.RESULTS_VARIABLE));
        if (propagatedVariables != null) {
            ((Map<String, Serializable>) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).putAll(javaExecutable.getVariables());
        }
        output.close();
        error.close();
        return execute;
    } catch (Throwable e) {
        throw new ScriptException(new TaskException(getStackTraceAsString(e), e));
    }
}
Also used : PrintStream(java.io.PrintStream) Serializable(java.io.Serializable) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) JavaExecutable(org.ow2.proactive.scheduler.common.task.executable.JavaExecutable) ScriptException(javax.script.ScriptException) TaskException(org.ow2.proactive.scheduler.task.exceptions.TaskException) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) List(java.util.List) JavaStandaloneExecutableInitializer(org.ow2.proactive.scheduler.common.task.executable.internal.JavaStandaloneExecutableInitializer) Map(java.util.Map) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap)

Aggregations

Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)1 PrintStream (java.io.PrintStream)1 Serializable (java.io.Serializable)1 List (java.util.List)1 Map (java.util.Map)1 ScriptException (javax.script.ScriptException)1 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)1 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)1 JavaExecutable (org.ow2.proactive.scheduler.common.task.executable.JavaExecutable)1 JavaStandaloneExecutableInitializer (org.ow2.proactive.scheduler.common.task.executable.internal.JavaStandaloneExecutableInitializer)1 TaskException (org.ow2.proactive.scheduler.task.exceptions.TaskException)1 VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)1