Search in sources :

Example 1 with ScriptEngine

use of javax.script.ScriptEngine in project cas by apereo.

the class ScriptedRegisteredServiceAttributeReleasePolicy method getAttributesInternal.

@Override
protected Map<String, Object> getAttributesInternal(final Map<String, Object> attributes, final RegisteredService service) {
    try {
        String engineName = null;
        if (this.scriptFile.endsWith(".py")) {
            engineName = "python";
        } else if (this.scriptFile.endsWith(".js")) {
            engineName = "js";
        } else if (this.scriptFile.endsWith(".groovy")) {
            engineName = "groovy";
        }
        final ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
        if (engine == null || StringUtils.isBlank(engineName)) {
            LOGGER.warn("Script engine is not available for [{}]", engineName);
        } else {
            final File theScriptFile = ResourceUtils.getResourceFrom(this.scriptFile).getFile();
            if (theScriptFile.exists()) {
                LOGGER.debug("Created python object instance from class [{}]", theScriptFile.getCanonicalPath());
                final Object[] args = { attributes, LOGGER };
                LOGGER.debug("Executing python script's run method, with parameters [{}]", args);
                engine.eval(new FileReader(theScriptFile));
                final Invocable invocable = (Invocable) engine;
                final Map<String, Object> personAttributesMap = (Map<String, Object>) invocable.invokeFunction("run", args);
                LOGGER.debug("Final set of attributes determined by the script are [{}]", personAttributesMap);
                return personAttributesMap;
            }
            LOGGER.warn("Python script [{}] does not exist, or cannot be loaded", scriptFile);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new HashMap<>();
}
Also used : Invocable(javax.script.Invocable) HashMap(java.util.HashMap) ScriptEngineManager(javax.script.ScriptEngineManager) FileReader(java.io.FileReader) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) ScriptEngine(javax.script.ScriptEngine)

Example 2 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class GroovyTest method interactingWithJRuby.

@Test
public void interactingWithJRuby() throws Exception {
    ScriptEngine rb = JRubyTest.getJRubyEngine();
    RubyProc proc = (RubyProc) rb.eval(":*.to_proc");
    Closure<?> closure = toClosure(LambdaJRuby.toFn2(proc));
    groovy.put("f", closure);
    assertEquals(6L, groovy.eval("f(2, 3)"));
}
Also used : RubyProc(org.jruby.RubyProc) ScriptEngine(javax.script.ScriptEngine) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 3 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class GroovyTest method interactingWithJavaScript.

@Test
public void interactingWithJavaScript() throws Exception {
    ScriptEngine js = JavaScriptTest.getJavaScriptEngine();
    Function f = (Function) js.eval("var f = function(n, m) { return n * m; }; f;");
    ClosureFn2 closure = toClosure(LambdaJavaScript.toFn2(f));
    assertEquals(6.0, closure.call(new Object[] { 2, 3 }));
    groovy.put("closure", closure);
    assertEquals(120.0, groovy.eval("[1, 2, 3, 4, 5].inject(1, closure)"));
}
Also used : Function(sun.org.mozilla.javascript.internal.Function) ClosureFn2(org.enumerable.lambda.support.groovy.LambdaGroovy.ClosureFn2) ScriptEngine(javax.script.ScriptEngine) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 4 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class JavaScriptTest method interactingWithGroovy.

@Test
public void interactingWithGroovy() throws Exception {
    ScriptEngine groovy = GroovyTest.getGroovyEngine();
    Closure<?> closure = (Closure<?>) groovy.eval("{ n, m -> n * m }");
    FunctionFn2 f = toFunction(LambdaGroovy.toFn2(closure));
    js.put("f", f);
    assertEquals(6.0, js.eval("f(2, 3)"));
}
Also used : Closure(groovy.lang.Closure) FunctionFn2(org.enumerable.lambda.support.javascript.LambdaJavaScript.FunctionFn2) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest)

Example 5 with ScriptEngine

use of javax.script.ScriptEngine in project enumerable by hraberg.

the class ClojureTest method interactingWithJRuby.

@Test
public void interactingWithJRuby() throws Exception {
    ScriptEngine rb = JRubyTest.getJRubyEngine();
    RubyProc proc = (RubyProc) rb.eval(":*.to_proc");
    IFn times = toIFn(LambdaJRuby.toFn2(proc));
    assertEquals(6L, times.invoke(2, 3));
    defn("times-rb", times);
    assertEquals(120L, clj.eval("(reduce times-rb 1 [1, 2, 3, 4, 5])"));
}
Also used : IFn(clojure.lang.IFn) RubyProc(org.jruby.RubyProc) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Aggregations

ScriptEngine (javax.script.ScriptEngine)369 ScriptEngineManager (javax.script.ScriptEngineManager)163 ScriptException (javax.script.ScriptException)123 Bindings (javax.script.Bindings)51 IOException (java.io.IOException)46 Test (org.junit.Test)40 Invocable (javax.script.Invocable)31 File (java.io.File)27 FileReader (java.io.FileReader)25 ScriptEngineFactory (javax.script.ScriptEngineFactory)25 SimpleBindings (javax.script.SimpleBindings)25 InputStreamReader (java.io.InputStreamReader)24 Map (java.util.Map)24 Compilable (javax.script.Compilable)20 Reader (java.io.Reader)19 ScriptContext (javax.script.ScriptContext)19 CompiledScript (javax.script.CompiledScript)18 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)17 BufferedReader (java.io.BufferedReader)13