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

Start levels


Although this is not necessary with well designed and implemented bundles, there is value in being able to define a sequence in which bundles are started when starting a service platform. This is to control the start and stop of groups of installed bundles, stepwise.

The Start Level Service

The Start Level Service on the OSGi framework allows just that the idea is to assign a "bundle start level" to each bundle, a non-negative number, and to be able to change the "active start level" in a stepwise manner in order to control which group of bundles are active at that time.

The Start Level Service also allows setting an initial bundle start level to be assigned to newly installed bundles. The default bundle start level is 1. This level can be changed by either issuing a command to change it (we'll cover this command in Chapter 3, Felix Gogo) or by changing it in the framework configuration. In Felix, the configuration property to set the initial bundle start level is:

felix.startlevel.bundle=1

Change this property in conf/config.properties of the installed distribution.

The active start level

For example, in the following diagram, we have a Felix instance with an additional three bundles installed (bundles A, B, and C). In this example, the installed bundles are given start level 2 and Bundle C is not started.

The start level 0 is assigned to the System Bundle, no other bundle is allowed on that start level, and the bundles provided with the Felix distribution are on start level 1.

When the framework is starting up, it will first have an active start level of 0, at which point the System Bundle is starting. Once this is done, it will go onto start level 1 and start all the bundles that were persistently marked for start. All bundles on a start level are started before going onto the next.

In the Chapter 3, we'll look at some of the Felix shell commands and learn how to check and change the active and bundle start levels.

In this example, if we change the framework's active start level, it will attempt to start Bundle A and Bundle B, and then set the active start level to 3.

Since Bundle C was not persistently marked as started (it's stopped), it will only be resolved.

By default, Felix will start up until active start level 1. To make it set a different active start level on startup, change the configuration entry that sets the beginning start level; for example, to set it to 3, you would add a property in conf/config.properties as such:

org.osgi.framework.startlevel.beginning=3

Using start levels

As mentioned previously, there are cases where grouping the start of bundles into distinct steps can be useful.

For example, in development scenarios, one may want to split the bundles into "Validated" and "Under Test" and assign those the start levels of 2 and 3 respectively. This would allow separating bundles that may cause issues from the others and control their startup more closely.

In other situations, when the start-up time is lengthy, a splash screen bundle may be placed on start level 1 and would only be removed when all bundles are started.

In our case, we will separate the bundles from a functional point of view. Those bundles are assigned start levels that simplify the operational and support activities needed to maintain the application:

This diagram does not contain all the bundles that will be used for this case study: some of them have been hidden to reduce clutter.

The start levels that we'll use to organize the bundles in our study are as follows:

  • Common Services (level 1), to which are assigned validated common services and libraries, in addition to the bundles provided as part of the distribution.

  • Tier 3 Services (level 2), to which are assigned the data access related bundles. In our case, those will be the Bookshelf Inventory API and the Bookshelf Inventory Mock Impl bundles.

  • Tier 2 Services (level 3), to which are assigned application business logic bundles; in our case, the Bookshelf Service bundle.

  • Tier 1 Service Providers (level 4), to which are assigned bundles that provide user interaction services. For example, the Http Service (which we will look at in details in Chapter 11) is given the start level 4.

  • Tier 1 Services (level 5), to which are assigned bundles that plug into user interface providers. For example, a bundle that implements the text UI commands (in Chapter 8, Adding a command-line interface) is assigned start level 5.

For example, when going through a data migration or cleansing activity; the active start level is set to 2, which keeps only the inventory bundles active and stops the ones on higher start levels.

In the case where web-server maintenance is required, going down to active start level 3 is enough.