After hearing about OSGi fragments, I was wondering:
Can I use a fragment to inject a patch, that is, replace an existing class in an existing, signed bundle?
Is there documentation how to do that?
After hearing about OSGi fragments, I was wondering:
Can I use a fragment to inject a patch, that is, replace an existing class in an existing, signed bundle?
Is there documentation how to do that?
The answer is no.
OSGi fragments don't have their own class loader and they are seen as part of the host. They can't inject the new code into replacing the existing class. The common usage of fragment is providing different implementations for different hosts. Another bundles who require its capability(exported by the host bundle) won't depend on the concrete implementation.
Bundle already is the atom concept of OSGi, you should provide newer version of it if you want to patch something.
I haven't played around with signed bundles, but their is a way to get fragments to override classes in the host bundle. However you need to have planned for this ahead of time.
In your Host Bundle define a Bundle-Classpath thus:
Bundle-Classpath: jar-not-in-host.jar;.
This jar file should not exist in the host. Then your fragment can contain a jar called jar-not-in-host.jar. When the fragment is attached the classes in the jar-not-in-host.jar will be loaded before the classes in the root of the host bundle.
Not very nice I know, but it works.