Search in sources :

Example 1 with CompiledScript

use of javax.script.CompiledScript in project dubbo by alibaba.

the class ScriptRouter method route.

@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
    try {
        List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
        Compilable compilable = (Compilable) engine;
        Bindings bindings = engine.createBindings();
        bindings.put("invokers", invokersCopy);
        bindings.put("invocation", invocation);
        bindings.put("context", RpcContext.getContext());
        CompiledScript function = compilable.compile(rule);
        Object obj = function.eval(bindings);
        if (obj instanceof Invoker[]) {
            invokersCopy = Arrays.asList((Invoker<T>[]) obj);
        } else if (obj instanceof Object[]) {
            invokersCopy = new ArrayList<Invoker<T>>();
            for (Object inv : (Object[]) obj) {
                invokersCopy.add((Invoker<T>) inv);
            }
        } else {
            invokersCopy = (List<Invoker<T>>) obj;
        }
        return invokersCopy;
    } catch (ScriptException e) {
        //fail then ignore rule .invokers.
        logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
        return invokers;
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) Invoker(com.alibaba.dubbo.rpc.Invoker) Compilable(javax.script.Compilable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Bindings(javax.script.Bindings)

Example 2 with CompiledScript

use of javax.script.CompiledScript in project openhab1-addons by openhab.

the class EBusTelegramParser method evaluateScript.

/**
     * Evaluates the compiled script of a entry.
     * 
     * @param entry The configuration entry to evaluate
     * @param scopeValues All known values for script scope
     * @return The computed value
     * @throws ScriptException
     */
private Object evaluateScript(Entry<String, TelegramValue> entry, Map<String, Object> scopeValues) throws ScriptException {
    Object value = null;
    // executes compiled script
    if (entry.getValue().getCsript() != null) {
        CompiledScript cscript = entry.getValue().getCsript();
        // Add global variables thisValue and keyName to JavaScript context
        Bindings bindings = cscript.getEngine().createBindings();
        bindings.putAll(scopeValues);
        value = cscript.eval(bindings);
    }
    // try to convert the returned value to BigDecimal
    value = ObjectUtils.defaultIfNull(NumberUtils.toBigDecimal(value), value);
    // round to two digits, maybe not optimal for any result
    if (value instanceof BigDecimal) {
        ((BigDecimal) value).setScale(2, BigDecimal.ROUND_HALF_UP);
    }
    return value;
}
Also used : CompiledScript(javax.script.CompiledScript) Bindings(javax.script.Bindings) BigDecimal(java.math.BigDecimal)

Example 3 with CompiledScript

use of javax.script.CompiledScript in project es6draft by anba.

the class CompilableTest method getEngine.

@Test
public void getEngine() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal - 1");
    assertThat(script, notNullValue());
    assertThat(script.getEngine(), sameInstance(engine));
}
Also used : CompiledScript(javax.script.CompiledScript) Test(org.junit.Test)

Example 4 with CompiledScript

use of javax.script.CompiledScript in project es6draft by anba.

the class CompilableTest method compileString.

@Test
public void compileString() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    assertThat(script, notNullValue());
    engine.put("numberVal", 10);
    assertThat(script.eval(), instanceOfWith(Number.class, is(numberCloseTo(20))));
}
Also used : CompiledScript(javax.script.CompiledScript) Test(org.junit.Test)

Example 5 with CompiledScript

use of javax.script.CompiledScript in project es6draft by anba.

the class CompilableTest method compileReader.

@Test
public void compileReader() throws ScriptException {
    CompiledScript script = compilable.compile(new StringReader("numberVal * 4"));
    assertThat(script, notNullValue());
    engine.put("numberVal", 10);
    assertThat(script.eval(), instanceOfWith(Number.class, is(numberCloseTo(40))));
}
Also used : CompiledScript(javax.script.CompiledScript) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

CompiledScript (javax.script.CompiledScript)25 Bindings (javax.script.Bindings)14 ScriptException (javax.script.ScriptException)9 ScriptContext (javax.script.ScriptContext)8 Test (org.junit.Test)8 SimpleBindings (javax.script.SimpleBindings)6 SimpleScriptContext (javax.script.SimpleScriptContext)5 Compilable (javax.script.Compilable)4 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)3 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 CachedScript (org.apache.sling.scripting.api.CachedScript)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 ArrayList (java.util.ArrayList)2 ScriptNameAwareReader (org.apache.sling.scripting.core.ScriptNameAwareReader)2 Context (org.mozilla.javascript.Context)2 Script (org.mozilla.javascript.Script)2 Invoker (com.alibaba.dubbo.rpc.Invoker)1