osgiosgi-fragment

Replacing classes/resources with an OSGi fragment - possible without including a jar in the fragment?


We want to replace certain resources in a host OSGi bundle by adding an OSGi fragment.

As I understand it, the resources included in an OSGi fragment are merely added to the classpath of the host bundle. There is no guarantee that if the fragment is adding a resource that already exists in the host bundle, the resource from the fragment will be loaded: it could also still be the host version.

In order to make sure the fragment version of the resource is loaded instead of the host version, http://wiki.osgi.org/wiki/Fragment mentions it is possible to use the Bundle-ClassPath header to specify resources as "first"(preferred).

It gives patch.jar as an example:

Bundle-ClassPath: patch.jar,.

As mentioned there: "Since patch.jar is ahead of '.' it will allow classes to be preferentially loaded from the fragment instead of the host."

I could use this technique, but this means I first have to bundle my fragment resources in a separate jar and then include this jar in the fragment bundle.

Is there a way to preferentially load classes/resources from the fragment instead of the host without having to include a jar in the fragment?


Solution

  • You don't have to include a jar. You can instead use a "patch" directory and then put your classes in a patch directory in the fragment.