Search code examples
javanetbeansjava-native-interfaceosgiosgi-bundle

OSGi and Java Native Interface


I want to develop OSGi bundle which can call Java Native interface. I have a few questions:

  1. Is it possible to develop OSGi bundle and place in it C wrapper classes and JNI? Is there already developed example which I can use?

  2. Is it possible java methods placed in OSGi bundle to call java methods placed into managed bean?

Best wishes

P.S. One more question: How I can make one simple managed bean into EJB?


Solution

  • You can do this fairly easily, and in a very portable way across platforms, by including the Bundle-NativeCode header in your bundle manifest.

    For example:

    Bundle-NativeCode: lib/mylib1.dll ; lib/mylib2.dll ;
      osname=Win32 ;
      processor=x86,
      lib/libmylib1.so ; lib/libmylib2.so ;
      osname=linux;
      processor=x86
    

    Will load

    • lib/mylib1.dll and lib/mylib2.dll (from inside the bundle) if the OS is Win32 and processor type is x86

    or

    • lib/libmylib1.so and lib/libmylib2.so if the OS is Linux and the processor type is x86

    The beauty of this approach is that you can include various different native libraries based on the architecture, and the OSGi runtime will automatically select the correct set for the current platofmr when you, for example, call System.loadLibrary("mylib1");

    Here's an old blog on the topic: http://robertvarttinen.blogspot.co.uk/2008/12/bundle-nativecode-in-osgi-manifest.html