List of usage examples for javax.script ScriptEngineManager ScriptEngineManager
public ScriptEngineManager()
ScriptEngineManager(Thread.currentThread().getContextClassLoader())
. From source file:org.samjoey.gui.GraphicalViewer.java
private void selectedPGN(File file) { String fileLoc = file.getAbsolutePath(); //fileLoc = "File:" + fileLoc.substring(2); if (fileLoc.substring(fileLoc.length() - 3).equals("pgn")) { for (int i = 0; i < fileLoc.length(); i++) { if (fileLoc.substring(i, i + 1).equals("/")) { fileLoc = fileLoc.substring(0, i) + "\\" + fileLoc.substring(i + 1); }/*from w w w . j av a 2s .c o m*/ } try { // Below: Talk to JavaScript ScriptEngineManager factory = new ScriptEngineManager(); // create JavaScript engine final ScriptEngine engine = factory.getEngineByName("JavaScript"); // evaluate JavaScript code from given file - specified by first argument engine.put("engine", engine); ClassLoader cl = GraphicalViewer.class.getClassLoader(); // Talk to GameLooper_1 URL url = cl.getResource("\\org\\samjoey\\gameLooper\\GameLooper_1.js"); String loopLoc = url.toString().substring(5); for (int i = 0; i < loopLoc.length() - 3; i++) { if (loopLoc.substring(i, i + 3).equals("%5c")) { loopLoc = loopLoc.substring(0, i) + "/" + loopLoc.substring(i + 3); } } engine.put("loopLoc", loopLoc); // Talk to calcDefs_1 url = cl.getResource("\\org\\samjoey\\calculator\\calcDefs_1.js"); String defsLoc = url.toString().substring(5); for (int i = 0; i < defsLoc.length() - 3; i++) { if (defsLoc.substring(i, i + 3).equals("%5c")) { defsLoc = defsLoc.substring(0, i) + "/" + defsLoc.substring(i + 3); } } engine.put("defsLoc", defsLoc); // Talk to Calculator url = cl.getResource("\\org\\samjoey\\calculator\\Calculator.js"); String calcLoc = url.toString().substring(5); for (int i = 0; i < calcLoc.length() - 3; i++) { if (calcLoc.substring(i, i + 3).equals("%5c")) { calcLoc = calcLoc.substring(0, i) + "/" + calcLoc.substring(i + 3); } } engine.put("calcLoc", calcLoc); String args[] = { "-g:false", fileLoc }; engine.put("arguments", args); // Create a Thread to update the parser's progress bar parserProgress.setStringPainted(true); running = false; running = true; Thread thread = new Thread() { @Override public void run() { long last = 0l; boolean print = true; double p = .01; while (engine.get("progress") == null || engine.get("size") == null || Integer.parseInt((String) engine.get("progress")) != Integer .parseInt((String) engine.get("size"))) { //if (Parser.numGames > 0 && Parser.progress == Parser.numGames) { try { parserProgress.setValue(Integer.parseInt((String) engine.get("progress"))); parserProgress.setMaximum(Integer.parseInt((String) engine.get("size"))); if (last == 0l) { last = System.nanoTime(); } if ((double) parserProgress.getValue() / (double) parserProgress.getMaximum() > p && print) { //System.out.println(p + ": " + (System.nanoTime() - last)); //print = false; p += .01; } } catch (Exception e) { } //} else { // parserProgress.setMaximum(Parser.numGames - 1); // parserProgress.setMinimum(0); // parserProgress.setValue(Parser.progress); //} } // finally parserProgress.setValue(parserProgress.getMaximum()); } }; thread.start(); // I have no clue what's here!?? engine.eval(new java.io.FileReader( GraphicalViewer.class.getClassLoader().getResource("driver_1.js").toString().substring(5))); while (games == null || games.get((int) (Math.random() * games.size())).getVarData().size() < 20) { games = (LinkedList<Game>) engine.get("gameList"); } } catch (ScriptException | FileNotFoundException ex) { Logger.getLogger(GraphicalViewer.class.getName()).log(Level.SEVERE, null, ex); } } else if (fileLoc.substring(fileLoc.length() - 4).equals("jsca")) { JSCAParser jsca = new JSCAParser(fileLoc); games = (LinkedList<Game>) jsca.getGamesList(); } Set<String> keys = games.get(0).getVarData().keySet(); for (String key : keys) { Variable_Chooser.addItem(key); } graphs = GraphUtility.getGraphs(games); setViewer(0, 0); }
From source file:azkaban.execapp.FlowRunner.java
private boolean evaluateExpression(final String expression) { boolean result = false; final ScriptEngineManager sem = new ScriptEngineManager(); final ScriptEngine se = sem.getEngineByName("JavaScript"); // Restrict permission using the two-argument form of doPrivileged() try {/*ww w . ja v a2 s.c o m*/ final Object object = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws ScriptException { return se.eval(expression); } }, new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, null) }) // no permissions ); if (object != null) { result = (boolean) object; } } catch (final Exception e) { this.logger.error("Failed to evaluate the expression.", e); } this.logger.info("Evaluate expression result: " + result); return result; }
From source file:com.inkubator.hrm.service.impl.PayTempKalkulasiServiceImpl.java
@Override @Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void executeBatchFinalSalaryCalculation(EmpData empData) throws ScriptException { PayTempKalkulasiEmpPajak tax_23 = payTempKalkulasiEmpPajakDao .getEntityByEmpDataIdAndTaxComponentId(empData.getId(), 23L); PayTempKalkulasi taxKalkulasi = payTempKalkulasiDao .getEntityByEmpDataIdAndSpecificModelComponent(empData.getId(), HRMConstant.MODEL_COMP_TAX); //jika pajak PPh pasal 21 kurang dari 0(minus), maka di set 0 saja di payTempKalkulasi if (tax_23.getNominal() > 0) { taxKalkulasi.setNominal(new BigDecimal(tax_23.getNominal())); } else {/*from www . ja v a2s. c o m*/ taxKalkulasi.setNominal(new BigDecimal(0)); } payTempKalkulasiDao.update(taxKalkulasi); PayTempKalkulasi ceilKalkulasi = payTempKalkulasiDao .getEntityByEmpDataIdAndSpecificModelComponent(empData.getId(), HRMConstant.MODEL_COMP_CEIL); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); double ceiling = (Double) jsEngine.eval(ceilKalkulasi.getPaySalaryComponent().getFormula()); PayTempKalkulasi totalIncomeKalkulasi = payTempKalkulasiDao.getEntityByEmpDataIdAndSpecificModelComponent( empData.getId(), HRMConstant.MODEL_COMP_TAKE_HOME_PAY); BigDecimal totalIncome = totalIncomeKalkulasi.getNominal(); totalIncome = this.calculateTotalIncome(totalIncome, taxKalkulasi); //dapatkan nilai sisa/pembulatan BigDecimal val[] = totalIncome.divideAndRemainder(new BigDecimal(ceiling)); ceilKalkulasi.setNominal(val[1]); payTempKalkulasiDao.update(ceilKalkulasi); totalIncome = this.calculateTotalIncome(totalIncome, ceilKalkulasi); totalIncomeKalkulasi.setNominal(totalIncome); payTempKalkulasiDao.update(totalIncomeKalkulasi); }
From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java
public void intializeScriptEngine(SourcePojo source, UnstructuredAnalysisConfigPojo uap) { if (null == engine) { //use the passed in sah one if possible if (null != this.get_sahEngine()) { engine = this.get_sahEngine(); } else if (null == factory) //otherwise create our own {/*ww w. j a v a2 s.co m*/ factory = new ScriptEngineManager(); engine = factory.getEngineByName("JavaScript"); //grab any json cache and make it available to the engine } //once engine is created, do some initialization if (null != engine) { if (null != source) { loadLookupCaches(uap.getCaches(), source.getCommunityIds(), source.getOwnerId()); List<String> scriptFiles = null; if (null != uap.getScriptFiles()) { scriptFiles = Arrays.asList(uap.getScriptFiles()); } loadGlobalFunctions(scriptFiles, uap.getScript()); } if (null == parsingScript) { parsingScript = JavaScriptUtils.generateParsingScript(); } try { securityManager.eval(engine, parsingScript); } catch (ScriptException e) { // Just do nothing and log e.printStackTrace(); logger.error("intializeScriptEngine: " + e.getMessage()); } } } //end start engine up }