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);
}
}
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);
}
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;
}
}
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"));
}
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));
}
Aggregations