Java Code Examples for javax.script.Bindings#containsKey()

The following examples show how to use javax.script.Bindings#containsKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SelectionScript.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * SelectionScript must give its result in the 'result_script' variable.
 *
 * @see org.ow2.proactive.scripting.Script#getResult(Object, Bindings)
 */
@Override
protected ScriptResult<Boolean> getResult(Object evalResult, Bindings bindings) {
    if (bindings.containsKey(RESULT_VARIABLE)) {
        Object result = bindings.get(RESULT_VARIABLE);

        if (result instanceof Boolean) {
            return new ScriptResult<>((Boolean) result);
        } else if (result instanceof Integer) {
            return new ScriptResult<>((Integer) result != 0);
        } else if (result instanceof CharSequence) {
            return new ScriptResult<>(!(result.equals("false") || result.equals("False")));
        } else {
            return new ScriptResult<>(new Exception("Bad result format : awaited Boolean (or Integer when not existing), found " +
                                                    result.getClass().getName()));
        }
    } else {
        String msg = "No binding for key : " + RESULT_VARIABLE +
                     "\na Selection script must define a variable named '" + RESULT_VARIABLE +
                     "' set to true or false";
        logger.error(msg);
        return new ScriptResult<>(new Exception(msg));
    }
}
 
Example 2
Source File: TaskScript.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected ScriptResult<Serializable> getResult(Object evalResult, Bindings bindings) {
    if (bindings.containsKey(RESULT_VARIABLE)) {
        Object result = bindings.get(RESULT_VARIABLE);
        if (result == null) {
            return new ScriptResult<>(null);
        } else {
            if (result instanceof Serializable) {
                return new ScriptResult<>((Serializable) result);
            } else {
                return new ScriptResult<>(new Exception("Bad result format : awaited Serializable, found " +
                                                        result.getClass().getName()));
            }
        }
    } else {
        if (evalResult != null && evalResult instanceof Serializable) {
            return new ScriptResult<>((Serializable) evalResult);
        } else {
            // assuming script ran fine
            return new ScriptResult<Serializable>(true);
        }
    }
}
 
Example 3
Source File: GremlinGroovyScriptEngine.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@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);
    inner.putAll(bytecode.getBindings());
    inner.put(HIDDEN_G, b);
    org.apache.tinkerpop.gremlin.process.traversal.Script script = GroovyTranslator.of(HIDDEN_G, typeTranslator).translate(bytecode);
    script.getParameters().ifPresent(inner::putAll);
    return (Traversal.Admin) this.eval(script.getScript(), inner);
}
 
Example 4
Source File: JexlScriptEngine.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public boolean has(final String name) {
    Bindings bnd = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    return bnd.containsKey(name);
}