get Script Engine Manager - Java javax.script

Java examples for javax.script:ScriptEngine

Description

get Script Engine Manager

Demo Code


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

public class Main{
    private static ScriptEngineManager scriptEngineManager;
    private static final Map<String, List<ScriptEngineFactory>> languageFactoryMap = new HashMap<String, List<ScriptEngineFactory>>();
    private static List<ScriptEngineFactory> engineFactories = null;
    public static ScriptEngineManager getScriptEngineManager() {
        if (scriptEngineManager == null) {
            System.setProperty("org.jruby.embed.localvariable.behavior",
                    "persistent");// required for JRuby, transparent
            // for others
            scriptEngineManager = new ScriptEngineManager(
                    ScriptUtils.class.getClassLoader());
            populateEngineFactoryMap();/*from w  w  w. j a  va 2  s . c  o  m*/
        }
        return scriptEngineManager;
    }
    private static void populateEngineFactoryMap() {
        engineFactories = getScriptEngineManager().getEngineFactories();
        if (engineFactories != null) {
            for (ScriptEngineFactory factory : engineFactories) {
                final String languageName = factory.getLanguageName();
                List<ScriptEngineFactory> languageFactories = languageFactoryMap
                        .get(languageName);
                if (languageFactories == null) {
                    languageFactories = new ArrayList<ScriptEngineFactory>();
                    languageFactoryMap.put(languageName, languageFactories);
                }
                languageFactories.add(factory);
            }
        }
    }
}

Related Tutorials