Example usage for javax.script Bindings containsKey

List of usage examples for javax.script Bindings containsKey

Introduction

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

Prototype

public boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:Main.java

public static void main(String[] args) {
    Bindings params = new SimpleBindings();
    params.put("stringKey", "Hello");
    params.put("valueKey", 2015);

    Object msg = params.get("stringKey");
    Object year = params.get("valueKey");
    System.out.println("stringKey" + msg);
    System.out.println("valueKey = " + year);

    params.remove("valueKey");
    year = params.get("valueKey");

    boolean containsYear = params.containsKey("valueKey");
    System.out.println("valueKey = " + year);
    System.out.println("params contains year = " + containsYear);
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    Bindings params = engine.createBindings();

    params.put("stringKey", "Hello");
    params.put("valueKey", 2015);

    Object msg = params.get("stringKey");
    Object year = params.get("valueKey");
    System.out.println("stringKey" + msg);
    System.out.println("valueKey = " + year);

    params.remove("valueKey");
    year = params.get("valueKey");

    boolean containsYear = params.containsKey("valueKey");
    System.out.println("valueKey = " + year);
    System.out.println("params contains year = " + containsYear);
}

From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.java

@Override
public Traversal.Admin eval(final Bytecode bytecode, final Bindings bindings, final String traversalSource)
        throws ScriptException {
    // these validations occur before merging in bytecode bindings which will override existing ones. need to
    // extract the named traversalsource prior to that happening so that bytecode bindings can share the same
    // namespace as global bindings (e.g. traversalsources and graphs).
    if (traversalSource.equals(HIDDEN_G))
        throw new IllegalArgumentException(
                "The traversalSource cannot have the name " + HIDDEN_G + " - it is reserved");

    if (bindings.containsKey(HIDDEN_G))
        throw new IllegalArgumentException("Bindings cannot include " + HIDDEN_G + " - it is reserved");

    if (!bindings.containsKey(traversalSource))
        throw new IllegalArgumentException(
                "The bindings available to the ScriptEngine do not contain a traversalSource named: "
                        + traversalSource);

    final Object b = bindings.get(traversalSource);
    if (!(b instanceof TraversalSource))
        throw new IllegalArgumentException(traversalSource + " is of type " + b.getClass().getSimpleName()
                + " and is not an instance of TraversalSource");

    final Bindings inner = new SimpleBindings();
    inner.putAll(bindings);/*from www  .  j av  a  2 s.co m*/
    inner.putAll(bytecode.getBindings());
    inner.put(HIDDEN_G, b);

    return (Traversal.Admin) this.eval(GroovyTranslator.of(HIDDEN_G).translate(bytecode), inner);
}

From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineTest.java

@Test
public void shouldClearEngineScopeOnReset() throws Exception {
    final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
    engine.eval("x = { y -> y + 1}");
    Bindings b = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    assertTrue(b.containsKey("x"));
    assertEquals(2, ((Closure) b.get("x")).call(1));

    // should clear the bindings
    engine.reset();// w  ww.  j  av a2  s . co m
    try {
        engine.eval("x(1)");
        fail("Bindings should have been cleared.");
    } catch (Exception ex) {

    }

    b = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    assertFalse(b.containsKey("x"));

    // redefine x
    engine.eval("x = { y -> y + 2}");
    assertEquals(3, engine.eval("x(1)"));
    b = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    assertTrue(b.containsKey("x"));
    assertEquals(3, ((Closure) b.get("x")).call(1));
}

From source file:org.lsc.utils.GroovyEvaluator.java

/**
 * Local instance evaluation./*from   www. j  a v a 2  s. c o m*/
 *
 * @param expression
 *                the expression to eval
 * @param params
 *                the keys are the name used in the
 * @return the evaluation result
 */
private Object instanceEval(final Task task, final String expression, final Map<String, Object> params) {
    Bindings bindings = engine.createBindings();

    /* Allow to have shorter names for function in the package org.lsc.utils.directory */
    String expressionImport =
            //                  "import static org.lsc.utils.directory.*\n" +
            //                  "import static org.lsc.utils.*\n" + 
            expression;

    // add LDAP interface for destination
    if (!bindings.containsKey("ldap") && task.getDestinationService() instanceof AbstractSimpleJndiService) {
        ScriptableJndiServices dstSjs = new ScriptableJndiServices();
        dstSjs.setJndiServices(((AbstractSimpleJndiService) task.getDestinationService()).getJndiServices());
        bindings.put("ldap", dstSjs);
    }

    // add LDAP interface for source
    if (!bindings.containsKey("srcLdap") && task.getSourceService() instanceof AbstractSimpleJndiService) {
        ScriptableJndiServices srcSjs = new ScriptableJndiServices();
        srcSjs.setJndiServices(((AbstractSimpleJndiService) task.getSourceService()).getJndiServices());
        bindings.put("srcLdap", srcSjs);
    }

    if (params != null) {
        for (String paramName : params.keySet()) {
            bindings.put(paramName, params.get(paramName));
        }
    }

    Object ret = null;
    try {
        if (task.getScriptIncludes() != null) {
            for (File scriptInclude : task.getScriptIncludes()) {
                String extension = FilenameUtils.getExtension(scriptInclude.getAbsolutePath());
                if ("groovy".equals(extension) || "gvy".equals(extension) || "gy".equals(extension)
                        || "gsh".equals(extension)) {
                    FileReader reader = new FileReader(scriptInclude);
                    try {
                        engine.eval(reader, bindings);
                    } finally {
                        reader.close();
                    }
                }
            }
        }
        ret = engine.eval(expressionImport, bindings);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        LOGGER.error(e.toString());
        LOGGER.debug(e.toString(), e);
        return null;
    }

    return ret;
}

From source file:org.lsc.utils.JScriptEvaluator.java

/**
 * Local instance evaluation./*from   www.ja va  2 s  .  c o  m*/
 *
 * @param expression
 *                the expression to eval
 * @param params
 *                the keys are the name used in the
 * @return the evaluation result
 * @throws LscServiceException 
 */
private Object instanceEval(final Task task, final String expression, final Map<String, Object> params)
        throws LscServiceException {
    //      Script script = null;
    Bindings bindings = engine.createBindings();

    /* Allow to have shorter names for function in the package org.lsc.utils.directory */
    String expressionImport = "var version = java.lang.System.getProperty(\"java.version\");\n"
            + "if (version.startsWith(\"1.8.0\")) { load(\"nashorn:mozilla_compat.js\"); }\n"
            + "importPackage(org.lsc.utils.directory);\n" + "importPackage(org.lsc.utils);\n" + expression;

    //      if (cache.containsKey(expressionImport)) {
    //         script = cache.get(expressionImport);
    //      } else {
    //         script = cx.compileString(expressionImport, "<cmd>", 1, null);
    //         cache.put(expressionImport, script);
    //      }

    // add LDAP interface for destination
    if (!bindings.containsKey("ldap") && task.getDestinationService() instanceof AbstractSimpleJndiService) {
        ScriptableJndiServices dstSjs = new ScriptableJndiServices();
        dstSjs.setJndiServices(((AbstractSimpleJndiService) task.getDestinationService()).getJndiServices());
        bindings.put("ldap", dstSjs);
    }

    // add LDAP interface for source
    if (!bindings.containsKey("srcLdap") && task.getSourceService() instanceof AbstractSimpleJndiService) {
        ScriptableJndiServices srcSjs = new ScriptableJndiServices();
        srcSjs.setJndiServices(((AbstractSimpleJndiService) task.getSourceService()).getJndiServices());
        bindings.put("srcLdap", srcSjs);
    }

    if (params != null) {
        for (String paramName : params.keySet()) {
            bindings.put(paramName, params.get(paramName));
        }
    }

    Object ret = null;
    try {
        if (task.getScriptIncludes() != null) {
            for (File scriptInclude : task.getScriptIncludes()) {
                if ("js".equals(FilenameUtils.getExtension(scriptInclude.getAbsolutePath()))) {
                    FileReader reader = new FileReader(scriptInclude);
                    try {
                        engine.eval(reader, bindings);
                    } finally {
                        reader.close();
                    }
                }
            }
        }
        ret = engine.eval(expressionImport, bindings);
    } catch (ScriptException e) {
        LOGGER.error("Fail to compute expression: " + expression + " on "
                + (params.containsKey("srcBean") && ((IBean) params.get("srcBean")).getMainIdentifier() != null
                        ? "id=" + ((IBean) params.get("srcBean")).getMainIdentifier()
                        : (params.containsKey("dstBean")
                                && ((IBean) params.get("dstBean")).getMainIdentifier() != null
                                        ? "id=" + ((IBean) params.get("dstBean")).getMainIdentifier()
                                        : "unknown id !"))
                + "\nReason: " + e.toString());
        LOGGER.debug(e.toString(), e);
        throw new LscServiceException(e);
    } catch (RuntimeException e) {
        throw new LscServiceException(e);
    } catch (Exception e) {
        LOGGER.error(e.toString());
        LOGGER.debug(e.toString(), e);
        return null;
    }

    return ret;
}