Example usage for jdk.nashorn.internal.runtime Context Context

List of usage examples for jdk.nashorn.internal.runtime Context Context

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime Context Context.

Prototype

public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err,
        final ClassLoader appLoader, final ClassFilter classFilter) 

Source Link

Document

Constructor

Usage

From source file:org.kihara.util.JavascriptEngine.java

License:Open Source License

/**
 * Make a new Nashorn Context to compile and/or run JavaScript files.
 *
 * @param i input stream for Shell/*from www . j  av a2s.  co  m*/
 * @param o output stream for Shell
 * @param e error stream for Shell
 *
 * @return null if there are problems with option parsing.
 */
@SuppressWarnings("resource")
private static Context makeContext(final InputStream i, final OutputStream o, final OutputStream e) {
    final PrintWriter out = new PrintWriter(o instanceof PrintStream ? (PrintStream) o : new PrintStream(o),
            true);
    final PrintWriter err = new PrintWriter(e instanceof PrintStream ? (PrintStream) e : new PrintStream(e),
            true);

    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final ErrorManager errors = new ErrorManager(err);
    final Options options = new Options("nashorn", err);

    options.set("scripting", true);
    options.set("language", "es6");
    options.set("optimistic.types", true);
    options.set("lazy.compilation", true);

    final ClassFilter filter = (name) -> {
        if (Arrays.stream(prohibitedClasses).parallel().filter(name::equals).findAny().isPresent()) {
            System.err.println("Access Manager: Denied item " + name + ".");
            return false;
        } else
            return true;
    };

    return new Context(options, errors, out, err, loader, filter);
}