In a few words OSGi is a standard used to build modular applications. It adds new level of modularity - bundles (aka. components, modules). Each bundle contains classes and interfaces and what is the most important it must explicitly state:
- which other components or Java packages it uses,
- and which packages it wants to expose to be used by other components.
From technical point of view bundles are jar files which have a bit extended META-INF/MANIFEST.MF
file. All the aforementioned information is stored in MANIFEST.MF
file.
From practical point of view this explicitness has both advantages and disadvantages. For me the biggest advantage is that you are forced to think about your architecture, modules and interaction between them more than in standard applications. This may create better architecture where each module is responsible for well defined tasks and modules can be reused. If it comes to disadvantages, creating a lot of modules may be painful sometimes. It is quite easy to have a lot of modules and if you really have a lot of them it may be hard to maintain all the dependencies between the modules (dependency cycles are quite painful).
OSGi is not only a standard of only building modular applications. It also specifies the environment in which bundles exist and are run. This is something you should be aware of - when using OSGi you must start your application in a special environment. This environment (for example Eclipse Equinox) is responsible for running your application. It provides some fancy possibilities. For example you can add a bundle to already running application without a need to stop this application - this may be a really important element in some types of applications (but IMHO there are not that many application which will really need this).
What is really really important is also a fact that some open source libraries may be not fully compatible with OSGi infrastructure and it may be hard to use them out of the box as it is in standard Java applications (remember that in OSGi everything should be a bundle). However, a lot of popular libraries were wrapped into bundles - for example Spring provides its bundles repository which contains many popular libraries (but not all).
OSGi is quite complicated and it is hard to describe it and its possibilities in a few words. What I wrote are just the most important (IMO) elements of OSGi. But OSGI is much more, e.g. bundles can send event to each other, they can provide services to each other. If you are interested in more details I suggest going through a tutorial. I may recommend this one in Java World. After that you may take a look at this free e-book about OSGi (it contains a lot of details). All the details about OSGi may be found in official specifications but I wouldn't say they are easy to read (at least at the beginning) - you can find them here (before downloading you will have to accept the license and some legal notices).
To summarize I think OSGi is useful while building modular applications but this is for sure not for free. It is rather a heavy standard which may disallow you to do some things and force you to do the things the OSGi way.
Some related SO questions: