Search in sources :

Example 91 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project lucene-solr by apache.

the class StatelessScriptUpdateProcessorFactoryTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    Assume.assumeNotNull((new ScriptEngineManager()).getEngineByExtension("js"));
    initCore("solrconfig-script-updateprocessor.xml", "schema12.xml");
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) BeforeClass(org.junit.BeforeClass)

Example 92 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project lucene-solr by apache.

the class ScriptEngineTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    assumeFalse("https://twitter.com/UweSays/status/260487231880433664 / SOLR-4233: OS X bogusly starts AWT!", Constants.MAC_OS_X);
    Assume.assumeNotNull((new ScriptEngineManager()).getEngineByExtension("js"));
    Assume.assumeNotNull((new ScriptEngineManager()).getEngineByName("JavaScript"));
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) BeforeClass(org.junit.BeforeClass)

Example 93 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project lucene-solr by apache.

the class StatelessScriptUpdateProcessorFactory method initEngines.

//================================================ Helper Methods ==================================================
/**
   * Initializes a list of script engines - an engine per script file.
   *
   * @param req The solr request.
   * @param rsp The solr response
   * @return The list of initialized script engines.
   */
private List<EngineInfo> initEngines(SolrQueryRequest req, SolrQueryResponse rsp) throws SolrException {
    List<EngineInfo> scriptEngines = new ArrayList<>();
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager(resourceLoader.getClassLoader());
    scriptEngineManager.put("logger", log);
    scriptEngineManager.put("req", req);
    scriptEngineManager.put("rsp", rsp);
    if (params != null) {
        scriptEngineManager.put("params", params);
    }
    for (ScriptFile scriptFile : scriptFiles) {
        ScriptEngine engine = null;
        if (null != engineName) {
            engine = scriptEngineManager.getEngineByName(engineName);
            if (engine == null) {
                String details = getSupportedEngines(scriptEngineManager, false);
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No ScriptEngine found by name: " + engineName + (null != details ? " -- supported names: " + details : ""));
            }
        } else {
            engine = scriptEngineManager.getEngineByExtension(scriptFile.getExtension());
            if (engine == null) {
                String details = getSupportedEngines(scriptEngineManager, true);
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No ScriptEngine found by file extension: " + scriptFile.getFileName() + (null != details ? " -- supported extensions: " + details : ""));
            }
        }
        if (!(engine instanceof Invocable)) {
            String msg = "Engine " + ((null != engineName) ? engineName : ("for script " + scriptFile.getFileName())) + " does not support function invocation (via Invocable): " + engine.getClass().toString() + " (" + engine.getFactory().getEngineName() + ")";
            log.error(msg);
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, msg);
        }
        if (scriptEngineCustomizer != null) {
            scriptEngineCustomizer.customize(engine);
        }
        scriptEngines.add(new EngineInfo((Invocable) engine, scriptFile));
        try {
            Reader scriptSrc = scriptFile.openReader(resourceLoader);
            try {
                engine.eval(scriptSrc);
            } catch (ScriptException e) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to evaluate script: " + scriptFile.getFileName(), e);
            } finally {
                IOUtils.closeQuietly(scriptSrc);
            }
        } catch (IOException ioe) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to evaluate script: " + scriptFile.getFileName(), ioe);
        }
    }
    return scriptEngines;
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) ArrayList(java.util.ArrayList) ScriptEngineManager(javax.script.ScriptEngineManager) Reader(java.io.Reader) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) SolrException(org.apache.solr.common.SolrException)

Example 94 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project yyl_example by Relucent.

the class BindingsScript method main.

public static void main(String[] args) {
    try {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
        String script = "print('hello')";
        Bindings bindings = new SimpleBindings();
        if (engine instanceof Compilable) {
            System.out.println("Compiling....");
            Compilable compEngine = (Compilable) engine;
            CompiledScript cs = compEngine.compile(script);
            cs.eval(bindings);
        } else {
            engine.eval(script, bindings);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleBindings(javax.script.SimpleBindings) Compilable(javax.script.Compilable) ScriptEngineManager(javax.script.ScriptEngineManager) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 95 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project yyl_example by Relucent.

the class HelloWorld method main.

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    testHelloWorld(engine);
    testScriptVariables(engine);
    testInvokeScriptMethod(engine);
    testScriptInterface(engine);
    testUsingJDKClasses(engine);
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngineManager (javax.script.ScriptEngineManager)109 ScriptEngine (javax.script.ScriptEngine)66 Test (org.junit.Test)23 ScriptException (javax.script.ScriptException)20 ScriptEngineFactory (javax.script.ScriptEngineFactory)14 File (java.io.File)12 Invocable (javax.script.Invocable)11 QuickTest (com.hazelcast.test.annotation.QuickTest)10 IOException (java.io.IOException)10 FileReader (java.io.FileReader)9 Bindings (javax.script.Bindings)9 Before (org.junit.Before)9 Map (java.util.Map)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)6 SimpleBindings (javax.script.SimpleBindings)6 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 InputStreamReader (java.io.InputStreamReader)3