A console application using TerrJavaRemote

This is sample Java code for a simple console application using the TerrJavaRemote API.

This is very similar to the code in A console application, except that it uses TerrJavaRemote methods to create and control a spawned process with a TIBCO Enterprise Runtime for R engine, rather than creating an embedded engine via TerrJava. Another difference is that the TIBCO Enterprise Runtime for R and Java home paths are passed in as arguments, rather than being accessed via environment variables.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.tibco.terr.TerrJava;
import com.tibco.terr.TerrJavaRemote;
public class TerrJavaRemoteConsoleExample {
    public static void main(String[] args) throws Throwable {
        System.out.println("TerrJavaRemoteConsoleExample:");
        // create TerrJavaRemote object
        TerrJavaRemote engine = new TerrJavaRemote();

        // set paths for spawned engine
        if (args.length<2) {
            throw new Exception("need two arguments: TERR home and Java home");
        }
        engine.setTerrHome(args[0]);
        engine.setJavaHome(args[1]);

        // set up output/input handlers
        TerrJava.OutputHandler out = new TerrJava.OutputHandler() {
                public void write(String data, boolean prompted) {
                    System.out.print(data);
                }
            };
        TerrJava.InputHandler in = new TerrJava.InputHandler() {
                public String readLine() {
                    try {
                        BufferedReader bin = new BufferedReader(
                              new InputStreamReader(System.in));
                        String cmd = bin.readLine();
                        return cmd;
                    } catch (Exception ex) {
                        return "";
                    }
                }
            };
        engine.setOutputHandler(out);
        engine.setInputHandler(in);
        // start TIBCO Enterprise Runtime for R engine
        engine.startEngine();
        // repeatedly read input lines and evaluate them
        String resultCode = "";
        String prompt = "";
        StringBuffer expression = new StringBuffer();
        while (true) {
            // get prompt string from TIBCO Enterprise Runtime for R
            if (resultCode.equals("IncompleteString")) {
                prompt = engine.evaluateToString("getOption('continueString')");
            } else if (resultCode.equals("IncompleteExpression")) {
                prompt = engine.evaluateToString("getOption('continue')");
            } else {
                prompt = engine.evaluateToString("getOption('prompt')");
                expression.setLength(0);
            }
            out.write(prompt, true);
            String line = in.readLine();
            // ignore empty input lines, except when we are reading a multi-line string
            if (line.isEmpty() && !resultCode.equals("IncompleteString")) {
                continue;
            }
            // accumulate multi-line expression, until we can parse it
            if (expression.length() > 0) {
                expression.append("\n");
            }
            expression.append(line);
            resultCode = engine.evaluateInteractive(expression.toString());
            // exit if engine just evaluated q()
            if (resultCode.equals("Quit")) {
                resultCode = "Success";
                break;
            }
            // print error message if any
            if (resultCode.equals("EvaluationError") ||
                resultCode.equals("ParserError")) {
                String error = engine.getLastErrorMessage();
                if (!error.isEmpty()) {
                    out.write(error + "\n", false);
                }
            }
        }
    }
}