0

In the JavaEE/CDI world, I know how to have the list of instances of classes implementing a given interface : by using Instance<MyInterface> combined with reflections library.

But in the OSGI/iPOJO world, how to do so ?

I know I get one instance by using @Requires MyInterface anInstance. But how can I have a programmatic access to all those classes ?

Riduidel
  • 22,052
  • 14
  • 85
  • 185

1 Answers1

0

Just use the BundleContext:

@Context BundleContext context;

public List<ServiceReference> getInstancesImplementingX() {
    return context.getServiceReferences(X.class);   
}
Clement
  • 2,817
  • 1
  • 12
  • 11
  • Unfortnuatly, this requires all my loaded classes to be services, which I did not expected ... – Riduidel Sep 30 '15 at 07:05
  • How, there is a way without having to load the class: context.getServiceReferences("name.of.my.class", null); It returns an array, null if no service matches. – Clement Oct 01 '15 at 08:28