ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

The Mustang Meets the Rhino: Scripting in Java 6
Pages: 1, 2, 3, 4

Retreiving Results

So now you can run a script, what next? You will generally want to fetch calculated values or expressions from the scripting environment for use in your Java code. There are two ways to do this. The first one is that the eval() function returns the value returned by the execution of the script. By default, this is the value of the last executed expression.



The following example illustrates the premium calculations for an imaginary insurance company. Any driver less than 25 years of age will pay 50% extra. If a driver over 25 has a no claims bonus, the company offers a 25% discount. Otherwise, the standard premiums will apply. This rule could be implemented using a JavaScript expression as follows:


        ScriptEngineManager manager
                   = new ScriptEngineManager();
        ScriptEngine engine
                   = manager.getEngineByName("js");
        engine.put("age", 26);
        engine.put("noClaims", Boolean.TRUE);
        Object result = engine.eval(
            "if (age < 25){ " +
                        "    riskFactor = 1.5;" +
                        "} else if (noClaims) {" +
                        "    riskFactor = 0.75;" +
                        "} else {" +
                        "    riskFactor = 1.0;" +
                        "}");
        assertEquals(result,0.75);
    }

The returned value is the value of the last executed instruction, so in this case it will be the value assigned to riskFactor. Note that the name of the JavaScript variable that contains the result (in this case, riskFactor) is irrelevant: only the value is returned.

The second way to interact with the script is to use a Bindings object. A Bindings object is essentially a map of key/value pairs that can be used to exchange information between your Java application and the JavaScript script.


    public void testEvalWithBindings()
                          throws ScriptException {
        ScriptEngineManager manager
                 = new ScriptEngineManager();
        ScriptEngine engine
                 = manager.getEngineByName("js");
        Bindings bindings
                 = engine.createBindings();
        bindings.put("age", 26);
        bindings.put("noClaims", Boolean.TRUE);
        bindings.put("riskFactor", 1);

        engine.eval(
            "if (age < 25){ " +
            "    riskFactor = 1.5;" +
            "} else if (noClaims) {" +
            "    riskFactor = 0.75;" +
            "} else {" +
            "    riskFactor = 1.0;" +
            "}");

        double risk = bindings.get("riskFactor");
        assertEquals(risk,0.75);
    }

Accessing Java Resources

You can also access Java classes and resources from within scripts. The Rhino JavaScript engine supports the importPackage() function, which allows you to import Java packages. Once imported, you can instantiate Java objects within the script just as you would in Java:


        engine.eval("importPackage(java.util); " +
                    "today = new Date(); " +
                    "print('Today is ' + today);");

Calling methods on Java classes is also easy to do, both on object instances passed to the script engine, and on static class members.


        engine.put("name","John Doe");
        engine.eval(
            "name2 = name.toUpperCase();" +
            "print('Converted name = ' + name2);");
        > Converted name = JOHN DOE

Pages: 1, 2, 3, 4

Next Pagearrow