Search in sources :

Example 21 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)

Example 22 with ScriptEngine

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

the class ScriptBuilder method checkForOSGiEngine.

private static ScriptEngine checkForOSGiEngine(String language) {
    LOG.debug("No script engine found for {} using standard javax.script auto-registration. Checking OSGi registry.", language);
    try {
        // Test the OSGi environment with the Activator
        Class<?> c = Class.forName("org.apache.camel.script.osgi.Activator");
        Method mth = c.getDeclaredMethod("getBundleContext");
        Object ctx = mth.invoke(null);
        LOG.debug("Found OSGi BundleContext: {}", ctx);
        if (ctx != null) {
            Method resolveScriptEngine = c.getDeclaredMethod("resolveScriptEngine", String.class);
            return (ScriptEngine) resolveScriptEngine.invoke(null, language);
        }
    } catch (Throwable t) {
        LOG.debug("Unable to detect OSGi. ScriptEngine for " + language + " cannot be resolved.", t);
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) ScriptEngine(javax.script.ScriptEngine)

Example 23 with ScriptEngine

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

the class ScriptBuilder method evaluateScript.

protected Object evaluateScript(Exchange exchange) {
    try {
        if (reuseScriptEngine(exchange)) {
            // It's not safe to do the evaluation with a single scriptEngine
            synchronized (this) {
                LOG.debug("Calling doEvaluateScript without creating a new scriptEngine");
                return doEvaluateScript(exchange, scriptEngine);
            }
        } else {
            LOG.debug("Calling doEvaluateScript with a new scriptEngine");
            // get a new engine which we must for each exchange
            ScriptEngine engine = scriptEngineFactory.getScriptEngine();
            return doEvaluateScript(exchange, engine);
        }
    } catch (ScriptException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Script evaluation failed: " + e.getMessage(), e);
        }
        if (e.getCause() != null) {
            throw createScriptEvaluationException(e.getCause());
        } else {
            throw createScriptEvaluationException(e);
        }
    } catch (IOException e) {
        throw createScriptEvaluationException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Example 24 with ScriptEngine

use of javax.script.ScriptEngine 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 25 with ScriptEngine

use of javax.script.ScriptEngine in project gremlin by tinkerpop.

the class GremlinGroovyScriptEngineTest method testGremlinLoading.

public void testGremlinLoading() throws Exception {
    ScriptEngine engine = new GremlinGroovyScriptEngine();
    List list = new ArrayList();
    engine.put("g", TinkerGraphFactory.createTinkerGraph());
    engine.put("list", list);
    assertEquals(list.size(), 0);
    engine.eval("g.v(1).outE.inV._.fill(list)");
    assertEquals(list.size(), 3);
}
Also used : ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngine (javax.script.ScriptEngine)147 ScriptEngineManager (javax.script.ScriptEngineManager)49 ScriptException (javax.script.ScriptException)38 Test (org.junit.Test)26 IOException (java.io.IOException)16 Bindings (javax.script.Bindings)15 Map (java.util.Map)13 Invocable (javax.script.Invocable)12 FileReader (java.io.FileReader)9 HashMap (java.util.HashMap)9 ScriptContext (javax.script.ScriptContext)9 ScriptEngineFactory (javax.script.ScriptEngineFactory)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 SimpleBindings (javax.script.SimpleBindings)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7 File (java.io.File)6 InputStreamReader (java.io.InputStreamReader)6 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)6