Search in sources :

Example 71 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test2.

private static void test2() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("function foo() { print('bar') };");
    SimpleScriptContext context = new SimpleScriptContext();
    engine.eval("print(Function);", context);
    engine.eval("foo();", context);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine)

Example 72 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test3.

private static void test3() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context = new SimpleScriptContext();
    context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    engine.eval("function foo() { print('bar') };", context);
    engine.eval("foo();", context);
    Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    System.out.println(bindings.get("foo"));
    System.out.println(context.getAttribute("foo"));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 73 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project freeplane by freeplane.

the class GenericScript method execute.

@Override
public Object execute(final NodeModel node) {
    try {
        if (errorsInScript != null && compileTimeStrategy.canUseOldCompiledScript()) {
            throw new ExecuteScriptException(errorsInScript.getMessage(), errorsInScript);
        }
        final ScriptingSecurityManager scriptingSecurityManager = createScriptingSecurityManager();
        final ScriptingPermissions originalScriptingPermissions = new ScriptingPermissions(ResourceController.getResourceController().getProperties());
        final FreeplaneSecurityManager securityManager = (FreeplaneSecurityManager) System.getSecurityManager();
        final boolean needToSetFinalSecurityManager = securityManager.needToSetFinalSecurityManager();
        final PrintStream oldOut = System.out;
        try {
            final SimpleScriptContext context = createScriptContext(node);
            if (compilationEnabled && engine instanceof Compilable) {
                compileAndCache((Compilable) engine);
                if (needToSetFinalSecurityManager)
                    securityManager.setFinalSecurityManager(scriptingSecurityManager);
                System.setOut(outStream);
                return compiledScript.eval(context);
            } else {
                if (needToSetFinalSecurityManager)
                    securityManager.setFinalSecurityManager(scriptingSecurityManager);
                System.setOut(outStream);
                return engine.eval(scriptSource.getScript(), context);
            }
        } finally {
            System.setOut(oldOut);
            if (needToSetFinalSecurityManager && securityManager.hasFinalSecurityManager())
                securityManager.removeFinalSecurityManager(scriptingSecurityManager);
            /* restore preferences (and assure that the values are unchanged!). */
            originalScriptingPermissions.restorePermissions();
        }
    } catch (final ScriptException e) {
        handleScriptRuntimeException(e);
        // And if: Shouldn't it raise an ExecuteScriptException?
        throw new RuntimeException(e);
    } catch (final Throwable e) {
        if (Controller.getCurrentController().getSelection() != null)
            Controller.getCurrentModeController().getMapController().select(node);
        throw new ExecuteScriptException(e.getMessage(), e);
    }
}
Also used : PrintStream(java.io.PrintStream) ScriptException(javax.script.ScriptException) SimpleScriptContext(javax.script.SimpleScriptContext) Compilable(javax.script.Compilable) FreeplaneSecurityManager(org.freeplane.main.application.FreeplaneSecurityManager)

Example 74 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project freeplane by freeplane.

the class GenericScript method createScriptContext.

private SimpleScriptContext createScriptContext(final NodeModel node) {
    final SimpleScriptContext context = new SimpleScriptContext();
    final OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
    context.setWriter(outWriter);
    context.setErrorWriter(outWriter);
    context.setBindings(createBinding(node), javax.script.ScriptContext.ENGINE_SCOPE);
    return context;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

SimpleScriptContext (javax.script.SimpleScriptContext)74 ScriptContext (javax.script.ScriptContext)59 Bindings (javax.script.Bindings)30 SimpleBindings (javax.script.SimpleBindings)26 Test (org.junit.Test)26 ScriptEngine (javax.script.ScriptEngine)10 ScriptException (javax.script.ScriptException)9 CompiledScript (javax.script.CompiledScript)8 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)7 ScriptEngineManager (javax.script.ScriptEngineManager)6 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Invocable (javax.script.Invocable)3 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2