Search in sources :

Example 91 with ScriptEngine

use of javax.script.ScriptEngine in project scheduling by ow2-proactive.

the class EvalScriptCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    ScriptEngine engine = currentContext.getEngine();
    Writer writer = currentContext.getDevice().getWriter();
    if (scriptArgs != null) {
        engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE).putAll(bindings(scriptArgs));
    }
    String script = FileUtility.readFileToString(new File(scriptPathname));
    try {
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace(new PrintWriter(writer, true));
    }
}
Also used : ScriptException(javax.script.ScriptException) File(java.io.File) ScriptEngine(javax.script.ScriptEngine) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 92 with ScriptEngine

use of javax.script.ScriptEngine in project scheduling by ow2-proactive.

the class Script method findScriptEngineCandidates.

private Map<ScriptEngine, Integer> findScriptEngineCandidates(boolean findByName) {
    Map<ScriptEngine, Integer> matchPositionPerScriptEngineCandidate = new HashMap<>();
    int matchPosition;
    List<String> lookupCriteria;
    for (ScriptEngineFactory factory : new ScriptEngineManager().getEngineFactories()) {
        matchPosition = 0;
        if (findByName) {
            lookupCriteria = factory.getNames();
        } else {
            lookupCriteria = factory.getExtensions();
        }
        for (String criteria : lookupCriteria) {
            if (criteria.equalsIgnoreCase(scriptEngineLookupName)) {
                matchPositionPerScriptEngineCandidate.put(factory.getScriptEngine(), matchPosition);
            }
            matchPosition++;
        }
    }
    return matchPositionPerScriptEngineCandidate;
}
Also used : HashMap(java.util.HashMap) ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 93 with ScriptEngine

use of javax.script.ScriptEngine in project scheduling by ow2-proactive.

the class Script method findBestScriptEngine.

private ScriptEngine findBestScriptEngine(Map<ScriptEngine, Integer> scriptEngineCandidates) {
    int minimumMatchingIndex = Integer.MAX_VALUE;
    ScriptEngine bestScriptEngine = null;
    for (Entry<ScriptEngine, Integer> candidate : scriptEngineCandidates.entrySet()) {
        if (candidate.getValue() < minimumMatchingIndex) {
            minimumMatchingIndex = candidate.getValue();
            bestScriptEngine = candidate.getKey();
        }
    }
    return bestScriptEngine;
}
Also used : ScriptEngine(javax.script.ScriptEngine)

Example 94 with ScriptEngine

use of javax.script.ScriptEngine in project yamcs-studio by yamcs.

the class ScriptStoreFactory method initJdkJSEngine.

/**
 * Must be called in UI Thread.
 *
 * @throws Exception
 */
private static void initJdkJSEngine() throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
    final Display display = Display.getCurrent();
    displayScriptEngineMap.put(display, engine);
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine) Display(org.eclipse.swt.widgets.Display)

Example 95 with ScriptEngine

use of javax.script.ScriptEngine in project camel by apache.

the class ScriptBuilder method tryCreateScriptEngine.

/**
     * Attemps to create the script engine for the given langauge using the classloader
     *
     * @return the engine, or <tt>null</tt> if not able to create
     */
private static ScriptEngine tryCreateScriptEngine(String language, ClassLoader classLoader) {
    ScriptEngineManager manager = new ScriptEngineManager(classLoader);
    ScriptEngine engine = null;
    // some script names has alias
    String[] names = getScriptNames(language);
    for (String name : names) {
        try {
            engine = manager.getEngineByName(name);
            if (engine != null) {
                break;
            }
        } catch (NoClassDefFoundError ex) {
            LOG.warn("Cannot load ScriptEngine for " + name + ". Please ensure correct JARs is provided on classpath (stacktrace in DEBUG logging).");
            // include stacktrace in debug logging
            LOG.debug("Cannot load ScriptEngine for " + name + ". Please ensure correct JARs is provided on classpath.", ex);
        }
    }
    if (engine == null) {
        engine = checkForOSGiEngine(language);
    }
    if (engine != null && isPython(language)) {
        ScriptContext context = engine.getContext();
        context.setAttribute("com.sun.script.jython.comp.mode", "eval", ScriptContext.ENGINE_SCOPE);
    }
    return engine;
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptContext(javax.script.ScriptContext) ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngine (javax.script.ScriptEngine)175 ScriptEngineManager (javax.script.ScriptEngineManager)70 ScriptException (javax.script.ScriptException)46 Test (org.junit.Test)29 Bindings (javax.script.Bindings)20 IOException (java.io.IOException)19 Map (java.util.Map)14 Invocable (javax.script.Invocable)13 File (java.io.File)11 FileReader (java.io.FileReader)11 ScriptEngineFactory (javax.script.ScriptEngineFactory)11 HashMap (java.util.HashMap)10 ScriptContext (javax.script.ScriptContext)10 SimpleBindings (javax.script.SimpleBindings)10 InputStreamReader (java.io.InputStreamReader)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 PrintWriter (java.io.PrintWriter)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7