Sharing Our Passion for Technology
& continuous learning
〈  Back to Blog

Hands-on OSGi and Modular Web Applications - Part I - Toes First

A Brief Introduction

This is the first in a series of blog posts that will attempt to demystify OSGi and demonstrate how it enables the creation of modular web applications. We will explore various aspects of the technology along with the challenges of using this technology. I encourage you to join in the discussion by posting any comments about your own experiences or challenges you have faced developing OSGi applications. We start with the assumption that we understand what OSGi is and the specific modularity problem it tries to solve. Here are some resources you can visit to read up on this.

  1. http://neilbartlett.name/blog/2008/06/06/what-is-osgi-for/ - this one talks about the problem space
  2. http://www.infoq.com/interviews/osgi-adrian-colyer - this one brings Spring and OSGi together

Turn on the ignition

Lets get started. This first post will show you how to launch an OSGi framework and how you can interact with it. You will first need to have a JDK installed. I recommend the Sun JDK. You then need an OSGi implementation. There are several implementations of the OSGi 4.2 specification. We will stick with equinox from the Eclipse Foundation. It is the same core technology that powers the Eclipse application framework. If you have Eclipse installed browse to the plugins folder and copy the org.eclipse.osgi_<version>.jar to a working folder (say c:\work\toes). You can also download the equinox jar from the eclipse download site.

Take it out for a spin

Open a command window to c:\work\toes where you have placed the equinox jar. Run the following command to start up the Equinox OSGi framework remembering to replace the equinox jar with the version you have.

c:\work\toes\java -jar org.eclipse.osgi_3.5.1.R35x_v20090827.jar -console
osgi>

You are greeted with the OSGi console prompt. Type ss - for ‘short status’ at the console.

osgi> ss
Framework is launched.
id      State       Bundle
0       ACTIVE      org.eclipse.osgi_3.5.1.R35x_v20090827
osgi>

The list of bundles available in the framework along with their current status is displayed. This output shows that we have one bundle installed in the framework and it is the framework bundle itself. And you can see it is an the ACTIVE state. We will see more about the bundle lifecycle in the next post. The bundle id is the handle with which you interact with the bundle. Try the following command:

osgi> headers 0
Bundle headers:
Bundle-Activator = org.eclipse.osgi.framework.internal.core.SystemBundleActivat
or
Bundle-Copyright = Copyright (c) 2003, 2004 IBM Corporation and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
Bundle-Description = OSGi System Bundle
Bundle-DocUrl = http://www.eclipse.org
Bundle-Localization = systembundle
Bundle-ManifestVersion = 2
..................
...............

You will see a screen full of information. This is information from the bundle’s MANIFEST file. We will be covering most of this in later posts. For now just know that the bundle id is your handle to poke at the bundle and make it do interesting things.

Type help at the OSGi prompt to see what other commands are available. This list is mostly the same across the OSGi frameworks with some having extra commands and sometimes having different names for the same command.

Finally type exit to shutdown the framework.

Let's go remote

Now that you know how to interact with an OSGi console, don’t you want to hack into that corporate OSGi application. No. But maybe you need to poke into a remote OSGi application. Let’s see how you can do that. Pass a port number parameter to your OSGi framework launch command like below:

c:\work\toes\java -jar org.eclipse.osgi_3.5.1.R35x_v20090827.jar -console 9999
Listening on port 9999 ...

This lauches the OSGi framework as a process and waits for telnet connections on port 9999. Now you can connect to the console on via c:\> telnet localhost 9999.

Since eclipse is built on OSGi you can just as easily attach to the console. Just run eclipse.exe from the command prompt with -console <port no>. But be careful not to stop any bundles unless you know what you are doing. You may mess up your eclipse workspace and you don’t want to have to start from scratch.

In the next post we will go more than just toes in. We will build a simple OSGi bundle and look at bundle headers in more detail.

〈  Back to Blog