use of javax.script.ScriptEngine in project lucene-solr by apache.
the class StatelessScriptUpdateProcessorFactoryTest method testMultipleScripts.
public void testMultipleScripts() throws Exception {
SolrCore core = h.getCore();
for (final String chain : new String[] { "dual-scripts-arr", "dual-scripts-strs" }) {
UpdateRequestProcessorChain chained = core.getUpdateProcessingChain(chain);
final StatelessScriptUpdateProcessorFactory factory = ((StatelessScriptUpdateProcessorFactory) chained.getProcessors().get(0));
final List<String> functionMessages = new ArrayList<>();
ScriptEngineCustomizer customizer = new ScriptEngineCustomizer() {
@Override
public void customize(ScriptEngine engine) {
engine.put("functionMessages", functionMessages);
}
};
factory.setScriptEngineCustomizer(customizer);
assertNotNull(chained);
SolrInputDocument d = processAdd(chain, doc(f("id", "2"), f("name", " foo "), f("subject", "bar")));
assertEquals(chain + " didn't add Double field", 42.3d, d.getFieldValue("script_added_d"));
assertEquals(chain + " didn't add integer field", new Integer(42), d.getFieldValue("script_added_i"));
processCommit("run-no-scripts");
assertQ(chain + ": couldn't find doc by id", req("q", "id:2"), "//result[@numFound=1]");
processDeleteById(chain, "2");
processCommit(chain);
assertEquals(chain, 6, functionMessages.size());
assertTrue(chain, functionMessages.contains("processAdd0"));
assertTrue(chain, functionMessages.contains("processAdd1"));
assertTrue(chain + ": script order doesn't match conf order", functionMessages.indexOf("processAdd0") < functionMessages.indexOf("processAdd1"));
assertTrue(chain, functionMessages.contains("processDelete0"));
assertTrue(chain, functionMessages.contains("processDelete1"));
assertTrue(chain + ": script order doesn't match conf order", functionMessages.indexOf("processDelete0") < functionMessages.indexOf("processDelete1"));
assertTrue(chain, functionMessages.contains("processCommit0"));
assertTrue(chain, functionMessages.contains("processCommit1"));
assertTrue(chain + ": script order doesn't match conf order", functionMessages.indexOf("processCommit0") < functionMessages.indexOf("processCommit1"));
finish(chain);
assertEquals(chain, 8, functionMessages.size());
assertTrue(chain, functionMessages.contains("finish0"));
assertTrue(chain, functionMessages.contains("finish1"));
assertTrue(chain + ": script order doesn't match conf order", functionMessages.indexOf("finish0") < functionMessages.indexOf("finish1"));
assertQ(chain + ": found deleted doc", req("q", "id:2"), "//result[@numFound=0]");
}
}
use of javax.script.ScriptEngine in project lucene-solr by apache.
the class ScriptEngineTest method testEvalText.
public void testEvalText() throws ScriptException, NoSuchMethodException {
ScriptEngine engine = manager.getEngineByName("JavaScript");
assertNotNull(engine);
engine.eval("function add(a,b) { return a + b }");
Number result = (Number) ((Invocable) engine).invokeFunction("add", 1, 2);
assertNotNull(result);
assertEquals(3, result.intValue());
}
use of javax.script.ScriptEngine in project lucene-solr by apache.
the class ScriptEngineTest method testEvalReader.
public void testEvalReader() throws ScriptException, NoSuchMethodException {
ScriptEngine engine = manager.getEngineByName("JavaScript");
assertNotNull(engine);
StringReader reader = new StringReader("function add(a,b) { return a + b }");
engine.eval(reader);
Number result = (Number) ((Invocable) engine).invokeFunction("add", 1, 2);
assertNotNull(result);
assertEquals(3, result.intValue());
}
use of javax.script.ScriptEngine in project incubator-atlas by apache.
the class Titan0Graph method executeGremlinScript.
private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
Object result = null;
ScriptEngine engine = getGremlinScriptEngine();
try {
Bindings bindings = engine.createBindings();
bindings.put("g", getGraph());
result = engine.eval(gremlinQuery, bindings);
} catch (ScriptException e) {
throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
} finally {
releaseGremlinScriptEngine(engine);
}
return result;
}
use of javax.script.ScriptEngine in project jmeter by apache.
the class JSR223Sampler method sample.
@Override
public SampleResult sample(Entry entry) {
SampleResult result = new SampleResult();
result.setSampleLabel(getName());
result.setSuccessful(true);
result.setResponseCodeOK();
result.setResponseMessageOK();
final String filename = getFilename();
if (filename.length() > 0) {
result.setSamplerData("File: " + filename);
} else {
result.setSamplerData(getScript());
}
result.setDataType(SampleResult.TEXT);
result.sampleStart();
try {
ScriptEngine scriptEngine = getScriptEngine();
Bindings bindings = scriptEngine.createBindings();
bindings.put("SampleResult", result);
Object ret = processFileOrScript(scriptEngine, bindings);
if (ret != null && (result.getResponseData() == null || result.getResponseData().length == 0)) {
result.setResponseData(ret.toString(), null);
}
} catch (IOException | ScriptException e) {
log.error("Problem in JSR223 script {}, message: {}", getName(), e, e);
result.setSuccessful(false);
// $NON-NLS-1$
result.setResponseCode("500");
result.setResponseMessage(e.toString());
}
result.sampleEnd();
return result;
}
Aggregations