First, what is OSGi? That answer is actually quite simple, it is a container. Much like EJB, Servlet, or JEE, OSGI is just another java based container that adds value and makes doing certain things in java easier (and some harder). Just as servlets let your java code function easily on the web by handling all the nity gritty of sockets, http, and syncrhonization for you, the OSGi container tries to overcome a few specific weaknesses of the java classloader. Mainly allowing the following:
- Multiple version of the same classes and packages to exist in the same JVM
- Manage dependencies between different versions
- Allow code to be easily swapped in and out w/o ever stopping the JVM
Now there are a varity of features in OSGi that help achieve this, the service registry, life cycle management, etc. To put it simply however, all code is split up into special jars which OSGi calls bundles. These bundles are just regular jars with a bit more data in there MANIFEST.MF file. The container gives each bundles it’s own classloader, much like a servlet container gives each ear it’s own classloader. Then it tells each classloader about only the packages that bundle imports via the extra tags in MANIFEST.MF.
An example: Let’s say you have package A and package B and you want all code in A to use logback version 0.8.x and all code in B to use version 0.9.x. As you know in a traditional JVM the last version in the classpath will be assigned to both A and B. However if you were to make A, B, and both versions of logback OSGi bundles you could easily say A import-package:ch.qos.logback;version=”[0.8.0,0.9.0)” and B import-package:ch.qos.logback;version=”[0.9.0)”. This means A can use any version from 0.8.0 up to but not including 0.9.0 while B can use anything from 0.9.0 up to but not including 1.0.0. With me so far? If you need some more reading you can check the wiki http://en.wikipedia.org/wiki/OSGi or some of the great docs from any of the OSGi container sites. Also before I go on, just like with servlets where you have IBM, Sun, JBoss, and many others making servlet and JEE containers, there are also many providers of OSGi containers. Some popular implementations include: Spring DM a.k.a. Virgo, Felix, and Equinox. Equinox is the OSGi container that runs the popular IDE Eclipse…. yes many of the popular java based IDEs and servers you use are running on OSGi.. very meta!