Search in sources :

Example 11 with ScriptEngine

use of javax.script.ScriptEngine in project hazelcast by hazelcast.

the class ScriptRunnable method run.

@Override
public void run() {
    final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine e = scriptEngineManager.getEngineByName("javascript");
    if (map != null) {
        for (Map.Entry<String, ?> entry : map.entrySet()) {
            e.put(entry.getKey(), entry.getValue());
        }
    }
    e.put("hazelcast", hazelcastInstance);
    try {
        // for new JavaScript engine called Nashorn we need the compatibility script
        if (e.getFactory().getEngineName().toLowerCase().contains("nashorn")) {
            e.eval("load('nashorn:mozilla_compat.js');");
        }
        e.eval("importPackage(java.lang);");
        e.eval("importPackage(java.util);");
        e.eval("importPackage(com.hazelcast.core);");
        e.eval("importPackage(com.hazelcast.config);");
        e.eval("importPackage(java.util.concurrent);");
        e.eval("importPackage(org.junit);");
        e.eval(script);
    } catch (ScriptException e1) {
        throw new RuntimeException(e1);
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) Map(java.util.Map) ScriptEngine(javax.script.ScriptEngine)

Example 12 with ScriptEngine

use of javax.script.ScriptEngine in project binnavi by google.

the class ScriptRunner method runScript.

/**
   * Executes a script file. The language of the script is determined by the file extension.
   * 
   * @param file
   * @param bindings
   * @throws ScriptException
   * @throws IOException
   */
public static void runScript(final File file, final List<Pair<String, Object>> bindings) throws ScriptException, IOException {
    final ScriptEngine engine = manager.getEngineByExtension(FileUtils.getFileExtension(file));
    Preconditions.checkNotNull(engine, "Error: Script %s has an unknown extension.", file.getAbsolutePath());
    final String script = FileUtils.readTextfile(file);
    runScript(engine, script, bindings);
}
Also used : ScriptEngine(javax.script.ScriptEngine)

Example 13 with ScriptEngine

use of javax.script.ScriptEngine in project spring-framework by spring-projects.

the class ScriptTemplateView method getEngine.

protected ScriptEngine getEngine() {
    if (Boolean.FALSE.equals(this.sharedEngine)) {
        Map<Object, ScriptEngine> engines = enginesHolder.get();
        if (engines == null) {
            engines = new HashMap<>(4);
            enginesHolder.set(engines);
        }
        Object engineKey = (!ObjectUtils.isEmpty(this.scripts) ? new EngineKey(this.engineName, this.scripts) : this.engineName);
        ScriptEngine engine = engines.get(engineKey);
        if (engine == null) {
            engine = createEngineFromName();
            engines.put(engineKey, engine);
        }
        return engine;
    } else {
        // Simply return the configured ScriptEngine...
        return this.engine;
    }
}
Also used : ScriptEngine(javax.script.ScriptEngine)

Example 14 with ScriptEngine

use of javax.script.ScriptEngine in project spring-framework by spring-projects.

the class ScriptTemplateViewTests method customEngineAndRenderFunction.

@Test
public void customEngineAndRenderFunction() throws Exception {
    ScriptEngine engine = mock(InvocableScriptEngine.class);
    given(engine.get("key")).willReturn("value");
    this.view.setEngine(engine);
    this.view.setRenderFunction("render");
    this.view.setApplicationContext(this.wac);
    engine = this.view.getEngine();
    assertNotNull(engine);
    assertEquals("value", engine.get("key"));
    DirectFieldAccessor accessor = new DirectFieldAccessor(this.view);
    assertNull(accessor.getPropertyValue("renderObject"));
    assertEquals("render", accessor.getPropertyValue("renderFunction"));
    assertEquals(StandardCharsets.UTF_8, accessor.getPropertyValue("charset"));
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

Example 15 with ScriptEngine

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

the class ScriptEngineFactoryTest method testGetScriptEngine.

@Test
public void testGetScriptEngine() {
    ScriptEngine scriptEngine = factory.getScriptEngine();
    ScriptEngine scriptEngine2 = factory.getScriptEngine();
    assertThat(scriptEngine, notNullValue());
    assertThat(scriptEngine2, notNullValue());
    assertThat(scriptEngine, not(sameInstance(scriptEngine2)));
    assertThat(scriptEngine.getFactory(), is(factory));
    assertThat(scriptEngine2.getFactory(), is(factory));
}
Also used : ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

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