[vtkusers] VTK + OSGi & RenderCreate native method

Thiébault Benoît dev at artenum.com
Fri Nov 5 08:01:38 EDT 2010


Hi everyone,

I am very pleased to tell you I finally managed to have a working OSGi bundle with VTK for Mac OS X.

The error message I had came from the way OSGi bundle classloader manages package visibility.

In standard Java applications, the Classloader makes all packages accessible by anyone. In a modular frameworks like OSGi, the idea is that each bundle has to declare what it needs (Import-Package) and what it provides (Export-Package). The framework then enforces these visibility constraints in order to build a loosely coupled module system.

In OSGi, when a package is not explicitly exported, it is not visible to others. And when it is not explicitly imported by a bundle, this one cannot access it. By default in OSGi, only java.* packages are available to anyone.

And this is what causes our error above: the RenderCreate native method requires apple.awt.ComponentModel to work properly. But the apple.awt package was not declared in the bundle Import-Package tag, so when the native code is called, it does not find it.

There are two ways to solve the problem:

	• In Felix/Equinox configuration settings, add the "org.osgi.framework.bootdelegation=apple.*" property. This tells OSGi that all apple.* packages are visible to all bundles. This works, but you somewhat lose the modularity of OSGi, as everyone can access those packages.
	• Explicitly import the package in the bundle manifest Import-Package entry. For OSGi to resolve this new dependency of your bundle, you can either:
		• add org.osgi.framework.system.packages.extra=apple.awt in Felix/Equinox configuration settings (your configuration is however now platform-dependent)
		• create a system bundle fragment that exports apple.awt (and contains no other files than the manifest)

I have updated my article on the subject, so if anyone is interested, you can read it here: http://dev.artenum.com/blog/ben/posts/osgi_vtk_and_macosx

Kind regards,

Benoît


Le 3 nov. 2010 à 14:18, Thiébault Benoît a écrit :

> By the way, wouldn't it be simpler to have vtkPanel based on Swing rather than AWT?
> 
> If I recall correctly, there were already some comments about that in recent posts on the mailing list.
> What are Kitware plans for Swing support?
> 
> Benoît
> 
> Le 3 nov. 2010 à 13:12, Thiébault Benoît a écrit :
> 
>> Hi everyone,
>> 
>> I have narrowed my search for the error I talked about with OSGi.
>> 
>> The error comes from file Common/vtkJavaAwt.h, line 87:
>> dsi = ds->GetDrawingSurfaceInfo(ds);
>> 
>> It is upon calling this method (that is declared in jawt.h) that the JVM crashes.
>> Have anyone already seen this?
>> 
>> What I especially don't understand is why it works in a classic Java application and not in OSGi... Any idea
>> 
>> Kind regards
>> 
>> Benoît
>> 
>> Le 29 oct. 2010 à 16:02, Thiébault Benoît a écrit :
>> 
>>> Hi everyone
>>> 
>>> as explained in this other post (http://www.vtk.org/pipermail/vtkusers/2010-October/112895.html) and in my article (http://dev.artenum.com/blog/ben/posts/osgi_vtk_and_macosx), I have embedded VTK dynamic libraries inside an OSGi bundle.
>>> 
>>> I have the same error message than Jérémie who did not embed the libraries in the JAR bundle file but passed the DYLD_LIBRARY_PATH to the external libraries directory.
>>> 
>>> However, since I used the -Xcheck:jni option, I have a more verbose error:
>>> 
>>> WARNING in native method: JNI call made with exception pending
>>> 	at vtk.vtkPanel.RenderCreate(Native Method)
>>> 	at vtk.vtkPanel.Render(vtkPanel.java:166)
>>> 	- locked <1060ffa88> (a vtk.vtkPanel)
>>> 	at vtk.vtkPanel.paint(vtkPanel.java:189)
>>> 	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
>>> 	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
>>> 	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
>>> 	at java.awt.Component.dispatchEventImpl(Component.java:4790)
>>> 	at java.awt.Component.dispatchEvent(Component.java:4544)
>>> 	at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
>>> 	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
>>> 	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
>>> 	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
>>> 	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
>>> 	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
>>> 	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
>>> FATAL ERROR in native method: Bad global or local ref passed to JNI
>>> 	at vtk.vtkPanel.RenderCreate(Native Method)
>>> 	at vtk.vtkPanel.Render(vtkPanel.java:166)
>>> 	- locked <1060ffa88> (a vtk.vtkPanel)
>>> 	at vtk.vtkPanel.paint(vtkPanel.java:189)
>>> 	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:276)
>>> 	at sun.awt.RepaintArea.paint(RepaintArea.java:241)
>>> 	at apple.awt.ComponentModel.handleEvent(ComponentModel.java:263)
>>> 	at java.awt.Component.dispatchEventImpl(Component.java:4790)
>>> 	at java.awt.Component.dispatchEvent(Component.java:4544)
>>> 	at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
>>> 	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
>>> 	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
>>> 	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
>>> 	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
>>> 	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
>>> 	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
>>> Invalid memory access of location 0x0 rip=0x1010af1f4
>>> ./ben.sh: line 11: 23453 Abort trap              java -Xcheck:jni -jar org.eclipse.osgi_3.6.0.v20100517.jar -console
>>> 
>>> This is not a ClassLoader issue. In OSGi, each bundle has its own classloader. My VTK bundle both loads the vtk.jar and the native libraries and should thus use the same classloader to perform both.
>>> 
>>> To be sure about it, I added in the vtkNativeLibrary.java class a new method, called LoadAllNativeLibrariesForOSGi(), that performs all the System.loadLibrary calls that I previously did in my OSGi BundleActivator (this way, it is executed in the same ClassLoader that the one that loaded vtk.jar). I re-compiled VTK and ended with the same error message.
>>> 
>>> Do you have any idea what could cause this problem?
>>> I read here http://www.vtk.org/pipermail/vtkusers/2007-February/089476.html that it could come from a compatibility issue with OSX Cocoa. The solution suggested there was to compile using Carbon instead of Cocoa. But:
>>> - this is not a satisfactory solution, as Carbon is less and less supported by Apple
>>> - it is not possible on the latest versions of MacOSX, as Java is 64bits only and Carbon cannot be compiled in 64bits
>>> 
>>> Any suggestion? :-)
>>> 
>>> Kind regards,
>>> 
>>> Benoît
>>> 
>>>>> De : Gerrick Bivins <gbivins at objectreservoir.com>
>>>>> Date : 11 octobre 2010 17:31:40 HAEC
>>>>> À : Jérémie Turbet <jet at artenum.com>
>>>>> Cc : vtkusers at vtk.org
>>>>> Objet : Rép : [vtkusers] VTK + OSGi & RenderCreate native method
>>>>> 
>>>>> Hi,
>>>>> I haven't done osgi specifically, but have created a netbeans platform module (similar concept, see this link http://wiki.netbeans.org/OSGiAndNetBeans#NetBeans_and_OSGi) that wraps vtk. 
>>>>> 
>>>>> I ended up forcing the load of the required libs for my app when the module loads via the module installer (not sure what the equivalent is in osgi terms,bundle activator maybe?) similar to the technique described here for loading jogl in a netbeans application:
>>>>> http://forums.netbeans.org/viewtopic.php?t=16272
>>>>> 
>>>>> I had issues on mac where it wouldn't load the libraries unless I specified the 
>>>>> arch type of the libs matched that of the jvm. Also, had some weirdness with the suffixes of the libs (jnilib vs dylib see :http://forums.netbeans.org/ntopic16681.html) 
>>>>> 
>>>>> On windows or linux, things worked as expected (ie how it was described in the docs) so you may want to try your module on one of those platforms before trying on OSX.
>>>>> 
>>>>> biv
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> On Mon, Oct 11, 2010 at 7:29 AM, Jérémie Turbet <jet at artenum.com> wrote:
>>>>> 	Hi all,
>>>>> 
>>>>> Before my question, a small description of the context: 
>>>>>   I'm trying to create an OSGi bundle displaying some VTK stuff using Java wrapping.
>>>>>   The service class is a simple POJO (OSGi classes and configuration are managed using iPOJO). when I test it out of OSGi environment, it works well, load libraries, and displays a beautiful 3D cone in a window. When I ask to the OSGi container to launch my service, libraries are loaded, objects are instantiated, but as soon as I ask a rendering (using vtkPanel::Render() ) I get this error "Invalid memory access of location 0x0 rip=0x10109cdcc" which looks like a null pointer exception...
>>>>>   
>>>>>   For a debugging purpose, I have made a copy/paste of vtkPanel class into MyVtkPanel (which is in my application package) and replace the vtkPanel by MyVtkPanel into my small test application. I expected no change in the resulting behaviour, instead I have an unsatisfied link error on RenderCreate() method.
>>>>> This method is declared as native, but my DYLD_LIBRARY_PATH points to the VTK libs. I thought it should have found this method....
>>>>> 
>>>>> Does someone has an idea of why this error occurs, is there some access restriction due to the class location (which is the only change... I guess...) ?
>>>>> 
>>>>> to summarise:
>>>>> vtkPanel panel = new vtkPanel()
>>>>> panel.Render()
>>>>> works fine
>>>>>  
>>>>> vtkPanel panel = new vtkPanel()
>>>>> panel.Render()
>>>>> leads to "Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: test.vtk.impl.MyVtkPanel.RenderCreate(Lvtk/vtkRenderWindow;)I
>>>>> 	at test.vtk.impl.MyVtkPanel.RenderCreate(Native Method)"
>>>>> 
>>>>> 
>>>>> >nm libvtkRenderingJava.dylib  | grep RenderCreate
>>>>> 0000000000047b9b T _Java_vtk_vtkPanel_RenderCreate
>>>>> 
>>>>> Tested under Mac OSX snow leopard with VTK 5.40, 5.6.0 and 5.6.1
>>>>> 
>>>>> 	Regards,
>>>>> 
>>>>> 		JeT
>>>>> 
>>>>> PS: if someone has some experience with VTK + OSGi, some tips would be of great help. I'm a little bit stuck with it...
>>>>> 
>>>>> _______________________________________________
>>>>> Powered by www.kitware.com
>>>>> 
>>>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>>> 
>>>>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>>>> 
>>>>> Follow this link to subscribe/unsubscribe:
>>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>>> 
>>>>> 
>>>>> _______________________________________________
>>>>> Powered by www.kitware.com
>>>>> 
>>>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>>> 
>>>>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>>>> 
>>>>> Follow this link to subscribe/unsubscribe:
>>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>> 
>>> 
>>> 
>>> _______________________________________________
>>> Powered by www.kitware.com
>>> 
>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>> 
>>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>> 
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.vtk.org/mailman/listinfo/vtkusers
>> 
>> _______________________________________________
>> Powered by www.kitware.com
>> 
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>> 
>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20101105/dab7d3e2/attachment.htm>


More information about the vtkusers mailing list