use of javax.script.ScriptEngineManager in project camel by apache.
the class ScriptBuilder method lookupScriptEngineFactory.
protected static ScriptEngineFactory lookupScriptEngineFactory(String language) {
ScriptEngineManager manager = new ScriptEngineManager();
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
// some script names has alias
String[] names = getScriptNames(language);
for (String name : names) {
if (factory.getLanguageName().equals(name)) {
return factory;
}
}
}
// fallback to get engine by name
ScriptEngine engine = createScriptEngine(language, true);
if (engine != null) {
return engine.getFactory();
}
return null;
}
use of javax.script.ScriptEngineManager in project hazelcast by hazelcast.
the class ScriptExecutorOperation method run.
@Override
public void run() throws Exception {
ScriptEngineManager scriptEngineManager = ScriptEngineManagerContext.getScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByName(engineName);
if (engine == null) {
throw new IllegalArgumentException("Could not find ScriptEngine named '" + engineName + "'.");
}
engine.put("hazelcast", getNodeEngine().getHazelcastInstance());
if (bindings != null) {
Set<Map.Entry<String, Object>> entries = bindings.entrySet();
for (Map.Entry<String, Object> entry : entries) {
engine.put(entry.getKey(), entry.getValue());
}
}
try {
this.result = engine.eval(script);
} catch (ScriptException e) {
this.result = e.getMessage();
}
}
use of javax.script.ScriptEngineManager in project hazelcast by hazelcast.
the class ScriptCallable method call.
@Override
public Object call() {
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);");
return e.eval(script);
} catch (ScriptException e1) {
throw new RuntimeException(e1);
}
}
use of javax.script.ScriptEngineManager 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.ScriptEngineManager in project hazelcast by hazelcast.
the class HazelcastOSGiScriptEngineFactoryTest method registerAndGetScriptEngineByExtensionSuccessfully.
@Test
public void registerAndGetScriptEngineByExtensionSuccessfully() {
ScriptEngineManager scriptEngineManager = ScriptEngineManagerContext.getScriptEngineManager();
GroovyScriptEngineFactory groovyScriptEngineFactory = new GroovyScriptEngineFactory();
scriptEngineManager.registerEngineExtension("groovy", groovyScriptEngineFactory);
ScriptEngine scriptEngine = scriptEngineManager.getEngineByExtension("groovy");
assertNotNull(scriptEngine);
ScriptEngineFactory scriptEngineFactory = scriptEngine.getFactory();
verifyScriptEngineFactory(scriptEngineFactory);
}
Aggregations