List of usage examples for javax.script SimpleBindings SimpleBindings
public SimpleBindings()
From source file:org.jahia.services.render.scripting.bundle.BundleScriptEngineManager.java
private BundleScriptEngineManager() { this.globalScopeBindings = new SimpleBindings(); }
From source file:org.apache.tinkerpop.gremlin.groovy.engine.ScriptEnginesTest.java
@Test public void shouldMergeBindingsWhereLocalOverridesGlobal() throws Exception { final ScriptEngines engines = new ScriptEngines(se -> { });/*from w ww . j av a 2 s.c om*/ engines.reload("gremlin-groovy", Collections.<String>emptySet(), Collections.<String>emptySet(), Collections.emptyMap()); engines.loadPlugins(Arrays.asList(new GremlinPlugin() { @Override public String getName() { return "mock"; } @Override public void pluginTo(final PluginAcceptor pluginAcceptor) throws IllegalEnvironmentException, PluginInitializationException { pluginAcceptor.addBinding("y", "here"); } })); // the "y" below should override the global variable setting. final Bindings localBindings = new SimpleBindings(); localBindings.put("y", "there"); localBindings.put("z", "where"); assertEquals("therewhere", engines.eval("y+z", localBindings, "gremlin-groovy")); }
From source file:org.jahia.modules.macros.filter.MacrosFilter.java
private Bindings getBindings(RenderContext renderContext, Resource resource, ScriptContext scriptContext, Matcher matcher) {//from w w w.ja v a 2s . c o m Bindings bindings = new SimpleBindings(); bindings.put("currentUser", renderContext.getUser()); bindings.put("currentNode", resource.getNode()); bindings.put("currentResource", resource); bindings.put("renderContext", renderContext); bindings.put("url", new URLGenerator(renderContext, resource)); String group = matcher.group(3); if (group != null) { int i = 1; for (String s : StringUtils.split(group, ",")) { bindings.put("param" + (i++), s); } } try { bindings.put("currentAliasUser", renderContext.getMainResource().getNode().getSession().getAliasedUser()); } catch (RepositoryException e) { logger.error(e.getMessage(), e); } return bindings; }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineTest.java
@Test public void shouldEvalWithBindings() throws Exception { final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(); final Bindings b = new SimpleBindings(); b.put("x", 2); assertEquals(3, engine.eval("1+x", b)); }
From source file:Engine.Lua.PlayerLua.java
public void tester(String luaFile) { Dog dog = new Dog("Rex"); Cat cat = new Cat("Felix"); ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine scriptEngine = sem.getEngineByName("luaj"); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream("Scripts/" + luaFile); InputStreamReader isr = new InputStreamReader(is); CompiledScript script;//from ww w . java2 s .c om try { script = ((Compilable) scriptEngine).compile(isr); isr.close(); is.close(); Bindings sb = new SimpleBindings(); script.eval(sb); // Put the Lua functions into the sb environment LuaValue luaDog = CoerceJavaToLua.coerce(dog); // Java to Lua //CoerceLuaToJava() LuaFunction onTalk = (LuaFunction) sb.get("onTalk"); // Get Lua function LuaValue b = onTalk.call(luaDog); // Call the function System.out.println("onTalk answered: " + b); LuaFunction onWalk = (LuaFunction) sb.get("onWalk"); LuaValue[] dogs = { luaDog }; Varargs dist = onWalk.invoke(LuaValue.varargsOf(dogs)); // Alternative System.out.println("onWalk returned: " + dist); Dog retunredDog = (Dog) CoerceLuaToJava.coerce(luaDog, Dog.class); System.out.println("AAAAAAAAAa:" + retunredDog.name); } catch (ScriptException ex) { Logger.getLogger(PlayerLua.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PlayerLua.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorTest.java
@Test public void shouldEvalScriptWithBindings() throws Exception { final GremlinExecutor gremlinExecutor = GremlinExecutor.build().create(); final Bindings b = new SimpleBindings(); b.put("x", 1); assertEquals(2, gremlinExecutor.eval("1+x", b).get()); gremlinExecutor.close();/*from w w w . j a va 2s .c om*/ }
From source file:com.netflix.genie.web.services.loadbalancers.script.ScriptLoadBalancer.java
/** * {@inheritDoc}//from w ww .ja v a 2s. c o m */ @Override public Cluster selectCluster(@Nonnull @NonNull @NotEmpty final Set<Cluster> clusters, @Nonnull @NonNull final JobRequest jobRequest) throws GenieException { final long selectStart = System.nanoTime(); log.debug("Called"); final Set<Tag> tags = Sets.newHashSet(); try { if (this.isConfigured.get() && this.script.get() != null) { log.debug("Evaluating script for job {}", jobRequest.getId().orElse("without id")); final Bindings bindings = new SimpleBindings(); // TODO: For now for backwards compatibility with balancer scripts continue writing Clusters out in // V3 format. Change to V4 once stabalize a bit more bindings.put(CLUSTERS_BINDING, this.mapper.writeValueAsString( clusters.stream().map(DtoConverters::toV3Cluster).collect(Collectors.toSet()))); bindings.put(JOB_REQUEST_BINDING, this.mapper.writeValueAsString(jobRequest)); // Run as callable and timeout after the configured timeout length final String clusterId = this.asyncTaskExecutor .submit(() -> (String) this.script.get().eval(bindings)) .get(this.timeoutLength.get(), TimeUnit.MILLISECONDS); // Find the cluster if not null if (clusterId != null) { for (final Cluster cluster : clusters) { if (clusterId.equals(cluster.getId())) { tags.add(Tag.of(MetricsConstants.TagKeys.STATUS, STATUS_TAG_FOUND)); return cluster; } } } log.warn("Script returned a cluster not in the input list: {}", clusterId); } else { log.debug("Script returned null"); tags.add(Tag.of(MetricsConstants.TagKeys.STATUS, STATUS_TAG_NOT_CONFIGURED)); return null; } tags.add(Tag.of(MetricsConstants.TagKeys.STATUS, STATUS_TAG_NOT_FOUND)); // Defer to any subsequent load balancer in the chain return null; } catch (final Exception e) { tags.add(Tag.of(MetricsConstants.TagKeys.STATUS, STATUS_TAG_FAILED)); tags.add(Tag.of(MetricsConstants.TagKeys.EXCEPTION_CLASS, e.getClass().getCanonicalName())); log.error("Unable to execute script due to {}", e.getMessage(), e); return null; } finally { this.registry.timer(SELECT_TIMER_NAME, tags).record(System.nanoTime() - selectStart, TimeUnit.NANOSECONDS); } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineTest.java
@Test public void shouldEvalWithNullInBindings() throws Exception { final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(); final Bindings b = new SimpleBindings(); b.put("x", null); assertNull(engine.eval("x", b)); }
From source file:com.tussle.script.StackedBindings.java
public void push() { bindingStack.push(new SimpleBindings()); }
From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor.java
/** * Evaluate a script with empty bindings. *//*from www .j a v a 2 s . c o m*/ public CompletableFuture<Object> eval(final String script) { return eval(script, null, new SimpleBindings()); }