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