Example usage for javax.script ScriptEngineManager getEngineFactories

List of usage examples for javax.script ScriptEngineManager getEngineFactories

Introduction

In this page you can find the example usage for javax.script ScriptEngineManager getEngineFactories.

Prototype

public List<ScriptEngineFactory> getEngineFactories() 

Source Link

Document

Returns a list whose elements are instances of all the ScriptEngineFactory classes found by the discovery mechanism.

Usage

From source file:org.pentaho.platform.plugin.condition.scriptable.ScriptableCondition.java

public void setListAvailableEngines(final boolean value) {
    this.listAvailableEngines = value;
    if (value) {//from  w w  w .j  a v  a  2  s .com
        System.out.println("*** DEBUG - Display Script Engine List ***");
        ScriptEngineManager manager = new ScriptEngineManager();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();
        for (ScriptEngineFactory factory : factories) {
            System.out.println(String.format("Engine %s, Version %s, Language %s, Registered Names: %s",
                    factory.getEngineName(), factory.getEngineVersion(), factory.getLanguageName(),
                    factory.getNames().toString()));
        }
    }
}

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);//w  w  w. j  a va2  s  .  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:ca.hedlund.jiss.preprocessor.LangPreprocessor.java

private void printLangs(JissModel model, StringBuffer cmd) {
    final ScriptEngineManager manager = new ScriptEngineManager(JissModel.class.getClassLoader());
    final List<String> cmds = new ArrayList<String>();

    for (ScriptEngineFactory factory : manager.getEngineFactories()) {
        final String engineInfo = factory.getLanguageName() + " " + factory.getLanguageVersion() + ":"
                + factory.getEngineName() + " " + factory.getEngineVersion();
        cmds.add(createPrintCmd(model, engineInfo));
    }//w w  w  .j a v a 2 s . c om
    final ScriptEngineFactory factory = model.getScriptEngine().getFactory();
    final String prog = StringEscapeUtils.unescapeJava(factory.getProgram(cmds.toArray(new String[0])));
    cmd.append(prog);
}

From source file:ca.hedlund.jiss.preprocessor.LangPreprocessor.java

@Override
public boolean preprocessCommand(JissModel jissModel, String orig, StringBuffer cmd) {
    final String c = cmd.toString();
    if (c.equals("::langs")) {
        cmd.setLength(0);/*  w w w. j  ava 2s . com*/
        printLangs(jissModel, cmd);
    } else if (c.equals("::lang")) {
        cmd.setLength(0);
        printCurrentLang(jissModel, cmd);
    } else if (c.startsWith("::lang")) {
        cmd.setLength(0);

        final String parts[] = c.split("\\p{Space}");
        if (parts.length == 2) {
            final String lang = parts[1];

            final ScriptEngineManager manager = new ScriptEngineManager(JissModel.class.getClassLoader());
            ScriptEngine newEngine = null;
            for (ScriptEngineFactory factory : manager.getEngineFactories()) {
                if (factory.getLanguageName().equals(lang) || factory.getExtensions().contains(lang)
                        || factory.getMimeTypes().contains(lang)) {
                    newEngine = factory.getScriptEngine();
                    break;
                }
            }

            if (newEngine != null) {
                jissModel.setScriptEngine(newEngine);
                printCurrentLang(jissModel, cmd);
            }
        }
    }
    return false;
}

From source file:org.apache.accumulo.core.util.shell.commands.ScriptCommand.java

private void listJSREngineInfo(ScriptEngineManager mgr, Shell shellState) throws IOException {
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    Set<String> lines = new TreeSet<String>();
    for (ScriptEngineFactory factory : factories) {
        lines.add("ScriptEngineFactory Info");
        String engName = factory.getEngineName();
        String engVersion = factory.getEngineVersion();
        String langName = factory.getLanguageName();
        String langVersion = factory.getLanguageVersion();
        lines.add("\tScript Engine: " + engName + " (" + engVersion + ")");
        List<String> engNames = factory.getNames();
        for (String name : engNames) {
            lines.add("\tEngine Alias: " + name);
        }//from w  w w  .  j  a  v  a  2  s  .  co  m
        lines.add("\tLanguage: " + langName + " (" + langVersion + ")");
    }
    shellState.printLines(lines.iterator(), true);

}

From source file:com.aionemu.commons.scripting.AionScriptEngineManager.java

private AionScriptEngineManager() {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = scriptEngineManager.getEngineFactories();
    if (USE_COMPILED_CACHE) {
        _cache = loadCompiledScriptCache();
    } else {//from w  ww .  ja v  a  2s .  c om
        _cache = null;
    }
    log.info("Initializing Script Engine Manager");

    for (ScriptEngineFactory factory : factories) {
        try {
            log.info("Script Engine: " + factory.getEngineName() + " " + factory.getEngineVersion()
                    + " - Language: " + factory.getLanguageName() + " " + factory.getLanguageVersion());

            ScriptEngine engine = factory.getScriptEngine();

            for (String name : factory.getNames()) {
                if (_nameEngines.containsKey(name))
                    throw new IllegalStateException("Multiple script engines for the same name!");

                _nameEngines.put(name, engine);
            }

            for (String ext : factory.getExtensions()) {
                if (_extEngines.containsKey(ext))
                    throw new IllegalStateException("Multiple script engines for the same extension!");

                _extEngines.put(ext, engine);
            }
        } catch (Exception e) {
            log.warn("Failed initializing factory.", e);
        }
    }
}

From source file:com.l2jfree.gameserver.scripting.L2ScriptEngineManager.java

private L2ScriptEngineManager() {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = scriptEngineManager.getEngineFactories();
    if (USE_COMPILED_CACHE) {
        _cache = loadCompiledScriptCache();
    } else {/*from  w  w  w.ja  va2 s. c o m*/
        _cache = null;
    }
    _log.info("Initializing Script Engine Manager");

    for (ScriptEngineFactory factory : factories) {
        try {
            _log.info("Script Engine: " + factory.getEngineName() + " " + factory.getEngineVersion()
                    + " - Language: " + factory.getLanguageName() + " " + factory.getLanguageVersion());

            ScriptEngine engine = factory.getScriptEngine();

            for (String name : factory.getNames()) {
                if (_nameEngines.containsKey(name))
                    throw new IllegalStateException("Multiple script engines for the same name!");

                _nameEngines.put(name, engine);
            }

            for (String ext : factory.getExtensions()) {
                if (_extEngines.containsKey(ext))
                    throw new IllegalStateException("Multiple script engines for the same extension!");

                _extEngines.put(ext, engine);
            }
        } catch (Exception e) {
            _log.warn("Failed initializing factory.", e);
        }
    }

    preConfigure();
}