Search in sources :

Example 51 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project groovy by apache.

the class JSR223SpecTest method testSimpleExample.

@Test
public void testSimpleExample() throws ScriptException {
    // tag::jsr223_init[]
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("groovy");
    // end::jsr223_init[]
    // tag::jsr223_basic[]
    Integer sum = (Integer) engine.eval("(1..10).sum()");
    assertEquals(new Integer(55), sum);
    // end::jsr223_basic[]
    // tag::jsr223_variables[]
    engine.put("first", "HELLO");
    engine.put("second", "world");
    String result = (String) engine.eval("first.toLowerCase() + ' ' + second.toUpperCase()");
    assertEquals("hello WORLD", result);
// end::jsr223_variables[]
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

Example 52 with ScriptEngineManager

use of javax.script.ScriptEngineManager 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)

Example 53 with ScriptEngineManager

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

the class Jsr223Test method testLanguageNames.

@Test
public void testLanguageNames() throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    for (String scriptName : scriptNames) {
        ScriptEngine engine = manager.getEngineByName(scriptName);
        assertNotNull("We should get the script engine for " + scriptName, engine);
    }
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

Example 54 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project es6draft by anba.

the class InvocableTest method setUp.

@Before
public void setUp() {
    manager = new ScriptEngineManager();
    engine = manager.getEngineByName("es6draft");
    assertThat(engine, notNullValue());
    assertThat(engine, instanceOf(Invocable.class));
    invocable = (Invocable) engine;
}
Also used : Invocable(javax.script.Invocable) ScriptEngineManager(javax.script.ScriptEngineManager) Before(org.junit.Before)

Example 55 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project es6draft by anba.

the class TypeConversionTest method setUp.

@Before
public void setUp() {
    manager = new ScriptEngineManager();
    engine = manager.getEngineByName("es6draft");
    assertThat(engine, notNullValue());
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) Before(org.junit.Before)

Aggregations

ScriptEngineManager (javax.script.ScriptEngineManager)84 ScriptEngine (javax.script.ScriptEngine)46 Test (org.junit.Test)19 ScriptException (javax.script.ScriptException)15 ScriptEngineFactory (javax.script.ScriptEngineFactory)11 QuickTest (com.hazelcast.test.annotation.QuickTest)10 Invocable (javax.script.Invocable)10 IOException (java.io.IOException)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 Before (org.junit.Before)8 FileReader (java.io.FileReader)7 File (java.io.File)6 Reader (java.io.Reader)6 Bindings (javax.script.Bindings)6 Map (java.util.Map)5 HashMap (java.util.HashMap)3 SimpleBindings (javax.script.SimpleBindings)3 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)3 Plugin (the.bytecode.club.bytecodeviewer.api.Plugin)3 Person (com.winterbe.java8.samples.lambda.Person)2