Friday, July 31, 2009

Simple Rhino example

I have been working in RhinoScript for a few months now. Totally bedazzled by its prowess!! Its amazing fit in to our requirement made our work a smooth flow. I am uploading a very basic rhino script example. This would be helpful for anyone who needs to have a look at the working of rhinoscript and run few feasibility tests before getting too serious about it.

Note : If you run the Executor program by giving the appropriate rhino script's (.js) file's path in the executor code, it will run the script without the need of deploying it in any context.


RhinoExecutor.java (Rhino Script)



package com.caller;

import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class RhinoExecutor {

public static void main (String[] args){
Connection con = null;
try {
Context cx = Context.getCurrentContext();

// Parameters to pass to the method written in the js file
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@shastra:1521:shastra", "MIDBTR3", "MIDBTR3");

Object[] argsObj = new Object[2];
argsObj[0] = con;

// the location of the js file which would have rhino code written inside it
ScriptableObject scr = (ScriptableObject)getScriptableRhinoObject("D:/Eclipse Workspaces/workspace 2/RhinoProj/src/com/js/Trial.js");

// the function name inside the js file to be executed
Object objErrors = scr.callMethod(cx, scr, "Trial", argsObj);

// below is a way to get native exceptions beig thrown by rhino at times.
if (objErrors instanceof NativeJavaObject) {
NativeJavaObject nJOTemp = (NativeJavaObject)objErrors;
Object objErr = nJOTemp.unwrap();
ArrayList al = (ArrayList)objErr;
System.out.println("Error : "+al.get(0));
}

if (con != null)
con.close();

} catch(Exception e){
e.printStackTrace();
}
}

public static Scriptable getScriptableRhinoObject(String scriptName) {
Context context = Context.enter();
Context.getCurrentContext();
context.setLanguageVersion(Context.VERSION_1_5);
Scriptable scriptable = context.initStandardObjects();
FileReader reader = null;
try {
reader = new FileReader(scriptName);
context.evaluateReader(scriptable,reader, "Line", 1, null);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
Float fl = new Float(0);
return scriptable;
}

public static Scriptable getScriptableRhinoObjectUsingString(String scriptContents) {
Context context = Context.enter();
Context.getCurrentContext();
context.setLanguageVersion(Context.VERSION_1_5);
Scriptable scriptable = context.initStandardObjects();
FileReader reader = null;
context.evaluateString(scriptable,scriptContents, "Line", 1, null);
return scriptable;
}

}



The script being tested and executed by the above mentioned code is "Trial.js"

Trial.js (Rhino Script)



function Trial(connection) {

var DEBUG = true;
var exception = new java.lang.Exception();
var errorList = new java.util.ArrayList();
if (DEBUG)
java.lang.System.out.println("#### Inside Trial Script");
try {
var stmt = connection.createStatement();
var rs = stmt.executeQuery("SELECT * FROM MI_TXNMASTER");

/*
if (true){
throw new java.lang.Exception("Forced Exception");
}
*/

while (rs.next()){
java.lang.System.out.println("TXN - ID : "+rs.getString("TXN_ID"));
}

}catch( e){
java.lang.System.out.println("Exc : "+e.getClass());

if (e.getClass() == "class java.lang.Exception"){
var eMsg = e.getMessage();
java.lang.System.out.println("Exception in Trial 1 " + eMsg);
var exp = new java.lang.Exception(eMsg);
errorList.add(exp);
return errorList;
}else{
java.lang.System.out.println("Exception in Trial 2 " + e.message);
var exp = new java.lang.Exception(e.message);
errorList.add(exp);
return errorList;
}
}
if (DEBUG)
java.lang.System.out.println("#### End of Trial");

}