Plain Old Java Object (POJO) example
Start with a Plain Old Java™ Object (POJO, in this example) that contains all of the business logic for your extension.
For example:
public class Extension {
public static void log(String msg) {
System.out.println(msg);
}
}
In this case, the POJO contains a single method. Your typical extension contains more logic. For example:
static class FESIExtension implements JSExtension {
public void initializeExtension(JSGlobalObject go) throws JSException {
// Create the prototype
final JSObject prototype = go.makeJSObject();
prototype.setMember("log", new JSFunctionAdapter() {
public Object doCall(JSObject thisObject, Object[] args)
throws JSException {
if (args.length >= 1) {
Extension.log(args[0].toString());
}
return null;
}
});
final JSObject obj = go.makeJSObject(prototype);
// This is the name of the object to be used in JavaScript Code
go.setMember("CustomExtension", obj);
go.setMember("log", new JSFunctionAdapter() {
public Object doCall(JSObject thisObject, Object[] args)
throws JSException {
if (args.length >= 1) {
Extension.log(args[0].toString());
}
return null;
}
});
go.setMember("Logger", new JSFunctionAdapter() {
public Object doNew(JSObject thisObject, Object[] args)
throws JSException {
JSGlobalObject go = thisObject.getGlobalObject();
JSObject proto = go.makeJSObject();
proto.setMember("log", new JSFunctionAdapter() {
public Object doCall(JSObject thisObject, Object[] args)
throws JSException {
if (args.length >= 1) {
Extension.log(args[0].toString());
}
return null;
}
});
final JSObject obj = go.makeJSObject(proto);
return obj;
}
});
}
}
This FESI extension has three main parts:
- First, the extension makes a
JSObject
named prototype and adds the method “log” to prototype:final JSObject prototype = go.makeJSObject(); prototype.setMember("log", new JSFunctionAdapter() { public Object doCall(JSObject thisObject, Object[] args) throws JSException { if (args.length >= 1) { Extension.log(args[0].toString()); } return null; } }); go.setMember("CustomExtension", obj);
The prototype
JSObject
is then added to theJSGlobalObject
with the nameCustomExtension
. This addition allows scripts to call:CustomExtension.log("message");
- The second part of the extension creates a global function named
log
.go.setMember("log", new JSFunctionAdapter() { public Object doCall(JSObject thisObject, Object[] args) throws JSException { if (args.length >= 1) { Extension.log(args[0].toString()); } return null; } });
Now, a script can call:
log("message");
- The third part of the extension creates a constructor that can
be called from scripts. For example:
go.setMember("Logger", new JSFunctionAdapter() { public Object doNew(JSObject thisObject, Object[] args) throws JSException { JSGlobalObject go = thisObject.getGlobalObject(); JSObject proto = go.makeJSObject(); proto.setMember("log", new JSFunctionAdapter() { public Object doCall(JSObject thisObject, Object[] args) throws JSException { if (args.length >= 1) { Extension.log(args[0].toString()); } return null; } }); final JSObject obj = go.makeJSObject(proto); return obj; } });
With this constructor, scripts can do the following:
var logger = new Logger(); logger.log("message");