Java Code Examples for javax.script.ScriptContext#getWriter()

The following examples show how to use javax.script.ScriptContext#getWriter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Global.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Object printImpl(final boolean newLine, final Object... objects) {
    final ScriptContext sc = currentContext();
    @SuppressWarnings("resource")
    final PrintWriter out = sc != null? new PrintWriter(sc.getWriter()) : getContext().getEnv().getOut();
    final StringBuilder sb = new StringBuilder();

    for (final Object obj : objects) {
        if (sb.length() != 0) {
            sb.append(' ');
        }

        sb.append(JSType.toString(obj));
    }

    // Print all at once to ensure thread friendly result.
    if (newLine) {
        out.println(sb.toString());
    } else {
        out.print(sb.toString());
    }

    out.flush();

    return UNDEFINED;
}
 
Example 2
Source File: Global.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private Object printImpl(final boolean newLine, final Object... objects) {
    final ScriptContext sc = currentContext();
    @SuppressWarnings("resource")
    final PrintWriter out = sc != null? new PrintWriter(sc.getWriter()) : getContext().getEnv().getOut();
    final StringBuilder sb = new StringBuilder();

    for (final Object obj : objects) {
        if (sb.length() != 0) {
            sb.append(' ');
        }

        sb.append(JSType.toString(obj));
    }

    // Print all at once to ensure thread friendly result.
    if (newLine) {
        out.println(sb.toString());
    } else {
        out.print(sb.toString());
    }

    out.flush();

    return UNDEFINED;
}
 
Example 3
Source File: Global.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private Object printImpl(final boolean newLine, final Object... objects) {
    final ScriptContext sc = currentContext();
    @SuppressWarnings("resource")
    final PrintWriter out = sc != null? new PrintWriter(sc.getWriter()) : getContext().getEnv().getOut();
    final StringBuilder sb = new StringBuilder();

    for (final Object obj : objects) {
        if (sb.length() != 0) {
            sb.append(' ');
        }

        sb.append(JSType.toString(obj));
    }

    // Print all at once to ensure thread friendly result.
    if (newLine) {
        out.println(sb.toString());
    } else {
        out.print(sb.toString());
    }

    out.flush();

    return UNDEFINED;
}
 
Example 4
Source File: Global.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private Object printImpl(final boolean newLine, final Object... objects) {
    final ScriptContext sc = currentContext();
    @SuppressWarnings("resource")
    final PrintWriter out = sc != null? new PrintWriter(sc.getWriter()) : getContext().getEnv().getOut();
    final StringBuilder sb = new StringBuilder();

    for (final Object obj : objects) {
        if (sb.length() != 0) {
            sb.append(' ');
        }

        sb.append(JSType.toString(obj));
    }

    // Print all at once to ensure thread friendly result.
    if (newLine) {
        out.println(sb.toString());
    } else {
        out.print(sb.toString());
    }

    out.flush();

    return UNDEFINED;
}
 
Example 5
Source File: Global.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Object printImpl(final boolean newLine, final Object... objects) {
    final ScriptContext sc = currentContext();
    @SuppressWarnings("resource")
    final PrintWriter out = sc != null? new PrintWriter(sc.getWriter()) : getContext().getEnv().getOut();
    final StringBuilder sb = new StringBuilder();

    for (final Object obj : objects) {
        if (sb.length() != 0) {
            sb.append(' ');
        }

        sb.append(JSType.toString(obj));
    }

    // Print all at once to ensure thread friendly result.
    if (newLine) {
        out.println(sb.toString());
    } else {
        out.print(sb.toString());
    }

    out.flush();

    return UNDEFINED;
}
 
Example 6
Source File: Global.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private Object printImpl(final boolean newLine, final Object... objects) {
    final ScriptContext sc = currentContext();
    @SuppressWarnings("resource")
    final PrintWriter out = sc != null? new PrintWriter(sc.getWriter()) : getContext().getEnv().getOut();
    final StringBuilder sb = new StringBuilder();

    for (final Object obj : objects) {
        if (sb.length() != 0) {
            sb.append(' ');
        }

        sb.append(JSType.toString(obj));
    }

    // Print all at once to ensure thread friendly result.
    if (newLine) {
        out.println(sb.toString());
    } else {
        out.print(sb.toString());
    }

    out.flush();

    return UNDEFINED;
}
 
Example 7
Source File: Global.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private Object printImpl(final boolean newLine, final Object... objects) {
    final ScriptContext sc = currentContext();
    @SuppressWarnings("resource")
    final PrintWriter out = sc != null? new PrintWriter(sc.getWriter()) : getContext().getEnv().getOut();
    final StringBuilder sb = new StringBuilder();

    for (final Object obj : objects) {
        if (sb.length() != 0) {
            sb.append(' ');
        }

        sb.append(JSType.toString(obj));
    }

    // Print all at once to ensure thread friendly result.
    if (newLine) {
        out.println(sb.toString());
    } else {
        out.print(sb.toString());
    }

    out.flush();

    return UNDEFINED;
}
 
Example 8
Source File: VelocityCompiledScript.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
@Override
public Object eval(ScriptContext scriptContext) throws ScriptException
{
    VelocityContext velocityContext = VelocityScriptEngine.getVelocityContext(scriptContext);
    Writer out = scriptContext.getWriter();
    if (out == null)
    {
        out = new StringWriter();
        scriptContext.setWriter(out);
    }
    try
    {
        template.merge(velocityContext, out);
        out.flush();
    }
    catch (Exception exp)
    {
        throw new ScriptException(exp);
    }
    return out;
}
 
Example 9
Source File: VelocityScriptEngine.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate the given script.
 * If you wish to get a resulting string, call getContext().setWriter(new StringWriter()) then call toString()
 * on the returned writer.
 * @param reader script source reader
 * @param ctx script context
 * @return the script context writer (by default a PrintWriter towards System.out)
 * @throws ScriptException
 */
public Object eval(Reader reader, ScriptContext ctx)
                   throws ScriptException
{
    initVelocityEngine(ctx);
    String fileName = getFilename(ctx);
    VelocityContext vctx = getVelocityContext(ctx);
    Writer out = ctx.getWriter();
    if (out == null)
    {
        out = new StringWriter();
        ctx.setWriter(out);
    }
    try
    {
        velocityEngine.evaluate(vctx, out, fileName, reader);
        out.flush();
    }
    catch (Exception exp)
    {
        throw new ScriptException(exp);
    }
    return out;
}
 
Example 10
Source File: JavaClassScriptEngine.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
@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));
    }
}