List of usage examples for javax.script ScriptEngine eval
public Object eval(Reader reader, Bindings n) throws ScriptException;
eval(String, Bindings)
except that the source of the script is provided as a Reader
. From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); manager.put("global", "global bindings"); dumpBindings(manager.getBindings()); ScriptEngine engine = manager.getEngineByExtension("js"); engine.put("engine", "engine bindings"); dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE)); dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE)); Bindings bindings = engine.createBindings(); bindings.put("engine", "overridden engine bindings"); bindings.put("bindings", bindings); engine.eval("app.dumpBindings (bindings);", bindings); ScriptEngine engine2 = manager.getEngineByExtension("js"); engine2.put("engine2", "engine2 bindings"); dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE)); dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE)); dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE)); }
From source file:GetToKnowBindingsAndScopes.java
public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); manager.put("global", "global bindings"); dumpBindings(manager.getBindings()); ScriptEngine engine = manager.getEngineByExtension("js"); engine.put("engine", "engine bindings"); dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE)); dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE)); try {/*w w w . j a v a2 s . co m*/ Bindings bindings = engine.createBindings(); bindings.put("engine", "overridden engine bindings"); bindings.put("app", new GetToKnowBindingsAndScopes()); bindings.put("bindings", bindings); engine.eval("app.dumpBindings (bindings);", bindings); } catch (ScriptException se) { System.err.println(se.getMessage()); } ScriptEngine engine2 = manager.getEngineByExtension("js"); engine2.put("engine2", "engine2 bindings"); dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE)); dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE)); dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE)); }
From source file:com.xafero.vee.cmd.MainApp.java
private static void execute(String fileName) throws FileNotFoundException, ScriptException { File file = (new File(fileName)).getAbsoluteFile(); if (!file.exists()) throw new FileNotFoundException("There's no file named '" + file + "'!"); String extension = Files.getExtension(file); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByExtension(extension); Bindings env = engine.createBindings(); inject(env, file);// w ww . j a va 2 s.co m try { Object result = engine.eval(new FileReader(file), env); if (result != null) System.out.println(result.toString()); } catch (ScriptException e) { throw e; } }
From source file:jef.jre5support.script.JavaScriptUtil.java
public static void initEngine(ScriptEngine e, Bindings... b) { importClass(e, LogUtil.class, b); importClass(e, StringUtils.class, b); importClass(e, ArrayUtils.class, b); importClass(e, XMLUtils.class, b); importClass(e, IOUtils.class, b); importClass(e, ZipUtils.class, b); try {/* ww w .ja va2 s . co m*/ if (b.length > 0) { e.eval(printSource, b[0]); } else { e.eval(printSource); } } catch (ScriptException ex) { LogUtil.exception(ex); } }
From source file:org.fireflow.engine.modules.script.ScriptEngineHelper.java
private static Object evaluateJSR233Expression(RuntimeContext rtCtx, Expression fireflowExpression, Map<String, Object> contextObjects) { ScriptEngine scriptEngine = rtCtx.getScriptEngine(fireflowExpression.getLanguage()); ScriptContext scriptContext = new SimpleScriptContext(); Bindings engineScope = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE); engineScope.putAll(contextObjects);//w ww.j a va 2s . c o m try { Object result = scriptEngine.eval(fireflowExpression.getBody(), scriptContext); return result; } catch (ScriptException e) { throw new RuntimeException("Can NOT evaluate the expression. ", e); } }
From source file:org.jahia.tools.patches.GroovyPatcher.java
public static void executeScripts(Resource[] scripts) { long timer = System.currentTimeMillis(); logger.info("Found new patch scripts {}. Executing...", StringUtils.join(scripts)); for (Resource script : scripts) { try {//from w ww . j a v a2 s. c o m long timerSingle = System.currentTimeMillis(); String scriptContent = getContent(script); if (StringUtils.isNotEmpty(scriptContent)) { ScriptEngine engine = getEngine(); ScriptContext ctx = new SimpleScriptContext(); ctx.setWriter(new StringWriter()); Bindings bindings = engine.createBindings(); bindings.put("log", new LoggerWrapper(logger, logger.getName(), ctx.getWriter())); ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE); engine.eval(scriptContent, ctx); String result = ((StringWriter) ctx.getWriter()).getBuffer().toString(); logger.info("Execution of script {} took {} ms with result:\n{}", new String[] { script.toString(), String.valueOf(System.currentTimeMillis() - timerSingle), result }); } else { logger.warn("Content of the script {} is either empty or cannot be read. Skipping."); } rename(script, ".installed"); } catch (Exception e) { logger.error("Execution of script " + script + " failed with error: " + e.getMessage(), e); rename(script, ".failed"); } } logger.info("Execution took {} ms", (System.currentTimeMillis() - timer)); }
From source file:org.gatherdata.alert.detect.bsf.internal.BsfEventDetector.java
public Iterable<DetectedEvent> detect(Iterable<RuleSet> usingRules, Map<String, Object> attributes) { Set<DetectedEvent> detectedEvents = new HashSet<DetectedEvent>(); DateTime detectionTime = new DateTime(); for (RuleSet rule : usingRules) { if (rule.isActive()) { log.info("applying rule: " + rule); boolean anyMatch = false; boolean allMatch = true; for (LanguageScript predicate : rule.getPredicates()) { ScriptEngine engine = scriptEngineManager.getEngineByName(predicate.getLanguage()); if (engine != null) { try { Boolean doesMatch = (Boolean) engine.eval(predicate.getScript(), adaptToScriptContext(attributes)); log.info("\tpredicate [" + predicate + "] " + (doesMatch ? "matches" : "does not match")); anyMatch |= doesMatch; allMatch &= doesMatch; } catch (ScriptException e) { e.printStackTrace(); }//w ww .jav a 2 s. co m } else { log.warn("Cant't evaluate predicate for missing language: " + predicate.getLanguage()); } } if ((!rule.isSatisfyAll() && anyMatch) || allMatch) { detectedEvents.add(MutableDetectedEvent.createFor(detectionTime, CbidFactory.createCbid(attributes.get("body").toString()), rule)); } } else { log.warn("Skipping inactive rule:" + rule); } } return detectedEvents; }
From source file:org.xwiki.rendering.macro.script.AbstractJSR223ScriptMacro.java
/** * Execute the script./*from w ww. jav a 2 s . co m*/ * * @param content the script to be executed by the script engine * @param engine the script engine * @param scriptContext the script context * @return The value returned from the execution of the script. * @throws ScriptException if an error occurrs in script. ScriptEngines should create and throw * <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting * implementations. */ protected Object eval(String content, ScriptEngine engine, ScriptContext scriptContext) throws ScriptException { return engine.eval(content, scriptContext); }
From source file:org.apache.nifi.scripting.ScriptFactory.java
private Script getScriptInstance(final Map<String, String> properties) throws ScriptException { Map<String, Object> localThreadVariables = new HashMap<>(); final String extension = getExtension(scriptFileName); String loggerVariableKey = getVariableName("GLOBAL", "logger", extension); localThreadVariables.put(loggerVariableKey, logger); String propertiesVariableKey = getVariableName("INSTANCE", "properties", extension); localThreadVariables.put(propertiesVariableKey, properties); localThreadVariables.put(ScriptEngine.FILENAME, scriptFileName); final Bindings bindings = new SimpleBindings(localThreadVariables); final ScriptEngine scriptEngine = engineFactory.getEngine(extension); Script instance;//w w w .ja va 2 s.c o m if (compiledScript == null) { instance = (Script) scriptEngine.eval(scriptText, bindings); if (instance == null) { // which it will be for python and also for local variables in javascript instance = (Script) scriptEngine.eval("instance", bindings); } } else { instance = (Script) compiledScript.eval(bindings); if (instance == null) { // which it will be for python and also for local variables in javascript instance = (Script) compiledScript.getEngine().eval("instance", bindings); } } instance.setEngine(scriptEngine); return instance; }
From source file:edu.toronto.cs.phenotips.tools.PhenotypeMappingService.java
private Map<String, Object> parseGroovyMapping(DocumentReference mappingDoc) { ScriptEngine e = this.groovy.getScriptEngine(); ScriptContext c = this.scmanager.getScriptContext(); try {/*from w ww .j a v a 2s . c om*/ e.eval(this.bridge.getDocumentContentForDefaultLanguage(mappingDoc), c); } catch (Exception ex) { this.logger.error("Failed to parse mapping document [{}]", mappingDoc, ex); return null; } this.observationManager.addEvent(this.getName(), new DocumentUpdatedEvent(mappingDoc)); this.observationManager.addEvent(this.getName(), new DocumentDeletedEvent(mappingDoc)); @SuppressWarnings("unchecked") Map<String, Object> mappings = (Map<String, Object>) c.getAttribute("mappings"); setMappings(mappingDoc, mappings); return mappings; }