Example usage for javax.script ScriptContext getWriter

List of usage examples for javax.script ScriptContext getWriter

Introduction

In this page you can find the example usage for javax.script ScriptContext getWriter.

Prototype

public Writer getWriter();

Source Link

Document

Returns the Writer for scripts to use when displaying output.

Usage

From source file:org.ow2.parserve.PARServeEngine.java

/**
 * Initialize the Tailer thread used to read R output
 *///from   w  w  w  . j  av  a  2s. c o  m
private void initializeTailer(Bindings bindings, ScriptContext ctx) throws ScriptException {
    if (!serverEval) {
        try {
            Tailer tailer = null;
            outputFile = createOuputFile(bindings);

            listener = new PARScriptTailerListener(ctx.getWriter());
            tailer = new Tailer(outputFile, listener, TAILER_PERIOD, false, true);

            tailerThread = new Thread(tailer, "PARServeEngine Tailer");
            tailerThread.setDaemon(true);
            tailerThread.start();

            engine.initializeOutput(outputFile, ctx);

        } catch (Exception e) {
            logger.error("Error during tailer init:", e);
            throw new ScriptException(e);
        }
    }
}

From source file:org.ow2.proactive.scheduler.task.java.JavaClassScriptEngine.java

@Override
public Object eval(String userExecutableClassName, ScriptContext context) throws ScriptException {

    try {//from  w w  w.jav  a 2 s . com
        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));
    }
}