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:it.geosolutions.geobatch.action.scripting.ScriptingTest.java

@Test
public void testLoop() throws ScriptException {

    String engineName = "groovy";

    ScriptEngine foundEngine = null;
    // create a script engine manager
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    System.out.println("FOUND " + factories.size() + " factories");
    for (ScriptEngineFactory sef : factories) {
        System.out.println("FACTORY: " + "'" + sef.getEngineName() + "' " + "'" + sef.getLanguageName() + "' "
                + "'" + sef.getExtensions() + "' " + "'" + sef.getNames() + "' ");
        if (sef.getEngineName().contains(engineName)) {
            foundEngine = sef.getScriptEngine();
        }/* www  .  j  a  v  a2  s. com*/

    }

    assertNotNull("Can't find engine '" + engineName + "'", foundEngine);
    foundEngine.eval("print('Hello, World')");
}

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: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:org.apache.sling.scripting.console.internal.ScriptConsolePlugin.java

private String getScriptConfig0() throws JSONException {
    StringWriter sw = new StringWriter();
    JSONWriter jw = new JSONWriter(sw);
    jw.setTidy(true);/*from   w w  w. j a  va  2s  . c  om*/
    jw.array();

    for (ScriptEngineFactory sef : scriptEngineManager.getEngineFactories()) {
        jw.object();
        if (sef.getExtensions().isEmpty()) {
            continue;
        }
        jw.key("langName").value(sef.getLanguageName());
        jw.key("langCode").value(sef.getExtensions().get(0));

        //Language mode as per CodeMirror names
        String mode = determineMode(sef.getExtensions());
        if (mode != null) {
            jw.key("mode").value(mode);
        }

        jw.endObject();
    }

    jw.endArray();
    return sw.toString();
}

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

private String getScriptConfig0() throws JSONException {
    StringWriter sw = new StringWriter();
    JSONWriter jw = new JSONWriter(sw);
    jw.array();// w  w  w.  j a  v a2s . c  o m

    for (ScriptEngineFactory sef : scriptEngineManager.getEngineFactories()) {
        jw.object();
        if (sef.getExtensions().isEmpty()) {
            continue;
        }
        jw.key("langName").value(sef.getLanguageName());
        jw.key("langCode").value(sef.getExtensions().get(0));

        //Language mode as per CodeMirror names
        String mode = determineMode(sef.getExtensions());
        if (mode != null) {
            jw.key("mode").value(mode);
        }

        jw.endObject();
    }

    jw.endArray();
    return sw.toString();
}

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

@SuppressWarnings("unchecked")
private Collection<?> registerFactories(final EngineManagerState mgr, final Bundle bundle) {
    URL url = bundle.getEntry(ENGINE_FACTORY_SERVICE);
    InputStream ins = null;//from  w w w. jav  a  2s  .  c om
    final SortedSet<String> extensions = new TreeSet<String>();
    try {
        ins = url.openStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
        for (String className : getClassNames(reader)) {
            try {
                Class<ScriptEngineFactory> clazz = bundle.loadClass(className);
                ScriptEngineFactory spi = clazz.newInstance();
                registerFactory(mgr, spi, null);
                extensions.addAll(spi.getExtensions());
            } catch (Throwable t) {
                log.log(LogService.LOG_ERROR, "Cannot register ScriptEngineFactory " + className, t);
            }
        }

    } catch (IOException ioe) {
        // ignore
    } finally {
        IOUtils.closeQuietly(ins);
    }

    return extensions;
}

From source file:org.trafodion.rest.script.ScriptManager.java

private ScriptManager() {
    List<ScriptEngineFactory> engines = manager.getEngineFactories();
    if (engines.isEmpty()) {
        LOG.warn("No scripting engines were found");
        return;//w w  w  .  j  a  v a2s  .  c o m
    }

    StringBuffer sb = new StringBuffer();
    sb.append("\nThe following " + engines.size() + " scripting engine(s) were found");

    for (ScriptEngineFactory engine : engines) {
        sb.append("\nEngine name: " + engine.getEngineName() + "\nVersion: " + engine.getEngineVersion()
                + "\nLanguage: " + engine.getLanguageName());
        List<String> extensions = engine.getExtensions();
        if (extensions.size() > 0) {
            sb.append("\n\tEngine supports the following extensions:");
            for (String e : extensions) {
                sb.append("\n\t\t" + e);
            }
        }
        List<String> shortNames = engine.getNames();
        if (shortNames.size() > 0) {
            sb.append("\n\tEngine has the following short names:");
            for (String n : engine.getNames()) {
                sb.append("\n\t\t" + n);
            }
        }

        String[] params = { ScriptEngine.ENGINE, ScriptEngine.ENGINE_VERSION, ScriptEngine.LANGUAGE,
                ScriptEngine.LANGUAGE_VERSION, ScriptEngine.NAME, "THREADING" };

        sb.append("\n\tEngine has the following parameters:");
        for (String param : params) {
            sb.append("\n\t\t" + param + " = " + engine.getParameter(param));
        }
        sb.append("\n=========================");
    }
    LOG.debug(sb.toString());

    //Get -Drest.home.dir
    restHome = System.getProperty("rest.home.dir");

    //Start the scripts directory watcher
    watcherWorker = new ScriptManagerWatcher("ScriptManagerWatcher", restHome + "/bin/scripts");
}

From source file:org.geoserver.script.ScriptManager.java

public boolean hasEngineForExtension(String ext) {
    for (ScriptEngineFactory f : engineMgr.getEngineFactories()) {
        if (f.getExtensions().contains(ext)) {
            return true;
        }/*  w  ww  .  java2  s .c o m*/
    }
    return false;
}

From source file:org.jahia.utils.ScriptEngineUtils.java

/**
 * Determines whether the specified {@link ScriptEngineFactory} supports at least one of the script names
 * declared by the given OSGi headers assuming they provide a value for the
 * {@link BundleScriptingConfigurationConstants#JAHIA_MODULE_SCRIPTING_VIEWS} header.
 *
 * @param scriptFactory the ScriptEngineFactory whose support for views defined in the specified bundle is to be
 *                      determined//  w  w w . j  ava2  s  .c om
 * @param headers       the OSGi headers to be checked if declared view technologies defined by the {@link
 *                      BundleScriptingConfigurationConstants#JAHIA_MODULE_SCRIPTING_VIEWS} OSGI header are
 *                      supported by the specified ScriptEngineFactory
 * @return {@code true} if the specified ScriptEngineFactory can handle at least one of the specified script names,
 * {@code false} otherwise. Note that if the headers contain the {@link BundleScriptingConfigurationConstants#JAHIA_MODULE_HAS_VIEWS}
 * header with a "{@code no}" value, we won't look at other headers since the module has indicated that it shouldn't
 * contain any views so the factory shouldn't attempt to process any it might find.
 */
public static boolean canFactoryProcessViews(ScriptEngineFactory scriptFactory,
        Dictionary<String, String> headers) {
    if (scriptFactory == null) {
        throw new IllegalArgumentException("ScriptEngineFactory is null");
    }

    if (headers != null) {

        final String hasViews = headers.get(BundleScriptingConfigurationConstants.JAHIA_MODULE_HAS_VIEWS);
        if ("no".equalsIgnoreCase(StringUtils.trim(hasViews))) {
            // if the bundle indicated that it doesn't provide views, the factory shouldn't process it regardless
            // of other configuration
            return false;
        } else {
            final String commaSeparatedScriptNames = headers
                    .get(BundleScriptingConfigurationConstants.JAHIA_MODULE_SCRIPTING_VIEWS);
            // check if the bundle provided a list of of comma-separated scripting language names for the views it provides
            // the bundle should only be scanned if it defined the header and the header contains the name or language of the factory associated with the extension
            final String[] split = StringUtils.split(commaSeparatedScriptNames, ',');
            if (split != null) {
                // check extensions
                final List<String> extensions = scriptFactory.getExtensions();
                List<String> scriptNames = new ArrayList<>(split.length);
                for (String scriptName : split) {
                    String script = scriptName.trim().toLowerCase();
                    scriptNames.add(script);
                    if (extensions.contains(script)) {
                        return true;
                    }
                }

                return scriptNames.contains(scriptFactory.getEngineName().trim().toLowerCase())
                        || scriptNames.contains(scriptFactory.getLanguageName().trim().toLowerCase());
            }
        }
    }
    return false;
}

From source file:org.freeplane.plugin.script.ScriptingConfiguration.java

private String createScriptRegExp() {
    final ArrayList<String> extensions = new ArrayList<String>();
    //        extensions.add("clj");
    for (ScriptEngineFactory scriptEngineFactory : GenericScript.getScriptEngineManager()
            .getEngineFactories()) {/*from ww w  .jav  a  2s .  c  o m*/
        extensions.addAll(scriptEngineFactory.getExtensions());
    }
    LogUtils.info("looking for scripts with the following endings: " + extensions);
    return ".+\\.(" + StringUtils.join(extensions, "|") + ")$";
}