Search in sources :

Example 51 with ScriptEngine

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]");
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrCore(org.apache.solr.core.SolrCore) ArrayList(java.util.ArrayList) ScriptEngine(javax.script.ScriptEngine)

Example 52 with ScriptEngine

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

Example 53 with ScriptEngine

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());
}
Also used : StringReader(java.io.StringReader) ScriptEngine(javax.script.ScriptEngine)

Example 54 with ScriptEngine

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;
}
Also used : ScriptException(javax.script.ScriptException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 55 with ScriptEngine

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;
}
Also used : ScriptException(javax.script.ScriptException) SampleResult(org.apache.jmeter.samplers.SampleResult) IOException(java.io.IOException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngine (javax.script.ScriptEngine)175 ScriptEngineManager (javax.script.ScriptEngineManager)70 ScriptException (javax.script.ScriptException)46 Test (org.junit.Test)29 Bindings (javax.script.Bindings)20 IOException (java.io.IOException)19 Map (java.util.Map)14 Invocable (javax.script.Invocable)13 File (java.io.File)11 FileReader (java.io.FileReader)11 ScriptEngineFactory (javax.script.ScriptEngineFactory)11 HashMap (java.util.HashMap)10 ScriptContext (javax.script.ScriptContext)10 SimpleBindings (javax.script.SimpleBindings)10 InputStreamReader (java.io.InputStreamReader)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 PrintWriter (java.io.PrintWriter)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7