Relieving the pain of testing IA javascript

Have you ever wanted to break things (as in thrust physical violence on mostly innocent inanimate objects, not as in "improving" code that results in stack traces) when testing your changes to the javascript in the IA?

We're all familiar with the steps:

  1. Navigate to where ever the IA resides
  2. Open the integration.js file in a text editor.
  3. Find some nice piece of code that works fine.
  4. "improve" it.
  5. Navigate back to the IA/bin directory or find the service on windows
  6. Restart the agent
  7. Then
    1. Watch the log spit out a stack trace or
    2. Login to the management system
    3. Find a ticket
    4. Enter stuff
    5. Watch the log spit out a stack trace
  8. Repeat 1-7 until you find a big hammer or you have a stress induced ulcer

Yes, fun as this is, I have a better way!

Some of us remember the days of the dreaded JavaClient and programming in Java. Java is a very powerful language and you can build some really cool stuff with it. (I know a great notification platform) However, not terribly friendly for those without (or with!) Computer Science degrees.

Fortunately, our friendly xDevloper types whipped up the Integration Agent. This made many tasks much easier, partly because they introduced a javascripting engine called Rhino.

Ok, so why do I care and how is this going to help my blood pressure?

Well, what if I told you it is possible to get an interactive java script command environment? What if I told you it used the exact same Java and Rhino versions the IA uses?

Wow, so much violence in this post, I won't make it a habit.

Anyway, the goods. All you need is to run this command:

*Edited: This code below just gets you the raw environment. However, trying importing any useful IA Classes results in the need for more head bashing... see the second entry for a more realistic use case.

Code

[user@tree bin]$ cd $IAHOME

[user@tree integrationagent-5.0.7]$ ./jre/bin/java -jar ./lib/mule-1.4.3/lib/user/js.jar

Rhino 1.6 release 5 2006 11 18

js>

Throw this into a rhino.sh file and set the execute flag (chmod +x rhino.sh) and save to the IAHOME/bin directory

rhino.sh

#! /bin/sh

# Set the base install directory. This basically just gets the current working directory, then goes up one level

INSTALL_DIR=$( cd "$( dirname "$0" )" && pwd )/..

# Here we set the class path, first to include the js.jar (rhino shell), then the IA jar files

CP=$INSTALL_DIR/lib/mule-1.4.3/lib/user/js.jar:$INSTALL_DIR/lib/com.alarmpoint.apex.integrationagent.jar:$INSTALL_DIR/lib/integrationagent-utils.jar

# Finally, call java and pass the class path we created above. Then we invoke the Main method of the Rhino shell, stored in js.jar

$INSTALL_DIR/jre/bin/java -cp $CP org.mozilla.javascript.tools.shell.Main

Windows Batch file:

rhino.bat

@echo off

rem Keep environment variables local to this script (not supported in all versions of Windows)

IF (%OS%) == (Windows_NT) SETLOCAL

rem Get SFN path to install directory.

rem A bug in some versions of Windows makes %~dsp0 append the filename, so use this workaround.

rem Note: correct to append .. instead of \ since %~dsp0 ends with \

for %%i in (%~sf0) do set INSTALL_DIR=%%~dspi..

cd %INSTALL_DIR%

set CP=lib\mule-1.4.3\lib\user\js.jar;lib\com.alarmpoint.apex.integrationagent.jar;lib\integrationagent-utils.jar;conf\cli;lib\mule-1.4.3\lib\mule\*;lib\mule-1.4.3\lib\user\*;lib\mule-1.4.3\lib\opt\*

jre\bin\java -cp "%CP%" org.mozilla.javascript.tools.shell.Main

From here, you can import the necessary packages using the importClass and importPackage function the integrations use. Also note you can use the "load" function to pull in other javascript files.

I just managed to figure this out, so I just had to share. I hope you all like it!

xMatters Reference

Blog- Originally created by Travis Depuy

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk