List of usage examples for javax.script CompiledScript eval
public Object eval(Bindings bindings) throws ScriptException
CompiledScript
object using the supplied Bindings
of attributes as the ENGINE_SCOPE
of the associated ScriptEngine
during script execution. 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;/*w w w. j a v a 2s .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:de.ingrid.iplug.ckan.utils.ScriptEngine.java
/** * Execute the given scripts with the given parameters * @param scripts The script files//from www . j a va2 s . c om * @param parameters The parameters * @param compile Boolean indicating whether to compile the script or not * @return Map with the absolute paths of the scripts as keys and the execution results as values * If an execution returns null, the result will not be added * @throws Exception */ public static Map<String, Object> execute(Resource[] scripts, Map<String, Object> parameters, boolean compile) throws Exception { Map<Integer, Bindings> bindings = new Hashtable<Integer, Bindings>(); Map<String, Object> results = new Hashtable<String, Object>(); for (Resource script : scripts) { // get the engine for the script javax.script.ScriptEngine engine = getEngine(script); // initialize/get the bindings if (!bindings.containsKey(engine.hashCode())) { Bindings newBindings = engine.createBindings(); newBindings.putAll(parameters); bindings.put(engine.hashCode(), newBindings); } Bindings curBindings = bindings.get(engine.hashCode()); // execute the script CompiledScript compiledScript = null; Object result = null; if (compile && (compiledScript = getCompiledScript(script)) != null) { result = compiledScript.eval(curBindings); } else { result = engine.eval(new InputStreamReader(script.getInputStream()), curBindings); } if (result != null) { results.put(script.getFilename(), result); } } return results; }
From source file:io.lavagna.service.ApiHooksService.java
private static void executeScript(String name, CompiledScript script, Map<String, Object> scope) { try {/* ww w .ja v a 2 s . c om*/ ScriptContext newContext = new SimpleScriptContext(); Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE); engineScope.putAll(scope); engineScope.put("log", LOG); engineScope.put("GSON", Json.GSON); engineScope.put("restTemplate", new RestTemplate()); script.eval(newContext); } catch (ScriptException ex) { LOG.warn("Error while executing script " + name, ex); } }
From source file:be.solidx.hot.JSR223ScriptExecutor.java
@Override public Object execute(Script<CompiledScript> script, Map<String, Object> contextVars) throws ScriptException { try {//from w ww. j a va2 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: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 .com 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:com.bytelightning.opensource.pokerface.PokerFace.java
/** * This is where Nashorn compiles the script, evals it into global scope to get an endpoint, and invokes the setup method of the endpoint. * @param rootPath The root script directory path to assist in building a relative uri type path to discovered scripts. * @param f The javascript file./*from ww w. j ava 2 s . c om*/ * @param uriKey A "pass-back-by-reference" construct to w * @return */ private static void MakeJavaScriptEndPointDescriptor(Path rootPath, Path f, HierarchicalConfiguration scriptConfig, EndpointSetupCompleteCallback cb) { CompiledScript compiledScript; try (Reader r = Files.newBufferedReader(f, Charset.forName("utf-8"))) { compiledScript = ((Compilable) Nashorn).compile(r); } catch (Throwable e) { cb.setupFailed(f, "Unable to load and compile script at " + f.toAbsolutePath().toString(), e); return; } ScriptObjectMirror obj; try { obj = (ScriptObjectMirror) compiledScript.eval(Nashorn.getBindings(ScriptContext.GLOBAL_SCOPE)); } catch (Throwable e) { cb.setupFailed(f, "Unable to eval the script at " + f.toAbsolutePath().toString(), e); return; } assert f.startsWith(rootPath); String uriKey = FileToUriKey(rootPath, f); final JavaScriptEndPoint retVal = new JavaScriptEndPoint(uriKey, obj); try { if (obj.hasMember("setup")) { obj.callMember("setup", uriKey, scriptConfig, ScriptHelper.ScriptLogger, new SetupCompleteCallback() { @Override public void setupComplete() { cb.setupComplete(retVal); } @Override public void setupFailed(String msg) { cb.setupFailed(f, msg, null); } }); } else { cb.setupComplete(retVal); } } catch (Throwable e) { cb.setupFailed(f, "The script at " + f.toAbsolutePath().toString() + " did not expose the expected 'setup' method", e); return; } }
From source file:JrubyTest.java
/** * /* ww w.j a v a2 s .com*/ * @throws Exception */ @Test public void poi() throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("jruby"); Reader scriptReader = null; InputStream in = null; try { in = getClass().getClassLoader().getResourceAsStream("sample1.xls"); Workbook workbook = load(in); Book book = new Book(workbook); scriptReader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("test.rb")); if (engine instanceof Compilable) { CompiledScript script = ((Compilable) engine).compile(scriptReader); SimpleBindings bindings = new SimpleBindings(); bindings.put("@book", book); script.eval(bindings); } } finally { IOUtils.closeQuietly(scriptReader); IOUtils.closeQuietly(in); } }
From source file:JrubyTest.java
/** * //from w w w .java 2 s. com */ @Test public void repeatedTitle() throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("jruby"); Reader scriptReader = null; InputStream in = null; try { in = getClass().getClassLoader().getResourceAsStream("test-specifications.xls"); Workbook workbook = load(in); Book book = new Book(workbook); scriptReader = new InputStreamReader( getClass().getClassLoader().getResourceAsStream("test-specification-repeated-title.rb")); if (engine instanceof Compilable) { CompiledScript script = ((Compilable) engine).compile(scriptReader); SimpleBindings bindings = new SimpleBindings(); bindings.put("@book", book); script.eval(bindings); } } finally { IOUtils.closeQuietly(scriptReader); IOUtils.closeQuietly(in); } }
From source file:JrubyTest.java
/** * ?Excel????/*from w w w . ja v a 2 s . c om*/ * * @throws Exception */ @Test public void parseTestSpecification() throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("jruby"); Reader scriptReader = null; InputStream in = null; try { in = getClass().getClassLoader().getResourceAsStream("test-specifications.xls"); Workbook workbook = load(in); Book book = new Book(workbook); scriptReader = new InputStreamReader( getClass().getClassLoader().getResourceAsStream("test-specification-parser.rb")); if (engine instanceof Compilable) { CompiledScript script = ((Compilable) engine).compile(scriptReader); SimpleBindings bindings = new SimpleBindings(); bindings.put("@book", book); script.eval(bindings); } } finally { IOUtils.closeQuietly(scriptReader); IOUtils.closeQuietly(in); } }
From source file:com.lucidtechnics.blackboard.util.Jsr223ScriptingUtil.java
public Object executeScript(String _scriptResource) { ScriptContext scriptContext = getScriptEngine().getContext(); Object result = null;/* w ww . j a va 2 s . co m*/ try { for (String key : getBindingsMap().keySet()) { scriptContext.setAttribute(key, getBindingsMap().get(key), ScriptContext.ENGINE_SCOPE); } CompiledScript script = compileScript(_scriptResource); if (script != null) { result = script.eval(scriptContext); } else { result = getScriptEngine().eval(new InputStreamReader(findScript(_scriptResource)), scriptContext); } } catch (Throwable t) { throw new RuntimeException( "Unable to execute script: " + _scriptResource + " for this reason: " + t.toString(), t); } return result; }