Sahi Scripting - Calling Java



Sahi scripts run on the Rhino javascript engine. This allows Sahi to call any Java code in the classpath of Sahi.

Calling inbuilt Java classes

Inbuilt Java classes can be directly called using their fully qualified name. Eg.
function printThroughJava(s){
  java.lang.System.out.println("Through Java: " + s);
}
printThroughJava("Hi there");
// Should print "Through Java: Hi there" on the console.


Working with Java object directly in Sahi Script

Since Javascript does not have variable types, variables are declared with just var. Below is an example of reading property files via Java and Sahi Script: notextile.
Java CodeSahi Code
public Properties loadProperties(String path, boolean isXML) {
	Properties props = new Properties();
	try {
		FileInputStream inStream = new FileInputStream(path);
		if (isXML) {
			props.loadFromXML(inStream);
		} else {
			props.load(inStream);
		}
		inStream.close();
	} catch (Exception e) {
		// do nothing
	}
	return props;
}
// Usage
Properties props = loadProperties("a.properties", false);
System.out.println(props.getProperty("name"));
function loadProperties($path, $isXML) {
	var $props = new java.util.Properties();
	try {
		var $inStream = new java.io.FileInputStream($path);
		if ($isXML) {
			$props.loadFromXML($inStream);
		} else {
			$props.load($inStream);
		}
		$inStream.close();
	} catch (e) {
		// do nothing
	}
	return $props;
}
// Usage
var $props = loadProperties("a.properties", false);
_alert($props.getProperty("name"));

Calling Java classes in Custom Packages

Custom packages need to be prefixed with 'Packages' keyword. This is how _readFile is implemented in Sahi:
// Part of lib.js
Sahi.prototype._readFile = function (filePath) {
  return "" + Packages.net.sf.sahi.util.Utils.readFileAsString(filePath);
};


Accessing Your Own Custom Classes from Sahi

Sample Custom Class

Suppose you have built a custom class called LanguageUtils.java which has a function to read property files.
package com.example.utils;

import java.io.FileInputStream;
import java.util.Properties;

public class LanguageUtils {
	public static Properties loadProperties(String path, boolean isXML) {
		Properties props = new Properties();
		try {
			FileInputStream inStream = new FileInputStream(path);
			if (isXML) {
				props.loadFromXML(inStream);
			} else {
				props.load(inStream);
			}
			inStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return props;
	}
}
Let us assume that this class has been compiled and added to languageutils.jar file.

Add to Classpath

Copy languageutils.jar to sahi/userdata/extlib/ folder (create extlib folder if not present). Restart Sahi. Have a look at adding-jars-to-sahis-classpath for more details.

Call loadProperties Method from Sahi Script

var $LangUtils = Packages.com.example.utils.LanguageUtils;
var $props = $LangUtils.loadProperties("a.properties", false);
_alert($props.getProperty("name"));


Have a look at Rhino's documentation for a detailed discussion on Scripting Java through Rhino
Refer here for troubleshooting