use of javax.script.Invocable in project ofbiz-framework by apache.
the class ScriptUtil method executeScript.
/**
* Executes a compiled script and returns the result.
*
* @param script Compiled script.
* @param functionName Optional function or method to invoke.
* @param scriptContext Script execution context.
* @return The script result.
* @throws IllegalArgumentException
*/
public static Object executeScript(CompiledScript script, String functionName, ScriptContext scriptContext, Object[] args) throws ScriptException, NoSuchMethodException {
Assert.notNull("script", script, "scriptContext", scriptContext);
Object result = script.eval(scriptContext);
if (UtilValidate.isNotEmpty(functionName)) {
if (Debug.verboseOn()) {
Debug.logVerbose("Invoking function/method " + functionName, module);
}
ScriptEngine engine = script.getEngine();
try {
Invocable invocableEngine = (Invocable) engine;
result = invocableEngine.invokeFunction(functionName, args == null ? EMPTY_ARGS : args);
} catch (ClassCastException e) {
throw new ScriptException("Script engine " + engine.getClass().getName() + " does not support function/method invocations");
}
}
return result;
}
use of javax.script.Invocable in project cas by apereo.
the class ScriptingUtils method executeScriptEngine.
/**
* Execute groovy script engine t.
*
* @param <T> the type parameter
* @param scriptFile the script file
* @param args the args
* @param clazz the clazz
* @return the t
*/
public static <T> T executeScriptEngine(final String scriptFile, final Object[] args, final Class<T> clazz) {
try {
val engineName = getScriptEngineName(scriptFile);
if (StringUtils.isBlank(engineName)) {
LOGGER.warn("Script engine name can not be determined for [{}]", engineName);
return null;
}
val engine = new ScriptEngineManager().getEngineByName(engineName);
val resourceFrom = ResourceUtils.getResourceFrom(scriptFile);
val theScriptFile = resourceFrom.getFile();
if (theScriptFile.exists()) {
LOGGER.debug("Created object instance from class [{}]", theScriptFile.getCanonicalPath());
try (val reader = Files.newBufferedReader(theScriptFile.toPath(), StandardCharsets.UTF_8)) {
engine.eval(reader);
}
val invocable = (Invocable) engine;
LOGGER.debug("Executing script's run method, with parameters [{}]", args);
val result = invocable.invokeFunction("run", args);
LOGGER.debug("Groovy script result is [{}]", result);
return getGroovyScriptExecutionResultOrThrow(clazz, result);
}
LOGGER.warn("[{}] script [{}] does not exist, or cannot be loaded", StringUtils.capitalize(engineName), scriptFile);
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
}
return null;
}
use of javax.script.Invocable in project yyl_example by Relucent.
the class HelloWorld method testUsingJDKClasses.
public static void testUsingJDKClasses(ScriptEngine engine) throws Exception {
// Packages是脚本语言里的一个全局变量,专用于访问JDK的package
String js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}";
engine.eval(js);
Invocable inv = (Invocable) engine;
inv.invokeFunction("doSwing", "Scripting Swing");
}
use of javax.script.Invocable in project yyl_example by Relucent.
the class HelloWorld method testScriptInterface.
public static void testScriptInterface(ScriptEngine engine) throws ScriptException {
String script = "var obj = new Object(); obj.run = function() { println('run method called'); }";
engine.eval(script);
Object obj = engine.get("obj");
Invocable inv = (Invocable) engine;
Runnable r = inv.getInterface(obj, Runnable.class);
Thread th = new Thread(r);
th.start();
}
use of javax.script.Invocable in project yyl_example by Relucent.
the class HelloWorld method testInvokeScriptMethod.
public static void testInvokeScriptMethod(ScriptEngine engine) throws Exception {
String script = "function hello(name) { return 'Hello,' + name;}";
engine.eval(script);
Invocable inv = (Invocable) engine;
String result = (String) inv.invokeFunction("hello", "World!!");
System.out.println("res:" + result);
}
Aggregations