In this section: |
This section describes how to use _ESCRIPT and provides a few scripting examples.
Note: All the examples in this section use JavaScript as the scripting language.
The following example returns "hello world". Notice the script object that is obtained from the BSF host. The script object will allows access to the hosted methods available in iWay Service Manager.
importClass(java.lang.System); function mylog(s) { System.out.println("log: " + s); return s; } var script = bsf.lookupBean("script"); script.setResult("hello world");
The following example returns "hello world" and a name by querying an assumed internal document.
importClass(java.lang.System); function mylog(s) { System.out.println("log: " + s); return s; } var script = bsf.lookupBean("script"); var name = script.xpath("//parm[@name='name']/@value"); // name script.setResult("hello world "+name);
The following methods can be called from the script object:
You can pass parameters in a URL query string to the script. All iFL functions are passed an XML document from which to obtain values to process. Since there is not a document sent when issuing an HTTP GET on a URL, a document is constructed with the parameters sent in the query string of the URL.
The following is an example of a URL with a query string and its corresponding XML document, which would be passed to _ESCRIPT and the script which it calls:
URL:
http://localhost:9999/ism/enable-dat?regs=myescriptfeed¶m1=foo¶m2=bar
XML document
<myescriptfeed> <parm name="param1" value="foo"/> <parm name="param2" value="bar"/> </myescriptfeed>
These values can now be queried from the script as follows:
var param1 = script.xpath("//parm[@name='param1']/@value"); // foo var param2 = script.xpath("//parm[@name='param2']/@value"); // bar
The EnableDataSet is an internal class that can be used in your script to generate dat aset objects and serialize them as JSON objects.
importClass(java.lang.System); importClass(com.ibi.consolesm.plugin.models.ds.EnableDataSet); function mylog(s) { System.out.println("LOG: " + s); return s; } var script = bsf.lookupBean("script"); var format = script.xpath("//parm[@name='format']/@value"); // format var time = script.xpath("//parm[@name='time']/@value"); // time if (time.length() == 0) time = "28800000"; var project = script.xpath("//parm[@name='project']/@value"); // project if (project.length() == 0) project = bsf.lookupBean("parm"); var search = "lg-phones."+project; var eventsPiped = "" + script.ekeys(search); var eds = new EnableDataSet(); eds.addColumn("event"); eds.addColumn("count"); if (eventsPiped.length > 0) { for ( var i = 0; i < events.length; i++) { var events = (eventsPiped.indexOf("|") > -1 ? eventsPiped.split("|") : [ eventsPiped ]); var count = "" + script.ifl("_ecount(" + events[i] + ","+time+")"); var vals = [ count ]; if(events[i] == search) continue; var name = events[i].substr(search.length+1); if(name.indexOf('.') == -1) continue; eds.addRow(name, vals); } } script.setResult(eds.toString(1));
iWay Software |