All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.rexster.protocol.EngineController Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package com.tinkerpop.rexster.protocol;

import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Manages the list of EngineHolder items for the current ScriptEngineManager. By default, the ScriptEngine instance
 * is never reset.
 *
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public class EngineController {

    public static final int RESET_NEVER = -1;

    private final ScriptEngineManager manager = new ScriptEngineManager();
    private final Map engines = new HashMap();

    /**
     * All gremlin engines are prefixed with this value.
     */
    private static final String ENGINE_NAME_PREFIX = "gremlin-";

    /**
     * Add all flavors of gremlin to this list. This should be the true name of the language.
     */
    private final List gremlinEngineNames = new ArrayList() {{
        add("gremlin-groovy");
    }};

    private static EngineController engineController;
    private static String initializationScriptFile;
    private static int engineResetThreshold = RESET_NEVER;

    private EngineController() {
        // for ruby
        System.setProperty("org.jruby.embed.localvariable.behavior", "persistent");
        for (ScriptEngineFactory factory : this.manager.getEngineFactories()) {

            // only add engine factories for those languages that are gremlin based.
            if (gremlinEngineNames.contains(factory.getLanguageName())) {
                this.engines.put(factory.getLanguageName(), new EngineHolder(
                        factory, engineResetThreshold, initializationScriptFile));
            }
        }
    }

    /**
     * Must call this before a call to getInstance() if the reset count is to be taken into account.
     */
    public static void configure(final int resetCount, final String initScriptFile){
        engineResetThreshold = resetCount;
        initializationScriptFile = initScriptFile;
    }

    public static EngineController getInstance() {
        if (engineController == null) {
            engineController = new EngineController();
        }

        return engineController;
    }

    public List getAvailableEngineLanguages() {
        final List languages = new ArrayList();
        for (String fullLanguageName : this.engines.keySet()) {
            languages.add(fullLanguageName.substring(fullLanguageName.indexOf(ENGINE_NAME_PREFIX) + ENGINE_NAME_PREFIX.length()));
        }

        return Collections.unmodifiableList(languages);
    }

    public boolean isEngineAvailable(final String languageName) {
        boolean available;
        try {
            getEngineByLanguageName(languageName);
            available = true;
        } catch (ScriptException se) {
            available = false;
        }

        return available;
    }

    public EngineHolder getEngineByLanguageName(final String languageName) throws ScriptException {

        final EngineHolder engine = this.engines.get(ENGINE_NAME_PREFIX + languageName);
        if (engine == null) {
            throw new ScriptException("No engine for the language: " + languageName);
        }

        return engine;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy