List of usage examples for javax.script ScriptContext getAttribute
public Object getAttribute(String name);
From source file:Main.java
public static void main(String[] args) { ScriptContext ctx = new SimpleScriptContext(); List<Integer> scopes = ctx.getScopes(); System.out.println("Supported Scopes: " + scopes); ctx.setAttribute("year", 2015, ENGINE_SCOPE); Bindings globalBindings = new SimpleBindings(); ctx.setBindings(globalBindings, GLOBAL_SCOPE); ctx.setAttribute("year", 2014, GLOBAL_SCOPE); ctx.setAttribute("name", "Jack", GLOBAL_SCOPE); String nameValue = (String) ctx.getAttribute("name"); System.out.println("nameValue = " + nameValue); int engineScopeYear = (Integer) ctx.getAttribute("year", ENGINE_SCOPE); int globalScopeYear = (Integer) ctx.getAttribute("year", GLOBAL_SCOPE); System.out.println("engineScopeYear = " + engineScopeYear); System.out.println("globalScopeYear = " + globalScopeYear); }
From source file:com.googlecode.starflow.core.script.spel.SpelScriptEngine.java
private StandardEvaluationContext getStandardEvaluationContext(ScriptContext scriptContext) { StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext( scriptContext.getAttribute(ROOT_OBJECT)); standardEvaluationContext.setVariables(getVariables(scriptContext)); return standardEvaluationContext; }
From source file:io.cloudslang.lang.runtime.bindings.ScriptEvaluator.java
public Serializable evalExpr(String expr, Map<String, ? extends Serializable> context) { ScriptContext scriptContext = new SimpleScriptContext(); for (Map.Entry<String, ? extends Serializable> entry : context.entrySet()) { scriptContext.setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE); }//from w ww .j a va 2 s .c o m if (scriptContext.getAttribute(TRUE) == null) scriptContext.setAttribute(TRUE, Boolean.TRUE, ScriptContext.ENGINE_SCOPE); if (scriptContext.getAttribute(FALSE) == null) scriptContext.setAttribute(FALSE, Boolean.FALSE, ScriptContext.ENGINE_SCOPE); try { return (Serializable) engine.eval(expr, scriptContext); } catch (ScriptException e) { ScriptException scriptException = new ScriptException(e); throw new RuntimeException("Error in running script expression or variable reference, for expression: '" + expr + "',\n\tScript exception is: " + scriptException.getMessage(), scriptException); } }
From source file:org.openscore.lang.runtime.bindings.ScriptEvaluator.java
public Serializable evalExpr(String expr, Map<String, ? extends Serializable> context) { ScriptContext scriptContext = new SimpleScriptContext(); for (Map.Entry<String, ? extends Serializable> entry : context.entrySet()) { scriptContext.setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE); }/*from ww w .jav a 2s . c o m*/ if (scriptContext.getAttribute(TRUE) == null) scriptContext.setAttribute(TRUE, Boolean.TRUE, ScriptContext.ENGINE_SCOPE); if (scriptContext.getAttribute(FALSE) == null) scriptContext.setAttribute(FALSE, Boolean.FALSE, ScriptContext.ENGINE_SCOPE); try { return (Serializable) engine.eval(expr, scriptContext); } catch (ScriptException e) { ScriptException scriptException = new ScriptException(e); throw new RuntimeException("Error in running script expression or variable reference, for expression: '" + expr + "', Script exception is: \n" + scriptException.getMessage(), scriptException); } }
From source file:edu.toronto.cs.phenotips.tools.PhenotypeMappingService.java
private Map<String, Object> parseGroovyMapping(DocumentReference mappingDoc) { ScriptEngine e = this.groovy.getScriptEngine(); ScriptContext c = this.scmanager.getScriptContext(); try {//from w w w . j av a 2 s .c o m e.eval(this.bridge.getDocumentContentForDefaultLanguage(mappingDoc), c); } catch (Exception ex) { this.logger.error("Failed to parse mapping document [{}]", mappingDoc, ex); return null; } this.observationManager.addEvent(this.getName(), new DocumentUpdatedEvent(mappingDoc)); this.observationManager.addEvent(this.getName(), new DocumentDeletedEvent(mappingDoc)); @SuppressWarnings("unchecked") Map<String, Object> mappings = (Map<String, Object>) c.getAttribute("mappings"); setMappings(mappingDoc, mappings); return mappings; }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.java
private Object callGlobal(final String name, final Object args[], final ScriptContext ctx) { final Closure closure = globalClosures.get(name); if (closure != null) { return closure.call(args); }/*from w w w . j a v a 2 s . co m*/ final Object value = ctx.getAttribute(name); if (value instanceof Closure) { return ((Closure) value).call(args); } else { throw new MissingMethodException(name, getClass(), args); } }
From source file:org.ow2.proactive.scheduler.task.java.JavaClassScriptEngine.java
@Override public Object eval(String userExecutableClassName, ScriptContext context) throws ScriptException { try {//from ww w. j a v a2 s . c o m JavaExecutable javaExecutable = getExecutable(userExecutableClassName); JavaStandaloneExecutableInitializer execInitializer = new JavaStandaloneExecutableInitializer(); PrintStream output = new PrintStream(new WriterOutputStream(context.getWriter()), true); execInitializer.setOutputSink(output); PrintStream error = new PrintStream(new WriterOutputStream(context.getErrorWriter()), true); execInitializer.setErrorSink(error); Map<String, byte[]> propagatedVariables = null; if (context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME) != null) { propagatedVariables = SerializationUtil.serializeVariableMap( ((VariablesMap) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)) .getPropagatedVariables()); execInitializer.setPropagatedVariables(propagatedVariables); } else { execInitializer.setPropagatedVariables(Collections.<String, byte[]>emptyMap()); } if (context.getAttribute(Script.ARGUMENTS_NAME) != null) { execInitializer.setSerializedArguments( (Map<String, byte[]>) ((Serializable[]) context.getAttribute(Script.ARGUMENTS_NAME))[0]); } else { execInitializer.setSerializedArguments(Collections.<String, byte[]>emptyMap()); } if (context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE) != null) { execInitializer.setThirdPartyCredentials( (Map<String, String>) context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE)); } else { execInitializer.setThirdPartyCredentials(Collections.<String, String>emptyMap()); } if (context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME) != null) { List<String> nodesURLs = (List<String>) context .getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME); execInitializer.setNodesURL(nodesURLs); } else { execInitializer.setNodesURL(Collections.<String>emptyList()); } javaExecutable.internalInit(execInitializer, context); Serializable execute = javaExecutable .execute((TaskResult[]) context.getAttribute(SchedulerConstants.RESULTS_VARIABLE)); if (propagatedVariables != null) { ((Map<String, Serializable>) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)) .putAll(javaExecutable.getVariables()); } output.close(); error.close(); return execute; } catch (Throwable e) { throw new ScriptException(new TaskException(getStackTraceAsString(e), e)); } }