Example usage for javax.script Bindings put

List of usage examples for javax.script Bindings put

Introduction

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

Prototype

public Object put(String name, Object value);

Source Link

Document

Set a named value.

Usage

From source file:io.github.jeddict.jcode.util.FileUtil.java

/**
 * In-memory template api/*www .ja va2 s . c  o m*/
 *
 * @param templateContent
 * @param values
 * @return
 */
public static String expandTemplateContent(String templateContent, Map<String, Object> values) {
    StringWriter writer = new StringWriter();
    ScriptEngine eng = getScriptEngine();
    Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    if (values != null) {
        bind.putAll(values);
    }
    bind.put(ENCODING_PROPERTY_NAME, Charset.defaultCharset().name());
    eng.getContext().setWriter(writer);
    Reader is = new StringReader(templateContent);
    try {
        eng.eval(is);
    } catch (ScriptException ex) {
        Exceptions.printStackTrace(ex);
    }

    return writer.toString();
}

From source file:org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory.java

public static void restoreCurrentContext(AuthenticationContext context, ScriptEngine engine)
        throws FrameworkException {

    Map<String, Object> map = (Map<String, Object>) context.getProperty(JS_BINDING_CURRENT_CONTEXT);
    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    if (map != null) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Object deserializedValue = FrameworkUtils.fromJsSerializable(entry.getValue(), engine);
            if (deserializedValue instanceof AbstractJSObjectWrapper) {
                ((AbstractJSObjectWrapper) deserializedValue).initializeContext(context);
            }//from w w  w  . j  a v  a 2 s  . co  m
            bindings.put(entry.getKey(), deserializedValue);
        }
    }
}

From source file:io.lavagna.service.ApiHooksService.java

private static void executeScript(String name, CompiledScript script, Map<String, Object> scope) {
    try {/*from www.j a  v a  2 s.  c om*/
        ScriptContext newContext = new SimpleScriptContext();
        Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
        engineScope.putAll(scope);
        engineScope.put("log", LOG);
        engineScope.put("GSON", Json.GSON);
        engineScope.put("restTemplate", new RestTemplate());
        script.eval(newContext);
    } catch (ScriptException ex) {
        LOG.warn("Error while executing script " + name, ex);
    }
}

From source file:com.espertech.esper.epl.script.ExprNodeScriptEvalJSR223.java

public Object evaluate(EventBean[] eventsPerStream, boolean isNewData, ExprEvaluatorContext context) {
    Bindings bindings = executable.getEngine().createBindings();
    bindings.put(ExprNodeScript.CONTEXT_BINDING_NAME, context.getAgentInstanceScriptContext());
    for (int i = 0; i < names.length; i++) {
        bindings.put(names[i], parameters[i].evaluate(eventsPerStream, isNewData, context));
    }//from   www .j ava2 s  .  com

    try {
        Object result = executable.eval(bindings);

        if (coercer != null) {
            return coercer.coerceBoxed((Number) result);
        }

        return result;
    } catch (ScriptException e) {
        String message = "Unexpected exception executing script '" + scriptName + "' for statement '"
                + statementName + "' : " + e.getMessage();
        log.error(message, e);
        throw new EPException(message, e);
    }
}

From source file:org.nuxeo.ecm.core.redis.embedded.RedisEmbeddedLuaEngine.java

public Object evalsha(String sha, List<String> keys, List<String> args) throws ScriptException {
    final CompiledScript script = binaries.get(sha);
    Bindings bindings = engine.createBindings();
    bindings.put("KEYS", keys.toArray(new String[keys.size()]));
    bindings.put("ARGV", args.toArray(new String[args.size()]));
    Object result = script.eval(bindings);
    if (result instanceof LuaValue) {
        LuaValue value = (LuaValue) result;
        if (value.isboolean() && value.toboolean() == false) {
            return null;
        }//from   w  ww .j av  a 2s. c o m
        return value.tojstring();
    }
    return result;
}

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 w  w . jav a2 s .  c om*/
        engine.eval(mapperSource, bindings);
    } catch (ScriptException e) {
        log.error("Error while executing JavaScript mapper", e);
    }
}

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

@Override
public Object wrapNuxeoPrincipal(NuxeoPrincipal principal, Object userObject,
        Map<String, Serializable> params) {
    if (StringUtils.isEmpty(wrapperSource)) {
        return null;
    }/*from  w  w w .  ja  va 2s. c om*/
    Bindings bindings = new SimpleBindings();
    bindings.put("nuxeoPrincipal", principal);
    bindings.put("userObject", userObject);
    bindings.put("params", params);
    try {
        engine.eval(wrapperSource, bindings);
    } catch (ScriptException e) {
        log.error("Error while executing JavaScript mapper", e);
    }
    return bindings.get("userObject");
}

From source file:org.paxml.test.SelfTest.java

public void testSyntax() throws Exception {
    ScriptEngine runtime = new ScriptEngineManager().getEngineByName("javascript");
    Bindings bindings = runtime.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("util", "xx");
    runtime.setBindings(new SimpleBindings() {

    }, ScriptContext.ENGINE_SCOPE);

}

From source file:com.jkoolcloud.tnt4j.streams.filters.GroovyExpressionFilter.java

@Override
public boolean doFilter(Object value, ActivityInfo ai) throws FilterException {
    Bindings bindings = new SimpleBindings();
    bindings.put(StreamsScriptingUtils.FIELD_VALUE_VARIABLE_EXPR, value);

    if (ai != null && CollectionUtils.isNotEmpty(exprVars)) {
        for (String eVar : exprVars) {
            Property eKV = resolveFieldKeyAndValue(eVar, ai);

            bindings.put(eKV.getKey(), eKV.getValue());
        }//w w  w  .  j  av  a 2 s  .  c om
    }

    try {
        boolean match = (boolean) script.eval(bindings);

        logEvaluationResult(bindings, match);

        return isFilteredOut(getHandleType(), match);
    } catch (Exception exc) {
        throw new FilterException(StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME,
                "ExpressionFilter.filtering.failed", filterExpression), exc);
    }
}

From source file:com.jkoolcloud.tnt4j.streams.transform.JavaScriptTransformation.java

@Override
public Object transform(Object value, ActivityInfo ai) throws TransformationException {
    Bindings bindings = new SimpleBindings();
    bindings.put(StreamsScriptingUtils.FIELD_VALUE_VARIABLE_EXPR, value);

    if (ai != null && CollectionUtils.isNotEmpty(exprVars)) {
        for (String eVar : exprVars) {
            Property eKV = resolveFieldKeyAndValue(eVar, ai);

            bindings.put(eKV.getKey(), eKV.getValue());
        }/*ww w  .ja  v  a  2  s . c om*/
    }

    try {
        Object tValue = script.eval(bindings);

        logEvaluationResult(bindings, tValue);

        return tValue;
    } catch (Exception exc) {
        throw new TransformationException(
                StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME,
                        "ValueTransformation.transformation.failed", getName(), getPhase()),
                exc);
    }
}