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