Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Guice with OSGi

Tags:

java

osgi

guice

I have a project that I am trying to convert to OSGi. However, this project has been built with Guice as its dependency injection manager. It's a large project with Guice interwoven all throughout. Guice has been giving me many fits with the conversion process. Specifically, it has been given me a NoClassDefFoundError for com.google.inject.internal.cglib.reflect.FastClass. I have made sure that nothing is package-private and I also downloaded and used the guice-2.0-customloader.jar. Still, to no avail.

Is there any advice from anyone that has gone down this path? It is worth mentioning that I use peaberry to get dynamic services from the OSGi environment (for a plugin-type architecture) that I then use in the program later on.

Thanks a lot, Steve

like image 898
Stephen Watkins Avatar asked May 30 '26 21:05

Stephen Watkins


2 Answers

Hard to say exactly what the problem is, classloader issues can be very situation specific in OSGi and hard to debug.

If guice really is a core to your application it may make sense to make it a Fragment bundle of the System Bundle. The System Bundle is the root node in the classloader tree for ALL bundles. If classes exist in this bundle, they'll be accessible everywhere. This is a technique I've used successfully to wrap Swing LAF (Substance, Alloy, Jide) as OSGi bundles. They've gotta be at the root level since all of my bundles use Swing (especially the Swing classloader)

You'll want to check the OSGi specs for creating a Fragment bundle and making that fragment bundle a framework extension fragment. I've only been able to do this using Eclipse's Equinox runtime. At the time I wrote most of the supporting classes, Apache Felix did not yet support framework extension fragments. It may have changed now. I have no idea about frameworks (knopplefish, spring-dm)

like image 168
basszero Avatar answered Jun 02 '26 11:06

basszero


Just use Peaberry. It has all the support you need for using Guice in OSGi. No need to reinvent everything from scratch!

like image 43
Neil Bartlett Avatar answered Jun 02 '26 10:06

Neil Bartlett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!