Tuesday, March 27, 2012

"Long may your Jib draw" - A good wish for the future.

East coast of Newfoundland
 as seen from above.


"Long may your Jib draw" - A good wish for the future.

About a year ago I struck out on my own to begin my career as an independent consultant, providing services in software development and testing. It was an amazing experience. I had the opportunity to see all aspects of projects coming together, navigating changes, and seeing the finalized product reach my clients hands. During this time I kept one foot firmly in the Apache community while delivering services to my clientele, however these local projects rarely let me dive deeply into the Apache projects I enjoy so much.

As of this spring I've signed on with Savoir Technologies as one of their Computer Systems Analysts. In this role I will be able to expand my involvment with Apache projects on a day to day basis. I'm looking forward to meeting new people, and helping them navigate towards successful deployments of Apache based projects. If you'd like to touch base with Savoir regarding project services and training availability please visit http://www.savoirtech.com/about-us/contact-us.

Monday, March 26, 2012

Making OSGi deployments easier with feature files

Q: How can we simplify provisioning bundles?
A: We use feature xml files.
When developing OSGi applications developers often end up with a large number of components that need to be deployed together. Doing this in a piece meal fashion can quickly become error prone, to help over come this problem Apache Karaf contains the concept of a feature file.

A feature file is a simple xml file that lists a collection of bundles that should be deployed together. It's really that simple! To make things even easier, a feature file may also reference other feature files allowing a developer to wire together even larger deployments. It should be noted that feature files are just a collection of references to resources - it is not a self contained artifact representing a ready to run application, that capability is encapsulated by the Apache Karaf Archive.

Luckily we've made creating a feature project really straight forward in Apache Karaf via the Karaf Feature Archetype. The archetype, when invoked, creates a skeleton Maven project for building a feature file. Using features to organize your provisioning of bundles marks one of the best practices for provisioning OSGi applications on Apache Karaf. Generally if I have two or more bundles that I need to deploy together then I'll go ahead and create a feature file.

To use the archetype you'll need the following:
  1. Java 1.5 or higher.
  2. Apache Maven 2.2.1 or higher.
  3. And Apache Karaf 2.2.6 kit or higher installed.
The general format of the archetype invocation for generating a feature project is as follows:
       mvn archetype:generate \
           -DarchetypeGroupId=org.apache.karaf.archetypes \
           -DarchetypeArtifactId=karaf-feature-archetype \
           -DarchetypeVersion=2.2.6-SNAPSHOT \
           -DgroupId=org.myorg \
           -DartifactId=my-feature \
           -Dversion=1.0.0-SNAPSHOT \
           -Dpackage=org.myorg.package

The command may look a little complicated but its mostly straight forward once you break it down into its parts.

The first three arguments to the archetype generate directive simply specify to maven which archetype to use, the next four arguments are what you're going to be most interested in configuring. The groupId, artifactId, version, and package should be configured to your own project name and versions.

Using the Karaf Feature Archetype to create Maven
feature project skeletons is easy!
Once you've invoked the command it will begin parsing your input, then ask to confirm its interpretation of your configuration before writing to disk a skeleton project. Once this is complete you'll have a new folder created that contains a pom file. This project can be built by running "mvn install", a feature file will appear in the target/classess directory.

The generated feature file can then be deployed by either coping it into Karaf's deploy folder, or by adding the feature file to Karaf via the features:addurl command, then issuing the features:install command for the feature. In either case the bundles specified in the feature file will be resolved, then started.

To help make things a little clearer I've included a feature demo in Karaf's demo/deployer folder (as of Karaf version 2.2.6). The particular feature project you'll find has been crafted to be as minimal as possible. You'll find a README file in the folder as well that outlines the instructions to build and deploy the demo feature.

Saturday, March 24, 2012

Making an OSGi bundle using Apache Karaf 2.2.x.

How do I make one of these OSGi bundles?
I received a few private messages regarding my last Apache Karaf demo post asking a more fundamental question on making OSGi bundles for use with Apache Karaf - that is how to just make a standalone bundle?

Before I dive into the above question, if you're wondering just what OSGi is then please visit: http://www.osgi.org/About/HowOSGi.

To the unfamiliar an OSGi bundle is essentially a special Java Archive file. The basic premise behind the idea is to add a set of properties to a jar's standard Manifest file. These properties correspond to OSGi naming and versioning standards, it also includes a few properties that enable an OSGi container to manage dependencies for the bundles deployed inside it. Outside of these properties, little else differs, except for a special class that implements the Bundle Activator interface; this is used by the OSGi container as a hook into bundle's life cycle (one would normally specify a Main method to execute code from a jar, OSGi uses an implementation of the Bundle Activator interface which contains a start, stop, and other methods to control life cycle states). The class implementing the Bundle Activator interface is specified in the Manifest file so that the OSGi container may interact with it.
A bundle is built up from a collection of classes, resources, and a manifest file.
Well luckily we've made creating a bundle project really straight forward in Apache Karaf via the Karaf Bundle Archetype. The archetype, when invoked, creates a skeleton Maven project for building an OSGi bundle. This skeleton goes so far as to even generate a bundle activator for you. Personally, I find this methodology much easier than creating all the artifacts by hand ;)

To use the archetype you'll need the following:
  1. Java 1.5 or higher.
  2. Apache Maven 2.2.1 or higher.
  3. And Apache Karaf 2.2.6 kit or higher installed.
The general format of the archetype invocation for generating a bundle project is as follows:
    mvn archetype:generate \
      -DarchetypeGroupId=org.apache.karaf.archetypes \ 
      -DarchetypeArtifactId=karaf-bundle-archetype \ 
      -DarchetypeVersion=2.2.6-SNAPSHOT \ 
      -DgroupId=org.myorg \
      -DartifactId=my-bundle \ 
      -Dversion=1.0-SNAPSHOT \ 
      -Dpackage=org.myorg.package
The command may look a little complicated but its mostly straight forward once you break it down into its parts.

The first three arguments to the archetype generate directive simply specify to maven which archetype to use, the next four arguments are what you're going to be most interested in configuring. The groupId, artifactId, and version are how you want your bundle to be named and versioned. The package argument is the name of the package you want the internal code to use.

Once you've invoked the command it will begin parsing your input, then ask to confirm its interpretation of your configuration before writing to disk a skeleton project. Once this is complete you'll have a new folder created that contains a pom file and src folder containing a sample activator class. This project can be built by running "mvn install", a bundle will appear in the target directory.
Using the Karaf Bundle Archetype to create Maven
bundle project skeletons is easy!

The generated bundle can then be deployed by coping it into Karaf's deploy folder or by invoking an install command on Karaf's console. When the bundle is started a message will be printed to the prompt, similarly when the bundle is stopped another message will be displayed (this behaviour can be removed by editing the start and stop methods of the Bundle Activator implementation).

To help make things a little clearer I've included a bundle demo in Karaf's demo/deployer folder (as of Karaf version 2.2.6). The particular bundle project you'll find has been crafted to be as minimal as possible. You'll find a README file in the folder as well that outlines the instructions to build and deploy the demo bundle.