Christopher has posted 3 posts at DZone. You can read more from them at their website. View Full User Profile

An OSGi Primer

02.25.2009
| 14184 views |
  • submit to reddit

For the benefit of those who don't know what OSGi actually is, I wrote an article on my blog, which I am reproducing here, mainly because I see this question asked quite a lot.  Most articles already assume that people know what OSGi is, but all I assume is that the reader is a Java programmer and familiar with the Eclipse IDE, in which case the reader has already been using OSGi without even realising it.

The Eclipse IDE is constructed from Plugins. Plugins are effectively OSGi bundles with a bit extra (usually a plugin.xml, sometimes a little more).

Eclipse is built on top of Equinox which is an implementation of the OSGi specification and then goes on to extend it with the Plugin Registry.

At its core an OSGi bundle is a standard Java JAR file with some extra information in the MANIFEST.MF which describes the bundle to the OSGi platform it is deployed to. The manifest describes things like the bundle's name, version, what packages and/or other bundles it depends on, what packages it exposes, and probably a fair chunk of other stuff I haven't mentioned.

OSGi is a service oriented architecture. This is quite an important point to me as most people hear or read SOA and automatically assume Web Services. Well Web Services are just one type of SOA. The specification of OSGi is inherently, and by design, service oriented.

Like I said, bundles can declare what packages they expose, but more importantly they declare and/or register services.

A service is essentially a Java interface and can be implemented by one or more bundles. A bundle that needs to use a particular service looks it up (or "tracks" when services are registered/unregistered) and then decides which service to use either by taking the first one it finds, or by homing in on a service that meets a specific need by specifying some service properties.

The most important thing is that services are dynamic. They can come and go and this is a part of the specification and something that any one who writes bundles has to accept and deal with.

As a result, OSGi has a tightly defined classloader mechanism which manages version control. You can have two bundles installed on your OSGi platform that expose the different versions of the same package, then other bundles can be explicit about which package version they depend on or not.

The OSGi specification consists of a core specification and a service compendium. The core specification is enough to write modular Java applications and the service compendium details a selection of services that OSGi implementations will usually implement a common set of, if not all of. These include things like Logging, Declarative Services (services can be declared using XML), HTTP service (you can expose web resources from a compliant container, some even support JSPs!) and others. See this page for more information:
http://www.osgi.org/Specifications/HomePage

Finally, what I haven't mentioned is that OSGi has been around for nearly a decade. It was originally intended for use as a dynamic platform for embedded devices but IMHO is really starting to make its mark through use on the desktop and (enterprise) server areas, especially in the last couple of years.

When you get in to it, the power of OSGi is mind boggling. Yet OSGi is simple and logical, while remaining comprehensive. OSGi is the dynamism and modularity that Java is currently missing.

I hope my brief description of OSGi does it justice. I deliberately wanted to keep it short and sweet and whet some appetites. 

This tutorial from the Knoplerfish team will introduce you to the Knoplerflish OSGi implementation, get you building a bundle, creating and consuming services.  It is also a good idea to then try out your bundle on other implementations such as Equinox, to give you a flavour for how different the implementations can be, while being able to run the same code.

Published at DZone with permission of its author, Christopher Brind.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Francisco Jose ... replied on Thu, 2009/02/26 - 2:56am

Well written and easy to read article. Now I have a little more idea about OSGI. Thanks

Philippe Lhoste replied on Thu, 2009/02/26 - 1:03pm

Excellent article, concrete, short, informative and to the point.

Thanks.

Raging Infernoz replied on Sun, 2009/03/01 - 1:15am

No, not everyone uses Eclipse, I personally hate the mess of its GUI, and have read nasty things about conflicts between its various OSGI plugins.  I use NetBeans 6.5, which is pretty reasonable, and has some usable plugins, but prefer JBuilder 6 for heavy duty work (I want nothing to do with the Eclipse crippled versions).

I may look at OSGI sometime, however I found this Primer seems no more than a bare sales pitch for Eclipse and OSGI, without a useful summary of what it actually does, and how it is better than other aproaches, so I'm not impressed.

 

Balint Persics replied on Sun, 2009/03/01 - 8:58pm in response to: Raging Infernoz

This primer is not a sales pitch for OSGi, since OSGi is not a product. It's a platform specification. Eclipse uses OSGi at its base (the Equinox implementation), Sun Glassfish v3 uses OSGi also (the Apache Felix implementation of the OSGi platform). OSGi is not about Eclipse or the development tools you use. You can code OSGi bundles woth JBuilder also. One must understand that OSGi is not a product itself. It is a specification for a modular Java application runtime.

Christopher Brind replied on Tue, 2009/03/03 - 7:13am in response to: Balint Persics

Yes, as persicsb points out this isn't a sales pitch.

My intention with this article was to help fill the gap in knowledge about what OSGi is for those people that read articles on OSGi but are left wondering what it's all about.

I'm just pointing out that a lot of people do use Eclipse (the figure of 2million developers was banded around about a year ago) and may not realise that what empowers the Eclipse platform and its plugins is built on top of Equinox which is one of many implementations of the OSGi specification.  In fact, as you can see I end the article with a link to Knoplerfish, which is another implementation and has nothing to do with Eclipse at all.

 

Raging Infernoz replied on Thu, 2009/03/05 - 2:21am

I've read the tutorial now, yes it showed some maybe service features, however it was rather bare.  The referenced one here, better showed why OSGI could be useful:

http://oscar-osgi.sourceforge.net/tutorial/

I still stand by the sales speal critique, too much disorganised waffling (I have to stop myself doing this, when writing documentation), just tell people:

what it it is: e.g. a standard for an LDAP-like registry service, for service classes packaged in jars.

why it is useful...

who and what uses it.... preferably with examples of non-development applications

where to find it...

some detail on how to support it...

 

Arbi Sookazian replied on Fri, 2009/03/06 - 6:12pm

No talk of Spring + OSGi?

http://www.springsource.org/osgi

matt green replied on Wed, 2009/03/11 - 11:14am in response to: Arbi Sookazian

>>No talk of Spring + OSGi?

Please.  With the serviceloader api of Java 1.6 many of the features of OSGi become redundant, and furthermore would require an extra jar or two to put it mildly.

Let's not argue for layering even more (spring) bloat on top of that as well.

 

 

Christopher Brind replied on Thu, 2009/04/23 - 7:10am in response to: matt green

Actually the ServiceLoader API has been around for a while, just not publically or in the java.util package.  It was previously known as the Service Provider API and was part of the JAR file specification.

ServiceLoader is also a mis-nomer.  It should really be called the 'Factory API' since all it is doing is loading instances of interfaces or abstract classes lazily.

ServiceLoader is not dynamic and does not enforce accessibility rules in the same way as OSGi and is thus susceptible to the usual classpath hell when loading required classes, etc, etc.

Spring and OSGi are independent entities.  I don't use Spring and using Spring with OSGi is, imho, an advanced topic with regards to using OSGi.

jame jack replied on Wed, 2009/06/17 - 7:46pm

تحميل برامج برامج جوالات العاب بنات تكنولوجيا كتب تعليم UltraSurf Internet Download Manager ProgDVB برامج مجانية أفضل المواقع العربية مشاهدة محطات مشفرة Online TV Player 3.0.0.940 Internet Download Manager 5.17 Build 4 رقص شرقي anyTV Pro 4.32 OnLineLive 7.1.1 هزي يانواعم ProgDVB 6.06.2 SopCast 3.0.3 Falco Image Studio 3.6 لعبة تزلج على الجليد UltraSurf 9.4 كاثرين هيغل Katherine Heigl محطة غنوة FreeZ Online TV 1.0 Free Video to Mp3 Converter 3.1.3.51 Advanced MP3 Converter 2.10 Xilisoft Video to Audio Converter 5.1.23.0515 Blaze Media Pro 8.02 AKRAM Media Creator 1.11 DVD Audio Extractor 4.5.4 Free WMA to MP3 Converter 1.16 لعبة نينجا المتقدم لعبة قذف كرة لعبة دراجات البهلوانية لعبة اعداء الغابة تحميل برامج Download DivX Subtitles 2.0 BullGuard 8.5 Google Chrome 2.0.181.1 Dev Dell Studio XPS Desktop 435T Intel Matrix Storage Manager A00 Gigabyte GA-EP45-UD3P Bios F9 Ambush HDConvertToX 1.1.229.1764 MSI Wind Nettop CS 120 Realtek Audio Driver 5.10.0.5618 Biostar T41-A7 6.x Realtek On-Board Audio Driver 5.10.0.5735 for 2000/2003/XP TweakNow RegCleaner 4.1.1 SpeedItup Free 4.97 برامج العاب - Internet Download Manager - برامج جوالات - العاب - محطة غنوة - قنوات فضائية - بنات - تكنولوجيا - كتب تعليم - UltraSurf -

dfdf dfd replied on Wed, 2009/06/24 - 10:29pm

I like the ed hardy clothing. ed hardy is one of the most popular brands. ed hardy sale displays the brilliant work of Don ed hardy uk. He is a gifted painter, printmaker and tattoo artist. cheap ed hardy offerings include ed hardy tank, Ed Hardy Kids T-shirts, ed hardy on sale, ed hardy handbags, ed hardy discount etc. ed hardy swimwear is just 4 years old and was launched by Audigier in 2004. There were many Hollywood stars who wear his ed hardy sunglasses. Some of the famous celebrities include Ed Hardy Belts, Jessica Alba, Mariah Carey, Paris Hilton etc.

Pippifin Cakasey replied on Sat, 2011/10/22 - 8:26am

Eclipse is built on top of Equinox which is an implementation of the OSGi specification and then goes on to extend it with the Plugin Registry. Nice article about OSGi Primer..

 aloe vera beauty

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.