Search code examples
eclipse-pluginosgiclassloaderbundleequinox

Am I allowed to change parent class loader of an OSGi bundle?


Problem Statement: Locate a resource (let say myClass) in Bundle A and load it, if not found Locate resource in Bundle B and load it pro grammatically, While the control is in Bundle C. I have a reference to ClassLoaders of both A and B bundle.

What I know is: For every OSGi bundle the parent classLoader is Bundle 0's class loader. and the System bla bla

Will it be ok If I change the parent of CL of bundle B ---to--> CL of bundle A. and getClass from CL of bundle B ?

will it be under best practices ?

Restrictions: do not use import, export, require statements for solution.

Thanks


Solution

  • The best practice in OSGi is to not change the classloaders. The system in OSGi is very well thought out and if you change it you will get into all sorts of trouble. Ideally you should not load classes by hand at all. If you need an instance of a class or a resource then load it in the bundle that contains it. If you then need it in another bundle the best practice is to publish a service with the instance or a factory with the instance.