Example usage for javax.script ScriptContext getErrorWriter

List of usage examples for javax.script ScriptContext getErrorWriter

Introduction

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

Prototype

public Writer getErrorWriter();

Source Link

Document

Returns the Writer used to display error output.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptContext ctx = new SimpleScriptContext();

    // Get the reader and writers from the script context
    Reader inputReader = ctx.getReader();
    Writer outputWriter = ctx.getWriter();
    Writer errWriter = ctx.getErrorWriter();

    // Write all script outputs to an out.txt file
    Writer fileWriter = new FileWriter("out.txt");
    ctx.setWriter(fileWriter);//from www  .  j  a v a 2  s.co  m
}

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

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

    try {//from   w ww  . ja  v  a 2  s.  c om
        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));
    }
}

From source file:org.jahia.services.workflow.jbpm.JBPMMailProducer.java

private String evaluateExpression(Execution execution, String scriptToExecute, JCRSessionWrapper session)
        throws RepositoryException, ScriptException {
    ScriptContext scriptContext = scriptEngine.getContext();
    if (bindings == null) {
        bindings = getBindings(execution, session);
    }//from   w  ww  .  j av  a  2  s.c  o  m
    scriptContext.setWriter(new StringWriter());
    scriptContext.setErrorWriter(new StringWriter());
    scriptEngine.eval(scriptToExecute, bindings);
    String error = scriptContext.getErrorWriter().toString();
    if (error.length() > 0) {
        logger.error("Scripting error : " + error);
    }
    return scriptContext.getWriter().toString().trim();
}

From source file:org.apache.felix.webconsole.plugins.scriptconsole.internal.ScriptConsolePlugin.java

private void eval(String script, String lang, ScriptContext ctx) throws ScriptException, IOException {
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByExtension(lang);
    if (scriptEngine == null) {
        throw new IllegalArgumentException("No ScriptEngineFactory found for extension " + lang);
    }/*from  w w  w. j  a  v  a 2s  . c o m*/

    // evaluate the script
    //Currently we do not make use of returned object
    final Object result = scriptEngine.eval(script, ctx);

    // allways flush the error channel
    ctx.getErrorWriter().flush();

}

From source file:org.jahia.services.workflow.jbpm.custom.email.JBPMMailProducer.java

protected String evaluateExpression(WorkItem workItem, String scriptToExecute, JCRSessionWrapper session)
        throws RepositoryException, ScriptException {
    ScriptContext scriptContext = new SimpleScriptContext();
    if (bindings == null) {
        bindings = getBindings(workItem, session);
    }//from   w ww.j  ava  2 s.  c o  m
    scriptContext.setWriter(new StringWriter());
    scriptContext.setErrorWriter(new StringWriter());
    scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    scriptContext.setBindings(scriptContext.getBindings(ScriptContext.GLOBAL_SCOPE),
            ScriptContext.GLOBAL_SCOPE);
    scriptEngine.eval(scriptToExecute, scriptContext);
    String error = scriptContext.getErrorWriter().toString();
    if (error.length() > 0) {
        logger.error("Scripting error : " + error);
    }
    return scriptContext.getWriter().toString().trim();
}