List of usage examples for javax.script ScriptEngine getFactory
public ScriptEngineFactory getFactory();
ScriptEngineFactory
for the class to which this ScriptEngine
belongs. From source file:Main.java
public static void main(String[] args) { // Get the script engine factory for Nashorn ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); ScriptEngineFactory factory = engine.getFactory(); // Get the script String script = factory.getOutputStatement("\"java2s\""); System.out.println("Syntax: " + script); // Evaluate the script try {/*from w w w . j a v a 2 s.co m*/ engine.eval(script); } catch (ScriptException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.intuit.tank.tools.debugger.ConfiguredLanguage.java
/** * // w ww .j av a 2s. c om * @param scriptName * @return */ public static ConfiguredLanguage getLanguagebyExtension(String scriptName) { ConfiguredLanguage ret = null; String extension = FilenameUtils.getExtension(scriptName); ScriptEngine engineByExtension = manager.getEngineByExtension(extension); for (ConfiguredLanguage lang : configuredLanguages) { if (lang.name == engineByExtension.getFactory().getLanguageName()) { ret = lang; break; } } return ret; }
From source file:ca.hedlund.jiss.preprocessor.InfoPreprocessor.java
@Override public boolean preprocessCommand(JissModel jissModel, String orig, StringBuffer cmd) { final String c = cmd.toString(); if (c.equals(INFO_CMD)) { // clear string cmd.setLength(0);// ww w . ja va 2 s . co m final ScriptEngine se = jissModel.getScriptEngine(); final ScriptEngineFactory seFactory = se.getFactory(); final boolean incnl = !seFactory.getOutputStatement("").startsWith("println"); final String infoCmd = seFactory.getOutputStatement(INFO_TXT + (incnl ? "\\n" : "")); final String langCmd = seFactory.getOutputStatement("Language:" + seFactory.getLanguageName() + " " + seFactory.getLanguageVersion() + (incnl ? "\\n" : "")); final String engineCmd = seFactory.getOutputStatement("Engine:" + seFactory.getEngineName() + " " + seFactory.getEngineVersion() + (incnl ? "\\n" : "")); final String program = seFactory.getProgram(infoCmd, langCmd, engineCmd); cmd.append(StringEscapeUtils.unescapeJava(program)); } // we want the scripting engine to handle the replaced command return false; }
From source file:ca.hedlund.jiss.blocks.preprocessor.ListBlocksPreprocessor.java
@Override public boolean preprocessCommand(JissModel jissModel, String orig, StringBuffer cmd) { if (!orig.startsWith("::block list")) return false; final ScriptEngine se = jissModel.getScriptEngine(); final ScriptEngineFactory seFactory = se.getFactory(); final boolean incnl = !seFactory.getOutputStatement("").startsWith("println"); final List<String> blockInfoCmds = new ArrayList<String>(); blockInfoCmds.add(seFactory.getOutputStatement("Available blocks:" + (incnl ? "\\n" : ""))); final Matcher matcher = LIST_BLOCKS_PATTERN.matcher(orig); if (matcher.matches()) { final String blockNameRegex = matcher.group(1); final BlockManager bm = new BlockManager(); for (String blockName : bm.getBlocks()) { boolean printBlock = true; if (blockNameRegex != null) { printBlock = blockName.matches(blockNameRegex); }/*from ww w . j a va2 s .c om*/ if (printBlock) { blockInfoCmds.add(seFactory.getOutputStatement(blockName) + (incnl ? "\\n" : "")); } } } final String program = seFactory.getProgram(blockInfoCmds.toArray(new String[0])); cmd.setLength(0); cmd.append(StringEscapeUtils.unescapeJava(program)); return false; }
From source file:sample.fa.ScriptRunnerApplication.java
void loadScript(File f) { try {// w w w. java 2s .co m scriptContents.setText(String.join("\n", Files.readAllLines(f.toPath()))); String name = f.getName(); int lastDot = name.lastIndexOf('.'); if (lastDot >= 0) { String extension = name.substring(lastDot + 1); for (int i = 0; i < languagesModel.getSize(); i++) { ScriptEngine se = languagesModel.getElementAt(i); if (se.getFactory().getExtensions().indexOf(extension) >= 0) { languagesModel.setSelectedItem(se); break; } } } } catch (IOException ex) { JOptionPane.showMessageDialog(scriptContents, ex.getMessage(), "Error Loading Script", JOptionPane.ERROR_MESSAGE); } }
From source file:it.geosolutions.geobatch.action.scripting.ScriptingTest.java
@Test public void testGroovy() throws ScriptException { String engineName = "groovy"; ScriptEngineManager mgr = new ScriptEngineManager(); // create a JavaScript engine ScriptEngine engine = mgr.getEngineByName(engineName); assertNotNull("Can't find engine '" + engineName + "'", engine); ScriptEngineFactory sef = engine.getFactory(); System.out.println("FACTORY for " + engineName + ": " + "'" + sef.getEngineName() + "' " + "'" + sef.getLanguageName() + "' " + "'" + sef.getExtensions() + "' " + "'" + sef.getNames() + "' "); // evaluate code from String engine.eval("println \"hello, groovy\""); }
From source file:it.geosolutions.geobatch.action.scripting.ScriptingTest.java
@Test public void testGetEngineByExt() throws ScriptException { String engineExt = "js"; ScriptEngineManager mgr = new ScriptEngineManager(); // create a JavaScript engine ScriptEngine engine = mgr.getEngineByExtension(engineExt); assertNotNull("Can't find engine '" + engineExt + "'", engine); ScriptEngineFactory sef = engine.getFactory(); System.out.println("FACTORY for " + engineExt + ": " + "'" + sef.getEngineName() + "' " + "'" + sef.getLanguageName() + "' " + "'" + sef.getExtensions() + "' " + "'" + sef.getNames() + "' "); // evaluate JavaScript code from String engine.eval("print('Hello, World')"); }
From source file:sample.fa.ScriptRunnerApplication.java
void createGUI() { Box buttonBox = Box.createHorizontalBox(); JButton loadButton = new JButton("Load"); loadButton.addActionListener(this::loadScript); buttonBox.add(loadButton);//from w w w . j av a 2s . c om JButton saveButton = new JButton("Save"); saveButton.addActionListener(this::saveScript); buttonBox.add(saveButton); JButton executeButton = new JButton("Execute"); executeButton.addActionListener(this::executeScript); buttonBox.add(executeButton); languagesModel = new DefaultComboBoxModel(); ScriptEngineManager sem = new ScriptEngineManager(); for (ScriptEngineFactory sef : sem.getEngineFactories()) { languagesModel.addElement(sef.getScriptEngine()); } JComboBox<ScriptEngine> languagesCombo = new JComboBox<>(languagesModel); JLabel languageLabel = new JLabel(); languagesCombo.setRenderer((JList<? extends ScriptEngine> list, ScriptEngine se, int index, boolean isSelected, boolean cellHasFocus) -> { ScriptEngineFactory sef = se.getFactory(); languageLabel.setText(sef.getEngineName() + " - " + sef.getLanguageName() + " (*." + String.join(", *.", sef.getExtensions()) + ")"); return languageLabel; }); buttonBox.add(Box.createHorizontalGlue()); buttonBox.add(languagesCombo); scriptContents = new JTextArea(); scriptContents.setRows(8); scriptContents.setColumns(40); scriptResults = new JTextArea(); scriptResults.setEditable(false); scriptResults.setRows(8); scriptResults.setColumns(40); JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scriptContents, scriptResults); JFrame frame = new JFrame("Script Runner"); frame.add(buttonBox, BorderLayout.NORTH); frame.add(jsp, BorderLayout.CENTER); frame.pack(); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.setVisible(true); }
From source file:com.galeoconsulting.leonardinius.rest.service.ScriptRunner.java
@PUT @Path("/sessions") @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML }) public Response newSession(Language language) { if (!isAdministrator()) { return responseForbidden(); }/*from w ww. ja v a2 s .c o m*/ ScriptEngine engine; try { engine = createScriptEngine(language.getLanguage(), new Script("", "cli", ImmutableList.<String>of(), ImmutableMap.<String, String>of())); } catch (IllegalArgumentException e) { return responseInternalError(Arrays.asList((e.getMessage()))); } SessionId sessionId = sessionManager .putSession(ScriptSession.newInstance(getActorName(userManager.getRemoteUsername()), engine)); return Response .ok(new SessionIdWrapper(sessionId.getSessionId(), LanguageUtils.getLanguageName(engine.getFactory()), LanguageUtils.getVersionString(engine.getFactory()))) .cacheControl(CacheControl.NO_CACHE).build(); }
From source file:org.apache.nifi.scripting.ScriptEngineFactory.java
ScriptEngine getEngine(String extension) { ScriptEngine engine = threadSafeEngines.get(extension); if (null == engine) { engine = scriptEngMgr.getEngineByExtension(extension); if (null == engine) { throw new IllegalArgumentException("No ScriptEngine exists for extension " + extension); }/*from w w w . j av a2s . c o m*/ Object threading = engine.getFactory().getParameter(THREADING); // the MULTITHREADED status means that the scripts need to be careful about sharing state if (THREAD_ISOLATED.equals(threading) || STATELESS.equals(threading) || MULTITHREADED.equals(threading)) { ScriptEngine cachedEngine = threadSafeEngines.putIfAbsent(extension, engine); if (null != cachedEngine) { engine = cachedEngine; } } } return engine; }