List of usage examples for javax.script ScriptEngine createBindings
public Bindings createBindings();
Bindings
. From source file:Main.java
public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); Bindings params = engine.createBindings(); params.put("stringKey", "Hello"); params.put("valueKey", 2015); Object msg = params.get("stringKey"); Object year = params.get("valueKey"); System.out.println("stringKey" + msg); System.out.println("valueKey = " + year); params.remove("valueKey"); year = params.get("valueKey"); boolean containsYear = params.containsKey("valueKey"); System.out.println("valueKey = " + year); System.out.println("params contains year = " + containsYear); }
From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); manager.put("global", "global bindings"); dumpBindings(manager.getBindings()); ScriptEngine engine = manager.getEngineByExtension("js"); engine.put("engine", "engine bindings"); dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE)); dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE)); Bindings bindings = engine.createBindings(); bindings.put("engine", "overridden engine bindings"); bindings.put("bindings", bindings); engine.eval("app.dumpBindings (bindings);", bindings); ScriptEngine engine2 = manager.getEngineByExtension("js"); engine2.put("engine2", "engine2 bindings"); dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE)); dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE)); dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE)); }
From source file:GetToKnowBindingsAndScopes.java
public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); manager.put("global", "global bindings"); dumpBindings(manager.getBindings()); ScriptEngine engine = manager.getEngineByExtension("js"); engine.put("engine", "engine bindings"); dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE)); dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE)); try {/*from w ww . j a va 2 s .co m*/ Bindings bindings = engine.createBindings(); bindings.put("engine", "overridden engine bindings"); bindings.put("app", new GetToKnowBindingsAndScopes()); bindings.put("bindings", bindings); engine.eval("app.dumpBindings (bindings);", bindings); } catch (ScriptException se) { System.err.println(se.getMessage()); } ScriptEngine engine2 = manager.getEngineByExtension("js"); engine2.put("engine2", "engine2 bindings"); dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE)); dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE)); dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE)); }
From source file:Main.java
public static void main(String[] args) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); manager.put("n1", 1); String script = "var sum = n1 + n2;print(msg + " + "' n1=' + n1 + ', n2=' + n2 + " + "', sum=' + sum);"; engine.put("n2", 2); engine.put("msg", "a string"); engine.eval(script);// w w w .j a v a 2s . com Bindings bindings = engine.createBindings(); bindings.put("n2", 3); bindings.put("msg", "another string"); engine.eval(script, bindings); ScriptContext ctx = new SimpleScriptContext(); Bindings ctxGlobalBindings = engine.createBindings(); ctx.setBindings(ctxGlobalBindings, GLOBAL_SCOPE); ctx.setAttribute("n1", 4, GLOBAL_SCOPE); ctx.setAttribute("n2", 5, ENGINE_SCOPE); ctx.setAttribute("msg", "ScriptContext:", ENGINE_SCOPE); engine.eval(script, ctx); engine.eval(script); }
From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); if (!(engine instanceof Compilable)) { System.out.println("Script compilation not supported."); return;//from ww w . j a v a 2 s . com } Compilable comp = (Compilable) engine; CompiledScript cScript = comp.compile("print(n1 + n2)"); Bindings scriptParams = engine.createBindings(); scriptParams.put("n1", 2); scriptParams.put("n2", 3); cScript.eval(scriptParams); scriptParams.put("n1", 9); scriptParams.put("n2", 7); cScript.eval(scriptParams); }
From source file:com.xafero.vee.cmd.MainApp.java
private static void execute(String fileName) throws FileNotFoundException, ScriptException { File file = (new File(fileName)).getAbsoluteFile(); if (!file.exists()) throw new FileNotFoundException("There's no file named '" + file + "'!"); String extension = Files.getExtension(file); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByExtension(extension); Bindings env = engine.createBindings(); inject(env, file);/* w w w .j av a 2s .c om*/ try { Object result = engine.eval(new FileReader(file), env); if (result != null) System.out.println(result.toString()); } catch (ScriptException e) { throw e; } }
From source file:be.solidx.hot.JSR223ScriptExecutor.java
@Override public Object execute(Script<CompiledScript> script, Map<String, Object> contextVars) throws ScriptException { try {//from www .jav a 2 s . c o m ScriptEngine scriptEngine = getEngine(); Bindings bindings = scriptEngine.createBindings(); ScriptContext scriptContext = new SimpleScriptContext(); scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); bindings.putAll(contextVars); executePreExecuteScripts(scriptContext); CompiledScript compiledScript = getCachedScript(script); Object object = compiledScript.eval(scriptContext); if (object == null) return bindings; return object; } catch (javax.script.ScriptException e) { throw new ScriptException(e); } }
From source file:org.tomitribe.tribestream.registryng.bootstrap.Provisioning.java
@PostConstruct public void init() { loginContext.setUsername("system"); ofNullable(script).ifPresent(s -> { final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js"); final Bindings bindings = engine.createBindings(); bindings.put("props", System.getProperties()); final File file = new File(s); if (file.isFile()) { try (final Reader reader = new FileReader(file)) { engine.eval(reader, bindings); } catch (final IOException | ScriptException e) { throw new IllegalArgumentException(e); }//from w w w. jav a2s .c o m } else { try { engine.eval(s, bindings); } catch (final ScriptException e) { throw new IllegalArgumentException(e); } } }); restore(); }
From source file:be.solidx.hot.JSR223ScriptExecutor.java
@Override public Object execute(Script<CompiledScript> script, Map<String, Object> contextVars, Writer writer) throws ScriptException { try {//from w w w . j a v a 2s.c o m ScriptEngine scriptEngine = getEngine(); SimpleScriptContext simpleScriptContext = new SimpleScriptContext(); Bindings bindings = scriptEngine.createBindings(); bindings.putAll(contextVars); simpleScriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); executePreExecuteScripts(simpleScriptContext); simpleScriptContext.setWriter(writer); CompiledScript compiledScript = getCachedScript(script); Object object = compiledScript.eval(simpleScriptContext); writer.flush(); if (object == null) return bindings; return object; } catch (Exception e) { throw new ScriptException(e); } }
From source file:org.jahia.modules.googleAnalytics.GoogleAnalyticsFilter.java
@Override public String execute(String previousOut, RenderContext renderContext, Resource resource, RenderChain chain) throws Exception { String out = previousOut;/*from ww w . j a v a 2s . c o m*/ String webPropertyID = renderContext.getSite().hasProperty("webPropertyID") ? renderContext.getSite().getProperty("webPropertyID").getString() : null; if (StringUtils.isNotEmpty(webPropertyID)) { String script = getResolvedTemplate(); if (script != null) { Source source = new Source(previousOut); OutputDocument outputDocument = new OutputDocument(source); List<Element> headElementList = source.getAllElements(HTMLElementName.HEAD); for (Element element : headElementList) { final EndTag headEndTag = element.getEndTag(); String extension = StringUtils.substringAfterLast(template, "."); ScriptEngine scriptEngine = scriptEngineUtils.scriptEngine(extension); ScriptContext scriptContext = new GoogleScriptContext(); final Bindings bindings = scriptEngine.createBindings(); bindings.put("webPropertyID", webPropertyID); String url = resource.getNode().getUrl(); if (renderContext.getRequest().getAttribute("analytics-path") != null) { url = (String) renderContext.getRequest().getAttribute("analytics-path"); } bindings.put("resourceUrl", url); bindings.put("resource", resource); bindings.put("gaMap", renderContext.getRequest().getAttribute("gaMap")); scriptContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE); // The following binding is necessary for Javascript, which doesn't offer a console by default. bindings.put("out", new PrintWriter(scriptContext.getWriter())); scriptEngine.eval(script, scriptContext); StringWriter writer = (StringWriter) scriptContext.getWriter(); final String googleAnalyticsScript = writer.toString(); if (StringUtils.isNotBlank(googleAnalyticsScript)) { outputDocument.replace(headEndTag.getBegin(), headEndTag.getBegin() + 1, "\n" + AggregateCacheFilter.removeEsiTags(googleAnalyticsScript) + "\n<"); } break; // avoid to loop if for any reasons multiple body in the page } out = outputDocument.toString().trim(); } } return out; }