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:com.tussle.script.StackedBindings.java

public StackedBindings(Map<String, Object> startValues) {
    bindingStack = new ArrayDeque<>();
    bindingMap = new HashMap<>();
    bindingStack.push(new SimpleBindings(startValues));
    for (Map.Entry<String, Object> entry : startValues.entrySet()) {
        ArrayDeque<Object> toPut = new ArrayDeque<>();
        toPut.push(entry.getValue());//from  w ww.  j  a  va  2 s.c o m
        bindingMap.put(entry.getKey(), toPut);
    }
}

From source file:org.springframework.data.hadoop.scripting.Jsr223ScriptEvaluator.java

@Override
public Object evaluate(ScriptSource script, Map<String, Object> arguments) {
    ScriptEngine engine = discoverEngine(script, arguments);

    Bindings bindings = (!CollectionUtils.isEmpty(arguments) ? new SimpleBindings(arguments) : null);

    try {//from   w w w . j  a va2 s . c o  m
        return (bindings == null ? engine.eval(script.getScriptAsString())
                : engine.eval(script.getScriptAsString(), bindings));
    } catch (IOException ex) {
        throw new ScriptCompilationException(script, "Cannot access script", ex);
    } catch (ScriptException ex) {
        throw new ScriptCompilationException(script, "Execution failure", ex);
    }
}

From source file:org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.java

@Override
public Object call(final RenderContext renderContext, Object... arguments) {
    ExtensionUtils.checkArgumentCount(RuntimeFunction.USE, arguments, 2);
    RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
    String identifier = runtimeObjectModel.toString(arguments[0]);
    if (StringUtils.isEmpty(identifier)) {
        throw new SightlyException("data-sly-use needs to be passed an identifier");
    }//from  w w w  . j  av a 2 s.  com
    Map<String, Object> useArgumentsMap = runtimeObjectModel.toMap(arguments[1]);
    Bindings useArguments = new SimpleBindings(Collections.unmodifiableMap(useArgumentsMap));
    ArrayList<UseProvider> providers = new ArrayList<>(providersMap.values());
    ListIterator<UseProvider> iterator = providers.listIterator(providers.size());
    while (iterator.hasPrevious()) {
        UseProvider provider = iterator.previous();
        ProviderOutcome outcome = provider.provide(identifier, renderContext, useArguments);
        Throwable failureCause;
        if (outcome.isSuccess()) {
            return outcome.getResult();
        } else if ((failureCause = outcome.getCause()) != null) {
            throw new SightlyException(
                    "Identifier " + identifier + " cannot be correctly instantiated by the Use API",
                    failureCause);
        }
    }
    throw new SightlyException("No use provider could resolve identifier " + identifier);
}

From source file:me.tfeng.toolbox.dust.NashornEngine.java

private Object evaluate(String script, Map<String, Object> data) throws ScriptException {
    Bindings bindings = new SimpleBindings(data);
    scriptEngine.getContext().setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
    return scriptEngine.eval(script);
}

From source file:com.adobe.cq.wcm.core.components.testing.WCMUsePojoBaseTest.java

/**
 * Retrieves the {@link Bindings} map associated with the {@link org.apache.sling.api.SlingHttpServletRequest} from the {@link
 * AemContext}. This map can be augmented and then passed to spy objects retrieved from {@link #getSpiedObject()} to initialise them.
 *
 * @return the bindings map/* www.j  ava2s  .c o  m*/
 */
protected Bindings getDefaultSlingBindings() {
    SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
    if (slingBindings != null) {
        return new SimpleBindings(slingBindings);
    }
    return new SimpleBindings();
}

From source file:com.tussle.script.StackedBindings.java

public void push(Bindings values) {
    bindingStack.push(new SimpleBindings(values));
    for (Map.Entry<? extends String, ? extends Object> entry : values.entrySet()) {
        putOver(entry.getKey(), entry.getValue());
    }//from  w  ww. j  a  v  a  2 s.  c o  m
}

From source file:at.alladin.rmbt.qos.testscript.TestScriptInterpreter.java

/**
 * //from  w  ww  .  j ava  2s  . c om
 * @param args
 * @param hstore
 * @param object
 * @return
 * @throws ScriptException
 */
private static Object eval(String[] args, Hstore hstore, AbstractResult<?> object) throws ScriptException {
    try {
        final Bindings bindings = new SimpleBindings(object.getResultMap());
        //System.out.println(object.getResultMap().toString());
        jsEngine.eval(args[0], bindings);
        final Object result = bindings.get("result");
        EvalResult evalResult = null;

        if (result != null) {
            if (result.getClass().getCanonicalName().equals("sun.org.mozilla.javascript.NativeObject") || result
                    .getClass().getCanonicalName().equals("sun.org.mozilla.javascript.internal.NativeObject")) {
                if (!alredayLookedForGetter && jsEngineNativeObjectGetter == null) {
                    alredayLookedForGetter = true;
                    System.out.println("js getter is null, trying to get methody with reflections...");
                    try {
                        jsEngineNativeObjectGetter = result.getClass().getMethod("get", Object.class);
                        System.out.println("method found: " + jsEngineNativeObjectGetter.getName());
                    } catch (Exception e) {
                        System.out.println("method not found: " + e.getMessage());
                    }
                }

                if (jsEngineNativeObjectGetter != null) {
                    final String type = (String) jsEngineNativeObjectGetter.invoke(result, "type");
                    final String key = (String) jsEngineNativeObjectGetter.invoke(result, "key");

                    evalResult = new EvalResult(EvalResultType.valueOf(type.toUpperCase(Locale.US)), key);

                    //System.out.println("Result: " + evalResult);
                }
            }
        }

        return evalResult == null ? (result == null ? "" : result) : evalResult;
    } catch (Exception e) {
        e.printStackTrace();
        throw new ScriptException(e.getMessage());
    }
}

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

/**
 * Evaluate a script with a {@link Map} of bindings.
 *///from w  w  w  .  jav  a  2  s.  com
public CompletableFuture<Object> eval(final String script, final Map<String, Object> boundVars) {
    return eval(script, null, new SimpleBindings(boundVars));
}

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

private Script getScriptInstance(final Map<String, String> properties) throws ScriptException {

    Map<String, Object> localThreadVariables = new HashMap<>();
    final String extension = getExtension(scriptFileName);
    String loggerVariableKey = getVariableName("GLOBAL", "logger", extension);
    localThreadVariables.put(loggerVariableKey, logger);
    String propertiesVariableKey = getVariableName("INSTANCE", "properties", extension);
    localThreadVariables.put(propertiesVariableKey, properties);
    localThreadVariables.put(ScriptEngine.FILENAME, scriptFileName);
    final Bindings bindings = new SimpleBindings(localThreadVariables);
    final ScriptEngine scriptEngine = engineFactory.getEngine(extension);
    Script instance;/*from  w w w .  j  a  v a2s  .c o  m*/
    if (compiledScript == null) {
        instance = (Script) scriptEngine.eval(scriptText, bindings);
        if (instance == null) { // which it will be for python and also for local variables in javascript
            instance = (Script) scriptEngine.eval("instance", bindings);
        }
    } else {
        instance = (Script) compiledScript.eval(bindings);
        if (instance == null) { // which it will be for python and also for local variables in javascript
            instance = (Script) compiledScript.getEngine().eval("instance", bindings);
        }
    }
    instance.setEngine(scriptEngine);
    return instance;
}

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

/**
 * Evaluate a script.//from www .  j  av a 2 s .  c  o  m
 *
 * @param script the script to evaluate
 * @param language the language to evaluate it in
 * @param boundVars the bindings as a {@link Map} to evaluate in the context of the script
 */
public CompletableFuture<Object> eval(final String script, final String language,
        final Map<String, Object> boundVars) {
    return eval(script, language, new SimpleBindings(boundVars));
}