Search in sources :

Example 16 with ScriptEngine

use of javax.script.ScriptEngine in project orientdb by orientechnologies.

the class OGremlinHelper method execute.

public static Object execute(final OrientBaseGraph graph, final String iText, final Map<Object, Object> iConfiguredParameters, Map<Object, Object> iCurrentParameters, final List<Object> iResult, final OGremlinCallback iBeforeExecution, final OGremlinCallback iAfterExecution) {
    try {
        final ScriptEngine engine = getGremlinEngine(graph);
        try {
            final String output = OGremlinHelper.bindParameters(engine, iConfiguredParameters, iCurrentParameters);
            if (iBeforeExecution != null)
                if (!iBeforeExecution.call(engine, graph))
                    return null;
            if (iText == null) {
                return null;
            }
            final Object scriptResult = engine.eval(iText);
            if (iAfterExecution != null)
                if (!iAfterExecution.call(engine, graph))
                    return null;
            // - Map -> ODocument
            if (output != null) {
                if (scriptResult instanceof GremlinPipeline) {
                    Iterator<?> it = ((GremlinPipeline<?, ?>) scriptResult).iterator();
                    while (it.hasNext()) // ignore iCurrentRecord but traverse still required
                    it.next();
                }
                final Map<String, Object> map = (Map<String, Object>) engine.get(output);
                ODocument oDocument = new ODocument(map);
                iResult.add(oDocument);
                return oDocument;
            }
            // returned for this call in the last pipe
            if (scriptResult instanceof GremlinPipeline) {
                final Iterator<?> it = ((GremlinPipeline<?, ?>) scriptResult).iterator();
                Object finalResult = null;
                List<Object> resultCollection = null;
                while (it.hasNext()) {
                    Object current = it.next();
                    if (finalResult != null) {
                        if (resultCollection == null) {
                            // CONVERT IT INTO A COLLECTION
                            resultCollection = new ArrayList<Object>();
                            resultCollection.add(finalResult);
                        }
                        resultCollection.add(current);
                    } else
                        finalResult = current;
                }
                if (resultCollection != null) {
                    iResult.addAll(resultCollection);
                    return resultCollection;
                } else {
                    if (finalResult != null)
                        iResult.add(finalResult);
                    return finalResult;
                }
            } else if (scriptResult != null)
                iResult.add(scriptResult);
            return scriptResult;
        } catch (Exception e) {
            throw OException.wrapException(new OCommandExecutionException("Error on execution of the GREMLIN script"), e);
        } finally {
            OGremlinHelper.global().releaseEngine(engine);
        }
    } finally {
        OGremlinHelper.global().releaseGraph(graph);
    }
}
Also used : GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) GremlinGroovyScriptEngine(com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine) ScriptEngine(javax.script.ScriptEngine) OException(com.orientechnologies.common.exception.OException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 17 with ScriptEngine

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

the class ScriptTemplateView method createEngineFromName.

protected ScriptEngine createEngineFromName() {
    if (this.scriptEngineManager == null) {
        this.scriptEngineManager = new ScriptEngineManager(getApplicationContext().getClassLoader());
    }
    ScriptEngine engine = StandardScriptUtils.retrieveEngineByName(this.scriptEngineManager, this.engineName);
    loadScripts(engine);
    return engine;
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 18 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.context);
    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("defaultCharset"));
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ScriptEngine(javax.script.ScriptEngine) Test(org.junit.Test)

Example 19 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 20 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)

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