Tea Engine Programmer's Guide

Who is the Tea Engine library for?

For people who want to integrate Tea code into existing Java frameworks using the standard JSR-223
JavaTM Scripting API, without having to learn internal details of Tea Java interfaces.
Reading of Java 1.6 scripting features and Tea documentation is highly recommended.

top


Examples

The same examples provided in the Java Scripting Programmers guide are provided, adapted to Tea, for convinience.

"Hello, World"

From the ScriptEngineManager instance, we request a Tea engine instance using getEngineByName method. On the script engine, the eval method is called to execute a given String as Tea code! For brevity, in this as well as in subsequent examples, we have not shown exception handling. There are checked and runtime exceptions thrown from javax.script API. Needless to say, you have to handle the exceptions appropriately.


public class HelloWorld {

public static void main(String[] args) throws Exception {
// create a script engine manager
ScriptEngineManager m = new ScriptEngineManager();
// create engine
ScriptEngine engine = m.getEngineByName("tea");
// evaluate JavaScript code from string.
engine.eval("echo \"Tea says Hello Java Standard World!\"");
//((TeaScriptEngine)engine).end();
}
}

top

Evaluating a Script File

In this example, we call the eval method that accepts java.io.Reader for the input source. The script read by the given reader is executed. This way it is possible to execute scripts from files, URLs and resources by wrapping the relevant input stream objects as readers.

import javax.script.*;
public class EvalFile {
public static void main(String[] args) throws Exception {
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create engine
ScriptEngine engine = m.getEngineByName("tea");
// evaluate Tea code from file with filename as 1st argument.
engine.eval(new java.io.FileReader(args[0]));
}
}
Let us assume that we have the file named "sample.tea" with the following text:

echo "Hello"

We can run the above Java as


java EvalFile sample.tea

top

Script Variables

When you embed script engines and scripts with your Java application, you may want to expose your application objects as global variables to scripts. This example demonstrates how you can expose your application objects as global variables to a script. We create a java.io.File in the application and expose the same as a global variable with the name "file". The script can access the variable - for example, it can call public methods on it. Note that the syntax to access Java objects, methods and fields is dependent on the scripting language. 

public class ScriptVars { 
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("Tea Engine");

String s = new String("test string");
// expose File object as variable to script
engine.put("aString", s);

// evaluate a script string. The script accesses "file"
// variable and calls method on it
engine.eval("echo $aString");
}
}

top

Invoking Script Functions and Methods

Sometimes you may want to call a specific scripting function repeatedly - for example, your application menu functionality might be implemented by a script. In your menu's action event handler you may want to call a specific script function. The following example demonstrates invoking a specific script function from Java code.


import javax.script.*;

public class InvokeScriptFunction {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("Tea Engine");

// evaluate a script string.
engine.eval("global hello ( arg ) { echo \"hello \" $arg }"); // javax.script.Invocable is an optional interface.

// Check whether your script engine implements or not!
// Note that the Tea Engine implements Invocable interface.
Invocable inv = (Invocable) engine;

// invoke the global function named "hello"
inv.invokeFunction("hello", "Scripting!!" );
}
}

Tea supports objects through the native TOS library, so you can invoke a script method on a script object.


import javax.script.*;

public class InvokeScriptMethod {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("Tea Engine");

// evaluate a script string. // TODO: load should not be needed
engine.eval("load \"com.pdmfc.tea.modules.tos.SModuleTos\"; class Test ( ) ; method Test hello ( arg ) { echo \"hello \" $arg } ; global obj [new Test]");

// javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the Tea Engine implements Invocable interface.
Invocable inv = (Invocable) engine;

// get script object on which we want to call the method
Object obj = engine.get("obj");

// invoke the method named "hello" on the script object "obj"
inv.invokeMethod(obj, "hello", "Script Method !!" );
}
}

top

Copyright 2007 PDM&FC