Class.forName() and OSGi

When calling Class.forName() in an OSGi bundle you need to ensure the target class is visible within the context of the calling code’s bundle. For example Berkeley DB (BDB) Java Edition Direct Persistence Layer (DPL) uses Class.forName() to introspect annotated entities. If you have bundled up the BDB runtime in one bundle, then that bundle will need to be able to discover the class files of any DPL entity classes in your app (which are likely in a different bundle). You likely don’t want to declare an Import-Package dependency on each and every package that contains annotated entities in your BDB bundle, instead use the DynamicImport-Package manifest directive to express a dependency on say your top level application layer packages (e.g. com.foo.app.*). Finally you need to export any packages containing annotated entities from the bundles they reside in (which smells bad to me, but I don’t know a better way to accomplish this).

Advertisements