Book Image

OSGi and Apache Felix 3.0 Beginner's Guide

By : Walid Joseph Gédéon
Book Image

OSGi and Apache Felix 3.0 Beginner's Guide

By: Walid Joseph Gédéon

Overview of this book

<p>The OSGi specification is a module system and service platform that implements a complete and dynamic component model. Wasn't that a complicated definition! So how would you really use it in practical modular applications? Let this book break down the seemingly overwhelming OSGi standards for you by explaining Apache Felix's powerful architecture in a simple and easy-to-understand manner using Apache Felix framework to get you up and running sooner than you could expect.<br /><br />The OSGi standards have found a wide range of applications in the context of the Enterprise, Telecommunications, Telematics, Smart Home, E-Health, and Mobile applications, to name just a few. Apache Felix is one of the most famous implementations of the OSGi framework specification. This book introduces OSGi on the simple and extensible Felix framework and guides you from the development environment setup to the troubleshooting of potential issues, walking you through the development of an OSGi-based application and explaining relevant software design concepts.<br /><br />The book starts with an introduction to the OSGi Service Platform, its parts, and its bundle structure. It then walks you through setting up the Felix framework and your development environment. It describes the Felix Framework and how to operate it using Gogo. It will teach you everything possible about the practical implementation of OSGi using the Felix Framework as a launch pad.<br /><br />The book then kicks off the Bookshelf project, a case study that will be used to progressively explain the important concepts around OSGi using the Felix framework. The Bookshelf project feature trail will set the context to explain OSGi headers, the bundle activator, the bundle context, and so on.<br /><br />As you implement the bookshelf step by step, you learn about OBR repositories, dependency management, and bundle version management with Felix.<br /><br />Moving ahead, a few more advanced topics are covered, such as using iPOJO for dependency injection and service registration; then the book moves on to the implementation of a web-based graphical interface, first using a simple Servlet, and then building a JSP-based Web Application Bundle.<br /><br />OSGi service specifications such as the Log Service, Http Service, and Web Container are explained. Finally, the book describes some of the common pitfalls during bundle development, and gives hints on troubleshooting them in Felix.</p>
Table of Contents (22 chapters)
OSGi and Apache Felix 3.0
Credits
About the Author
About the Reviewers
Preface

Setting up Eclipse and plugins


Installing Eclipse typically consists of downloading the latest distribution and unzipping it to a chosen location. Maybe then you can add a shortcut to its executable from a convenient place.

There are a few Eclipse packages based on the core Eclipse functionality and bundled with a selection of plugins that are mostly useful for a target application. For example, you'll find distributions for C/C++, PHP, or Java EE developers.

I'm working with Helios for Java EE. It comes with a combination of plugins that is suitable for a good range of Java development, and as all packages, it can be extended with a variety of plugins for additional features.

The two additional plugins that we're using in this appendix are:

Maven integration plugin

The integration of Maven with Eclipse is a great tool. It assists in the creation of new Maven projects by doing the leg work involved in the construction of the directory structure. It also configures the project to reflect its POM settings, adding JARs to the classpath, and so on.

Although I usually still prefer to use the mvn shell commands for the build, package, and deploy cycles of the main releases. The m2clipse plugin integration points with Maven provide hooks into the build cycle phases for quick test cycles between releases.

The most commonly used Maven phases can be launched through the run configurations provided (under the Run As and Debug As shortcuts). More customized build sequences can be configured using the "Maven build..." run configuration.

We'll install m2clipse in the following section.

Note

Another Maven plugin for Eclipse that's worth investigating is IAM (Integration for Apache Maven). To know more, you can go to http://www.eclipse.org/iam/.

OSGi framework container plugin

Another tool that's useful to have when developing for a framework is an embedded framework container. Embedding a framework in an IDE provides a test environment that's easy to obtain. This is especially useful in early testing phases, where there's a good chance that bundles are frequently updated to avoid disturbing the common continuous integration environment.

OSP4J Pax provides an Eclipse plugin, Pax Runner, which serves as a container to the most common OSGi frameworks, including Equinox, Knopflefish, and Felix.

Pax Runner will be installed in a bit and will be configured and used in the final sections of this appendix.

Choosing the workspace

A developer typically works on multiple project streams at a time, each with a set of deliverable projects. In Eclipse, workspaces are used to group projects that are related. A workspace keeps the related projects in a directory structure and uses that structure to add Eclipse and plugin-specific configuration. Then this configuration is applied to the Eclipse environment when switching workspaces.

Unless instructed otherwise, on startup, Eclipse will pop up the Workspace Launcher to prompt which workspace it should use. Otherwise, the workspace can also be switched through the File -> Switch Workspace menu option.

In the example that we're using here, the code is structured under the directory C:/projects/felixbook/sources/. We will use this same location as our workspace.

Installing the Eclipse plugins

Installing plugins for Eclipse is pretty straightforward. Eclipse connects to a remote update site and downloads a descriptor that contains the plugins made available.

In this section, you'll cover the step-by-step approach for installing a plugin, using Pax Runner as an example. The information for the m2clipse plugin is provided later. Just follow the same process to install them both.

Installing Pax Runner

Eclipse has the concept of an update site, which is conceptually very closely related to the OBR repositories described in Chapter 6, Using the OSGi Bundle Repository. Each provider publishes a set of descriptors that list the software that it provides, made available at a URL. The user adds the update sites that are of interest to them and uses them to install new software components or to update existing software components.

To install Pax Runner, first launch the Install new software... wizard (through the Help menu item).

Then add a new update site for OPS4J Pax by clicking on Add.... The update site details for OSP4J Pax are as follows:

Note

The name of the update site is an arbitrary display name that you give to an update site. You can change it to a name of your choice.

With the update site added and selected, Eclipse will connect to the remote update site and retrieve a list of the available plugins.

Select the two Pax Runner plugins and continue by clicking Next. Eclipse will check for dependencies and determine the plugins to be installed. It then provides you with the plugin license terms.

Accept the terms of the licenses and finish the install. Once the installation is complete, restart Eclipse (if requested). If all went well, this set of plugins is now installed and you should be ready to use it to set up embedded OSGi platforms such as Felix.

To double check that your plugins are indeed installed, you can inspect the current installation through the Help -> About Eclipse menu sequence.

Installing m2clipse

Follow the previous procedure to install the m2clipse plugin, using the following update site details:

For the m2clipse install, you select the one provided plugin. For reference, the installation procedure can be found at the m2clipse site (http://m2eclipse.sonatype.org/installing-m2eclipse.html).