Search in sources :

Example 56 with Invocable

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

Example 57 with Invocable

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;
}
Also used : lombok.val(lombok.val) Invocable(javax.script.Invocable) ScriptEngineManager(javax.script.ScriptEngineManager) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) IOException(java.io.IOException)

Example 58 with Invocable

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

Example 59 with Invocable

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

Example 60 with Invocable

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

Aggregations

Invocable (javax.script.Invocable)74 ScriptException (javax.script.ScriptException)45 ScriptEngine (javax.script.ScriptEngine)34 ScriptEngineManager (javax.script.ScriptEngineManager)24 IOException (java.io.IOException)19 File (java.io.File)10 InputStreamReader (java.io.InputStreamReader)10 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)10 FileReader (java.io.FileReader)9 Reader (java.io.Reader)9 Compilable (javax.script.Compilable)8 CompiledScript (javax.script.CompiledScript)8 Bindings (javax.script.Bindings)7 Map (java.util.Map)5 BufferedReader (java.io.BufferedReader)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 Writer (java.io.Writer)3 HashMap (java.util.HashMap)3 ScriptContext (javax.script.ScriptContext)3