Java Code Examples for javax.script.ScriptContext#setBindings()
The following examples show how to use
javax.script.ScriptContext#setBindings() .
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: MultiScopes.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
public static void main(final String[] args) throws Exception { final ScriptEngineManager manager = new ScriptEngineManager(); final ScriptEngine engine = manager.getEngineByName("nashorn"); engine.put("x", "hello"); // print global variable "x" engine.eval("print(x);"); // the above line prints "hello" // Now, pass a different script context final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); final Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE); // add new variable "x" to the new engineScope engineScope.put("x", "world"); // execute the same script - but this time pass a different script context engine.eval("print(x);", newContext); // the above line prints "world" }
Example 2
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 6 votes |
@Test public void userEngineScopeBindingsTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); e.eval("function func() {}"); final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); // we are using a new bindings - so it should have 'func' defined final Object value = e.eval("typeof func", newContext); assertTrue(value.equals("undefined")); }
Example 3
Source File: ConfigurationValidator.java From revapi with Apache License 2.0 | 6 votes |
private ScriptEngine getJsEngine(Writer output) throws IOException, ScriptException { ScriptEngine ret = null; if (jsEngine != null) { ret = jsEngine.get(); } if (ret == null) { ret = new ScriptEngineManager().getEngineByName("javascript"); ScriptContext ctx = new SimpleScriptContext(); Bindings globalScope = ret.createBindings(); ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); initTv4(ret, globalScope); ret.setContext(ctx); jsEngine = new WeakReference<>(ret); } ret.getContext().setWriter(output); ret.getContext().setErrorWriter(output); return ret; }
Example 4
Source File: TrustedScriptEngineTest.java From hottub with GNU General Public License v2.0 | 6 votes |
@Test public void globalPerEngineTest() throws ScriptException { final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); final String[] options = new String[] { "--global-per-engine" }; final ScriptEngine e = fac.getScriptEngine(options); e.eval("function foo() {}"); final ScriptContext newCtx = new SimpleScriptContext(); newCtx.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); // all global definitions shared and so 'foo' should be // visible in new Bindings as well. assertTrue(e.eval("typeof foo", newCtx).equals("function")); e.eval("function bar() {}", newCtx); // bar should be visible in default context assertTrue(e.eval("typeof bar").equals("function")); }
Example 5
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 6 votes |
@Test public void invokeFunctionInGlobalScopeTest() throws Exception { final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); final ScriptContext ctxt = engine.getContext(); // define a function called "func" engine.eval("func = function() { return 42 }"); // move ENGINE_SCOPE Bindings to GLOBAL_SCOPE ctxt.setBindings(ctxt.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.GLOBAL_SCOPE); // create a new Bindings and set as ENGINE_SCOPE ctxt.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); // define new function that calls "func" now in GLOBAL_SCOPE engine.eval("newfunc = function() { return func() }"); // call "newfunc" and check the return value Object value = ((Invocable)engine).invokeFunction("newfunc"); assertTrue(((Number)value).intValue() == 42); }
Example 6
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals. */ @Test public static void multiThreadedPrimitiveTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); final Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext); final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt); assertEquals(obj1, "original context"); assertEquals(obj2, "new context"); final String sharedScript = "''.foo"; final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); t1.start(); t2.start(); t1.join(); t2.join(); final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); assertEquals(obj3, "newer context"); final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); t3.start(); t4.start(); t3.join(); t4.join(); Assert.assertEquals(e.eval(sharedScript), "original context"); Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); }
Example 7
Source File: ScopeTest.java From jdk8u_nashorn with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to prototype user accessor properties for shared script classes with multiple globals. */ @Test public static void multiThreadedAccessorTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'original context' })", origContext); e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'new context', configurable: true })", newCtxt); final String sharedScript = "({}).foo"; final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); t1.start(); t2.start(); t1.join(); t2.join(); final Object obj3 = e.eval("delete Object.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); assertEquals(obj3, "newer context"); final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); t3.start(); t4.start(); t3.join(); t4.join(); }
Example 8
Source File: ScopeTest.java From jdk8u_nashorn with GNU General Public License v2.0 | 5 votes |
public void method() throws ScriptException { // a context with a new global bindings, same engine bindings final ScriptContext sc = new SimpleScriptContext(); final Bindings global = new SimpleBindings(); sc.setBindings(global, ScriptContext.GLOBAL_SCOPE); sc.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE); global.put("text", "methodText"); String value = engine.eval("text", sc).toString(); Assert.assertEquals(value, "methodText"); }
Example 9
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 5 votes |
@Test public static void engineOverwriteTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = new SimpleBindings(); b.put("engine", "hello"); b.put("foo", 32); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.setContext(newCtxt); assertEquals(e.eval("engine"), "hello"); assertEquals(((Number)e.eval("foo")).intValue(), 32); }
Example 10
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to undefined global variables for shared script classes with multiple globals. */ @Test public static void multiThreadedGlobalTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); assertEquals(e.eval("foo = 'original context';", origContext), "original context"); assertEquals(e.eval("foo = 'new context';", newCtxt), "new context"); final String sharedScript = "foo"; final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); t1.start(); t2.start(); t1.join(); t2.join(); final Object obj3 = e.eval("delete foo; foo = 'newer context';", newCtxt); assertEquals(obj3, "newer context"); final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); t3.start(); t4.start(); t3.join(); t4.join(); Assert.assertEquals(e.eval(sharedScript), "original context"); Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); }
Example 11
Source File: ScopeTest.java From jdk8u_nashorn with GNU General Public License v2.0 | 5 votes |
public void program() throws ScriptException { ScriptContext sc = new SimpleScriptContext(); Bindings global = new SimpleBindings(); sc.setBindings(global, ScriptContext.GLOBAL_SCOPE); sc.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE); global.put("text", "programText"); String value = engine.eval("text", sc).toString(); Assert.assertEquals(value, "programText"); engine.put("program", this); engine.eval("program.method()"); // eval again from here! value = engine.eval("text", sc).toString(); Assert.assertEquals(value, "programText"); }
Example 12
Source File: ScopeTest.java From jdk8u_nashorn with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded scope function invocation for shared script classes with multiple globals. */ @Test public static void multiThreadedFunctionTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext); assertEquals(origContext.getAttribute("scopeVar"), 1); assertEquals(e.eval("scopeTest()"), 1); e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt); assertEquals(newCtxt.getAttribute("scopeVar"), 1); assertEquals(e.eval("scopeTest();", newCtxt), 1); assertEquals(e.eval("scopeVar = 3;", newCtxt), 3); assertEquals(newCtxt.getAttribute("scopeVar"), 3); final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000)); t1.start(); t2.start(); t1.join(); t2.join(); }
Example 13
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 5 votes |
@Test public static void contextOverwriteTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = new SimpleBindings(); b.put("context", "hello"); b.put("foo", 32); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.setContext(newCtxt); assertEquals(e.eval("context"), "hello"); assertEquals(((Number)e.eval("foo")).intValue(), 32); }
Example 14
Source File: ScopeTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to prototype user accessor properties for shared script classes with multiple globals. */ @Test public static void multiThreadedAccessorTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'original context' })", origContext); e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'new context', configurable: true })", newCtxt); final String sharedScript = "({}).foo"; final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); t1.start(); t2.start(); t1.join(); t2.join(); final Object obj3 = e.eval("delete Object.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); assertEquals(obj3, "newer context"); final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); t3.start(); t4.start(); t3.join(); t4.join(); }
Example 15
Source File: ScopeTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals. */ @Test public static void multiThreadedPrimitiveTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); final Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext); final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt); assertEquals(obj1, "original context"); assertEquals(obj2, "new context"); final String sharedScript = "''.foo"; final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); t1.start(); t2.start(); t1.join(); t2.join(); final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); assertEquals(obj3, "newer context"); final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); t3.start(); t4.start(); t3.join(); t4.join(); Assert.assertEquals(e.eval(sharedScript), "original context"); Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); }
Example 16
Source File: ScopeTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to primitive prototype user accessor properties for shared script classes with multiple globals. */ @Test public static void multiThreadedPrimitiveAccessorTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'original context' })", origContext); e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'new context' })", newCtxt); final String sharedScript = "''.foo"; final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); t1.start(); t2.start(); t1.join(); t2.join(); final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); assertEquals(obj3, "newer context"); final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); t3.start(); t4.start(); t3.join(); t4.join(); }
Example 17
Source File: ScopeTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Test public void userEngineScopeBindingsTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); e.eval("function func() {}"); final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); // we are using a new bindings - so it should have 'func' defined final Object value = e.eval("typeof func", newContext); assertTrue(value.equals("undefined")); }
Example 18
Source File: ScopeTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Test public static void contextOverwriteTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = new SimpleBindings(); b.put("context", "hello"); b.put("foo", 32); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.setContext(newCtxt); assertEquals(e.eval("context"), "hello"); assertEquals(((Number)e.eval("foo")).intValue(), 32); }
Example 19
Source File: SpagoBIScriptManager.java From Knowage-Server with GNU Affero General Public License v3.0 | 4 votes |
public Object runScript(String script, String language, Map<String, Object> bindings, List imports) { Object results; logger.debug("IN"); results = null; try { if (imports != null) { StringBuffer importsBuffer = new StringBuffer(); for (Object importedScriptReference : imports) { if (importedScriptReference instanceof File) { importsBuffer.append(this.getImportedScript((File) importedScriptReference) + "\n"); } else if (importedScriptReference instanceof URL) { importsBuffer.append(this.getImportedScript((URL) importedScriptReference) + "\n"); } else { logger.warn("Impossible to resolve import reference of type [" + importedScriptReference.getClass().getName() + "]"); } } script = importsBuffer.toString() + script; } if (isGroovy(language)) { return evaluateGroovy(script, bindings); } final ScriptEngine scriptEngine = getScriptEngine(language); if (scriptEngine == null) { throw new RuntimeException("No engine available to execute scripts of type [" + language + "]"); } else { logger.debug("Found engine [" + scriptEngine.NAME + "]"); } ScriptContext scriptContext = new SimpleScriptContext(); if (bindings != null) { Bindings scriptBindings = new SimpleBindings(bindings); scriptContext.setBindings(scriptBindings, ScriptContext.ENGINE_SCOPE); } scriptEngine.setContext(scriptContext); PermissionCollection pc = new Permissions(); // This means no permissions at all CodeSource codeSource = scriptEngine.getClass().getProtectionDomain().getCodeSource(); ProtectionDomain protectionDomain = new ProtectionDomain(codeSource, pc); ProtectionDomain[] context = new ProtectionDomain[] { protectionDomain }; AccessControlContext accessControlContext = new AccessControlContext(context); final String _script = script; results = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws ScriptException { return scriptEngine.eval(_script); } }, accessControlContext); } catch (Throwable t) { logger.error("Error while executing Javascript:\n" + script, t); throw new SpagoBIRuntimeException("An unexpected error occured while executing script", t); } finally { logger.debug("OUT"); } return results; }
Example 20
Source File: ScopeTest.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test // check that engine.js definitions are visible in all new global instances public void checkBuiltinsInNewBindingsTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); // check default global instance has engine.js definitions final Bindings g = (Bindings) e.eval("this"); Object value = g.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = g.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // check new global instance created has engine.js definitions Bindings b = e.createBindings(); value = b.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = b.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // put a mapping into GLOBAL_SCOPE final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); globalScope.put("x", "hello"); // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval assertTrue(e.eval("x").equals("hello")); final ScriptContext ctx = new SimpleScriptContext(); ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // try some arbitray Bindings for ENGINE_SCOPE Bindings sb = new SimpleBindings(); ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // engine.js builtins are still defined even with arbitrary Bindings assertTrue(e.eval("typeof print", ctx).equals("function")); assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition sb.put("x", "newX"); assertTrue(e.eval("x", ctx).equals("newX")); }