Example usage for javax.script SimpleScriptContext setWriter

List of usage examples for javax.script SimpleScriptContext setWriter

Introduction

In this page you can find the example usage for javax.script SimpleScriptContext setWriter.

Prototype

public void setWriter(Writer writer) 

Source Link

Usage

From source file:be.solidx.hot.JSR223ScriptExecutor.java

@Override
public Object execute(Script<CompiledScript> script, Writer writer) throws ScriptException {
    try {/*w  w  w.  j  av a2s .  c om*/
        ScriptEngine scriptEngine = getEngine();
        CompiledScript compiledScript = getCachedScript(script);
        SimpleScriptContext simpleScriptContext = new SimpleScriptContext();
        executePreExecuteScripts(simpleScriptContext);
        simpleScriptContext.setWriter(writer);
        Object object = compiledScript.eval(simpleScriptContext);
        writer.flush();
        if (object == null)
            return scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
        return object;
    } catch (Exception e) {
        throw new ScriptException(e);
    }
}

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

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final String contentType = getContentType(req);
    resp.setContentType(contentType);//from  w w w. j a v  a2s.  c  o  m
    if (contentType.startsWith("text/")) {
        resp.setCharacterEncoding("UTF-8");
    }
    final String script = getCodeValue(req);
    final Bindings bindings = new SimpleBindings();
    final PrintWriter pw = resp.getWriter();
    final ScriptHelper osgi = new ScriptHelper(getBundleContext());
    final Writer errorWriter = new LogWriter(log);
    final Reader reader = new StringReader(script);
    //Populate bindings
    bindings.put("request", req);
    bindings.put("reader", reader);
    bindings.put("response", resp);
    bindings.put("out", pw);
    bindings.put("osgi", osgi);

    //Also expose the bundleContext to simplify scripts interaction with the
    //enclosing OSGi container
    bindings.put("bundleContext", getBundleContext());

    final String lang = WebConsoleUtil.getParameter(req, "lang");
    final boolean webClient = "webconsole".equals(WebConsoleUtil.getParameter(req, "client"));

    SimpleScriptContext sc = new SimpleScriptContext();
    sc.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    sc.setWriter(pw);
    sc.setErrorWriter(errorWriter);
    sc.setReader(reader);

    try {
        log.log(LogService.LOG_DEBUG, "Executing script" + script);
        eval(script, lang, sc);
    } catch (Throwable t) {
        if (!webClient) {
            resp.setStatus(500);
        }
        pw.println(exceptionToString(t));
        log.log(LogService.LOG_ERROR, "Error in executing script", t);
    } finally {
        if (osgi != null) {
            osgi.cleanup();
        }
    }
}

From source file:org.freeplane.plugin.script.GenericScript.java

private SimpleScriptContext createScriptContext(final NodeModel node) {
    final SimpleScriptContext context = new SimpleScriptContext();
    final OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
    context.setWriter(outWriter);
    context.setErrorWriter(outWriter);/*  w w  w.  j a v a2s  . c o  m*/
    context.setBindings(createBinding(node), javax.script.ScriptContext.ENGINE_SCOPE);
    return context;
}

From source file:be.solidx.hot.JSR223ScriptExecutor.java

@Override
public Object execute(Script<CompiledScript> script, Map<String, Object> contextVars, Writer writer)
        throws ScriptException {
    try {//from   w w w  .  j  av a 2 s . co  m
        ScriptEngine scriptEngine = getEngine();
        SimpleScriptContext simpleScriptContext = new SimpleScriptContext();
        Bindings bindings = scriptEngine.createBindings();
        bindings.putAll(contextVars);
        simpleScriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        executePreExecuteScripts(simpleScriptContext);
        simpleScriptContext.setWriter(writer);
        CompiledScript compiledScript = getCachedScript(script);
        Object object = compiledScript.eval(simpleScriptContext);
        writer.flush();
        if (object == null)
            return bindings;
        return object;
    } catch (Exception e) {
        throw new ScriptException(e);
    }
}