OSGi with spring 4 and hibernate 4

OSGi Key concepts

I won’t give here a detailed explanation what OSGi is. There are a lot of good explanations out there. For a great overviesw I can recommend the RefCard Getting Started with Equinox & OSGi written by Jeff McAffer. It is about using equinox which is an implementation of the OSGi specification. You might need to register to read the card. It is free and gives you access to great content.

At this place I highly recommend to download the OSGi specs. They are well written and a great source to find out how thinks are (at least) supposed to work: OSGi Spec Downloads

Here is what the specification says what the OSGi framework is:

It provides a general-purpose, secure, and managed Java framework that supports the deployment of extensible and downloadable applications known as bundles.

— OSGi Alliance
OSGi Core Specification 6

It is important to understand that the OSGi world distinguishes between different specifications. For Release 5 in 2012 there were 2 specifications:

  1. OSGi Core and

  2. OSGi Enterprise

Since Release 6 the specifications have been reorganized into:

OSGi Core (June 2014)

Defines the basic framework. Basically it covers the definition of

  • bundles

  • their lifecycle (installation, resolution, starting, etc.)

  • dependencies between bundles and their resolution

  • certain APIs to interact with the OSGi container.

OSGi Enterprise (July 2015)

The enterprise specification drives to integrate Java EE features into the OSGi framework. In this specification services are defined that cover JavaEE aspects like:

  • JTA container managed transactions,

  • JDBC Driver management and

  • services for logging, administration, configuration, ReST, etc.

OSGi Residential (July 2015)

Specifies an API for large scale remote management of OSGi framework and their applications/services.

OSGi Compendium (July 2015)

Is the compendium of all OSGi service specifications.

Basic Terminology

The core component an OSGi framework handles is the so called bundle. In general a bundle is a simple jar file that inlcudes OSGi-specific headers within its META-INF/MANIFEST.mf file.

The core specification R6 lists 23 headers. list the important header

OSGi Container Basics

In this section we are going to download an OSGi container. We will use Apache Felix.

Go and download the last distribution from here. The current (August 2015) release is 5.0.1. Extract the archive to destination folder of your choice and navigate into it. You should see the following folders:

felix-framework-5.0.1
|-- bin
|   |-- felix.jar
|-- bundle
|-- conf
|   |-- config.properties
|-- doc

Open a command window at the destionation folder and type java -jar bin/felix.jar (*nix systems) or java -jar bin\felix.jar for Windows systems.

You should see a similar screen like below.

$ cd felix-framework-5.0.1
$ java -jar bin\felix.jar
____________________________
Welcome to Apache Felix Gogo

g!

The OSGi container has been startet.

Preinstalled with Apache Felix comes the Gogo Shell. This is a handy command line interface you can use to interact with the OSGi container.

For example type in

$ cd felix-framework-5.0.1
$ java -jar bin\felix.jar
____________________________
Welcome to Apache Felix Gogo

g! lb                                                               (1)
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (5.0.1)                       (2)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.4)      (3)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.16.2)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
g! stop 0                                                           (4)
$
  1. lb stands for list bundles

  2. the bundle with the ID 0 is the special System Bundle that is installed per default within the OSGi container. It holds the JDK specific packages that are provided to all bundles by default.

  3. The Bundle Repository bundle can be used to install bundles from a repository.

  4. stopping the system bundle (via its ID 0) the OSGi container is shut down.

Using the help gogo command will give you a list of possible commands.

Using the OBR commands to install bundles from a repository

TODO: point to the OBR spec

$ cd felix-framework-5.0.1
$ java -jar bin\felix.jar
____________________________
Welcome to Apache Felix Gogo

g! help
[...]
obr:deploy
obr:info
obr:javadoc
obr:list
obr:repos
obr:source
g!

With the obr:list command you can get a list of bundles known from the repository.

____________________________
Welcome to Apache Felix Gogo

g! obr:list
Apache Felix Bundle Repository (1.6.6, ...)
Apache Felix Configuration Admin Service (1.2.4, ...)
Apache Felix Declarative Services (1.6.0, ...)
Apache Felix Dependency Manager (3.2.0, ...)
Apache Felix Dependency Manager Runtime (3.2.0)
Apache Felix Dependency Manager Shell (3.2.0)
Apache Felix EventAdmin (1.0.0)
Apache Felix File Install (3.0.2, ...)
Apache Felix Gogo Command (0.10.0, ...)
Apache Felix Gogo Runtime (0.10.0, ...)
Apache Felix Gogo Shell (0.10.0, ...)
Apache Felix Gogo Shell Commands (0.2.0)
Apache Felix Gogo Shell Console (0.2.0)
Apache Felix Gogo Shell Launcher (0.2.0)
Apache Felix Http Api (2.0.4)
Apache Felix Http Base (2.0.4)
Apache Felix Http Bridge (2.0.4)
Apache Felix Http Bundle (2.0.4)
Apache Felix Http Jetty (2.0.4)
[...]
g!

Lets try to install the Http Api bundle.

g! obr:deploy "Apache Felix Http Api"       (1)
Target resource(s):
-------------------
   Apache Felix Http Api (2.0.4)

Required resource(s):
---------------------
   Apache Felix Http Bundle (2.0.4)         (2)

Optional resource(s):                       (3)
---------------------
   Apache Felix Configuration Admin Service (1.2.4)
   Apache Felix Log Service (1.0.0)

Deploying...
done.
g!
  1. Via obr:deploy <Bundle Name> you can deploy the bundle

  2. The required dependencies are resolved

  3. the optional dependencies are resolved

Lets check the state of our OSGi container.

g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (5.0.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.4)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.16.2)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
   9|Installed  |    1|Apache Felix Configuration Admin Service (1.2.4)
   10|Installed  |    1|Apache Felix Http Bundle (2.0.4)
   11|Installed  |    1|Apache Felix Http Api (2.0.4)
   12|Installed  |    1|Apache Felix Log Service (1.0.0)
g!

In bold you see the newly installed bundles.

Note
Currently the bundles are in the state installed. This means you cannot consume services from these bundles because they have not been activated yet. To activate you need to type start <ID> to activate the bundle. E.g. start 9 would activate the Configuration Admin Service bundle, which means the bundle is resolved and the bundle’s Activator is called.

Some thoughts on IDEs

If you are coming from Web Development, no matter which of the big Java IDEs Intellij, Eclipse, Netbeans you use: if you develop Maven based Java EE Applications whether WAR or EAR projects you get a quite good support from your IDE. Also Spring is widly supported within the IDEs e.g. giving you code completion in spring xml configuration or hints in source files.

For OSGi development it looks different. If you are looking for plugins only Eclipse offers a good plugin solution Bnd Tools. It is also used within the book from O’Reilly: Building Modular Cloud Apps with OSGi.

But if you are used to develop software using Maven (or any other build tool capable of resolving dependencies) then you will face the first wall the needs to be taken. In the beginning Bndtools felt quite good, allthough it differs from developing maven projects. Netbeans has some kind of support if you develop Netbeans Modules. But I ended up using plain Maven projects, because those can run in a general CI environment with Maven support out of the box. About OSMORC the OSGi plugin from Intellij I cannot say anything because I have not used it.