List of usage examples for javax.script Bindings put
public Object put(String name, Object value);
From source file:com.adaptris.core.services.ScriptingServiceImp.java
@Override public final void doService(AdaptrisMessage msg) throws ServiceException { try {/*from w ww .j av a 2s . c o m*/ Bindings vars = engine.createBindings(); vars.put("message", msg); vars.put("log", log); try (Reader input = createReader()) { engine.eval(input, vars); } } catch (Exception e) { throw ExceptionHelper.wrapServiceException(e); } }
From source file:org.nuxeo.ecm.webengine.security.guards.ScriptGuard.java
public boolean check(Adaptable context) { try {/*from w w w .j a v a 2 s . c o m*/ if (engine == null) { comp = compile(type, script); } Bindings bindings = new SimpleBindings(); bindings.put("Context", context); bindings.put("doc", context.getAdapter(DocumentModel.class)); bindings.put("session", context.getAdapter(CoreSession.class)); bindings.put("principal", context.getAdapter(Principal.class)); Object result = null; if (comp != null) { result = comp.eval(bindings); if (result == null) { result = bindings.get("__result__"); } } else { result = engine.eval(new StringReader(script), bindings); } return booleanValue(result); } catch (Exception e) { log.error(e, e); return false; } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GroovyTranslatorTest.java
@Test public void shouldHandleStrategies() throws Exception { final TinkerGraph graph = TinkerFactory.createModern(); GraphTraversalSource g = graph.traversal(); g = g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() { {// w w w.ja v a 2 s . c om put(SubgraphStrategy.VERTICES, __.has("name", "marko")); } }))); final Bindings bindings = new SimpleBindings(); bindings.put("g", g); Traversal.Admin<Vertex, Object> traversal = new GremlinGroovyScriptEngine() .eval(g.V().values("name").asAdmin().getBytecode(), bindings); assertEquals("marko", traversal.next()); assertFalse(traversal.hasNext()); // traversal = new GremlinGroovyScriptEngine() .eval(g.withoutStrategies(SubgraphStrategy.class).V().count().asAdmin().getBytecode(), bindings); assertEquals(new Long(6), traversal.next()); assertFalse(traversal.hasNext()); // traversal = new GremlinGroovyScriptEngine() .eval(g.withStrategies(SubgraphStrategy.create(new MapConfiguration(new HashMap<String, Object>() { { put(SubgraphStrategy.VERTICES, __.has("name", "marko")); } })), ReadOnlyStrategy.instance()).V().values("name").asAdmin().getBytecode(), bindings); assertEquals("marko", traversal.next()); assertFalse(traversal.hasNext()); }
From source file:org.wso2telco.dep.nashornmediator.NashornMediator.java
public boolean mediate(MessageContext messageContext) { try {// w w w . ja v a 2 s. c o m NashornMessageContext nashornMessageContext = new NashornMessageContext(messageContext, scriptEngine, emptyJsonObject); if (isJsonPayloadAware) { processJSONPayload(messageContext, nashornMessageContext); } Bindings bindings = scriptEngine.createBindings(); bindings.put(MC_VAR_NAME, nashornMessageContext); compiledScript.eval(bindings); return true; } catch (ScriptException e) { throw new SynapseException("Error occurred while evaluating script", e); } }
From source file:org.forgerock.openidm.info.impl.InfoService.java
@Override protected void handleRequest(final Context context, final Request request, final Bindings handler) { super.handleRequest(context, request, handler); handler.put("healthinfo", healthInfoSvc.getHealthInfo().asMap()); handler.put("version", object(field("productVersion", ServerConstants.getVersion()), field("productRevision", ServerConstants.getRevision()))); }
From source file:org.trafodion.rest.script.ScriptManager.java
public void runScript(ScriptContext ctx) { String scriptName;/*from w ww . j av a 2 s .c o m*/ if (ctx.getScriptName().length() == 0) scriptName = DEFAULT_SCRIPT_NAME; else if (!ctx.getScriptName().endsWith(".py")) scriptName = ctx.getScriptName() + PYTHON_SUFFIX; else scriptName = ctx.getScriptName(); try { ScriptEngine engine = manager.getEngineByName("python"); Bindings bindings = engine.createBindings(); bindings.put("scriptcontext", ctx); if (engine instanceof Compilable) { CompiledScript script = m.get(scriptName); if (script == null) { LOG.info("Compiling script " + scriptName); Compilable compilingEngine = (Compilable) engine; try { script = compilingEngine.compile(new FileReader(restHome + "/bin/scripts/" + scriptName)); } catch (Exception e) { LOG.warn(e.getMessage()); } m.put(scriptName, script); } script.eval(bindings); } else { try { engine.eval(new FileReader(restHome + "/bin/scripts/" + scriptName), bindings); } catch (Exception e) { LOG.warn(e.getMessage()); } } } catch (javax.script.ScriptException se) { LOG.warn(se.getMessage()); } }
From source file:org.jahia.services.scheduler.JSR223ScriptJob.java
@Override public void executeJahiaJob(JobExecutionContext jobExecutionContext) throws Exception { final JobDataMap map = jobExecutionContext.getJobDetail().getJobDataMap(); String jobScriptPath;// ww w . jav a 2 s. c o m boolean isAbsolutePath = false; if (map.containsKey(JOB_SCRIPT_ABSOLUTE_PATH)) { isAbsolutePath = true; jobScriptPath = map.getString(JOB_SCRIPT_ABSOLUTE_PATH); } else { jobScriptPath = map.getString(JOB_SCRIPT_PATH); } logger.info("Start executing JSR223 script job {}", jobScriptPath); ScriptEngine scriptEngine = ScriptEngineUtils.getInstance() .scriptEngine(FilenameUtils.getExtension(jobScriptPath)); if (scriptEngine != null) { ScriptContext scriptContext = new SimpleScriptContext(); final Bindings bindings = new SimpleBindings(); bindings.put("jobDataMap", map); InputStream scriptInputStream; if (!isAbsolutePath) { scriptInputStream = JahiaContextLoaderListener.getServletContext() .getResourceAsStream(jobScriptPath); } else { scriptInputStream = FileUtils.openInputStream(new File(jobScriptPath)); } if (scriptInputStream != null) { Reader scriptContent = null; try { scriptContent = new InputStreamReader(scriptInputStream); StringWriter out = new StringWriter(); scriptContext.setWriter(out); // The following binding is necessary for Javascript, which doesn't offer a console by default. bindings.put("out", new PrintWriter(scriptContext.getWriter())); scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); scriptContext.setBindings(scriptEngine.getContext().getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE); scriptEngine.eval(scriptContent, scriptContext); map.put(JOB_SCRIPT_OUTPUT, out.toString()); logger.info("...JSR-223 script job {} execution finished", jobScriptPath); } catch (ScriptException e) { logger.error("Error during execution of the JSR-223 script job " + jobScriptPath + " execution failed with error " + e.getMessage(), e); throw new Exception("Error during execution of script " + jobScriptPath, e); } finally { if (scriptContent != null) { IOUtils.closeQuietly(scriptContent); } } } } }
From source file:org.jahia.services.content.nodetypes.initializers.ScriptChoiceListInitializerImpl.java
public List<ChoiceListValue> getChoiceListValues(ExtendedPropertyDefinition epd, String param, List<ChoiceListValue> values, Locale locale, Map<String, Object> context) { if (param != null) { final String extension = Patterns.DOT.split(param)[1]; ScriptEngine byName;//from w ww . ja va2 s.c o m try { byName = scriptEngineUtils.scriptEngine(extension); } catch (ScriptException e) { logger.error(e.getMessage(), e); byName = null; } if (byName != null) { final Set<JahiaTemplatesPackage> forModule = ServicesRegistry.getInstance() .getJahiaTemplateManagerService().getModulesWithViewsForComponent( JCRContentUtils.replaceColon(epd.getDeclaringNodeType().getName())); final Bindings bindings = byName.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("values", values); for (JahiaTemplatesPackage template : forModule) { final Resource scriptPath = template .getResource(File.separator + "scripts" + File.separator + param); if (scriptPath != null && scriptPath.exists()) { Reader scriptContent = null; try { scriptContent = new InputStreamReader(scriptPath.getInputStream()); return (List<ChoiceListValue>) byName.eval(scriptContent, bindings); } catch (ScriptException e) { logger.error("Error while executing script " + scriptPath, e); } catch (IOException e) { logger.error(e.getMessage(), e); } finally { if (scriptContent != null) { IOUtils.closeQuietly(scriptContent); } } } } } } return Collections.emptyList(); }
From source file:org.jahia.services.render.filter.MarkedForDeletionFilter.java
protected String getTemplateOutput(RenderContext renderContext, Resource resource) { String out = StringUtils.EMPTY; try {//from w w w.j av a 2 s.c o m String template = getTemplateContent(); resolvedTemplate = null; if (StringUtils.isEmpty(template)) { return StringUtils.EMPTY; } ScriptEngine engine = ScriptEngineUtils.getInstance().scriptEngine(templateExtension); ScriptContext ctx = new SimpleScriptContext(); ctx.setWriter(new StringWriter()); Bindings bindings = engine.createBindings(); bindings.put("renderContext", renderContext); bindings.put("resource", resource); final ResourceBundle bundle = ResourceBundles.getInternal(renderContext.getUILocale()); bindings.put("bundle", bundle); bindings.put("i18n", LazyMap.decorate(new HashMap<String, String>(2), new Transformer() { public Object transform(Object input) { String value = null; String key = String.valueOf(input); try { value = bundle.getString(key); } catch (MissingResourceException e) { value = key; } return value; } })); ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE); engine.eval(template, ctx); out = ((StringWriter) ctx.getWriter()).getBuffer().toString(); } catch (Exception e) { logger.error(e.getMessage(), e); } return out; }
From source file:org.tomitribe.tribestream.registryng.bootstrap.Provisioning.java
@PostConstruct public void init() { loginContext.setUsername("system"); ofNullable(script).ifPresent(s -> { final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js"); final Bindings bindings = engine.createBindings(); bindings.put("props", System.getProperties()); final File file = new File(s); if (file.isFile()) { try (final Reader reader = new FileReader(file)) { engine.eval(reader, bindings); } catch (final IOException | ScriptException e) { throw new IllegalArgumentException(e); }/*www . java 2s . co m*/ } else { try { engine.eval(s, bindings); } catch (final ScriptException e) { throw new IllegalArgumentException(e); } } }); restore(); }