Topics: |
Scripts extend the capabilities of the iSM server by enabling users to offer services through interpreted programming languages, such as JavaScript and Python.
The Mozilla Rhino engine for the JavaScript programming language, is currently included as a feature in the iSM libraries. The iSM platform can use any script engines that comply with JSR 223. The website scripting.dev.java.net hosts an open project to maintain several script engines that conform to JSR 223 (like Python).
The site also links to engines maintained elsewhere. You can learn more about the embedded JavaScript technology engine by visiting the Mozilla Rhino website.
The script iSM command allows you to invoke scripts dynamically and review the results using the iSM console. The iSM console format is:
script location [-entry scriptFunction][ p1[ p2[ …pN]]]
Parameter |
Description |
---|---|
location |
The script file location or script input string.
|
scriptFunction |
Name of the function within the script to execute. |
p1 through pN |
The script parameters. |
The command line is parsed on white space between elements, therefore the input script c:/program files/scripts/f2.js -entry func1 par1 par2 will result in the following parameter array being processed by the script engine:
script c:/program files/scripts/f2.js -entry func1 par1 par2
If you want to pass an embedded space (for example, a path like c:/program files/scripts/f2.js) then the parameter needs to be quoted (either single or double), as shown below:
script "c:/program files/scripts/f2.js" -entry func1 par1 par2
This command line results in the following parameter array being processed by the script engine:
script -file c:/program files/scripts/f2.js -entry func1 par1 par2
This will load the script file c:/program files/scripts/f2.js and call the function func1(p1,p2) with parameters par1, par2.
If you want to embed a quote (either single or double) into a parameter the quote needs to be escaped by using the backslash (\) character.
For example, script "function printVariableA(a) { return (\"the variable is:\\"\"+a+\"\\"\"); }" -entry printVariableA foo produces the following output:
SCRIPT: results: 'the variable is:"foo"'
Likewise, the line script "function printVariableA(a){return('the variable is:\\''a+a'\\'');}" -entry printVariableA foo produces the following output:
SCRIPT: results: 'the variable is:'foo''
The (\\) is necessary to exit the escape character.
The same holds true for variables passed to the following script:
script "function printVariableA(a) { return('the variable is:\\''+a+'\\''); }" -entry printVariableA "I pitty da\'fool"
The script above, produces the following output:
SCRIPT: results: 'the variable is:'I pitty da'fool''
To pass a Special Register to the script the command line could look like this:
script "function printVariableA(a) { return('the variable is:\\''+a+'\\''); }" -entry printVariableA _SREG(engine)
It results in the following output:
SCRIPT: results: 'the variable is:'base''
To pass a Special Register to the script as a named variable, the command line could look like this:
script "print('the variable is:\\''+a+'\\'');" _CONCAT('a=',_SREG(engine))
It produces the following output:
SCRIPT: results: 'the variable is:'base''
To assign a value, an array, or stack to a variableName, use the following format:
Type |
Format |
---|---|
single |
variableName=value |
array |
variableName=val1,val2,val3,... |
stack |
variableName=valn,val(n-1),val(n-2),...val0 |
To pass value, an array, or stack to a script function as a parameter the format is:
Type |
Format |
---|---|
single |
value |
array |
val1,val2,val3,... |
stack |
valn,val(n-1),val(n-2),...val0 |
Topics: |
The scripting engine supports the following embedded iSM objects:
Variable Name |
Description |
---|---|
XDDOC |
Current XDDocument (available only when executed as an iFL). |
XDSRM |
Special Register Manager |
XDMGR |
Current XDManager |
Accessing of the iSM objects is just the same as accessing any other built in JavaScript object.
For example, object.reference where object is the name of the built in object. Document, XDDOC, and reference is either a method or variable within the object.
The XDDOC object is valid only when the script engine is invoked as an iFL. it contains the current XML document and allows the script developer access to the XML document being processed. For example, the following JavaScript finds all of the elements with the name title and produces an html document containing an ordered list of 'titles' values:
importPackage(com.ibi.edaqm); var xmlRoot=XDDOC.getRoot(); var nodeList=xmlRoot.findAllByName('title').toArray(); document.write('<h3>Titles:</h3><p>'); // build an ordered list for display document.write('<ol>'); for(x in nodeList) { document.write('<li>'+nodeList[x].getValue()+'</li>'); } document.write('</ol>');
If you are using the XML document (bookstore.xml), then you would receive the following results:
<h3>Titles:<h3><p> <ol> <li>Everyday Italian</li> <li>Harry Potter</li> <li>XQuery Kick Start</li> <li>Learning XML</li> </ol>
The JavaScript imports the com.ibi.edaqm package that contains the definition of the XDDocument object. The getRoot method obtains the root element of the document. The method findAllByName obtains a list of all elements that are named 'title' the toArray method converts the list to a String array that the for(x in nodeList)statement may iterate through.
For more information, see the XDDocument description found in the iWay Service Manager Programmer's Guide for the methods and variables of the object.
The XDSRM object is available to the iSM Administration Console, as well as scripts invoked using the iWay Functional Language (iFL) interface. The XDSRM object allows the JavaScript developer access to the iSM Special Register (SREG) mappings. If invoked from the iSM Administration Console, then only those SREGs are assigned at the global level (set through the Server, Register Settings area of the iSM Administration Console).
When invoked as an iFL the object has reference to any SREG set within the current channel context such as the thread of execution in a process flow, upwards to the system manager. For example, the following JavaScript looks up the SREG mapping and iterates through the key set of the map to get the name and value of the SREG:
importPackage(com.ibi.edaqm); document.write('<h2>Simple Ordered List of Special Registers:</h2><p>'); var map = XDSRM.getSpecialRegisters(); var keys = map.keySet().iterator(); // build an ordered list for display document.write('<ol>'); while (keys.hasNext()) { var key = keys.next(); document.write('<li>'+key+'='+XDSRM.lookupSpecialRegister(key,'???')+'</li>'); } document.write('</ol>');
The returned results would be an HTML document, as shown below:
<h2>Simple Ordered List of Special Registers:</h2><p> <ol> <li>iway.lastnode=Service</li> <li>locale=en_us</li> <li>iwayworkdir=C:/PROGRA~1/iway8/config/32CD7798534EECF2F41C303A0DF286 1</li> <li>iwayconfig=32CD7798534EECF2F41C303A0DF28671</li> <li>iway.pid=1592</li> <li>name=ExecProcess</li> <li>doclocation=config</li> <li>val6=x=32CD7798534EECF2F41C303A0DF28671, y=C:/PROGRA~1/iway8/config/8.0.0.SM</li> <li>protocol=Lcl</li><li>iway.flowname=flowTest</li> <li>engine=32CD7798534EECF2F41C303A0DF28671</li> <li>iway.serverip=192.168.0.102</li><li>iwayversion=8.0.0.SM</li> <li>iway.serverhost=informat-cde89b</li> <li>iway.serverfullhost=192.168.0.102</li> <li>iwayhome=C:/PROGRA~1/iWay8/</li> <li>iway.channel=ExecProcess</li> </ol>
Results will vary depending on your configuration.
The getSpecialRegisters method is used to obtain a HashMap of all SREGs. From the HashMap, the keySet method returns the key Set object, and the iterator method returns the iterator object for that Set. While there are key objects within the iterator, the key object is obtained from the iterator. You can use the lookupSpecialRegister method of the XDSRM object to look up the value associated with the key.
For more information, see the SREG description found in the iWay Service Manager Programmer's Guide for the methods and variables of the object.
The XDMGR object is available to the iSM Administration Console, as well as scripts invoked using the iWay Functional Language (iFL) interface. This object allows scripts to the iSM XDManager object allowing the JavaScript writer accesses to the iSM channels that are configured on the system. For example, the following JavaScript looks up the XDMaster mapping and iterates through the key set of the map to get the name and status of the Master:
importPackage(com.ibi.edaqm); importPackage(java.util); document.write('<h2>Simple Ordered List of Masters:</h2><p>'); var map = XDMGR.getMasters(); var keys = map.keySet().iterator(); // build an ordered list for display document.write('<ol>'); while (keys.hasNext()) { var key = keys.next(); var master = XDMGR.getMaster(key); document.write('<li>'+key+', state='+master.getStateName()+'</li>'); } document.write('</ol>');
The returned results would be an HTML document, as shown below:
<h2>Simple Ordered List of Masters:</h2><p> <ol> <li>BAMChannel, state=inactive</li> <li>FTPServerToDOCROOT, state=inactive</li> <li>FILE2SFTP, state=inactive</li> <li>SOAP1, state=inactive</li> <li>IBILOCAL, state=inactive</li> ><li>IBIMVS, state=inactive</li> </ol>'
Results vary depending on your configuration.
The getMasters method is used to obtain a HashMap of all XDMaster (Channel) objects. From the HashMap, the keySet method returns the key Set object, and the iterator method returns the iterator object for that Set. While there are key objects within the iterator, the key object is obtained from the iterator. You can use the getMaster method of the XDMGR object to look up the Master (Channel). The getStateName method is used to get the displayable state that is associated with that Master.
For more information on the XDManager and its uses, see the Javadoc document found in the sub directory …/etc/doc/javadoc/com/ibi/edaqm.
The accessed manager may be a special manager if used in a process flow test. Such a manager cannot supply information pertaining to the currently running system as a whole. Only the manager in control of the runtime system can provide such information as the state of all running channels.
The following is a list of rules and considerations: