Example usage for javax.script ScriptEngine setBindings

List of usage examples for javax.script ScriptEngine setBindings

Introduction

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

Prototype

public void setBindings(Bindings bindings, int scope);

Source Link

Document

Sets a scope of named values to be used by scripts.

Usage

From source file:tk.elevenk.restfulrobot.testcase.TestCase.java

/**
 * Runs the given test script file//from   ww  w.j a v  a 2 s .  c o  m
 * 
 * @param fileName
 */
public void runScript(String fileName) {

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
    engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

    if (!fileName.endsWith(".js")) {
        fileName = fileName.concat(".js");
    }

    FileReader scriptFile = null;
    // attempt to open the script file
    try {
        scriptFile = new FileReader(new File(RestfulRobot.SCRIPTS_PATH + fileName));
    } catch (FileNotFoundException e) {
        logger.error(ExceptionUtils.getStackTrace(e));
        result.setDetails(e.getMessage());
        result.setResultType(TestResult.TYPE_ERROR);
    }

    // run the script
    try {
        logger.info("Running script " + fileName);
        engine.eval(scriptFile);
    } catch (ScriptException e) {
        logger.error(ExceptionUtils.getStackTrace(e));
        result.setDetails(e.getMessage());
        result.setResultType(TestResult.TYPE_ERROR);
    }

    // pull data from script
    try {
        this.testCaseID = (String) engine.get("testCaseID");
        this.testCategory = (String) engine.get("testCategory");
        this.testPlan = (String) engine.get("testPlan");
        this.testProject = (String) engine.get("testProject");
        this.testType = (String) engine.get("testType");
        this.testName = (String) engine.get("testName");
    } catch (Exception e) {
        // TODO make this try each parameter
    }

    logger.info("Finished running script");
}

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

/**
 * DOCUMENT ME!//from  www  .j ava 2s  .c  om
 *
 * @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!// w  w w.  j a  va  2s.c  o  m
 *
 * @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;
}

From source file:utybo.branchingstorytree.swing.impl.XSFClient.java

@Override
public Object invokeScript(String resourceName, String function, XSFBridge bst, BranchingStory story, int line)
        throws BSTException {
    ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
    SimpleBindings binds = new SimpleBindings();
    binds.putAll(story.getRegistry().getAllInt());
    binds.putAll(story.getRegistry().getAllString());
    binds.put("bst", bst);
    scriptEngine.setBindings(binds, ScriptContext.ENGINE_SCOPE);
    try {/* w w  w  . j ava  2s  . c  om*/
        scriptEngine.eval(scripts.get(resourceName));
        return scriptEngine.eval(function + "()");
    } catch (ScriptException e) {
        throw new BSTException(line, "Script exception : " + e.getMessage(), story);
    }
}