[OSGi] Build OSGi Server by Eclipse Bndtools

[OSGi] Build OSGi Server by Eclipse Bndtools

I will show you how to build OSGi server serving HTTP using Eclipse IDE bndtools, I assume that the readers has at least one year of practical experience in writing JAVA programs.

1. Installing Eclipse Bndtools Plugin
Choose “Help –>Eclipse Marketplace”

osgi1

2. Creating a Bnd Configuration Project

Open “Bndtools perspective”, and Eclipse will open a create bnd configuration project dialog automatically. This configuration project stores a repository with all external project dependencies and global build configuration.

osgi2

Then Choose “Create a configuration project(recommended)”, and you will see next dialog:
osgi3

Select “Standard Configuration ” and click “finish”, when you create configuration project successfully, you will find a “cnf” project in your Eclipse:
osgi4

3. Create a new bnd project
Create a new bnd project using “File –> New –> Bndtools OSGi Project“:
osgi5

Named the project something like that”com.felixbundle.test” and choose “Component Development(Declarative Service)” as a default template:
osgi6

osgi7

Then you’ll find a new bnd project in Eclipse, and you have two project now, one is the cnf project, another is the bundle project. The bundle project have a bnd.bnd configuration file, and the MANIFEST.MF will be created automatically from this configuration:
osgi8

4. Add the felix.jetty bundle into your project
Choose “Run” tab, and click “+” in Run Bundles block:
osgi9

osgi10

Choose “Run As –> OSGi Run”:

osgi11

This will start up Felix framework with Jetty serving HTTP request:
osgi12

5. Build a servlet path and add a servlet component
If you need servlet API, you need add jetty into your path. Choose “Build” tab, and click “+” in Build Path block:
osgi13

Add “org.apache.felix.http.jetty” bundle into your build path:
osgi14

You should download Apache Felix HTTP whiteboard bundle and install it into your local repositories, because the Apache Felix HTTP Service Whiteboard bundle makes it very easy to publish Servlets using OSGi services.
osgi15

When you install Apache Felix HTTP Service Whiteboard into local repositories already, add org.apache.felix.webconsole and org.apache.felix.whiteboard into your run bundles.
osgi16

osgi17

Create a servlet class and annotate it with Bnd’s @Component annotation to make the servlet a declarative service component:
osgi18

6. Run the OSGi server again, and open http://localhost:8080/http://localhost:8080/system/console/bundles in your broswer
osgi19

osgi20

The bundle jar is automatically built and deployed when classes are changed. So try to change the servlet, save and reload the page. You should see your changes without restarting the server.

osgi21

(Visited 42 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail
Comments are closed.