Search code examples
osgiapache-felixbundles

get all implementations of an api


I have written an API Bundle and some implementing services.

Now i want to use them as plugins, so first of all i need a list of all the services i have.

I'm starting the api like this:

    Framework m_fwk = new org.apache.felix.framework.FrameworkFactory().newFramework(null);
    m_fwk.init();
    AutoProcessor.process(null, m_fwk.getBundleContext());
    m_fwk.start();

    Bundle api = m_fwk.getBundleContext().installBundle(
    "file:/foo/bar/api/target/api-1.0.jar");

    api.start();

So now the API is loaded. Now i need to know which bundles implements this API, how can i get this information from the framework?


Solution

  • You only seem to load an API bundle, I guess you want to install other bundles for the implementations? Most people then load a director or so:

    for ( File b : bundles.listFiles() ) {
        ctx.installBundle( b.toURI().toURL() );
    }
    

    Each of these bundle should look like (using DS):

    @Component
    public class Impl implements API {
      public whatever() { ... }
    }
    

    The bundle collecting the services could look like:

    @Component
    public class Collector {
      @Reference(type='*')
      void addAPI( API api ) { ... }
      void removeAPI( API api ) { ... }
    }
    

    This is done with the bnd annotations for DS (see bndtools for examples). However, you can also implement/collect the services in Blueprint, iPojo, and many other helpers.