Question

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?

Était-ce utile?

La 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.

Autres conseils

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.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top