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(Map<String, Object> m) 

Source Link

Document

Constructor uses an existing Map to store the values.

Usage

From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor.java

/**
 * Evaluate a script and allow for the submission of a transform {@link Function} that will transform the
 * result after script evaluates but before transaction commit and before the returned {@link CompletableFuture}
 * is completed.//from   w  w  w  .ja v a  2 s. c  o m
 *
 * @param script the script to evaluate
 * @param language the language to evaluate it in
 * @param boundVars the bindings to evaluate in the context of the script
 * @param transformResult a {@link Function} that transforms the result - can be {@code null}
 */
public CompletableFuture<Object> eval(final String script, final String language,
        final Map<String, Object> boundVars, final Function<Object, Object> transformResult) {
    return eval(script, language, new SimpleBindings(boundVars), transformResult, null);
}

From source file:org.apache.nifi.scripting.ScriptFactory.java

private void updateEngine() throws IOException, ScriptException {
    final String extension = getExtension(scriptFileName);
    // if engine is thread safe, it's being reused...if it's a JrubyEngine it
    File scriptFile = new File(this.scriptFileName);
    ScriptEngine scriptEngine = engineFactory.getNewEngine(scriptFile, extension);
    scriptText = getScriptText(scriptFile, extension);
    Map<String, Object> localThreadVariables = new HashMap<>();
    String loggerVariableKey = getVariableName("GLOBAL", "logger", extension);
    localThreadVariables.put(loggerVariableKey, logger);
    String propertiesVariableKey = getVariableName("INSTANCE", "properties", extension);
    localThreadVariables.put(propertiesVariableKey, new HashMap<String, String>());
    localThreadVariables.put(ScriptEngine.FILENAME, scriptFileName);
    if (scriptEngine instanceof Compilable) {
        Bindings bindings = new SimpleBindings(localThreadVariables);
        scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        compiledScript = ((Compilable) scriptEngine).compile(scriptText);
    }//from  www . j av a 2  s  .c  o  m
    logger.debug("Updating Engine!!");
}

From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor.java

/**
 * Evaluate a script and allow for the submission of a {@link Consumer} that will take the result for additional
 * processing after the script evaluates and after the {@link CompletableFuture} is completed, but before the
 * transaction is committed.//from  w  w w  .j  a  v a2  s.com
 *
 * @param script the script to evaluate
 * @param language the language to evaluate it in
 * @param boundVars the bindings to evaluate in the context of the script
 * @param withResult a {@link Consumer} that accepts the result - can be {@code null}
 */
public CompletableFuture<Object> eval(final String script, final String language,
        final Map<String, Object> boundVars, final Consumer<Object> withResult) {
    return eval(script, language, new SimpleBindings(boundVars), null, withResult);
}

From source file:org.openmrs.module.appframework.service.AppFrameworkServiceImpl.java

public boolean checkRequireExpression(Extension candidate, AppContextModel contextModel) {
    try {//from ww w. j a  v  a  2  s  .  c om
        String requireExpression = candidate.getRequire();
        if (StringUtils.isBlank(requireExpression)) {
            return true;
        } else {
            javascriptEngine.setBindings(new SimpleBindings(contextModel), ScriptContext.ENGINE_SCOPE);
            return javascriptEngine.eval("(" + requireExpression + ") == true").equals(Boolean.TRUE);
        }

    } catch (ScriptException e) {
        log.error("Failed to evaluate 'require' check for extension " + candidate.getId(), e);
        return false;
    }
}

From source file:org.forgerock.openidm.script.impl.ScriptRegistryService.java

public Promise<ActionResponse, ResourceException> handleAction(final Context context,
        final ActionRequest request) {
    String resourcePath = request.getResourcePath();
    JsonValue content = request.getContent();
    Map<String, Object> bindings = new HashMap<String, Object>();
    JsonValue config = new JsonValue(new HashMap<String, Object>());
    ScriptEntry scriptEntry = null;/*from   ww  w  .j  av  a 2 s  .co m*/
    try {
        if (resourcePath == null || "".equals(resourcePath)) {
            for (String key : content.keys()) {
                if (isSourceUnit(key)) {
                    config.put(key, content.get(key).getObject());
                } else {
                    bindings.put(key, content.get(key).getObject());
                }
            }
            // The script will be in the request content
            scriptEntry = takeScript(config);
            // Add any additional parameters to the map of bindings
            bindings.putAll(request.getAdditionalParameters());
        } else {
            throw new NotSupportedException("Actions are not supported for resource instances");
        }
        switch (request.getActionAsEnum(Action.class)) {
        case compile:
            if (scriptEntry.isActive()) {
                // just get the script - compilation technically happened above in takeScript
                scriptEntry.getScript(context);
                return newActionResponse(new JsonValue(true)).asPromise();
            } else {
                throw new ServiceUnavailableException();
            }
        case eval:
            if (scriptEntry.isActive()) {
                Script script = scriptEntry.getScript(context);
                return newResultPromise(
                        newActionResponse(new JsonValue(script.eval(new SimpleBindings(bindings)))));
            } else {
                throw new ServiceUnavailableException();
            }
        default:
            throw new BadRequestException("Unrecognized action ID " + request.getAction());
        }
    } catch (ResourceException e) {
        return e.asPromise();
    } catch (ScriptCompilationException e) {
        return new BadRequestException(e.getMessage(), e).asPromise();
    } catch (IllegalArgumentException e) { // from getActionAsEnum
        return new BadRequestException(e.getMessage(), e).asPromise();
    } catch (Exception e) {
        return new InternalServerErrorException(e.getMessage(), e).asPromise();
    }
}