List of usage examples for javax.script Bindings entrySet
Set<Map.Entry<K, V>> entrySet();
From source file:nz.net.orcon.kanban.automation.plugin.ScriptPlugin.java
@Override public Map<String, Object> process(Action action, Map<String, Object> context) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); for (Entry<String, Object> entry : context.entrySet()) { if (StringUtils.isNotBlank(entry.getKey())) { engine.put(entry.getKey(), entry.getValue()); }//from w ww . jav a 2 s . c o m } if (action.getProperties() != null) { for (Entry<String, String> entry : action.getProperties().entrySet()) { if (StringUtils.isNotBlank(entry.getKey())) { engine.put(entry.getKey(), entry.getValue()); } } } engine.put("lists", listController); engine.put("log", log); String script = null; if (StringUtils.isNotBlank(action.getResource())) { script = resourceController.getResource((String) context.get("boardid"), action.getResource()); } else { script = action.getMethod(); } engine.eval(script); Bindings resultBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); for (Entry<String, Object> entry : resultBindings.entrySet()) { if (entry.getKey().equals("context") || entry.getKey().equals("print") || entry.getKey().equals("println")) { continue; } context.put(entry.getKey(), entry.getValue()); } return context; }
From source file:com.tussle.script.StackedBindings.java
public void putOverAll(Bindings values) { for (Map.Entry<? extends String, ? extends Object> entry : values.entrySet()) { putOver(entry.getKey(), entry.getValue()); }//ww w . j a va 2 s . co m }
From source file:com.seajas.search.contender.scripting.FeedScriptsTestBase.java
/** * Evaluates the script.//from w w w. ja v a 2 s. c om */ protected SyndFeed run(final String feedUri, final String scriptFile) throws Exception { final ScriptEngine engine = engineManager.getEngineByMimeType("text/javascript"); injectionService = new MockInjectionService(); subject = new FeedScriptEvaluation(new WebResolverSettings()); subject.setScriptResolver(new ScriptCacheResolver<FeedScript>() { @Override public FeedScript resolve(final Bindings bindings) throws ScriptException { try { for (Map.Entry<String, Object> entry : bindings.entrySet()) engine.put(entry.getKey(), entry.getValue()); engine.eval(readFileToString(new File("src/main/scripts/feed/" + scriptFile), "UTF-8")); return ((Invocable) engine).getInterface(FeedScript.class); } catch (IOException e) { throw new IllegalArgumentException("Invalid script", e); } } }); if (scriptFile.endsWith(".js")) subject.setEngine(engine); else throw new IllegalArgumentException("Unknown script file extension."); subject.setFeedContent(samples.values().iterator().next()); subject.setWebPages(new WebPages()); subject.setWebResolver(new WebResolver() { @Override public String retrieveText(URI resource) { return samples.get(resource.toString()); } }); subject.setFeedURI(new URI(feedUri)); subject.setCacheResolver(new FeedScriptCacheResolver() { @Override public boolean isCached(String resource) { return false; } }); subject.setInjectionService(injectionService); subject.init(); subject.run(); SyndFeed feed = subject.getFeed(); assertNotNull("feed", feed); // The validated feed must be serializable: SyndFeed result = (SyndFeed) feed.clone(); WebFeeds.validate(result, new URI(feedUri)); new SyndFeedOutput().outputString(result, true); return feed; }
From source file:com.tussle.script.StackedBindings.java
public void push(Bindings values) { bindingStack.push(new SimpleBindings(values)); for (Map.Entry<? extends String, ? extends Object> entry : values.entrySet()) { putOver(entry.getKey(), entry.getValue()); }//from w ww. ja va 2 s . co m }
From source file:com.seajas.search.contender.service.modifier.ModifierScriptProcessor.java
/** * Evaluate the given script./*from w w w . j a va 2 s.c o m*/ * * @param settings * @param script * @param input * @param uri * @return String * @throws ScriptException */ private String evaluateScript(final WebResolverSettings settings, final ModifierScript script, String input, URI uri) throws ScriptException { final ScriptCacheEntry entry = scriptCache.acquireScript(script); try { final ScriptEngine engine = entry != null ? entry.getScript().getEngine() : engineManager.getEngineByName(script.getScriptLanguage().toLowerCase()); FeedScriptEvaluation evaluation = new FeedScriptEvaluation(settings); evaluation.setScriptResolver(new ScriptCacheResolver<FeedScript>() { @Override public FeedScript resolve(final Bindings bindings) throws ScriptException { for (Map.Entry<String, Object> binding : bindings.entrySet()) engine.put(binding.getKey(), binding.getValue()); if (entry != null) { if (logger.isTraceEnabled()) logger.trace("Executing compiled script with ID " + script.getId()); entry.getScript().eval(); } else { if (logger.isTraceEnabled()) logger.trace(String.format("Executing non-compiled script with ID %d and content '%s'", script.getId(), script.getScriptContent())); engine.eval(script.getScriptContent()); } return ((Invocable) engine).getInterface(FeedScript.class); } }); evaluation.setFeedURI(uri); evaluation.setFeedContent(input); evaluation.setEngine(engine); evaluation.setCacheResolver(new FeedScriptCacheResolver() { @Override public boolean isCached(final String resource) { logger.info("Cache requested URI: " + resource); String cacheKey = cacheService.createCompositeKey(resource, settings.getResultParameters()); return cacheService.isCached(cacheKey); } }); evaluation.setWebPages(new WebPages()); DefaultWebResolver resolver = new DefaultWebResolver(); resolver.setSettings(settings); resolver.setContenderService(contenderService); evaluation.setWebResolver(resolver); evaluation.init(); evaluation.run(); SyndFeed feed = evaluation.getFeed(); if (feed == null) throw new ScriptException("No result available"); try { WebFeeds.validate(feed, uri); SyndFeedOutput serializer = new SyndFeedOutput(); return serializer.outputString(feed, true); } catch (FeedException e) { throw new ScriptException("Can't serialize feed result: " + e); } } finally { scriptCache.releaseScript(script, entry); } }
From source file:org.graphwalker.machines.ExtendedFiniteStateMachine.java
public Set<Entry<String, Object>> getCurrentJsEngineData() { Bindings bindings = jsEngine.getBindings(ScriptContext.ENGINE_SCOPE); return bindings.entrySet(); }
From source file:org.apache.sling.scripting.javascript.internal.RhinoJavaScriptEngine.java
private Map<String, Object> setBoundProperties(Scriptable scope, Bindings bindings) { Map<String, Object> replacedProperties = new HashMap<String, Object>(); for (Object entryObject : bindings.entrySet()) { Entry<?, ?> entry = (Entry<?, ?>) entryObject; String name = (String) entry.getKey(); Object value = entry.getValue(); if (value != null) { // get the current property value, if set if (ScriptableObject.hasProperty(scope, name)) { replacedProperties.put(name, ScriptableObject.getProperty(scope, name)); }/* ww w .j a v a 2 s . c om*/ // wrap the new value and set it Object wrapped = ScriptRuntime.toObject(scope, value); ScriptableObject.putProperty(scope, name, wrapped); } } return replacedProperties; }
From source file:org.icesquirrel.jsr223.IcesquirrelScriptEngine.java
@Override public Object eval(Reader reader, ScriptContext context) throws ScriptException { SquirrelInterpretedScript sis = new SquirrelInterpretedScript(); /*//ww w .j av a 2 s . com * 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(); } } }