Running Knopflerfish within ServletContainer

  • vladi

    vladi - 2004-10-20

    I want to run KF in a Servlet, i.e. the OSGi framework should be setup and initialized in Servlet.init(ServletConfig).
    Now I need to get access to the registered services. As a starting point I need to retrieve an instance of BundleContext to resolve the services.
    Note that the Servlet itself is not a bundle/service so I don't have direct access to any bundle registered in the KF framework.
    Any ideas how to accomplish this ?

    • Erik Wistrand

      Erik Wistrand - 2004-10-20

      First, I must ask this: Do you *really* want to run the entire framework inside a servlet?

      The OSGi FW is kind of intended as being the kernel of an application, and will quite probably fight the servlet engine in several ways since they tend to want to manage similar things.

      But i guess it's theroetically possible, but depending on the servlet engine and permissions, servlet code might be quite in no file access, URL classloading might not be permitted etc etc. So the details on how to set this up will vary greatly.

      But, if you really want this, I suggest creating the fw manually, approx by

      fw = new Framework(new Main())

      and then just grab the system bundle context using


      If you don't want this, another trick to get a bc is to install a bundle which knows about a public static variable in your servlet code, and have that bundle store its own bc in that servlet member variable :)

      something like

      ---servlet class--
      package myservlet;

      import org.osgi.framework.BundleContext;

      class MyServlet extends HttpServlet {
        public static BundleContext bc;

      --- bundle ----

      import myserlvet;

      public class Activate implements BundleActivator {
        public void start(BundleContext bc) {
          MyServlet.bc = bc;


    • Sven Haiges

      Sven Haiges - 2004-10-21

      Vladi 5 - what is the reason for running an OSGi Framework within a servlet?

    • vladi

      vladi - 2004-10-21

      First of all, thanks for your help. So why do I need to run KF inside a servlet ?
      The system I have in mind is a system that contains some kind of "computation engine". This engine can
      be extended and configured in modular way using plugins (=OSGi bundles). On top of this engine there is an application/presentation layer,
      a Servlet would be one possible implementation of this layer. As a requirement this Servlet must be deployed in a standard servlet container such as Weblogic,
      using existing enterprise infrastructure, so I don't see an alternative to setting up the OSGi framework inside the servlet.

      • Sven Haiges

        Sven Haiges - 2004-10-21

        I could see another possibility that could be less "non-standard". What if you just ran KF independent of the Presentation Layer and connect them both via some kind of middleware. The Servlet could for example communicate with a special presentation bundle in the OSGi framework. I think there is a Web Service Bundle available, so my preferred solution would be:

        Presentation Layer: Java Web Application (Servlets)
        Business Layer: use an stateless session bean that invokes Web Services on the bundles, or invoke them directly from the servlets.

        Any other ideas?

    • Erik Wistrand

      Erik Wistrand - 2004-11-15

      The URL handler registration can now be disabled using



Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks