List of usage examples for javax.script ScriptContext getBindings
public Bindings getBindings(int scope);
Bindings
associated with the given scope in this ScriptContext
. From source file:org.fit.cssbox.scriptbox.demo.tester.ConsoleInjector.java
@Override public boolean inject(ScriptContext context) { Console console = new Console(textPane); Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("console", console); return true;//from www . j a v a 2s . c o m }
From source file:org.icesquirrel.jsr223.IcesquirrelScriptEngine.java
@Override public Object eval(Reader reader, ScriptContext context) throws ScriptException { SquirrelInterpretedScript sis = new SquirrelInterpretedScript(); /*//w ww . ja va 2 s.c o m * If the global bindings is already a SquirrelTable, use that directly. * A squirrel table is kind of compatible with a bindings object, except * the key may be an Object instead of a string. */ Bindings bindings = context.getBindings(ScriptContext.GLOBAL_SCOPE); if (bindings instanceof BindingsAdapter) { sis.setRootTable(((BindingsAdapter) bindings).getTable()); } else { // Otherwise copy global bindings into our root table for (Entry<String, Object> s : bindings.entrySet()) { bind(sis, s); } } // Now the engine scoped bindings. If these are already a squirrel // table, add them as // a delegate, otherwise copy Bindings engBindings = context.getBindings(ScriptContext.ENGINE_SCOPE); SquirrelTable addedDelegate = null; if (engBindings instanceof BindingsAdapter) { BindingsAdapter ba = (BindingsAdapter) engBindings; if (ba.getTable() != sis.getRootTable()) { SquirrelTable pt = sis.setRootTable(ba.getTable()); if (pt != null && !ba.getTable().getDelegates().contains(pt)) { ba.getTable().addDelegate(pt); addedDelegate = pt; } } } else { for (Entry<String, Object> s : engBindings.entrySet()) { bind(sis, s); } } SquirrelRuntime runtime = SquirrelRuntime.getDefaultRuntime(); SquirrelExecutionContext ctx = null; if (!SquirrelExecutionContext.is()) { ctx = new SquirrelExecutionContext(); ctx.start(sis.getRootTable(), runtime, new SquirrelInterpretedProcessor()); } try { try { // TODO encoding sis.execute(new ReaderInputStream(reader)); } catch (SquirrelException se) { // TODO line number etc throw new ScriptException(se); } catch (Exception e) { // TODO line number etc throw new ScriptException(e); } Object result = sis.getResult(); if (result instanceof SquirrelObjectWrapper) { return ((SquirrelObjectWrapper) result).getObject(); } return result; } finally { if (addedDelegate != null) { sis.getRootTable().removeDelegate(addedDelegate); } if (ctx != null) { ctx.stop(); } } }
From source file:org.jahia.modules.docrules.EmailDocumentRule.java
private String evaluate(String subject, JCRNodeWrapper document) { if (subject.contains("${")) { try {//from www . j a v a 2s .c o m ScriptEngine byName = ScriptEngineUtils.getInstance().getEngineByName("velocity"); ScriptContext scriptContext = byName.getContext(); final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("document", document); scriptContext.setWriter(new StringWriter()); scriptContext.setErrorWriter(new StringWriter()); byName.eval(subject, bindings); return scriptContext.getWriter().toString().trim(); } catch (ScriptException e) { logger.error("Error while evaluating value [" + subject + "]", e); } } return null; }
From source file:org.jahia.services.workflow.jbpm.custom.email.JBPMMailProducer.java
protected String evaluateExpression(WorkItem workItem, String scriptToExecute, JCRSessionWrapper session) throws RepositoryException, ScriptException { ScriptContext scriptContext = new SimpleScriptContext(); if (bindings == null) { bindings = getBindings(workItem, session); }/*w w w. ja v a2 s . c o m*/ scriptContext.setWriter(new StringWriter()); scriptContext.setErrorWriter(new StringWriter()); scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); scriptContext.setBindings(scriptContext.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE); scriptEngine.eval(scriptToExecute, scriptContext); String error = scriptContext.getErrorWriter().toString(); if (error.length() > 0) { logger.error("Scripting error : " + error); } return scriptContext.getWriter().toString().trim(); }
From source file:org.ow2.parserve.PARServeEngine.java
@Override public Object eval(String script, ScriptContext ctx) throws ScriptException { // Transfer all bindings from context into the rengine env if (ctx == null) { throw new ScriptException("No script context specified"); }//ww w.j a v a 2 s . co m Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE); if (bindings == null) { throw new ScriptException("No bindings specified in the script context"); } serverEval = false; Map<String, Serializable> jobVariables = (Map<String, Serializable>) bindings .get(SchedulerConstants.VARIABLES_BINDING_NAME); if (jobVariables != null) { serverEval = "true".equals(jobVariables.get(PARSERVE_SERVEREVAL)); } Map<String, String> resultMetadata = (Map<String, String>) bindings .get(SchedulerConstants.RESULT_METADATA_VARIABLE); engine = new PARServeConnection(Rsession.newInstanceTry("Script", rServeConf), serverEval); try { initializeTailer(bindings, ctx); Object resultValue = null; if (!serverEval) { prepareExecution(ctx, bindings); } // if there is an exception during the parsing, a ScriptException is immediately thrown engine.checkParsing(script, ctx); // otherwise each step is followed till the end REXP rexp = engine.engineEval(script, ctx); resultValue = retrieveResultVariable(ctx, bindings, rexp); retrieveOtherVariable(SelectionScript.RESULT_VARIABLE, ctx, bindings); retrieveOtherVariable(FlowScript.loopVariable, ctx, bindings); retrieveOtherVariable(FlowScript.branchSelectionVariable, ctx, bindings); retrieveOtherVariable(FlowScript.replicateRunsVariable, ctx, bindings); if (!serverEval) { this.updateJobVariables(jobVariables, ctx); this.updateResultMetadata(resultMetadata, ctx); } // server evaluation is for one task only, it must not be propagated if (serverEval) { jobVariables.put(PARSERVE_SERVEREVAL, "false"); } return resultValue; } catch (Exception ex) { engine.writeExceptionToError(ex, ctx); throw new ScriptException(ex.getMessage()); } finally { engine.terminateOutput(ctx); if (!serverEval) { engine.engineEval("setwd('" + Utils.toRpath(System.getProperty("java.io.tmpdir")) + "')", ctx); } engine.end(); terminateTailer(); if (!serverEval) { // PRC-32 A ScriptException() must be thrown if the script calls stop() function ScriptException toThrow = null; if (lastErrorMessage != null) { toThrow = new ScriptException(lastErrorMessage); } if (toThrow != null) { throw toThrow; } } } }
From source file:org.siphon.common.js.JsEngineUtil.java
public static Object getGlobal(ScriptEngine engine) { ScriptContext context = engine.getContext(); ScriptObjectMirror binding = (ScriptObjectMirror) context.getBindings(ScriptContext.ENGINE_SCOPE); Field globalField = null;// w w w. j a v a 2 s . c o m try { globalField = ScriptObjectMirror.class.getDeclaredField("global"); globalField.setAccessible(true); Object global = globalField.get(binding); return global; } catch (NoSuchFieldException e) { } catch (SecurityException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } return null; }