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