0

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?

reox
  • 5,036
  • 11
  • 53
  • 98

3 Answers3

2

It sounds like you're trying to re-implement the OSGi service registry. Have a look at Blueprint or Declarative Services instead. At the very least I'd suggest using the OSGi service API to register and consume services.

Holly Cummins
  • 10,767
  • 3
  • 23
  • 25
1

Given that a Framework is also a Bundle, you can get a BundleContext that allows you to find all services you need. You could do something like

m_fwk.getBundleContext().getServiceReferences("com.example.MyInterface", null)

to get all implementers of a given service.

However, you should be aware that you are living in a different classloader than the inhabitants of your framework are.

Angelo van der Sijpt
  • 3,611
  • 18
  • 24
  • Well, you need to be aware that the inhabitants of the framework have a different view of your interface than you have. If you just want all implementers, without worrying about assignability, use `getAllServiceReferences`. (If this assignability-stuff dazzles you, you can read up on OSGi class handling.) – Angelo van der Sijpt Mar 29 '12 at 06:17
1

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.

KristofMols
  • 3,487
  • 2
  • 38
  • 48
Peter Kriens
  • 15,196
  • 1
  • 37
  • 55
  • mh yes, i want to have an API, which i need to install explicit. Then i want grab all bundles from a directory and look for these bundles which implements the API. For example: I have a Plugin-API and lots of plugins. now i want to load all plugins that match the API Version i installed previously. I think your example will do this right? – reox Apr 09 '12 at 07:26