import java.util.*; import javax.script.*; public class TestBindings { public static void main(String args[]) throws Exception { String script = "function doSomething() {var d = date}"; ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); Compilable compilingEngine = (Compilable) engine; CompiledScript cscript = compilingEngine.compile(script); //Bindings bindings = cscript.getEngine().createBindings(); Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); for(Map.Entry me : bindings.entrySet()) { System.out.printf("%s: %s\n",me.getKey(),String.valueOf(me.getValue())); } bindings.put("date", new Date()); //cscript.eval(); cscript.eval(bindings); Invocable invocable = (Invocable) cscript.getEngine(); invocable.invokeFunction("doSomething"); } }
@Override public Serializable doSend(final Message message, final MeasurementUnit measurementUnit) throws Exception { final Bindings b = compiledScript.getEngine().createBindings(); b.put("message", message); b.put("measurementUnit", measurementUnit); b.put("log", log); return (Serializable) compiledScript.eval(b); }
private Bindings getBindings(ExprEvaluatorContext context) { Bindings bindings = executable.getEngine().createBindings(); bindings.put(ExprNodeScript.CONTEXT_BINDING_NAME, context.getAllocateAgentInstanceScriptContext()); return bindings; }
public void addToContext(CompiledScript script, String varName, DataPointRT dprt, ScriptPointValueSetter setCallback) { ScriptEngine engine = script.getEngine(); engine.put(varName, wrapPoint(engine, dprt, setCallback)); List<String> points = getPointList(engine); if (points != null) points.add(varName); }
public Object eval() throws ScriptException { return eval(getEngine().getContext()); }
/** * Executes the program stored in the CompiledScript object. * The default ScriptContext of the associated ScriptEngine is used. * The effect of calling this method is same as that of * eval(getEngine().getContext()). * * @return the resultant object after the evaluation of the * script (may be <tt>null</tt>) * @throws ScriptException if evaluation fails for any reason */ public Object eval() throws ScriptException { return eval(getEngine().getContext()); }
import java.util.*; import javax.script.*; public class TestBindings { public static void main(String args[]) throws Exception { String script = "function doSomething() {var d = date}"; ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); Compilable compilingEngine = (Compilable) engine; CompiledScript cscript = compilingEngine.compile(script); //Bindings bindings = cscript.getEngine().createBindings(); Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); for(Map.Entry me : bindings.entrySet()) { System.out.printf("%s: %s\n",me.getKey(),String.valueOf(me.getValue())); } bindings.put("date", new Date()); //cscript.eval(); cscript.eval(bindings); Invocable invocable = (Invocable) cscript.getEngine(); invocable.invokeFunction("doSomething"); } }
public void removeFromContext(CompiledScript script, String varName) { ScriptEngine engine = script.getEngine(); List<String> points = getPointList(engine); if (points != null) points.remove(varName); engine.getBindings(ScriptContext.ENGINE_SCOPE).remove(varName); }
@Override public boolean isValid(final Message originalMessage, final Message response, final Properties messageAttributes) { boolean result = false; try { final CompiledScript script = getCompiledScript(); final Bindings b = script.getEngine().createBindings(); b.put("originalMessage", originalMessage); b.put("message", response); b.put("attributes", messageAttributes); b.put("log", log); final Object ret = script.eval(b); if (ret instanceof Boolean) { result = (Boolean) ret; } } catch (ScriptException | ValidationException e) { log.warn("Error validating a message: ", e); result = false; } if (!result) { log.info(String.format("Script validating failed with the message '%s' using script validator.", response.toString())); } return result; }
@Override public T execute(String script,Map<String, Object> parameters) throws ScriptException { Bindings bindings = compiledScript.getEngine().createBindings(); if (parameters != null) { bindings.putAll(parameters); } T result = (T) compiledScript.eval(bindings); return result; }
public SimpleScript(CompiledScript src, ContextProvider[] providers) throws ScriptException { this.engine = src.getEngine(); this.invocable = (Invocable) engine; this.bindings = engine.createBindings(); for (ContextProvider provider : providers) provider.provide(this); this.root = src.eval(bindings); }
MangoJavaScriptResult result, boolean testRun) throws ScriptException { Bindings engineScope = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE); if(setter == null) setter = createValidationSetter(result, permissions); util.setScriptLog(log); util.setResult(result); util.takeContext(script.getEngine(), engineScope, setter, importExclusions, testRun); engineScope.put(util.getContextKey(), util); util.setScriptLog(log); util.setResult(result); util.takeContext(script.getEngine(), engineScope, setter, importExclusions, testRun); engineScope.put(util.getContextKey(), util); List<String> points = getPointList(script.getEngine()); if(context != null) { for (String varName : context.keySet()) { IDataPointValueSource point = context.get(varName); engineScope.put(varName, wrapPoint(script.getEngine(), point, setter)); if (points != null) points.add(varName); script.getEngine().getContext().setWriter(log.getStdOutWriter()); engineScope.put(ScriptLog.CONTEXT_KEY, log); script.getEngine().eval(getGlobalFunctions());
/** * 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; }
try{ compiledScript.getEngine().put(WRAPPER_CONTEXT_KEY, new WrapperContext(runtime));
/** * 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; }
public Object eval(Bindings bindings) throws ScriptException { ScriptEngine engine = getEngine(); SimpleScriptContext context = new SimpleScriptContext(); context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE); context.setReader(engine.getContext().getReader()); context.setWriter(engine.getContext().getWriter()); return eval(context); }
.bind(scriptEngine, compiledScript.getEngine().getBindings(ScriptContext.ENGINE_SCOPE), db, iContext, iArgs);