Java Code Examples for javax.script.ScriptContext#getBindings()
The following examples show how to use
javax.script.ScriptContext#getBindings() .
These examples are extracted from open source projects.
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 Project: TencentKona-8 File: ScopeTest.java License: GNU General Public License v2.0 | 6 votes |
@Test public void userEngineScopeBindingsRetentionTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); e.eval("function foo() {}", newContext); // definition retained with user's ENGINE_SCOPE Binding assertTrue(e.eval("typeof foo", newContext).equals("function")); final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE); // but not in another ENGINE_SCOPE binding newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); assertTrue(e.eval("typeof foo", newContext).equals("undefined")); // restore ENGINE_SCOPE and check again newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE); assertTrue(e.eval("typeof foo", newContext).equals("function")); }
Example 2
Source Project: jdk8u60 File: MultiScopes.java License: 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 3
Source Project: localization_nifi File: ClojureScriptEngine.java License: Apache License 2.0 | 6 votes |
@Override public Object eval(Reader reader, ScriptContext context) throws ScriptException { try { // Get engine bindings and send them to Clojure Bindings engineBindings = context.getBindings(ScriptContext.ENGINE_SCOPE); engineBindings.entrySet().forEach((entry) -> Var.intern(Namespace.findOrCreate(NAMESPACE_SYMBOL), Symbol.create(entry.getKey().intern()), entry.getValue(), true)); Var.pushThreadBindings( RT.map(RT.CURRENT_NS, RT.CURRENT_NS.deref(), RT.IN, new LineNumberingPushbackReader(context.getReader()), RT.OUT, context.getWriter(), RT.ERR, context.getErrorWriter())); Object result = Compiler.load(reader); return result; } catch (Exception e) { throw new ScriptException(e); } finally { Namespace.remove(NAMESPACE_SYMBOL); } }
Example 4
Source Project: openjdk-jdk8u File: MultiScopes.java License: 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 5
Source Project: java8-tutorial File: Nashorn11.java License: MIT License | 6 votes |
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")); }
Example 6
Source Project: asciidoctorj File: PrismJsHighlighter.java License: Apache License 2.0 | 6 votes |
@Override public HighlightResult highlight(Block node, String source, String lang, Map<String, Object> options) { ScriptContext ctx = scriptEngine.getContext(); // <4> Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("text", source); bindings.put("language", lang); try { String result = (String) scriptEngine.eval( "Prism.highlight(text, Prism.languages[language], language)", bindings); return new HighlightResult(result); } catch (ScriptException e) { throw new RuntimeException(e); } }
Example 7
Source Project: java8-tutorial File: Nashorn11.java License: MIT License | 6 votes |
private static void test5() throws ScriptException { NashornScriptEngine engine = createEngine(); engine.eval("var obj = { foo: 'foo' };"); engine.eval("function printFoo() { print(obj.foo) };"); ScriptContext defaultContext = engine.getContext(); Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); SimpleScriptContext context1 = new SimpleScriptContext(); context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); SimpleScriptContext context2 = new SimpleScriptContext(); context2.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); engine.eval("obj.foo = 'bar';", context1); engine.eval("printFoo();", context1); engine.eval("printFoo();", context2); }
Example 8
Source Project: java8-tutorial File: Nashorn11.java License: MIT License | 6 votes |
private static void test7() throws ScriptException { NashornScriptEngine engine = createEngine(); engine.eval("var foo = 23;"); ScriptContext defaultContext = engine.getContext(); Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); SimpleScriptContext context1 = new SimpleScriptContext(); context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); SimpleScriptContext context2 = new SimpleScriptContext(); context2.getBindings(ScriptContext.ENGINE_SCOPE).put("foo", defaultBindings.get("foo")); engine.eval("foo = 44;", context1); engine.eval("print(foo);", context1); engine.eval("print(foo);", context2); }
Example 9
Source Project: openjdk-jdk9 File: MultiScopes.java License: 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 10
Source Project: jdk8u_nashorn File: MultiScopes.java License: 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 11
Source Project: hottub File: MultiScopes.java License: 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 12
Source Project: hottub File: ScopeTest.java License: GNU General Public License v2.0 | 6 votes |
@Test public void userEngineScopeBindingsRetentionTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); e.eval("function foo() {}", newContext); // definition retained with user's ENGINE_SCOPE Binding assertTrue(e.eval("typeof foo", newContext).equals("function")); final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE); // but not in another ENGINE_SCOPE binding newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); assertTrue(e.eval("typeof foo", newContext).equals("undefined")); // restore ENGINE_SCOPE and check again newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE); assertTrue(e.eval("typeof foo", newContext).equals("function")); }
Example 13
Source Project: java8-tutorial File: Nashorn11.java License: MIT License | 5 votes |
private static void test4() throws ScriptException { NashornScriptEngine engine = createEngine(); engine.eval("function foo() { print('bar') };"); ScriptContext defaultContext = engine.getContext(); Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); SimpleScriptContext context = new SimpleScriptContext(); context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); engine.eval("foo();", context); System.out.println(context.getAttribute("foo")); }
Example 14
Source Project: es6draft File: ScriptEngineImpl.java License: MIT License | 5 votes |
private Realm getEvalRealm(ScriptContext context) { Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); if (bindings instanceof GlobalBindings) { // Return realm from engine scope bindings if compatible, i.e. from the same world instance. Realm realm = ((GlobalBindings) bindings).getRealm(); if (realm.getWorld() == world) { return realm; } } // Otherwise create a new realm. return newScriptingRealm(); }
Example 15
Source Project: o2oa File: SyncOrganization.java License: GNU Affero General Public License v3.0 | 5 votes |
private String initPassword(Business business, Person person) throws Exception { String str = Config.person().getPassword(); Pattern pattern = Pattern.compile(com.x.base.core.project.config.Person.REGULAREXPRESSION_SCRIPT); Matcher matcher = pattern.matcher(str); if (matcher.matches()) { String eval = ScriptFactory.functionalization(StringEscapeUtils.unescapeJson(matcher.group(1))); ScriptContext scriptContext = new SimpleScriptContext(); Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("person", person); Object o = ScriptFactory.scriptEngine.eval(eval, scriptContext); return o.toString(); } else { return str; } }
Example 16
Source Project: o2oa File: SyncOrganization.java License: GNU Affero General Public License v3.0 | 5 votes |
private String initPassword(Business business, Person person) throws Exception { String str = Config.person().getPassword(); Pattern pattern = Pattern.compile(com.x.base.core.project.config.Person.REGULAREXPRESSION_SCRIPT); Matcher matcher = pattern.matcher(str); if (matcher.matches()) { String eval = ScriptFactory.functionalization(StringEscapeUtils.unescapeJson(matcher.group(1))); ScriptContext scriptContext = new SimpleScriptContext(); Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("person", person); Object o = ScriptFactory.scriptEngine.eval(eval, scriptContext); return o.toString(); } else { return str; } }
Example 17
Source Project: netbeans File: RsrcLoader.java License: Apache License 2.0 | 5 votes |
RsrcLoader(FileObject fo, ScriptContext map) { this.fo = fo; this.map = map; this.engineScope = map.getBindings(ScriptContext.ENGINE_SCOPE); setTemplateLoader(this); setTemplateExceptionHandler(this); Logger.getLogger("freemarker.runtime").setLevel(Level.OFF); }
Example 18
Source Project: dynkt File: KotlinScriptEngine.java License: GNU Affero General Public License v3.0 | 5 votes |
@Override public Object eval(String script, ScriptContext context) throws ScriptException { try { File file = File.createTempFile("kc_evl", ".kts"); KotlinCompiledScript scr = new KotlinCompiledScript(this, script, file); Bindings ctx = context.getBindings(ScriptContext.ENGINE_SCOPE); return scr.eval(ctx); } catch (IOException e) { throw new ScriptException(e); } }
Example 19
Source Project: tomee File: OpenEJBScripter.java License: Apache License 2.0 | 4 votes |
private static void bindLocal(final ScriptContext context) { final Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("bm", new BeanManagerHelper()); }
Example 20
Source Project: o2oa File: ActionExecute.java License: GNU Affero General Public License v3.0 | 4 votes |
ActionResult<Wo> execute(EffectivePerson effectivePerson, String flag) throws Exception { try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { ActionResult<Wo> result = new ActionResult<>(); Agent agent = emc.flag(flag, Agent.class); if (null == agent) { throw new ExceptionAgentNotExist(flag); } if (LOCK.contains(agent.getId())) { throw new ExceptionAgentLastNotEnd(agent.getId(), agent.getName(), agent.getAlias(), DateTools.format(agent.getLastStartTime())); } else { try { LOCK.add(agent.getId()); emc.beginTransaction(Agent.class); agent.setLastStartTime(new Date()); emc.commit(); ScriptContext scriptContext = new SimpleScriptContext(); Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE); Resources resources = new Resources(); resources.setEntityManagerContainer(emc); resources.setContext(ThisApplication.context()); resources.setOrganization(new Organization(ThisApplication.context())); resources.setApplications(ThisApplication.context().applications()); resources.setWebservicesClient(new WebservicesClient()); bindings.put(ScriptFactory.BINDING_NAME_RESOURCES, resources); bindings.put(ScriptFactory.BINDING_NAME_APPLICATIONS, ThisApplication.context().applications()); String cacheKey = ApplicationCache.concreteCacheKey(ActionExecute.class, agent.getId()); Element element = CACHE.get(cacheKey); CompiledScript compiledScript = null; if ((null != element) && (null != element.getObjectValue())) { logger.print("has agent cache {}", agent.getId()); } compiledScript = ScriptFactory.compile(ScriptFactory.functionalization(agent.getText())); CACHE.put(new Element(cacheKey, compiledScript)); try { ScriptFactory.initialServiceScriptText().eval(scriptContext); compiledScript.eval(scriptContext); } catch (Exception e) { throw new ExceptionAgentEval(e, e.getMessage(), agent.getId(), agent.getName(), agent.getAlias(), agent.getText()); } } finally { LOCK.remove(agent.getId()); } } emc.beginTransaction(Agent.class); agent.setLastEndTime(new Date()); emc.commit(); Wo wo = new Wo(); wo.setId(agent.getId()); result.setData(wo); return result; } }