I'm Singaram Subramanian, and I work with CSC India as a Software Developer. My blog is an attempt to share my learnings with all (mainly, for those who desperately mine google finding ways to solve problems, fix issues, learn about a Java/Open source software, or deciding on tough choices etc. during software development as I do). Singaram is a DZone MVB and is not an employee of DZone and has posted 36 posts at DZone. You can read more from them at their website. View Full User Profile

How to create/generate OSGi bundles from existing third-party jars?

12.02.2011
| 6587 views |
  • submit to reddit

If you get to work with OSGi, you often have to generate OSGi bundles. Any third party jar can’t be included straightaway in your application – you need to create an OSGi bundle of the jar you want to include.

Quoting from http://blog.springsource.org/2008/02/18/creating-osgi-bundles/,

a bundle is a JAR file that:

  • Contains [...] resources
  • Contains a manifest file describing the contents of the JAR file and providing information about the bundle
  • Can contain optional documentation in the OSGI-OPT directory of the JAR file or one of its sub-directories

In short, a bundle = jar + OSGI information (specified in the JAR manifest file – META-INF/MANIFEST.MF), no extra files or predefined folder layout are required. This means that all it takes to create a bundle from a jar, is to add some entries to the JAR manifest.

Before you use any tool/plugin to generate OSGi bundles, do search in public OSGi repositories like SpringSource Enterprise Bundle Repository etc. to see if there’s an OSGi bundle already made available.

Assume that we’re wrapping a vanilla jar file, c3p0-0.9.1.2.jar, which is a popular connection pool library, as an OSGi bundle. Here’s the way I do it:

1. Download aQute’s BND tool from here: http://dl.dropbox.com/u/2590603/bnd/biz.aQute.bnd.jar
2. Create bnd.property file (See below for a sample file).

bnd.properties
version: 0.9.1.2
Bundle-ManifestVersion: 2
Bundle-SymbolicName: com.mchange.v2.c3p0
Bundle-Version: ${version}
Bundle-Name: c3p0
Export-Package: *;version=${version}
Import-Package: *

3. Run this command: java -jar biz.aQute.bnd.jar wrap -properties bnd.properties c3p0-OSGi.jar
4. You’ll get  c3p0-OSGi.bar, which needs to be renamed to  c3p0-OSGi.jar. That’s it, you have the OSGi bundle.

Related articles

From http://singztechmusings.wordpress.com/2011/11/29/how-to-creategenerate-osgi-bundles-from-existing-third-party-jars/

Published at DZone with permission of Singaram Subramanian, author and DZone MVB.

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

Tags:

Comments

Charlie Mordant replied on Fri, 2011/12/02 - 5:04am

Hi,

pax-construct does the same think for the jar and all its dependencies if needed.

You've to note that it work just for simples jars, not for advanced libraries like hibernate, jsr303, ... All that interacts with classloader, aop scanning...

 

Regards, OSGI to the top

Lars Heppler replied on Fri, 2011/12/02 - 7:34pm

Salve,
you can even do this on-the-fly with pax-url or with the eclipse plugin bndtools
there's a feature "wrap jar as osgi bundle"
Regards

Comment viewing options

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