List of usage examples for jdk.nashorn.api.scripting NashornScriptEngineFactory getScriptEngine
@Override
public ScriptEngine getScriptEngine()
From source file:com.mycompany.script.engine.js.nashorn.JsNashornScriptExecutor.java
/** * ? ?.//w ww .j a v a 2s . c o m * * @param scriptPath ? ? * @param basePath * @param logger * @param binding * @return */ @Override public ScriptResult execScript(String scriptPath, String basePath, Logger logger, Map<String, Object> binding) { ScriptResult result = new ScriptResult(); NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(); try { ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE); engineScope.putAll(binding); engineScope.put("logger", logger); engineScope.put("currentDir", new File("").getAbsolutePath()); result.setStart(System.currentTimeMillis()); try (Reader reader = Files.newBufferedReader(new File(basePath, scriptPath).toPath())) { Object value = engine.eval(reader, engineScope); result.setResult(value); } } catch (IOException | ScriptException | RuntimeException ex) { logger.error("", ex); result.setException(ex); } finally { result.setFinish(System.currentTimeMillis()); } return result; }
From source file:com.wx3.galacdecks.ai.EvaluatorAI.java
License:Open Source License
private static ScriptEngine getScriptEngine() { if (scriptEngine == null) { NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); scriptEngine = factory.getScriptEngine(); if (scriptEngine == null) { throw new RuntimeException("Unable to get script engine"); }//from w w w .j a v a 2s . c o m } return scriptEngine; }