protected Map<String,String> parseJsonResponse(String json) throws ScriptException { synchronized (scriptEngineLock) { if (scriptEngine == null) { scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript"); if (scriptEngine == null) { throw new RuntimeException("Failed to load JavaScript script engine"); } } Bindings bindings = new SimpleBindings(); bindings.put("json", json); return (Map<String, String>)scriptEngine.eval(JSON_RESPONSE_PARSING_SCRIPT, bindings); } } }
public static void LoadScriptFromTab( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext ) { try { for ( int i = 0; i < ArgList.length; i++ ) { // don't worry about "undefined" arguments String strToLoad = (String) ArgList[i]; String strScript = actualObject.get( strToLoad ).toString(); actualContext.eval( strScript, actualObject ); } } catch ( Exception e ) { // System.out.println(e.toString()); } }
protected Object evaluateExpression(Object expression, Map<String, Object> parameters) { try { Object result = null; StringWriter writer = new StringWriter(); ScriptContext context = new SimpleScriptContext(); for (Map.Entry<String, Object> property : engineProperties.entrySet()) { context.setAttribute(property.getKey(), property.getValue(), ScriptContext.ENGINE_SCOPE); } Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); bindings.putAll(parameters); context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); context.setWriter(writer); if (expression instanceof CompiledScript) { logger.debug("About to evaluate compiled expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = ((CompiledScript) expression).eval(context); } else { logger.debug("About to evaluate expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = scriptEngine.eval(expression.toString(), context); } if (result == null) { result = writer.toString(); } return result; } catch (ScriptException e) { throw new RuntimeException("Error when evaluating script", e); } }
ScriptEngineManager engineMgr = new ScriptEngineManager(); ScriptEngine engine = engineMgr.getEngineByName("JavaScript"); Bindings bindings = engine.createBindings(); String script = "javascript to eval goes here....."; bindings.put(varName1, value1); bindings.put(varName2, value2); Object obj = engine.eval(script, bindings)
public void accept(final Object a, final Object b, final Object c) { try { final Bindings bindings = new SimpleBindings(); bindings.put(A, a); bindings.put(B, b); bindings.put(C, c); this.engine.eval(this.script, bindings); } catch (final ScriptException e) { throw new IllegalArgumentException(e.getMessage()); } }
ScriptEngineManager mgr = new ScriptEngineManager(); engineName = cl.getOptionValue(engine.getOpt()); ScriptEngine engine = mgr.getEngineByName(engineName); if (engine == null) { shellState.printException(new Exception(engineName + " not found")); Bindings b = engine.getBindings(ScriptContext.ENGINE_SCOPE); putConnector(b, shellState.getAccumuloClient()); b.put("client", shellState.getAccumuloClient()); continue; } else if (parts.length == 1) { b.put(parts[0], null); argValues.add(null); } else if (parts.length == 2) { b.put(parts[0], parts[1]); argValues.add(parts[1]); ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); Object[] argArray = argValues.toArray(new Object[argValues.size()]); File f = new File(cl.getOptionValue(out.getOpt())); writer = new FileWriter(f); ctx.setWriter(writer);
Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE); if (bindings == null) { bindings = new SimpleBindings(); bindings.put("context", context); bindings.put("log", log); bindings.put("vmMetrics", vmMetrics); bindings.put(property.getKey().getName(), context.getProperty(property.getKey())); scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); configurator.eval(scriptEngine, scriptToRun, scriptingComponentHelper.getModules()); } else { scriptEngine.eval(scriptToRun);
this.scriptEvents = scriptEvents; ScriptEngineManager sem = new ScriptEngineManager(); scriptEngine = sem.getEngineByExtension(".lua"); Bindings bindings = scriptEngine.createBindings(); bindings.put("java", scriptTools); bindings.put("level", level); bindings.put("args", args); bindings.put("events", scriptEvents); scripts.get(id).eval(bindings);
@SuppressWarnings("unchecked") @Override public <T> T getElValue(String exp, Object target, Object[] arguments, Object retVal, boolean hasRetVal, Class<T> valueType) throws Exception { Bindings bindings = new SimpleBindings(); bindings.put(TARGET, target); bindings.put(ARGS, arguments); if (hasRetVal) { bindings.put(RET_VAL, retVal); } CompiledScript script = expCache.get(exp); if (null != script) { return (T) script.eval(bindings); } if (engine instanceof Compilable) { Compilable compEngine = (Compilable) engine; script = compEngine.compile(funcs + exp); expCache.put(exp, script); return (T) script.eval(bindings); } else { return (T) engine.eval(funcs + exp, bindings); } }
@Override public void write(final NullWritable key, final VertexWritable vertex) throws IOException { if (null != vertex) { try { final Bindings bindings = this.engine.createBindings(); bindings.put(VERTEX, vertex.get()); final String line = (String) engine.eval(WRITE_CALL, bindings); if (line != null) { this.out.write(line.getBytes(UTF8)); this.out.write(NEWLINE); } } catch (final ScriptException e) { throw new IOException(e.getMessage(), e); } } }
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 class JS { public static void main(String args[]) throws Exception { ScriptEngine js = new ScriptEngineManager().getEngineByName("javascript"); Bindings bindings = js.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("stdout", System.out); js.eval("stdout.println(Math.cos(Math.PI));"); // Prints "-1.0" to the standard output stream. } }
public static Object eval(String script, Object input) throws ScriptException { Bindings bindings = engine.createBindings(); bindings.put("$", input); return engine.eval(script, bindings); } }
ScriptEngine createScriptEngine(String engine) { ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(engine); if (scriptEngine == null) { scriptEngine = scriptEngineManager.getEngineByExtension(engine); } if (scriptEngine == null) { scriptEngine = scriptEngineManager.getEngineByMimeType(engine); } Preconditions.notNull(scriptEngine, () -> "Script engine not found: " + engine); Bindings bindings = scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE); bindings.put(Script.BIND_SYSTEM_PROPERTY, systemPropertyAccessor); bindings.put(Script.BIND_SYSTEM_ENVIRONMENT, environmentVariableAccessor); return scriptEngine; }
SingleGremlinScriptEngineManager.get(this.language); Bindings bindings = engine.createBindings(); bindings.putAll(this.bindings); bindings.put("g", this.factory.createTraversalSource(this.graph) .withStrategies(strategies)); bindings.put("graph", this.graph); Object value = bindings.get(entry.getValue()); if (value == null) { throw new IllegalArgumentException(String.format( entry.getKey(), entry.getValue())); bindings.put(entry.getKey(), value); Object result = engine.eval(this.script, bindings); throw new IllegalStateException(e.getMessage(), e);
@SuppressWarnings("unchecked") @Override public T execute(String script, Map<String, Object> parameters) throws ScriptException { ScriptEngine scriptEngine = manager.getEngineByName("js"); Bindings binding = scriptEngine.createBindings(); if (parameters != null) { binding.putAll(parameters); } Object val = scriptEngine.eval(script, binding); return ((T) val); }
protected Object evaluateScriptExpression(String expressionLanguage, VariableContext variableContext, String expressionText, CachedCompiledScriptSupport cachedCompiledScriptSupport) { ScriptEngine scriptEngine = getScriptEngineForName(expressionLanguage); // wrap script engine bindings + variable context and pass enhanced // bindings to the script engine. Bindings bindings = VariableContextScriptBindings.wrap(scriptEngine.createBindings(), variableContext); bindings.put("variableContext", variableContext); try { if (scriptEngine instanceof Compilable) { CompiledScript compiledScript = cachedCompiledScriptSupport.getCachedCompiledScript(); if (compiledScript == null) { synchronized (cachedCompiledScriptSupport) { compiledScript = cachedCompiledScriptSupport.getCachedCompiledScript(); if(compiledScript == null) { Compilable compilableScriptEngine = (Compilable) scriptEngine; compiledScript = compilableScriptEngine.compile(expressionText); cachedCompiledScriptSupport.cacheCompiledScript(compiledScript); } } } return compiledScript.eval(bindings); } else { return scriptEngine.eval(expressionText, bindings); } } catch (ScriptException e) { throw LOG.unableToEvaluateExpression(expressionText, scriptEngine.getFactory().getLanguageName(), e); } }
jsscope = jscx.getBindings( ScriptContext.ENGINE_SCOPE ); jsscope.put( jsScripts[i].getScriptName(), jsScripts[i].getScript() ); for ( int i = 0; i < getAddClasses().length; i++ ) { jsscope.put( getAddClasses()[i].getJSName(), getAddClasses()[i].getAddObject() ); jsscope.put( "SKIP_TRANSFORMATION", Integer.valueOf( Script.SKIP_TRANSFORMATION ) ); jsscope.put( "ABORT_TRANSFORMATION", Integer.valueOf( Script.ABORT_TRANSFORMATION ) ); jsscope.put( "ERROR_TRANSFORMATION", Integer.valueOf( Script.ERROR_TRANSFORMATION ) ); jsscope.put( "CONTINUE_TRANSFORMATION", Integer.valueOf( Script.CONTINUE_TRANSFORMATION ) ); } catch ( Exception ex ) { error_message = "Couldn't add Transformation Constants! Error:" + Const.CR + ex.toString(); jsscope.put( "_step_", dummyStep ); jsscope.put( "rowMeta", prev ); for ( int i = 0; i < prev.size(); i++ ) { ValueMetaInterface valueMeta = prev.getValueMeta( i ); jsscope.put( valueMeta.getName(), valueData ); jsscope.put( "Value", Value.class ); jsscope.put( "row", row ); } catch ( Exception ev ) { error_message = "Couldn't add Input fields to Script! Error:" + Const.CR + ev.toString();
ScriptEngine engine = MANAGER.getEngineByExtension(extension); if(engine==null) { return false; Exception ex = null; Bindings bindings = new BeanLookupBindings(appCtx); bindings.put("rawOut", rawOut); bindings.put("appCtx", appCtx); engine.eval(script, bindings); } catch (IOException e) { ex = e; } finally { bindings.put("rawOut", null); bindings.put("appCtx", null);
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); bindings.remove("print"); if (!loadAllowed) { bindings.remove("load"); bindings.remove("loadWithNewGlobal"); bindings.remove("exit"); bindings.remove("Java"); bindings.remove("readFully"); bindings.remove("readLine"); bindings.remove("print"); bindings.remove("echo"); bindings.remove("quit"); bindings.put("logger", logger); bindings.put("util", JS_UTIL); bindings.put("config", configManager); if (eventStore != null) { bindings.put("$$eventStore", eventStore); engine.eval("var eventStore = {store: function(call) { $$eventStore.store(JSON.stringify(call)); }}"); bindings.put("http", httpClient); engine.eval(code); binding.accept(engine, bindings);