web stats
Run Javascript in Channel Context Outside of Mirth - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 06-15-2019, 03:52 AM
Jeff Drumm Jeff Drumm is offline
Mirth Newb
 
Join Date: Jun 2019
Posts: 23
Jeff Drumm is on a distinguished road
Default Run (Some) Mirth Javascript from NotePad++

Hi all; short time Mirther, first time poster, LONG time healthcare integration nerd going back to the STC Datagate days. Happy to be here

I've searched this forum and scoured the web with no success ...

Is there any way to execute JavaScript outside of running it in a channel? I've been trying to figure out a Rhino configuration that "sees" the packages Mirth includes so I can write utility functions in an external editor and speed up the code/test process a bit. Being able to do it in Eclipse or even Notepad++ (via NPPExec) would be fantastic. Not that I don't love the syntax highlighting and code completion in the Mirth editor, but the code/save/deploy/submit message/view log(s) process is a bit more ... fiddly than I'd prefer.

I'm not looking to have a real-time debugger active (at least yet), but if that was an option with Eclipse I sure wouldn't kick it out of bed

I've tried setting the classpath various ways and invoking Rhino from the server-lib directory with different combinations of command line options, but it keeps choking on attempts to load the mirth-supplied Java classes with the infamous "uncaught JavaScript runtime exception: TypeError: Cannot call property <method> in object [JavaPackage <package>]. It is not a function, it is "object".

Everything I've read points to this being an issue with Rhino either unable to find the jar file(s) or that I'm running it under the wrong version of Java ... but:

Code:
C:\>java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)


Thanks in advance for any help you can provide!
Attached Images
File Type: png MurphKinectJavaVer.png (17.3 KB, 51 views)

Last edited by Jeff Drumm; 06-19-2019 at 09:36 AM. Reason: Change to a more accurate title
Reply With Quote
  #2  
Old 06-18-2019, 03:25 PM
Jeff Drumm Jeff Drumm is offline
Mirth Newb
 
Join Date: Jun 2019
Posts: 23
Jeff Drumm is on a distinguished road
Default

So ... I'm not sure whether I need to apologize to the moderators or not, but I'm going to do so proactively since I've been bugging them about the fact that this post hadn't appeared in the forum after I posted it. I'm now wondering whether it DID appear and I just missed it ...


Anyway, I've figured out a partial solution to my original request using Notepad++, NPPExec and the Rhino interpreter and debugger included with Mirth Connect. This is handy for "quick 'n dirty" testing of code you intend to include in a transformer script or use as a function in a code template. There are probably ways to make it more useful than it is currently, too ...
  • Install the NppExec plugin
  • Enable the Console using Plugins | NppExec | Show Console
  • Select Plugins | NppExec | Execute
  • Enter the following in the Command(s) window:
Code:
ENV_SET CLASSPATH=.\*;.\commons\*;.\database\*;.\donkey\*;.\javax\*;.\jersey\*;.\jms\*;.\swagger\*
set rhinover=200
CD C:\Program Files\Mirth Connect\server-lib
NPP_SAVE
java org.mozilla.javascript.tools.shell.Main -version $(rhinover)  "$(FULL_CURRENT_PATH)"
  • Click Save and enter a command name (I used "Run Javascript")
  • In the line that begins with the word "java" {the last line), change the word "shell" to "debugger"
  • Click Save and enter a command name (I used "Debug Javascript")
Save your file and choose Run Javascript to execute and view the output/errors in the console window. Alternately you can select Debug Javascript and it will launch the Rhino GUI debugger. You'll have to exit the debugger if you want to launch it again after re-saving your file. Alternately you can go to the File menu in the debugger and reload it.

I haven't tried any message parsing/manipulation with E4X yet, so I don't know that this will be usable for that. At least I can test general string/date/number manipulation and invoke Java methods available via the Packages interface (such as the org.apache.commons.ssl.OpenSSL.encrypt/decrypt methods) without the save/deploy/trigger channel/view logs cycle from within Mirth.

If this is stupid or otherwise useless, let me know. I can take it

Last edited by Jeff Drumm; 06-18-2019 at 07:18 PM.
Reply With Quote
  #3  
Old 06-18-2019, 06:34 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 967
agermano is on a distinguished road
Default

Great first post! I use the interactive rhino shell all the time. Neat what you did to integrate it with Notepad++. e4x most definitely works.

Your class paths hit a good deal of what you might need. What are you missing is all of the plugins in the extensions directory (rather than server-lib.) This includes all of the data types (and their related serializers.)
Reply With Quote
  #4  
Old 06-18-2019, 06:42 PM
Jeff Drumm Jeff Drumm is offline
Mirth Newb
 
Join Date: Jun 2019
Posts: 23
Jeff Drumm is on a distinguished road
Default

Quote:
Originally Posted by agermano View Post
Great first post! I use the interactive rhino shell all the time. Neat what you did to integrate it with Notepad++. e4x most definitely works.

Your class paths hit a good deal of what you might need. What are you missing is all of the plugins in the extensions directory (rather than server-lib.) This includes all of the data types (and their related serializers.)

Thanks! I'll update the initial post as I get the CLASSPATH requirements figured out.


EDIT: Yikes! There's a LOT of stuff in the extensions directory. I'm no Java expert, but I understand that it doesn't recurse directories in the CLASSPATH so I'd need one that contains all of them (or perhaps multiple NPPExec scripts each containing a classpath with a subset of them, depending on what sort of script you want to execute/debug).


I've made other changes to the NPPExec configuration that provide a cleaner output (background and text colors, filters) and I'll include those as well. Stay tuned ...

Last edited by Jeff Drumm; 06-18-2019 at 06:53 PM. Reason: Required a Yikes!
Reply With Quote
  #5  
Old 06-18-2019, 06:45 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 967
agermano is on a distinguished road
Default

Also, the SerializerFactory from the User API doesn't work outside of mirth because it checks with the controllers to see which data types are currently registered.

You can still build them yourself, though.

Code:
var deserializationProperties = new com.mirth.connect.plugins.datatypes.hl7v2.HL7v2DeserializationProperties();
var serializationProperties = new com.mirth.connect.plugins.datatypes.hl7v2.HL7v2SerializationProperties();
var serializerProperties = new com.mirth.connect.model.datatype.SerializerProperties(serializationProperties,deserializationProperties,null);
var ser = new com.mirth.connect.plugins.datatypes.hl7v2.ER7Serializer(serializerProperties);
var msg = new XML(ser.toXML('DG1|1||250.00^^I9'));
msg.DG1['DG1.1']['DG1.1.1'] = 2;
var encoded = ser.fromXML(msg.toXMLString());
// DG1|2||250.00^^I9
Reply With Quote
  #6  
Old 06-18-2019, 06:58 PM
Jeff Drumm Jeff Drumm is offline
Mirth Newb
 
Join Date: Jun 2019
Posts: 23
Jeff Drumm is on a distinguished road
Default

Quote:
Originally Posted by agermano View Post
Also, the SerializerFactory from the User API doesn't work outside of mirth because it checks with the controllers to see which data types are currently registered.

You can still build them yourself, though.

Code:
var deserializationProperties = new com.mirth.connect.plugins.datatypes.hl7v2.HL7v2DeserializationProperties();
var serializationProperties = new com.mirth.connect.plugins.datatypes.hl7v2.HL7v2SerializationProperties();
var serializerProperties = new com.mirth.connect.model.datatype.SerializerProperties(serializationProperties,deserializationProperties,null);
var ser = new com.mirth.connect.plugins.datatypes.hl7v2.ER7Serializer(serializerProperties);
var msg = new XML(ser.toXML('DG1|1||250.00^^I9'));
msg.DG1['DG1.1']['DG1.1.1'] = 2;
var encoded = ser.fromXML(msg.toXMLString());
// DG1|2||250.00^^I9

Ahh, good stuff. We need a "like" button
Reply With Quote
  #7  
Old 06-18-2019, 07:02 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 967
agermano is on a distinguished road
Default

Another thing you'll want to do is set the appropriate Rhino language version when you launch the shell. I'm assuming you found this already https://developer.mozilla.org/en-US/...ts/Rhino/Shell

For all mirth versions prior to 3.7 mirth runs with version 0 (I realize they don't list it as a valid option on that page, but it is.) They call this the "default" version. It's roughly equivalent to 160.

In 3.7 they let you specify the rhino version in the mirth.properties file. If you upgrade to 3.7 from a prior version it will keep the version of default. In a fresh install it will be set to ES6, which is actually Rhino version 200. The rhino that ships with 3.7 will launch the shell in version 180 if you don't specify.

Last edited by agermano; 06-18-2019 at 07:08 PM.
Reply With Quote
  #8  
Old 06-18-2019, 07:18 PM
Jeff Drumm Jeff Drumm is offline
Mirth Newb
 
Join Date: Jun 2019
Posts: 23
Jeff Drumm is on a distinguished road
Default

Quote:
Originally Posted by agermano View Post
Another thing you'll want to do is set the appropriate Rhino language version when you launch the shell. I'm assuming you found this already https://developer.mozilla.org/en-US/...ts/Rhino/Shell

For all mirth versions prior to 3.7 mirth runs with version 0 (I realize they don't list it as a valid option on that page, but it is.) They call this the "default" version. It's roughly equivalent to 160.

In 3.7 they let you specify the rhino version in the mirth.properties file. If you upgrade to 3.7 from a prior version it will keep the version of default. In a fresh install it will be set to ES6, which is actually Rhino version 200. The rhino that ships with 3.7 will launch the shell in version 180 if you don't specify.

Didn't catch that, thanks. I've updated the sample script in the second post to set it to 200 (I'm at MC 3.7.1). Y'all are welcome to set it to whatever version suits you
Reply With Quote
  #9  
Old 06-19-2019, 03:40 AM
Jeff Drumm Jeff Drumm is offline
Mirth Newb
 
Join Date: Jun 2019
Posts: 23
Jeff Drumm is on a distinguished road
Default

Ok, change in plans.

I couldn't get NPPExec to set the full CLASSPATH; I think there's a string length limitation of 1,024 bytes for the variable's value in its ENV_SET function. So here's what I've done ...

I created a Windows .CMD file called runRhino.cmd in a directory I use for utility scripts so that they don't get clobbered by software reinstallations or cleanups (C:\bin\ ):
Code:
@ECHO OFF
SET MIRTHDIR=C:\Program Files\Mirth Connect\
SET CLASS=%1
SET VER=%2
SET FILE=%3
CD %MIRTHDIR%
SET CLASSPATH=.\server-lib\*;.\server-lib\commons\*;.\server-lib\database\*;.\server-lib\donkey\*;.\server-lib\javax\*;.\server-lib\jersey\*;.\server-lib\jetty\*;.\server-lib\jms\*;.\server-lib\swagger\*;.\server-lib\javax\jaxb\*;.\server-lib\javax\jaxws\*;.\server-lib\javax\jaxb\ext\*;.\server-lib\javax\jaxws\ext\*;.\server-lib\jersey\ext\*;.\server-lib\jetty\jsp\*;.\server-lib\swagger\ext\*;.\custom-lib\*;.\extensions\*;.\extensions\dashboardstatus\*;.\extensions\datapruner\*;.\extensions\datatype-delimited\*;.\extensions\datatype-dicom\*;.\extensions\datatype-edi\*;.\extensions\datatype-hl7v2\*;.\extensions\datatype-hl7v3\*;.\extensions\datatype-json\*;.\extensions\datatype-ncpdp\*;.\extensions\datatype-raw\*;.\extensions\datatype-xml\*;.\extensions\destinationsetfilter\*;.\extensions\dicom\*;.\extensions\dicomviewer\*;.\extensions\directoryresource\*;.\extensions\doc\*;.\extensions\extensionmanager\*;.\extensions\file\*;.\extensions\globalmapviewer\*;.\extensions\http\*;.\extensions\httpauth\*;.\extensions\imageviewer\*;.\extensions\javascriptrule\*;.\extensions\javascriptstep\*;.\extensions\jdbc\*;.\extensions\jms\*;.\extensions\js\*;.\extensions\mapper\*;.\extensions\messagebuilder\*;.\extensions\mllpmode\*;.\extensions\pdfviewer\*;.\extensions\rulebuilder\*;.\extensions\scriptfilerule\*;.\extensions\scriptfilestep\*;.\extensions\serverlog\*;.\extensions\smtp\*;.\extensions\tcp\*;.\extensions\textviewer\*;.\extensions\vm\*;.\extensions\ws\*;.\extensions\xsltstep\*;.\extensions\dicom\lib\*;.\extensions\dicomviewer\lib\*;.\extensions\doc\lib\*;.\extensions\file\lib\*;.\extensions\httpauth\src\*;.\extensions\pdfviewer\lib\*
java org.mozilla.javascript.tools.%CLASS%.Main -version %VER% %FILE%
I then replaced the Run Javascript command with:
Code:
set rhinover=200
NPP_SAVE
C:\bin\runRhino.cmd shell $(rhinover) "$(FULL_CURRENT_PATH)"
And the Debug Javascript command with:
Code:
set rhinover=200
NPP_SAVE
C:\bin\runRhino.cmd debugger $(rhinover)  "$(FULL_CURRENT_PATH)"
Seems to work, so far. Fingers crossed!

Last edited by Jeff Drumm; 06-20-2019 at 06:04 PM. Reason: CLASSPATH incorrrect
Reply With Quote
  #10  
Old 06-19-2019, 10:02 AM
Jeff Drumm Jeff Drumm is offline
Mirth Newb
 
Join Date: Jun 2019
Posts: 23
Jeff Drumm is on a distinguished road
Default

Question for the mods: The thread title doesn't really describe how this all ended up. Would it be possible to change it to something like "Testing Mirth/Rhino JavaScript in NotePad++?"
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 08:55 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation