Example usage for javax.script ScriptEngineFactory getEngineName

List of usage examples for javax.script ScriptEngineFactory getEngineName

Introduction

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

Prototype

public String getEngineName();

Source Link

Document

Returns the full name of the ScriptEngine.

Usage

From source file:tools.httpserver.custom.ScriptList.java

/**
 * Create a new empty SpoonScript file using provided parameters
 * @param params_values parameters to use for file creation (table containing "param=value" strings)
 * @param type Set if script concerns REQMOD or RESPMOD
 * @param user User who generates the request
 * @return Action to do after: edit the new file if all is ok, return error otherwise
 *//*  ww w. j av  a2s .com*/
public static String create(String[] params_values, Icap.TYPE type, User user) {
    String name = null;
    String language = null;

    for (String s : params_values) {
        String[] values = s.split("=");
        if (values.length != 2)
            continue;
        if (values[0].equals("script.name"))
            name = values[1].replace("+", " ").trim();
        if (values[0].equals("script.language"))
            language = TextTools.hexToUTF8(values[1]).trim();
    }
    if (name == null || language == null) {
        error = "Invalid script name or language";
        return "";
    }
    SpoonScript[] scripts = type == Icap.TYPE.REQMOD ? icap.services.GreasySpoon.reqSpoonScripts
            : icap.services.GreasySpoon.respSpoonScripts;
    for (SpoonScript script : scripts) {
        if (script.getScriptName().equalsIgnoreCase(name)) {
            error = "A script with this name already exists.";
            return "";
        }
    }

    ScriptEngineFactory factory = null;
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    for (ScriptEngineFactory factories : scriptEngineManager.getEngineFactories()) {
        if (factories.getEngineName().equalsIgnoreCase(language)) {
            factory = factories;
            break;
        }
    }
    String factorylanguage;
    String extension;
    if (factory == null) {
        if (language.equals("java")) {
            factorylanguage = "java";
            extension = "java";
        } else {
            error = "Provided language cannot be founded.";
            return "";
        }
    } else {
        factorylanguage = factory.getLanguageName();
        extension = factory.getExtensions().get(0);
    }

    String scriptdirectory = icap.services.GreasySpoon.scriptsDirectory;
    String commentstring = icap.services.GreasySpoon.languageComments.getProperty(factorylanguage, "//").trim();
    String tag;
    if (type == Icap.TYPE.REQMOD) {
        tag = icap.services.GreasySpoon.greasySpoonReqTag;
    } else {
        tag = icap.services.GreasySpoon.greasySpoonRespTag;
    }
    String filecomment = SpoonScript.getScriptSkeleton(type, extension, commentstring);

    String filename = scriptdirectory + File.separator + name.replace(" ", "_").replace("%20", "_") + tag
            + extension;

    SpoonScript.save(filename, filecomment.replace("%name%", name), user.getRights());
    icap.services.GreasySpoon.forceReload();
    return "edit=" + filename;
}

From source file:tools.httpserver.custom.ScriptList.java

/**
 * @return a vector containing Scriptengines names and associated languages
 *//*from  w  w w.  ja  v a  2s.  c  om*/
public static Vector<String[]> getScriptEngines() {
    Vector<String[]> vector = new Vector<String[]>();
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    for (ScriptEngineFactory factories : scriptEngineManager.getEngineFactories()) {
        boolean inserted = false;
        for (int i = 0; i < vector.size(); i++) {
            String[] s = vector.elementAt(i);
            if (s[0].compareToIgnoreCase(factories.getEngineName()) > 0) {
                vector.insertElementAt(new String[] { factories.getEngineName(), factories.getLanguageName() },
                        i);
                inserted = true;
                break;
            }
        }
        if (!inserted)
            vector.add(new String[] { factories.getEngineName(), factories.getLanguageName() });
    }
    if (javax.tools.ToolProvider.getSystemJavaCompiler() != null) {
        vector.add(new String[] { "java", "java" });
    }
    return vector;
}

From source file:com.tc.websocket.scripts.Script.java

/**
 * Prints the engines.//ww  w .j  ava2 s . c  om
 */
public static void printEngines() {
    ScriptEngineManager manager = new ScriptEngineManager();
    List<ScriptEngineFactory> engines = manager.getEngineFactories();
    for (ScriptEngineFactory engine : engines) {
        System.out.println(engine.getEngineName());
    }
}

From source file:org.gatherdata.alert.detect.bsf.internal.BsfEventDetector.java

public BsfEventDetector() {
    this.scriptEngineManager = new ScriptEngineManager();
    List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories();
    log.info("Available ScriptEngineFactories...");
    for (ScriptEngineFactory factory : engineFactories) {
        log.info("\t" + factory.getEngineName() + " - " + factory.getLanguageName() + " " + "ext:"
                + factory.getExtensions() + " " + "names:" + factory.getNames());
    }//from ww w . j  a  v  a2  s.c o  m
}

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();
        }//from  w w  w  .  j  a v a 2  s  .c  o m

    }

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

From source file:cc.osint.graphd.script.GScriptEngine.java

public void dumpScriptEngines() throws Exception {
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();

    for (ScriptEngineFactory factory : factories) {
        log.info("ScriptEngineFactory Info");
        String engName = factory.getEngineName();
        String engVersion = factory.getEngineVersion();
        String langName = factory.getLanguageName();
        String langVersion = factory.getLanguageVersion();

        System.out.printf("\tScript Engine: %s (%s)\n", engName, engVersion);

        List<String> engNames = factory.getNames();
        for (String name : engNames) {
            System.out.printf("\tEngine Alias: %s\n", name);
        }//  w w w. j  a  va2s.  com

        System.out.printf("\tLanguage: %s (%s)\n", langName, langVersion);
    }
}

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.trafodion.rest.script.ScriptManager.java

private ScriptManager() {
    List<ScriptEngineFactory> engines = manager.getEngineFactories();
    if (engines.isEmpty()) {
        LOG.warn("No scripting engines were found");
        return;/* w  ww.  ja  v  a 2s . c om*/
    }

    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.springframework.data.hadoop.scripting.Jsr223ScriptEvaluator.java

protected ScriptEngine discoverEngine(ScriptSource script, Map<String, Object> arguments) {
    ScriptEngineManager engineManager = new ScriptEngineManager(classLoader);
    ScriptEngine engine = null;//  www.jav  a 2 s  . c om

    if (StringUtils.hasText(language)) {
        engine = engineManager.getEngineByName(language);
    } else {
        // make use the extension (enhanced ScriptSource interface)
        Assert.hasText(extension, "no language or extension specified");
        engine = engineManager.getEngineByExtension(extension);
    }

    Assert.notNull(engine, "No suitable engine found for "
            + (StringUtils.hasText(language) ? "language " + language : "extension " + extension));

    if (log.isDebugEnabled()) {
        ScriptEngineFactory factory = engine.getFactory();
        log.debug(String.format("Using ScriptEngine %s (%s), language %s (%s)", factory.getEngineName(),
                factory.getEngineVersion(), factory.getLanguageName(), factory.getLanguageVersion()));
    }

    return engine;
}