Saturday, November 15, 2008

Guice and OSGi

I've been working on and off for the past couple years on a client-server data tracking framework I call "littleware", and over the last month I began revamping the dependency injection bootstrap code on the server side. I've already converted the client side to use Guice, and that has worked out great. A Guice trick that has worked very well for me on the client is to setup a Properties file that defines static user preferences .ini style, then have a Guice module pull Guice @Named constant values for injection.

On the server side I plan to try a two phase bootstrap process using both Guice and OSGi. The second OSGi phase will allow each bundle that contributes to the application to run through the OSGi startup/shutdown life cycle. Leveraging OSGi will also allow each module's code to take advantage of OSGi's service publish and discovery infrastructure. Here is what I have in mind.

  • First an application bootstrap method run by a configuration servlet will load a file to pull a list of Guice module classes and a list of OSGi BundleActivator classes.
  • Next the bootstrap method will allocate each Guice module via java reflection invocation of each module's no-argument constructor, and give the list of modules to Guice to create an injector.
  • Third the bootstrap method will use the Guice injector to allocate each OSGi bundle activator.
  • Finally, the bootstrap method will startup an embedded OSGi environment (probably Apache Felix), and seed the OSGi environment with the list of bundle activators.

This will be my first experience using OSGi, but I've read good things about it, and I know several prominent projects take advantage of it. I know OSGi has some support for dependency injection, but I've had greate experience with Guice, and Guice seems much better suited to that task. If all goes well, then I may extend the client to leverage OSGi in the same way. I'd like to extend the current client framework to embed several server-side technologies like javadb so that multiple clients running on the same machine can share services - especially cacheing.

Hopefully I'll be back in a month or so with a status update. I played around with maintaining a blog of XML files on my own server, but services like blogger are obviously a much simpler and better solution that I plan to take advantage of for now.

1 comment:

Unknown said...

It may be worth taking a look at the Peaberry project for Google-Guice which provides OSGi integration out the box. You can find details at: