OSGi – Service dependency management with BundleActivator, Blueprint, Declarative Services, and iPojo (part 1)

Posted: July 17, 2013 in Java, maven, OSGi


There are many good tutorials and wiki / blog postings on OSGi.  What I’m exploring with this post is to become more familiar with the different strategies for wiring up bundles and services within an OSGi container.

  1. Prototype a servlet filter in the Apache Felix OSGi container
    • Add a response header of “JAWSOME” with a value of “WIZARD”
    • Add a response body of “Hello World: today is ${formattedDate}”
    • Obtain the value of ${formattedDate} from an OSGi managed DateService
    • Update the DateService implementation to change the formattedDate string
  2. Evaluate service dependency management with BundleActivator, Blueprint, iPojo, Declarative Services

Setup for all Examples:

  1. Clone the git repository: https://github.com/CoderLisa/osgi-evaluation.git
  2. Build the examples in the git repository (maven and JDK are required)
  3. Download and install the latest Apache Felix Framework distribution (version 4.2.1 at time of this posting)
  4. Download and install the Apache Felix Http Jetty bundle:
    /opt/felix$java -jar bin/felix.jar 
    Welcome to Apache Felix Gogo
    g! lb
       ID|State      |Level|Name
        0|Active     |    0|System Bundle (4.2.1)
        1|Active     |    1|Apache Felix Bundle Repository (1.6.6)
        2|Active     |    1|Apache Felix Gogo Command (0.12.0)
        3|Active     |    1|Apache Felix Gogo Runtime (0.10.0)
        4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
    g! install file:/opt/felix/examples/setup/org.apache.felix.http.jetty-2.2.0.jar
    Bundle ID: 6
    g! start 6
    g! [INFO] Started jetty 6.1.x at port(s) HTTP:8080

OSGi Example Bundles:

You will find the code for these 3 different bundles in the osgi-evaluation/examples/wiring packages.

  1. Date Service API – a simple API that provides a formatted date string given a Date
    package org.osgiexample.service.date;
    import java.util.Date;
    public interface DateService {
        public String getFormattedDate(Date date);
  2. Date Service Impl – implementation of the Date Service API
    package org.osgiexample.service.date;
    import java.text.DateFormat;
    import java.util.Date;
    public class DateServiceImpl implements DateService {
        public String getFormattedDate(Date date) {
            return DateFormat.getDateInstance(DateFormat.LONG).format(date);
  3. HelloWorld Filter – extends Java ServletFilter.  Registers a simple servlet and itself.  When an HTTP request is filtered, adds “Hello World” to the response with a formatted date string obtained by the DateService
    package org.osgiexample.filters.helloworld;
    import org.apache.felix.http.api.ExtHttpService;
    import org.osgiexample.service.date.DateService;
    import javax.servlet.*;
    import java.io.IOException;
    import java.util.Date;
    public class HelloWorldFilter implements javax.servlet.Filter {
        DateService dateService;
        ExtHttpService httpService;
        public HelloWorldFilter(DateService dateService, ExtHttpService httpService) {
            this.dateService = dateService;
            this.httpService = httpService;
            httpService.registerServlet("/", new SimpleServlet(), null, null);
            httpService.registerFilter(this, ".*", null, 0, null);
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            Date now = new Date();
            chain.doFilter(request, response);
            response.getWriter().write("Hello World\n" + dateService.getFormattedDate(now));
        public void destroy() {
        public void init(FilterConfig filterConfig) throws ServletException {

Next Posts:

  1. […] « OSGi – Service dependency management with BundleActivator, Blueprint, Declarative Services, an… […]

  2. […] « OSGi – Service dependency management with BundleActivator, Blueprint, Declarative Services, an… […]

  3. Heya i’m just in my ballet shoes the following. I came across this specific aboard and I in finding It genuinely practical & that taught me to be away very much. I hope to supply something back again along with assistance people such as you assisted me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s