protected Object evaluateExpression(Object expression, Map<String, Object> parameters) { try { Object result = null; StringWriter writer = new StringWriter(); ScriptContext context = new SimpleScriptContext(); for (Map.Entry<String, Object> property : engineProperties.entrySet()) { context.setAttribute(property.getKey(), property.getValue(), ScriptContext.ENGINE_SCOPE); } Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); bindings.putAll(parameters); context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); context.setWriter(writer); if (expression instanceof CompiledScript) { logger.debug("About to evaluate compiled expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = ((CompiledScript) expression).eval(context); } else { logger.debug("About to evaluate expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = scriptEngine.eval(expression.toString(), context); } if (result == null) { result = writer.toString(); } return result; } catch (ScriptException e) { throw new RuntimeException("Error when evaluating script", e); } }
public class MyServlet extends HttpServlet { private ThreadLocal<ScriptEngine> engineHolder; @Override public void init() throws ServletException { engineHolder = new ThreadLocal<ScriptEngine>() { @Override protected ScriptEngine initialValue() { return new ScriptEngineManager().getEngineByName("nashorn"); } }; } @Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { try (PrintWriter writer = res.getWriter()) { ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(engineHolder.get().createBindings(), ScriptContext.ENGINE_SCOPE); Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE); engineScope.put("writer", writer); Object value = engineHolder.get().eval("writer.print('Hello, World!');", engineScope); writer.close(); } catch (IOException | ScriptException ex) { Logger.getLogger(MyServlet.class.getName()).log(Level.SEVERE, null, ex); } } }
private static void test6() throws ScriptException { NashornScriptEngine engine = createEngine(); ScriptContext defaultContext = engine.getContext(); defaultContext.getBindings(ScriptContext.GLOBAL_SCOPE).put("foo", "hello"); ScriptContext customContext = new SimpleScriptContext(); customContext.setBindings(defaultContext.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.ENGINE_SCOPE); Bindings bindings = new SimpleBindings(); bindings.put("foo", "world"); customContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE); // engine.eval("foo = 23;"); // overrides foo in all contexts, why??? engine.eval("print(foo)"); // hello engine.eval("print(foo)", customContext); // world engine.eval("print(foo)", defaultContext); // hello }
ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); Object[] argArray = argValues.toArray(new Object[argValues.size()]);
public void setBindings(Bindings bindings, int scope) { context.setBindings(bindings, scope); }
@Override public void setBindings(Bindings bindings, int scope) { context.setBindings(bindings, scope); }
/** {@inheritDoc} */ public void setBindings(Bindings bindings, int scope) { if (scope == ScriptContext.GLOBAL_SCOPE || scope == ScriptContext.ENGINE_SCOPE) { context.setBindings(bindings, scope); } else { throw new IllegalArgumentException("invalid scope"); } }
public ScriptEngine getEngineByMimeType(String mimeType) { if (mimeType == null) { throw new NullPointerException("Null mimeType"); } ScriptEngineFactory factory = mimetypeMap.get(mimeType); if (factory == null) { throw new IllegalArgumentException("No engine for " + mimeType); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public ScriptEngine getEngineByName(String shortName) { if (shortName == null) { throw new NullPointerException("Null shortName"); } ScriptEngineFactory factory = nameMap.get(shortName); if (factory == null) { throw new IllegalArgumentException("No engine for " + shortName); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public ScriptEngine getEngineByExtension(String extension) { if (extension == null) { throw new NullPointerException("Null extension"); } ScriptEngineFactory factory = extensionMap.get(extension); if (factory == null) { throw new IllegalArgumentException("No engine for " + extension); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public ScriptEngine getEngineByMimeType(String mimeType) { if (mimeType == null) { throw new NullPointerException("Null mimeType"); } ScriptEngineFactory factory = mimetypeMap.get(mimeType); if (factory == null) { throw new IllegalArgumentException("No engine for " + mimeType); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public ScriptEngine getEngineByName(String shortName) { if (shortName == null) { throw new NullPointerException("Null shortName"); } ScriptEngineFactory factory = nameMap.get(shortName); if (factory == null) { throw new IllegalArgumentException("No engine for " + shortName); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public ScriptEngine getEngineByName(String shortName) { if (shortName == null) { throw new NullPointerException("Null symbolicName"); } ScriptEngineFactory factory = nameMap.get(shortName); if (factory == null) { throw new IllegalArgumentException("No engine for " + shortName); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public ScriptEngine getEngineByMimeType(String mimeType) { if (mimeType == null) { throw new NullPointerException("Null mimeType"); } ScriptEngineFactory factory = mimetypeMap.get(mimeType); if (factory == null) { throw new IllegalArgumentException("No engine for " + mimeType); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public ScriptEngine getEngineByName(String shortName) { if (shortName == null) { throw new NullPointerException("Null symbolicName"); } ScriptEngineFactory factory = nameMap.get(shortName); if (factory == null) { throw new IllegalArgumentException("No engine for " + shortName); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public ScriptEngine getEngineByExtension(String extension) { if (extension == null) { throw new NullPointerException("Null extension"); } ScriptEngineFactory factory = extensionMap.get(extension); if (factory == null) { throw new IllegalArgumentException("No engine for " + extension); } ScriptEngine engine = factory.getScriptEngine(); engine.getContext().setBindings(globalMap, ScriptContext.GLOBAL_SCOPE); return engine; }
public Object execute(Map<String,Object> variables) { bindings.putAll(variables); try { scriptEngine.getContext().setBindings(bindings,ScriptContext.ENGINE_SCOPE); return compiledScript.eval(); } catch (ScriptException e) { throw new RuntimeException(e); } finally { variables.keySet().forEach(bindings::remove); } }
public void run(String code, String extension, Map<String, Object> vars) throws ScriptException { ScriptEngine engine = loadEngine(extension); if (engine == null) throw new ScriptException("Couldn't load engine with extension " + extension); ScriptContext context = new SimpleScriptContext(); if (vars != null) { context.setBindings(new SimpleBindings(vars), ScriptContext.ENGINE_SCOPE); } engine.eval(extension, context); }
@Nonnull @Override public ScriptContext createContext(final String language) { ScriptContext context = new SimpleScriptContext(); context.setBindings(engineForLanguage(language).createBindings(), ScriptContext.ENGINE_SCOPE); return context; }
ScriptContext context = new SimpleScriptContext(); context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); //Exception handling omitted for brevity //Evaluate the compiled runtime in our new context compiledRuntime.eval(context); //Evaluate the source in the same context engine.eval(source, context); //Call a function JSObject jsObject = (JSObject) context.getAttribute(function, ScriptContext.ENGINE_SCOPE); jsObject.call(null, args);