List of usage examples for javax.script ScriptEngine getBindings
public Bindings getBindings(int scope);
From source file:io.github.jeddict.jcode.parser.ejs.EJSParser.java
private ScriptEngine createEngine() { CompiledScript cscript;/* www. j av a 2 s . c om*/ Bindings bindings; ScriptEngine scriptEngine = new NashornScriptEngineFactory().getScriptEngine("--language=es6");//engine = new ScriptEngineManager().getEngineByName("nashorn"); try { try { if (base == null) { base = IOUtils.toString(EJSParser.class.getClassLoader() .getResourceAsStream("io/github/jeddict/jcode/parser/ejs/resources/base.js"), "UTF-8"); } if (ejs == null) { ejs = IOUtils.toString(EJSParser.class.getClassLoader() .getResourceAsStream("io/github/jeddict/jcode/parser/ejs/resources/ejs.js"), "UTF-8"); } } catch (IOException ex) { Exceptions.printStackTrace(ex); } scriptEngine.eval(base); Compilable compilingEngine = (Compilable) scriptEngine; cscript = compilingEngine.compile(ejs); bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE); cscript.eval(bindings); scriptEngine.eval(scripts.toString()); for (Map<String, Object> context : contexts) { context.keySet().stream().forEach((key) -> { try { bindings.put(key, context.get(key)); if (context.get(key) instanceof Collection) { scriptEngine.eval(String.format("%s = Java.from(%s);", key, key)); } } catch (ScriptException ex) { Exceptions.printStackTrace(ex); } }); } } catch (ScriptException ex) { Exceptions.printStackTrace(ex); } return scriptEngine; }
From source file:org.nuxeo.ecm.core.io.download.DownloadServiceImpl.java
@Override public boolean checkPermission(DocumentModel doc, String xpath, Blob blob, String reason, Map<String, Serializable> extendedInfos) { List<DownloadPermissionDescriptor> descriptors = registry.getDownloadPermissionDescriptors(); if (descriptors.isEmpty()) { return true; }/*from www . j a v a 2 s . c o m*/ xpath = fixXPath(xpath); Map<String, Object> context = new HashMap<>(); Map<String, Serializable> ei = extendedInfos == null ? Collections.emptyMap() : extendedInfos; NuxeoPrincipal currentUser = ClientLoginModule.getCurrentPrincipal(); context.put("Document", doc); context.put("XPath", xpath); context.put("Blob", blob); context.put("Reason", reason); context.put("Infos", ei); context.put("Rendition", ei.get("rendition")); context.put("CurrentUser", currentUser); for (DownloadPermissionDescriptor descriptor : descriptors) { ScriptEngine engine = scriptEngineManager.getEngineByName(descriptor.getScriptLanguage()); if (engine == null) { throw new NuxeoException("Engine not found for language: " + descriptor.getScriptLanguage() + " in permission: " + descriptor.getName()); } if (!(engine instanceof Invocable)) { throw new NuxeoException("Engine " + engine.getClass().getName() + " not Invocable for language: " + descriptor.getScriptLanguage() + " in permission: " + descriptor.getName()); } Object result; try { engine.eval(descriptor.getScript()); engine.getBindings(ScriptContext.ENGINE_SCOPE).putAll(context); result = ((Invocable) engine).invokeFunction(RUN_FUNCTION); } catch (NoSuchMethodException e) { throw new NuxeoException("Script does not contain function: " + RUN_FUNCTION + "() in permission: " + descriptor.getName(), e); } catch (ScriptException e) { log.error("Failed to evaluate script: " + descriptor.getName(), e); continue; } if (!(result instanceof Boolean)) { log.error("Failed to get boolean result from permission: " + descriptor.getName() + " (" + result + ")"); continue; } boolean allow = ((Boolean) result).booleanValue(); if (!allow) { return false; } } return true; }