Question

I am newbie to OSGi. My requirement is to have an android app talking to a server on OSGi framework.

I am looking at Apache Felix as container. From what I have understood, there are going to be bundles that are installed on Felix and they can be started and stopped from the program itself.

I managed to create bundles on the android side and get it up. However I am unable to get a clear picture of OSGi on the server side. The android app talks to the server using Http requests and responses. If I have Apache Felix alone on the server, will I be able to handle Http requests? Do I need Tomcat as well?

I saw this link -

Trouble understanding the whole OSGi web eco system

Here they talk about how web container is Tomcat/Jetty. Which tells me I need Tomcat and Felix on the server. Am I right on this? I also read about Jetty being embedded in Felix. But I am unable to connect the dots.

Please lead me in the right direction..

Was it helpful?

Solution

The preferred model for OSGi is to just register a Servlet service in the service registry. This servlet should be picked up by the Http server installed on that framework. This model will be the standard model in the near future but is already supported by Apache Felix. This is how you would make this in bnd(tools):

bnd.bnd
  -runfw: org.apache.felix.framework;version='[4,5)'
  -runbundles: \
    org.apache.felix.configadmin;           version=1.6.0, \
    org.apache.felix.log;                   version=1.0.1, \
    org.apache.felix.scr;                   version=1.6.0, \
    org.apache.felix.http.jetty;            version=2.2.0, \
    org.apache.felix.http.whiteboard;       version=2.2.0

If this is running you could write a servlet using Declarative Services like this:

  @Component(provide=Servlet.class,properties="alias=/hello") // makes it available on /hello 
  public class MyAndroidServer extends HttpServlet {
    public void doGet(HttpServletRequest rq, HttpSerletResponse rsp) throws IOException {
      rsp.getWriter().println("Hello World");
    }
  }

If you start with bndtools it should be quite easy to get this to work. As far as I know this is the most simple way to use servlets on an OSGi environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top