Example usage for javax.script ScriptEngine createBindings

List of usage examples for javax.script ScriptEngine createBindings

Introduction

In this page you can find the example usage for javax.script ScriptEngine createBindings.

Prototype

public Bindings createBindings();

Source Link

Document

Returns an uninitialized Bindings.

Usage

From source file:org.wso2.carbon.business.rules.core.util.TemplateManagerHelper.java

/**
 * Runs the script that is given as a string, and gives all the variables specified in the script
 *
 * @param script/* w  w w  .j a  v  a 2 s . c o m*/
 * @return Map of Strings
 * @throws TemplateManagerHelperException
 */
public static Map<String, String> getScriptGeneratedVariables(String script)
        throws RuleTemplateScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    ScriptContext scriptContext = new SimpleScriptContext();
    scriptContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);

    try {
        // Run script
        engine.eval(script);
        Map<String, Object> returnedScriptContextBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);

        // Variable names and their values as strings, from the script context binding
        Map<String, String> scriptVariables = new HashMap<>();
        for (Map.Entry scriptVariable : returnedScriptContextBindings.entrySet()) {
            if (scriptVariable.getValue() == null) {
                scriptVariables.put(scriptVariable.getKey().toString(), null);
            } else {
                scriptVariables.put(scriptVariable.getKey().toString(), scriptVariable.getValue().toString());
            }
        }
        return scriptVariables;
    } catch (ScriptException e) {
        throw new RuleTemplateScriptException(e.getCause().getMessage(), e);
    }
}

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

public ScriptEngine createEngine(AuthenticationContext authenticationContext) {

    ScriptEngine engine = factory.getScriptEngine("--no-java");

    Bindings bindings = engine.createBindings();
    engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
    engine.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    SelectAcrFromFunction selectAcrFromFunction = new SelectAcrFromFunction();
    //        todo move to functions registry
    bindings.put(FrameworkConstants.JSAttributes.JS_FUNC_SELECT_ACR_FROM,
            (SelectOneFunction) selectAcrFromFunction::evaluate);

    JsLogger jsLogger = new JsLogger();
    bindings.put(FrameworkConstants.JSAttributes.JS_LOG, jsLogger);
    return engine;
}

From source file:tk.tomby.tedit.services.ScriptingManager.java

/**
 * DOCUMENT ME!/*  w w  w.  j av a  2 s. co  m*/
 *
 * @param lang DOCUMENT ME!
 * @param script DOCUMENT ME!
 * @param buffer DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public static Object eval(String lang, String script, IBuffer buffer) {
    Object result = null;

    try {
        ScriptEngine engine = manager.getEngineByName(lang);

        if (buffer != null) {
            Bindings bindings = engine.createBindings();
            bindings.put("buffer", new BufferDecorator(buffer));
            engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        }

        result = engine.eval(script);
    } catch (ScriptException e) {
        log.error("error in script excution", e);
    }

    return result;
}

From source file:tk.tomby.tedit.services.ScriptingManager.java

/**
 * DOCUMENT ME!// ww  w .java2  s  . c om
 *
 * @param lang DOCUMENT ME!
 * @param script DOCUMENT ME!
 * @param buffer DOCUMENT ME!
 */
public static Object exec(String lang, InputStream stream, IBuffer buffer) {
    Object result = null;
    BufferedReader reader = null;

    try {
        ScriptEngine engine = manager.getEngineByName(lang);

        reader = new BufferedReader(new InputStreamReader(stream));

        if (buffer != null) {
            Bindings bindings = engine.createBindings();
            bindings.put("buffer", new BufferDecorator(buffer));
            engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        }

        result = engine.eval(reader);

    } catch (ScriptException e) {
        log.error("error in script excution", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                log.warn("error closing reader", e);
            }
        }
    }

    return result;
}