Search in sources :

Example 11 with ScriptEngineManager

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;
}
Also used : ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 12 with ScriptEngineManager

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();
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) Map(java.util.Map) HashMap(java.util.HashMap) ScriptEngine(javax.script.ScriptEngine)

Example 13 with ScriptEngineManager

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);
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) Map(java.util.Map) ScriptEngine(javax.script.ScriptEngine)

Example 14 with ScriptEngineManager

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);
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) Map(java.util.Map) ScriptEngine(javax.script.ScriptEngine)

Example 15 with ScriptEngineManager

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);
}
Also used : GroovyScriptEngineFactory(org.codehaus.groovy.jsr223.GroovyScriptEngineFactory) ScriptEngineFactory(javax.script.ScriptEngineFactory) GroovyScriptEngineFactory(org.codehaus.groovy.jsr223.GroovyScriptEngineFactory) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

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