Contents[hide ] |
OSGi-supported framework allows developers to develop appliactions in a modularized environment for ease of management and version control.
A few weeks ago Erko Knoll has shared an example on using ZK in OSGi environment. Now this small talk will demonstrate in detail how you can convert ZK jars into an OSGi bundle and how you can use this bundle to develop an OSGi-supported application.
If you are not familiar with OSGi it is recommended to read Erko's article [1] first before reading this small talk.
An OSGi Bundle is actually a jar file that contains MANIFEST.MS file under META-INF folder. The file describes the bundle with various attributes (Bundle-Name, Bundle-SymbolicName, Bundle-Version, Export-Package, Import-Package, and etc.) as the following example:
1
2
3
4
5
6
7
8
9
10
11
|
Manifest-Version:
1.0
Bundle-ManifestVersion:
2
Bundle-Name: OSGi Demo
Bundle-SymbolicName: osgi.demo;
Bundle-Version:
1.0
Bundle-RequiredExecutionEnvironment: J2SE-
1.5
Require-Bundle: other-bundle-name;bundle-version=
"1.0"
Export-Package: business.service,
model.pojo
Import-Package: javax.servlet;version=
"2.5.0"
,
javax.servlet.http;version=
"2.5.0"
|
However, to manually prepare this file, especially adding Export-Package and Import-Package of ZK jars in MANIFEST.MS is a time consuming job. Here we would like to introduce two methods to convert ZK jars into one OSGi Bundle with the assist of Eclipse IDE. The first method is to use Eclipse Plug-in Project, and the second method is to use BND.
Follow the steps to create a ZK OSGi bundle:
Another option is to use BND. BND tool is developed by Peter Kriens that can help to create an OSGi bundle and convert existed JAR files into OSGi bundle easily. Here we will show how we can use the Eclipse plug-in BndTools to convert ZK jars into an OSGi bundle. The steps are as follows,
With BndTools, we can convert ZK jars into OSGi bundle quickly, and it will prepare import packages automatically, where with Eclipse Plug-in Project we need to specify import packages by ourselves. However, one thing to note is that with BndTools we have to wrap ZK jar files one by one because the Export Packages are duplicate due to duplicated package name, such as metainfo.zk .
In the previous section, we have converted ZK jars into one OSGi bundle. Here we will show how we can use this bundle to develop an OSGi-supported ZK application - a modularized simple CRUD application.
Normally, we create an ZK application with MVC pattern where we will put the data access related classes in one package and the web view related classes in another. And in the case that we need a different web view with same back-end data set we will create another web project and copy the same data access code from the first project.
With OSGi's modular concept, we don't need to copy and paste as what we used to do. We can simply draw the data access code into a standalone OSGi bundle called Data Access Bundle, and both the two web view bundles can use this data access code by defining Require-Bundle in MANIFEST.MF . Here is an image that illustrates the concept:
The data access bundle is a simple plug-in project that contains POJO beans, DAO classes and Service classes. Here are the steps to create data access bundle:
1
2
3
4
5
6
7
8
|
Manifest-Version:
1.0
Bundle-ManifestVersion:
2
Bundle-Name: Data Access Bundle
Bundle-SymbolicName: osgi.demo.data
Bundle-Version:
1.0
.
0
Bundle-RequiredExecutionEnvironment: J2SE-
1.5
Export-Package: business.service,
model.pojo
|
The web view bundle is also a plug-in project that requires ZK OSGi bundle generated before and the data access bundle to present data. Here are the steps to create web view bundle project:
1
2
3
4
5
6
7
8
9
10
|
Manifest-Version:
1.0
Bundle-ManifestVersion:
2
Bundle-Name: Web View Bundle
Bundle-SymbolicName: osgi.demo.webview;singleton:=
true
Bundle-Version:
1.0
.
0
Bundle-RequiredExecutionEnvironment: J2SE-
1.5
Require-Bundle: zkee-osgi;bundle-version=
"5.0.10"
,
osgi.demo.data;bundle-version=
"1.0.0"
Web-ContextPath: /osgidemo
Webapp-Context: /osgidemo
|
In the previous section, we have modularized the project into two bundles. Here we will deploy these bundles in OSGi container and this is the last step. There are many OSGi containers available out there, and I have tried with the following three containers, Eclipse Equinox, Apache ServiceMix, and EclipseRT Virgo.
Eclipse Equinox [3] is an implementation of the OSGi R4 core framework specification, a set of bundles that implement various optional OSGi services and other infrastructure for running OSGi-based systems.
There is no need to deploy or to export the project with Eclipse Equinox, you can see the result instantly during development phase by EclipseRTWebStarterKit. Here are the steps that demonstrate how to use EclipseRTWebStarterKit in Eclipse IDE.
Apache ServiceMix [4] is a flexible, open-source integration container that unifies the features and functionality of Apache ActiveMQ , Camel , CXF , ODE , Karaf into a powerful runtime platform you can use to build your own integrations solutions. It provides a complete, enterprise ready ESB (enterprise service bus) exclusively powered by OSGi.
1
|
web:list
|
1
|
osgi:restart BundleID
|
The Virgo Web Server from EclipseRT is a completely module-based Java application server that is designed to run enterprise Java applications and Spring-powered applications with a high degree of flexibility and reliability. It offers a simple yet comprehensive platform to develop, deploy, and service enterprise Java applications.[5]
The OSGi standard provides powerful modularization and version control which I think is very convenient for developers to control the bundle versions with various functions, and can help developers to design the skeleton of the project with loose coupling. Also it reduces the time for restarting the server during development phase. Following this article you should be able to convert ZK jar files into OSGi bundles and start your first OSGi-supported application.
Download the sample bundles: