Recently, I was working on one assignment about OSGI framework in Java. At first sight, I was excited about its idea of dynamically changing the .jar module while the application is running. However, I soon gave up with OSGI since it’s too complex and there are little documents about OSGI on the internet. OSGI with Spring Dynamic Modules requires too many config to run just a simple application. Also, there were many bugs and exceptions happened while I was working with it but I cannot not find the solution due to the lack of help/document about OSGI on the internet.

Finally, I ended up with a simple application using OSGI with Spring Dynamic Module and JDBC for working with databse. I was not successful in making a web, CLI or GUI application because of some bugs that I don’t where they came from so that the application is just like a main function automatically calls all the functions inside the provider bundle. Hopefully, in the future I will have the chance to research more about this technology to enhance this application. Here are steps of how to make that simple application. I also included the source code so you can download it to test.

# Setting up the database

First you need to setup the database. In my example, I used MySQL and MySqlJDBC for connecting to the database. Create a new database named test_osgi in your MySql. After that, create 2 tables with the structure like this

This is the diagram of the two tables. They are just very simple tables. Each Drug Group contains many Drugs.

# OSGI Provider bundle

## - Maven Project

Next, you need to create a provider bundle, which will talk to the database and return the result for the calling consumer bundle. Create a new Maven project using any IDE or text editor that you like. In my example, the project is named DAO. When you have finished creating the project, open the project’s pom.xml file and add this content to it. I’ll explain further later

## - Model classes

Now we need to create model classes which represent objects in database. Create 2 new classes DrugGroup and Drug in the package tln.model. These 2 objects has nothing special to do. They are just Plain Old Java Objects, containing some properties which reflects the object in database.

The DrugGroup class

The Drug class

## - Connection Class

This step is an extra step for avoiding repeating creating connection in each class. Create a class DrugConnection in the package tln.connection.

## - DAO Interfaces

Next we need to create some interfaces for working with Spring Dynamic Module since the consumer bundle will only talk to the provider bundle through those interfaces. In my demo, I have one generic interface and 2 other interfaces which extends that interface for specifically working with Drug and DrugGroup. Create 3 interfaces in the package tln.able.

The GenericDAO interface

The DrugDAO interface

The DrugGroupDAO interface

# - Implementation Classes

Now you have the interfaces, you will need some more classes which implement those interface for Spring DM to inject it to the consumer bundle. Create 2 classes JDBCDrugDAO and JDBCDrugGroupDAO inside the package tln.impl. Of course you can create many other alternative implementation classes and later define it in the spring config file. You need to add the implementation to the example below. Also, don’t forget to crate empty constructor for them to work with Spring.

The JDBCDrugDAO class

The JDBCDrugGroupDAO class

## - Spring DM Configurations

In order to use this bundle with Spring Dynamic Module, you need the Spring configuration file. Create a folder name spring inside the folder META-INF under the resources folder. Any .xml file inside this folder will be auto recognized by Spring DM Extender. Let’s create a file an xml file here named dao-context.xml with the content like this

The only special thing here compare to normal Spring is the osgi:service tag. It tells the OSGI framework that the interface declared in that tag is an OSGI service which then will be initialized using the class defined in the ref attribute.

## - Compile the project

Now I’ll explain a little about the details inside the pom.xml file. One important thing you need to remember is the <packaging> tag. You must have that tag with bundle as the tag content, otherwise, it will not be recognized as an OSGI bundle. Also, you need the <Export-Package> tag for the bundle to export the packages that the consumer bundle needs. The last important thing is the the <Import-Package> tag. Since the bundle uses library from the outside (jdbc), we need to explicitly specify it here.

Last step is to run maven to compile and install the bundle to the local repository.

# The Consumer Bundle

## - Maven project

Now you have successfully created a bundle that supplies the services. Next we will create another bundle to consumer the service. Create another maven project named DrugGroup and put this inside the pom.xml file

## - Executable Class

Next we need another class for calling the functions inside the provider bundle.

The Test class

The start() function will run when the bundle is start and similarly the stop() function will be called when the bundle stops executing.

## - Spring DM Configurations

Similar to the previous bundle, we need a config file for Spring Dynamic Module to receive the output from DAO Bundle. Create an .xml in the folder resources/META-INF/spring with content like this

## - Compiling the project

Last step is to compile the project with Maven.

# Deploy the project in OSGI

## - Felix

To deploy an application in OSGI, you need an OSGI container. In this example, I used Felix. You can download felix from here. Extract it and rename it to felix. After that, copy that folder into a folder named my-osgi. Open Terminal and cd to the felix folder, use this command to start felix

## - Spring DM Libraries

In order to use Spring DM, you need to download its libraries here or you find it in my github repository. Copy all those .jar files into the my-osgi folder you’ve just created before.

Back to Terminal, install all these bundles into felix following this instruction

Open the target folder inside the root directories of the two projects you’ve have just created before and copy the 2 compiled .jar file there into the my-osgi folder. Back to the felix prompt in Terminal and install those 2 bundle

You can use the command lb, which stands for list bundle to view all installed bundles as well as their status

Finally, start the two bundle to see the results