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 a new Maven project in Eclipse


In this example, we will use the Bookshelf Inventory Impl - Mock bundle from Chapter 5, The Book Inventory Bundle, showing the parts that are specific to Eclipse and the m2clipse plugin.

A lot of what you'll see here relating to Maven is exactly the same as what you've seen in Chapter 5. The only difference is that the project contents are edited in Eclipse. We will mainly focus on the specifics around the setup and operation in the Eclipse environment.

If you already have the project setup, as described in Chapter 5, to add it to Eclipse, follow the steps in the upcoming Importing a Maven project into Eclipse section.

Creating the Maven project

At this point, Eclipse is open in Java perspective, with the m2clipse plugin installed and configured. We will now create a new Maven project in Eclipse. Let's quickly go through the project creation steps.

Go to the File menu and select New > Project...

This will open the New Project dialog, which requests the selection of the type of project to be created. This shows the project templates provided by the installed plugins.

Pick Maven Project and continue to configure this project's details by clicking Next.

The New Maven Project wizard gathers the project information which it will use to set up the project structure and create its POM (Project Object Model).

In Maven, new projects can be created based on a template (or archetype). This is especially useful when a big part of the project's contents can be generated or when the directory structure is elaborated.

In our case, the projects are simple. Therefore, we will not use archetypes: tick the Create a simple project checkbox.

Eclipse project working sets allow further group projects in the workspace to reduce clutter when working in workspaces with a large number of projects. We will not use any working sets for this example.

To continue with the project creation wizard, click Next. This gets us to the project identification.

In this step, fill in the project information, as described in Chapter 5. Notice that we've left the packaging to its default value (jar). The bundle custom packaging is defined by the maven-bundle-plugin, which we have not declared yet in the project configuration. We'll edit it in the POM in the next step when adding dependencies and plugin declarations.

Maven allows us to optionally organize projects into structures, where child projects inherit common POM configuration from their parents. The Parent Project section of this form would be used to specify this project's parent. We will not set a parent for this project.

Click Finish to create the project.

What just happened?

This wizard has gathered the information necessary to construct a base Maven project for us. It contains the typical source structure and a POM that's based on the identification information we have provided.

This results in a project layout that looks like this:

The m2clipse plugin keeps track of changes to the pom.xml file and updates the environment accordingly. For example, when we add our dependencies in the next step, they will be located by the Eclipse plugin and placed in the Maven Dependencies classpath container. Those are then made available to the environment when checking syntax, providing content assist, and so on. Also, when we set the maven-compiler-plugin constraints to the JVM version, the project assigned JRE System Library is updated.

The next step is to inspect the project POM and add the customizations for creating OSGi bundles.

Customizing the build process

As described in greater detail in Chapter 5, the Maven POM holds the project identification, as well as a wide range of description items. The POM that was generated by the m2clipse New Maven Project wizard is not yet complete from our project's point of view.

The m2clipse plugin provides a POM editor, which allows us to graphically edit the POM. When opened, by double-clicking on the pom.xml file, it displays a tabbed view showing the different parts of the POM, each part in a tab.

In our example, we'll just edit the POM in the XML format, in the last tab of the editor, and add the missing sections.