简体   繁体   中英

How get all classes implementing an interface in a specific OSGi Bundle

I have an Interface called 'Foo' and i have a bundle 'b'.

Now I want to get all classes implementing interface 'Foo' in bundle b

Something like below

org.osgi.framework.Bundle bundle = ...;
List<Class<? extends Foo>> allImplemetation = getAllImplementation(bundle);
  1. The clean way to find implementations in OSGi is to let each bundle publish each implementation as an OSGi service. This allows to keep the impl classes private and makes sure your central bundle is nicely decoupled from the user bundles. Here you can find some guidance how to do this .

  2. If this is not possible then you can use a BundleTracker to get a call back when new bundles are installed. You can then use Javassist or XBean finder to scan the bundle class path for classes that implement and interface. This variant is pretty difficult to do right though. So I would try to avoid it.

  3. A kind of a middle of the road solution would be to use the same approach as ServiceLoader. You create a file at a special directory in the jars that contains the class names of the implementations to load. You can then use the BundleTracker like above to find the newly installed bundle, read this resource and then use the ClassLoader of the bundle to load the implementation class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM