Sunday, September 25, 2011

Java api in .Net (JavaIntrop)

In my previous blog I have said something about using a java library in .Net . My case demanded me to use a java library in .Net . Many times we find  a decent and clean library to use but its a java library and some time we find a java library that is impossible to ignore , in that case we hope for a way to use java api in .Net . I roamed the net and tried the fit and that fit happened to fit my case.  That is IKVM.Net by Jeroen Frijters .

http://www.ikvm.net/index.html

IKVM.net is a open source  java implementation , that comes with the OpenJdk  implementation  in .Net and various tools to interop between  java and .Net  . Whole JVM is implemented in  .Net and its very easy to convert java jar files to .Net assemblies and .Net assemblies to java  jars (interface and stub classes ).

Taking on my case to convert java jar files to .Net assemblies , there comes a command line utility with IKVM called ikvmc.exe . To generate the .Net assembly for a jar file we first specify the command line switch [-target:library] for generating  a dll .  Without this command line switch ikvmc.exe will generate a exe for a jar file if it encounters a main method in a class.  On command prompt we change directory to IKVM binaries folder and specify command :

C:\IKVM>ikvmc.exe –target:library  hapi-base-1.2.jar

The jar file is in same directory as ikvm binaries , this will generate the hapi-base-1.2.dll . There will be many warnings by ikvmc.exe of noclassdeffound  , this is because we haven’t specified the dependency dll’s. IKVM comes with the OpenJdk implementation in .Net and they are in form of dll’s. Now we have to specify the OpenJdk dll’s that our jar file depends upon to rectify the warnings.

C:\IKVM>ikvmc.exe –target:library  hapi-base-1.2.jar –reference:IKVM.OpenJDK.Core.dll  -reference:IKVM.OpenJDK.Util.dll

In example I have only referenced some of OpenJdk dll’s , but we have to reference all the dll’s that our jar file depends upon.  Upon completion of the command a hapi-base-1.2.dll will be generated . Now moving on to next jar file (hapi-structures-v21-1.2.jar).

C:\IKVM>ikvmc.exe –target:library  hapi-structures-v21-1.2.jar –reference:IKVM.OpenJDK.Core.dll  -reference:IKVM.OpenJDK.Util.dll

As this new jar file have dependency on previous jar file , we have to reference the dll of previous jar file to make a successful porting of this jar file to a dll.

C:\IKVM>ikvmc.exe –target:library  hapi-structures-v21-1.2.jar –reference:hapi-base-1.2.dll  –reference:IKVM.OpenJDK.Core.dll  -reference:IKVM.OpenJDK.Util.dll

And so on we can create dll’s from jar files resolving the dependency using the [-reference:<dllname>] .Now these dll’s are ready to be used in your .Net project . We have to at least add reference of IKVM.Runtime.dll in our project and other OpenJdk dll’s that out converted jar dll’s depend upon.  Keep on porting .

Friday, September 23, 2011

Health Standard 7 (HL7) Development - C#

Getting into the development of  HL7 system , there are not many free libraries on net to start with.  HL7 being the standard way to talk between various components in a modern medical infrastructure is a way too old format to work with (except with new xml based HL7 3.0).  This lend to the complexity while dealing with the HL7 development. 

HL7 messages is a flat text with standard specifying the format of the message. HL7 message is divided into segments to hold the medical record information of various types (ex patient details, insurance details,patient visit etc). These segments are separated by carriage returns. 

MSH|^~\&|DDTEK LAB|ELAB-1|DDTEK OE|BLDG14|200502150930||ORU^R01^ORU_R01|CTRL-9876|P|2.4 CR 
PID|||010-11-1111||Estherhaus^Eva^E^^^^L|Smith|19720520|F|||256 Sherwood Forest CR
EVN|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||CR OBR|1|948642^DDTEK OE|917363^DDTEK LAB|1554-5^GLUCOSE|||200502150730||||||||| CR

Segments  are  divided into fields separated by the pipe (|) and further fields are divided into   components and subcomponents separated  by  “ ^ “ .  There has been many versions of HL7 standards with several updates and new additions, primarily we can deduct most important versions to be  2.1,  2.3,  2.4,  2.5,  2.6  (2.x version) . Out of which 2.5 is most widely used and  stable version . Version  3.0 of  HL7 is a move to shift from flat file base to new xml based HL7 standard format,  but it is not yet that much accepted in common use.

Now coming on to the development of HL7 standard from the view point of .Net developer, what many option do we really have ?.  Having good knowledge of what to implement is easy as there are many resources available to make our self familiar with nitty-gritty of HL7 standard  , but having the tools to implement HL7 is another matter.  There is support from Microsoft on HL7 in form of BizTalk Accelerator for HL7 ( http://www.microsoft.com/biztalk/en/us/accelerator-hl7.aspx )   , but as you guessed its not free  . Second option is to write a new library to deal with HL7, but its not a trivial task , as HL7 standard is divided into various versions and industry implement all these versions to certain degree . Another option is to write only specific demand based api for HL7 , targeting a single module or implementation of certain feature of HL7, but that's also not a very good solution as what happens when that specific api expands to accommodate multiple version of HL7. Why to reinvent the wheel , there should be some thing on web that targets the HL7 development in  .Net.  

Two top notch free solution for HL7 development are Hapi and NHapi . Hapi is open source extensive  java library   to deal with almost all the versions and angles of HL7 development.  Nhapi is  .Net implementation of the Hapi project.

Hapi:-  http://hl7api.sourceforge.net/

NHapi:- http://nhapi.sourceforge.net/home.php

So we do have .Net open source library for HL7 development . NHapi supports versions from 2.1  to  2.5 and will suffice for most of HL7 related development.  API’s are simple and easy to use . It creates a Object model for a HL7 message being parsed and can convert that into HL7 standard xml for easy use or for transformation via xslt. Using the HL7 message object model we can do variety of things related to the HL7 parsing and creation. 

Downside is that Nhapi is not as extensive as Hapi (Java library).    HL7 message validation is not there except for some low level validation (e.g  MSH segment validation , primitive validation etc ) . HL7 communication module is not there . Support up to 2.5 version of HL7 as comparison to  Hapi support up to 2.6 version . Most important of all there has been no activity recorded for Nhapi for some time and it makes future dependence on Nhapi a little scary (although its a open source and anybody is invited to contribute and extend ). 

Hapi is on the top list when it comes to the HL7 library , they recently released  Hapi 1.2 in June and forums are abuzz with discussions . Support for 2.6 is included and future support for HL7 3.0 and more are anticipated. HL7 message validation is very mature and there is good support for HL7 message communication .

So the question is how we,  .Net developers can leverage Hapi project and use it in our .Net applications . The answer can be  in using   java in [ .Net] ,  is it possible ? . Yes we can use java libraries in [.Net] , as they are [.Net] libraries and can use  full feature of  Hapi project.

Java interop tool developed  Jeroen Frijters (who is contributed for bringing  J# to its demise)  is the best and easy bet to use Hapi java api in .Net . I have done it and it works great . I will be soon writing on how to convert java jar files to .Net assemblies.

Technorati Tags: