Example usage for javax.script SimpleBindings SimpleBindings

List of usage examples for javax.script SimpleBindings SimpleBindings

Introduction

In this page you can find the example usage for javax.script SimpleBindings SimpleBindings.

Prototype

public SimpleBindings() 

Source Link

Document

Default constructor uses a HashMap .

Usage

From source file:org.apache.sling.scripting.sightly.js.impl.jsapi.ProxyAsyncScriptableFactory.java

public void registerProxies(Bindings bindings) {
    slyBindingsValuesProvider.initialise(bindings);
    Bindings bindingsCopy = new SimpleBindings();
    for (String factoryName : slyBindingsValuesProvider.getScriptPaths().keySet()) {
        ShadowScriptableObject shadowScriptableObject = new ShadowScriptableObject(factoryName, bindingsCopy);
        bindings.put(factoryName, shadowScriptableObject);
    }//from   w  w  w.  j  av a 2 s  .  co  m
    bindingsCopy.putAll(bindings);
}

From source file:org.onesec.raven.ivr.queue.actions.RegisterOperatorActionNode.java

@Override
public Collection<Node> getEffectiveChildrens() {
    if (getStatus() != Node.Status.STARTED)
        return null;
    Bindings bindings = new SimpleBindings();
    formExpressionBindings(bindings);//  w  w  w  . j a  v  a  2 s.co  m
    List<String> dtmfs = (List<String>) bindings.get(IvrEndpointConversation.DTMFS_BINDING);
    String operatorCode = StringUtils.join(dtmfs, "");
    String operatorNumber = (String) bindings.get(IvrEndpointConversation.NUMBER_BINDING);
    OperatorRegistratorNode auth = callsQueues.getOperatorRegistrator();
    OperatorDesc operator;
    if (operatorCode == null || operatorCode.isEmpty()
            || (operator = auth.register(operatorNumber, operatorCode)) == null) {
        if (isLogLevelEnabled(LogLevel.WARN))
            getLogger().warn("Invalid authentication code {} for operator number {}", operatorCode,
                    operatorNumber);
        return null;
    } else {
        if (isLogLevelEnabled(LogLevel.DEBUG))
            getLogger().debug("Operator successfully authenticated. {}; number - ({})", operator,
                    operatorNumber);
        getConversationState(bindings).setBinding(OPERATOR_BINDING, operator, BindingScope.CONVERSATION);
        return super.getEffectiveChildrens();
    }
}

From source file:org.nuxeo.ecm.webengine.security.guards.ScriptGuard.java

public boolean check(Adaptable context) {
    try {/*from  w  w  w. ja va 2s .  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.engine.ScriptEnginesTest.java

@Test
public void shouldNotPreserveInstantiatedVariablesBetweenEvals() throws Exception {
    final ScriptEngines engines = new ScriptEngines(se -> {
    });// ww w .  j ava 2s.c  o  m
    engines.reload("gremlin-groovy", Collections.<String>emptySet(), Collections.<String>emptySet(),
            Collections.emptyMap());

    final Bindings localBindingsFirstRequest = new SimpleBindings();
    localBindingsFirstRequest.put("x", "there");
    assertEquals("herethere", engines.eval("z = 'here' + x", localBindingsFirstRequest, "gremlin-groovy"));

    try {
        final Bindings localBindingsSecondRequest = new SimpleBindings();
        engines.eval("z", localBindingsSecondRequest, "gremlin-groovy");
        fail("Should not have knowledge of z");
    } catch (Exception ex) {
        final Throwable root = ExceptionUtils.getRootCause(ex);
        assertThat(root, instanceOf(MissingPropertyException.class));
    }
}

From source file:org.nuxeo.connect.connector.http.proxy.NashornProxyPacResolver.java

@Override
public String[] findPacProxies(String url) {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    ScriptContext context = new SimpleScriptContext();
    engine.setContext(context); // set as default context, for invokeFunctino
    SimpleBindings bindings = new SimpleBindings();
    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    try {/*from   w w  w .ja va2 s  .c  om*/
        // Register internet functions as Java upcalls
        engine.eval("var NashornProxyPacResolver = Java.type('" + getClass().getName() + "');"
                + "var dnsResolve = NashornProxyPacResolver.dnsResolve;"
                + "var myIpAddress = NashornProxyPacResolver.myIpAddress");
        // Register others pac methods
        engine.eval(getFileReader(PAC_FUNCTIONS_FILE));
        // Register remote pac function
        engine.eval(getRemotePacBodyReader());
        // Call and return pac resolution function
        String proxies = (String) ((Invocable) engine).invokeFunction(EXEC_PAC_FUNC, url, getHost(url));
        return proxies.split(";");
    } catch (IOException | ScriptException | NoSuchMethodException e) {
        log.warn(e, e);
    }
    return null;
}

From source file:org.apache.sling.scripting.sightly.js.impl.JsEnvironment.java

public JsEnvironment(ScriptEngine jsEngine) {
    this.jsEngine = jsEngine;
    engineBindings = new SimpleBindings();
    TimingBindingsValuesProvider.INSTANCE.addBindings(engineBindings);
}

From source file:JrubyTest.java

/**
 * ?Excel????/*from  w  ww.j a  va  2 s.co  m*/
 *
 * @throws Exception
 */
@Test
public void parseTestSpecification() throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("jruby");
    Reader scriptReader = null;
    InputStream in = null;
    try {
        in = getClass().getClassLoader().getResourceAsStream("test-specifications.xls");
        Workbook workbook = load(in);
        Book book = new Book(workbook);
        scriptReader = new InputStreamReader(
                getClass().getClassLoader().getResourceAsStream("test-specification-parser.rb"));
        if (engine instanceof Compilable) {
            CompiledScript script = ((Compilable) engine).compile(scriptReader);
            SimpleBindings bindings = new SimpleBindings();
            bindings.put("@book", book);
            script.eval(bindings);
        }
    } finally {
        IOUtils.closeQuietly(scriptReader);
        IOUtils.closeQuietly(in);
    }

}

From source file:org.nuxeo.usermapper.extension.NashornUserMapper.java

@Override
protected void resolveAttributes(Object userObject, Map<String, Serializable> searchAttributes,
        Map<String, Serializable> userAttributes, Map<String, Serializable> profileAttributes) {
    Bindings bindings = new SimpleBindings();
    bindings.put("searchAttributes", searchAttributes);
    bindings.put("profileAttributes", profileAttributes);
    bindings.put("userAttributes", userAttributes);
    bindings.put("userObject", userObject);

    try {/*from  w ww  .  j  a  va  2s .  c o m*/
        engine.eval(mapperSource, bindings);
    } catch (ScriptException e) {
        log.error("Error while executing JavaScript mapper", e);
    }
}

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>() {
        {//www .j  a va2  s. c  o  m
            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:com.tussle.script.SubactionScriptSystem.java

public SubactionScriptSystem(int i) {
    super(Family.all(ScriptContextComponent.class).get(), i);
    PipeBufferWriter warnStream = new PipeBufferWriter();
    processingErrStream = new PipeBufferWriter();
    stdInProcessor = new JsonDistributingWriter(warnStream);
    stdOutProcessor = new JsonCollectingWriter(warnStream.getNewReader());
    stdErrProcessor = new JsonCollectingWriter(processingErrStream.getNewReader());

    stdinInterpreter = new JsonParsingWriter(processingErrStream);
    destructionSignaller = new Signal<>();
    entityContexts = LazyMap.lazyMap(new HashMap<>(),
            (Entity ent) -> new EntityActionContext(subactionInterpreter.getContext(), new SimpleBindings(), //TODO: Insert API bindings
                    new SimpleBindings(), new SimpleBindings(), stdInProcessor.openReaderFor(ent),
                    stdOutProcessor.openWriterFor(ent), stdErrProcessor.openWriterFor(ent)));
}