Update: A great article on OSGi popped up on Javalobby today. Check it out.

I haven't yet written any thoughts about OSGi but it's something that's increasingly found its way on to my radar over the past year and a half or so. I've been doing a little bit of reading and research on it lately, (a quick introduction can be found via Adrian Colyer's talks on InfoQ about it). Needless to say it's got me excited. Really excited – to the point where I'm catching myself geeking out uncontrollably like Quagmire from Family Guy. What's got me all giggity? Let's take a step back first.

With Java there has always been a focus on modularity, but it's come up in different intermediate forms. Any typical Java developer can go on for hours about the importance for layering and properly isolating, decoupling and packaging components and subsystems. Java has the idea of a package, and the idea of JARs came about fairly early on in the process. But what became difficult was that often each layer had to have its own supporting infrastructure in place to be built independently, and someone needed to know how all of those JAR dependencies mapped out at build time and runtime. This was a necessary evil so that we could have individually deployable modules or subsystems. Once you got there, it was all worth it.

The problem was that while you may have been diligent and rigorous in your approach to applying how your projects were laid out and built, quite often the framework space solved this problem over and over again in many different ways. Going to upgrade a library was not an easy task, because you had to typically upgrade it across each of your layers, and what's more, there was no consistent approach to manage transitive dependencies amongst the modules each library used. Library selection and dependency management was the work of a greybeard within your project and it took a while to change a library or upgrade. Not to mention a near certainty that you'd need to execute your entire suite of regression tests to make sure nothing broke.

After seeing many Java frameworks being cajoled into a Maven build in order to manage builds and transitive dependencies, there has been increased awareness on the part of the vendor (be it commercial or open source) space on modularity. But we've only focused on the problem of solving modularity from a build-centric view. In other words, "How can I package and manage these build artifacts in such a way that consumers can easily integrate them into their project in a consistent and automated way?" This was the pie-in-the-sky idea behind Maven as I see it, and they pretty much made it happen. You'd be hard pressed to find a framework today that doesn't support it in some fashion.

But there's a whole separate piece of modularity that takes place at runtime. In the context of my historical explanation of modularity within Java, you can look at OSGi's ability to manage dependencies dynamically at runtime (via a header in your JAR MANIFEST file) as a "modularity win" and icing on the cake. But OSGi indeed goes farther than that. OSGi gives you the ability to install/uninstall, start and stop bundles dynamically (think Eclipse plugins) via its lifecycle, and that just plain gives me the "giggities." To account for truly dynamic modularity OSGi also gives you requires and provides semantics (also declared via headers) akin to the requires and provides semantics you see in dynamic languages like Ruby and some Javascript packaging/dependency management frameworks. While this may seem like its strength is only in making a clear separation between APIs you use and export, its real strength is that this allows you to truly isolate your modules at runtime from trickery that can (and has) happened on a classloader level. "That couldn't possibly work on an App server," you say? I was skeptical, too, but not so fast…

If you're the type that keeps up with the JCP space, you probably know there is a JSR "trinity" (equally as mysterious as its holy counterpart to the outside viewer) to provide a more robust modularization mechanism in varying degrees. That "quagmire" withstanding, OSGi has been mounting a considerable lead as the modularization system of choice in the JavaSE and EE/App server space. (It was already a formidable player in the embedded/mobile space, where it evolved from.) Nearly all major App server vendors have integrated the OSGi model into their core, which means they've probably cleaned up any classloader nastiness they've resorted to in the past. And now you're seeing some of the leading frameworks like Spring and Web Beans/Seam 3 (and subsequently have signed up to publish themselves as OSGi bundles.) Not to mention the success that Eclipse has seen since refactoring itself to work off its OSGi implementation, Equinox.

If OSGi can continue to make inroads in the framework space, I think its prospects to the "in the trenches" Java developer are very exciting indeed. Expect me to babble on more about this in the future…

More Related Content