Search in sources :

Example 26 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project intellij-community by JetBrains.

the class JavaFxInjectPageLanguageIntention method getAvailableLanguages.

public static Set<String> getAvailableLanguages(Project project) {
    final List<ScriptEngineFactory> engineFactories = new ScriptEngineManager(composeUserClassLoader(project)).getEngineFactories();
    if (engineFactories != null) {
        final Set<String> availableNames = new TreeSet<>();
        for (ScriptEngineFactory factory : engineFactories) {
            final String engineName = (String) factory.getParameter(ScriptEngine.NAME);
            availableNames.add(engineName);
        }
        return availableNames;
    }
    return null;
}
Also used : ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngineManager(javax.script.ScriptEngineManager)

Example 27 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project lucene-solr by apache.

the class StatelessScriptUpdateProcessorFactory method getSupportedEngines.

/**
   * For error messages - returns null if there are any exceptions of any 
   * kind building the string (or of the list is empty for some unknown reason).
   * @param ext - if true, list of extensions, otherwise a list of engine names
   */
private static String getSupportedEngines(ScriptEngineManager mgr, boolean ext) {
    String result = null;
    try {
        List<ScriptEngineFactory> factories = mgr.getEngineFactories();
        if (null == factories)
            return result;
        Set<String> engines = new LinkedHashSet<>(factories.size());
        for (ScriptEngineFactory f : factories) {
            if (ext) {
                engines.addAll(f.getExtensions());
            } else {
                engines.addAll(f.getNames());
            }
        }
        result = StringUtils.join(engines, ", ");
    } catch (RuntimeException e) {
    /* :NOOP: */
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ScriptEngineFactory(javax.script.ScriptEngineFactory)

Example 28 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project tomee by apache.

the class OpenEJBScripter method engine.

private static ScriptEngine engine(final String language) {
    ScriptEngine engine = ENGINES.get().get(language);
    if (engine == null) {
        final ScriptEngineFactory factory = ENGINE_FACTORIES.get(language);
        engine = factory.getScriptEngine();
        ENGINES.get().put(language, engine);
    }
    return engine;
}
Also used : ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngine(javax.script.ScriptEngine)

Example 29 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project sling by apache.

the class ScriptConsolePlugin method getScriptConfig0.

private String getScriptConfig0() throws IOException {
    StringWriter sw = new StringWriter();
    JSONWriter jw = new JSONWriter(sw);
    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();
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) StringWriter(java.io.StringWriter) ScriptEngineFactory(javax.script.ScriptEngineFactory)

Example 30 with ScriptEngineFactory

use of javax.script.ScriptEngineFactory in project jqa-core-framework by buschmais.

the class AnalyzerVisitor method executeScript.

/**
     * Execute an analysis script
     * 
     * @param <T>
     *            The result type.
     * @param scriptExecutable
     *            The script.
     * @param ruleParameters
     * @param severity
     *            The severity. @return The result.
     * @throws RuleExecutorException
     *             If script execution fails.
     */
private <T extends ExecutableRule> Result<T> executeScript(T executable, ScriptExecutable scriptExecutable, Map<String, Object> ruleParameters, Severity severity) throws RuleExecutorException {
    String language = scriptExecutable.getLanguage();
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
    if (scriptEngine == null) {
        List<String> availableLanguages = new ArrayList<>();
        for (ScriptEngineFactory factory : scriptEngineManager.getEngineFactories()) {
            availableLanguages.addAll(factory.getNames());
        }
        throw new RuleExecutorException("Cannot resolve scripting engine for '" + language + "', available languages are " + availableLanguages);
    }
    // Set default variables
    scriptEngine.put(ScriptVariable.STORE.getVariableName(), store);
    scriptEngine.put(ScriptVariable.RULE.getVariableName(), executable);
    scriptEngine.put(ScriptVariable.SEVERITY.getVariableName(), severity);
    // Set rule parameters
    for (Map.Entry<String, Object> entry : ruleParameters.entrySet()) {
        scriptEngine.put(entry.getKey(), entry.getValue());
    }
    Object scriptResult;
    try {
        scriptResult = scriptEngine.eval(scriptExecutable.getSource());
    } catch (ScriptException e) {
        throw new RuleExecutorException("Cannot execute script.", e);
    }
    if (!(scriptResult instanceof Result)) {
        throw new RuleExecutorException("Script returned an invalid result type, expected " + Result.class.getName() + " but got " + scriptResult);
    }
    return Result.class.cast(scriptResult);
}
Also used : ScriptException(javax.script.ScriptException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngine(javax.script.ScriptEngine) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Aggregations

ScriptEngineFactory (javax.script.ScriptEngineFactory)30 ScriptEngineManager (javax.script.ScriptEngineManager)11 ScriptEngine (javax.script.ScriptEngine)9 Test (org.junit.Test)9 QuickTest (com.hazelcast.test.annotation.QuickTest)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 ArrayList (java.util.ArrayList)4 URL (java.net.URL)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 JSONWriter (org.apache.felix.utils.json.JSONWriter)2 Result (com.buschmais.jqassistant.core.analysis.api.Result)1 RuleExecutorException (com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)1 OSQLScriptEngineFactory (com.orientechnologies.orient.core.sql.OSQLScriptEngineFactory)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1