Example usage for javax.script ScriptEngineFactory getExtensions

List of usage examples for javax.script ScriptEngineFactory getExtensions

Introduction

In this page you can find the example usage for javax.script ScriptEngineFactory getExtensions.

Prototype

public List<String> getExtensions();

Source Link

Document

Returns an immutable list of filename extensions, which generally identify scripts written in the language supported by this ScriptEngine.

Usage

From source file:org.apache.felix.webconsole.plugins.scriptconsole.internal.ScriptEngineManager.java

private Collection<?> registerFactory(final EngineManagerState mgr, final ScriptEngineFactory factory,
        final Map<Object, Object> props) {
    log.log(LogService.LOG_INFO,//from w w  w  .  j a  va  2 s . c o  m
            String.format("Adding ScriptEngine %s, %s for language %s, %s", factory.getEngineName(),
                    factory.getEngineVersion(), factory.getLanguageName(), factory.getLanguageVersion()));

    mgr.factories.add(factory);
    mgr.factoryProperties.put(factory, props);
    for (Object ext : factory.getExtensions()) {
        mgr.extensionAssociations.put((String) ext, factory);
    }
    return factory.getExtensions();
}

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   www  .j  a  va 2 s.co m

    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:org.jahia.services.render.scripting.bundle.BundleScriptResolver.java

/**
 * Removes the specified {@link ScriptEngineFactory} from the known ones, deactivating any associated views from
 * currently active bundles. This is called when a bundle declaring ScriptEngineFactories is stopping. Previously
 * registered extension observers associated with the ScriptEngineFactory's supported extensions are also removed.
 *
 * @param factory the factory to be removed
 * @param bundle  the bundle which declared the factory and which is being stopped
 *//* w  w  w.j a  v a 2  s.c  o m*/
public void remove(ScriptEngineFactory factory, Bundle bundle) {
    List<String> extensions = factory.getExtensions();
    for (String extension : extensions) {
        // we need to remove the views associated with our bundle
        availableScripts.remove(bundle.getSymbolicName());

        // remove all the bundle scripts for all the deployed bundles.
        for (Bundle otherBundle : bundle.getBundleContext().getBundles()) {
            if (otherBundle.getState() == Bundle.ACTIVE) {
                removeBundleScripts(otherBundle, extension);
            }
        }

        scriptFactoryMap.remove(extension);
        extensionPriorities.remove(extension);

        // remove associated observer
        removeObserver(extension);
    }
}

From source file:org.jahia.services.render.scripting.bundle.BundleScriptResolver.java

/**
 * Registers the specified {@link ScriptEngineFactory} defined in the context of the specified starting
 * {@link Bundle}. This results in registering the associated view extensions along with activating any existing
 * views in active bundles. This registration operation also takes care of script extension re-ordering if
 * necessary since bundles defining ScriptEngineFactories can do so.
 * <p>/*from   www.  j a  va2  s  .com*/
 * <p>Additionally, {@link ScriptBundleURLScanner} are also activated to look for any files with the newly
 * registered extensions during future bundle deployments.</p>
 *
 * @param scriptEngineFactory the ScriptEngineFactory instance we want to register
 * @param bundle              the starting bundle defining the given ScriptEngineFactory
 */
public void register(ScriptEngineFactory scriptEngineFactory, Bundle bundle) {
    final List<String> extensions = scriptEngineFactory.getExtensions();

    if (extensions.isEmpty()) {
        return;
    }

    // determine if the bundle provided any scripting-related information
    final BundleScriptingContext context;
    if (scriptEngineFactory instanceof BundleScriptEngineFactory) {
        BundleScriptEngineFactory engineFactory = (BundleScriptEngineFactory) scriptEngineFactory;
        context = engineFactory.getContext();
    } else {
        context = null;
    }

    for (String extension : extensions) {
        // first check that we don't already have a script factory assigned to that extension
        final ScriptFactory scriptFactory = scriptFactoryMap.get(extension);
        if (scriptFactory != null) {
            ScriptEngineFactory alreadyRegistered = BundleScriptEngineManager.getInstance()
                    .getFactoryForExtension(extension);
            throw new IllegalArgumentException("Extension " + extension
                    + " is already associated with ScriptEngineFactory " + alreadyRegistered);
        }

        scriptFactoryMap.put(extension, bundleScriptFactory);

        // compute or retrieve the extensions priority and record it
        final int priority = getPriority(extension, context);
        extensionPriorities.put(extension, priority);

        logger.info("ScriptEngineFactory {} registered extension {} with priority {}",
                new Object[] { scriptEngineFactory, extension, priority });

        // now we need to activate the bundle script scanner inside of newly deployed or existing bundles
        // register view script observers
        addBundleScripts(bundle, extension);

        // check existing bundles to see if they provide views for the newly deployed scripting language
        final BundleContext bundleContext = bundle.getBundleContext();
        if (bundleContext != null) {
            for (Bundle otherBundle : bundleContext.getBundles()) {
                if (otherBundle.getState() == Bundle.ACTIVE) {
                    addBundleScripts(otherBundle, extension);
                }
            }
        }

        // register extension observer
        registerObserver(extension);
    }

    // deal with extension priorities if needed
    if (context != null && context.specifiesExtensionPriorities()) {
        final Map<String, Integer> specifiedPriorities = context.getExtensionPriorities();
        final SortedMap<Integer, String> orderedPriorities = new TreeMap<>();

        for (Map.Entry<String, Integer> entry : extensionPriorities.entrySet()) {
            final String extension = entry.getKey();
            Integer priority = entry.getValue();

            final Integer newPriority = specifiedPriorities.get(extension);
            if (newPriority != null) {
                extensionPriorities.put(extension, newPriority);
                priority = newPriority;
            }
            orderedPriorities.put(priority, extension);
        }

        //check if we specified unknown extensions
        final Set<String> specifiedExtensions = specifiedPriorities.keySet();
        specifiedExtensions.removeAll(extensionPriorities.keySet());
        if (!specifiedExtensions.isEmpty()) {
            logger.warn("Module {} specified priorities for unknown extensions {}", bundle.getSymbolicName(),
                    specifiedExtensions);
        }

        logger.info("Extension priorities got re-ordered by module {} to {}", bundle.getSymbolicName(),
                orderedPriorities);
    }
}

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 {//www  .j a va  2  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);
        }
    }
}

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 .j a  va  2 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();
}

From source file:org.omegat.gui.scripting.ScriptingWindow.java

private String listScriptEngines() {
    StringBuilder sb = new StringBuilder(OStrings.getString("SCW_LIST_ENGINES") + "\n");
    for (ScriptEngineFactory engine : ScriptRunner.MANAGER.getEngineFactories()) {
        sb.append(" - ");
        sb.append(engine.getEngineName());
        sb.append(" ");
        sb.append(engine.getLanguageName());
        sb.append(" v.");
        sb.append(engine.getLanguageVersion());
        sb.append(" (").append(OStrings.getString("SCW_EXTENSIONS")).append(" ");
        boolean hasMore = false;
        for (String ext : engine.getExtensions()) {
            if (hasMore) {
                sb.append(", ");
            }//from w  w w . j a va 2  s . c  o m
            sb.append(ext);
            hasMore = true;
        }
        sb.append(")");
        sb.append("\n");
    }

    return sb.toString();
}