List of usage examples for javax.script SimpleScriptContext setBindings
public void setBindings(Bindings bindings, int scope)
Bindings
of attributes for the given scope. From source file:org.apache.felix.webconsole.plugins.scriptconsole.internal.ScriptConsolePlugin.java
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final String contentType = getContentType(req); resp.setContentType(contentType);//from w ww. j a v a2s . c om if (contentType.startsWith("text/")) { resp.setCharacterEncoding("UTF-8"); } final String script = getCodeValue(req); final Bindings bindings = new SimpleBindings(); final PrintWriter pw = resp.getWriter(); final ScriptHelper osgi = new ScriptHelper(getBundleContext()); final Writer errorWriter = new LogWriter(log); final Reader reader = new StringReader(script); //Populate bindings bindings.put("request", req); bindings.put("reader", reader); bindings.put("response", resp); bindings.put("out", pw); bindings.put("osgi", osgi); //Also expose the bundleContext to simplify scripts interaction with the //enclosing OSGi container bindings.put("bundleContext", getBundleContext()); final String lang = WebConsoleUtil.getParameter(req, "lang"); final boolean webClient = "webconsole".equals(WebConsoleUtil.getParameter(req, "client")); SimpleScriptContext sc = new SimpleScriptContext(); sc.setBindings(bindings, ScriptContext.ENGINE_SCOPE); sc.setWriter(pw); sc.setErrorWriter(errorWriter); sc.setReader(reader); try { log.log(LogService.LOG_DEBUG, "Executing script" + script); eval(script, lang, sc); } catch (Throwable t) { if (!webClient) { resp.setStatus(500); } pw.println(exceptionToString(t)); log.log(LogService.LOG_ERROR, "Error in executing script", t); } finally { if (osgi != null) { osgi.cleanup(); } } }
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 ww w .j a v a 2 s . 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.freeplane.plugin.script.GenericScript.java
private SimpleScriptContext createScriptContext(final NodeModel node) { final SimpleScriptContext context = new SimpleScriptContext(); final OutputStreamWriter outWriter = new OutputStreamWriter(outStream); context.setWriter(outWriter);/*from ww w .jav a2s .c o m*/ context.setErrorWriter(outWriter); context.setBindings(createBinding(node), javax.script.ScriptContext.ENGINE_SCOPE); return context; }