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);
}
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"));
}
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);
}
}
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;
}
Aggregations