I had a similar problem. The solution boils down to delegating to the "owning" bundle to load the class. To do this you need of course to know the name of the bundle. In my case I was able to solve this problem with a couple helper methods that wrap the PackageAdmin service provided by OSGi:

        public String getBundleName(Class<?> cls) {
                Bundle bundle = pkgAdmin.getBundle(cls);
                if (bundle != null) {
                        return bundle.getSymbolicName();
                } else {
                        return null;
                }
        }

public Class<?> loadClass(String className, String bundleName) throws ClassNotFoundException {
                Bundle[] bundles = pkgAdmin.getBundles(bundleName, null);
                if (bundles == null || bundles.length == 0) {
                        return null;
                }
                return bundles[0].loadClass(className);
        }

When serializing the object, you can use getBundleName to get the name of the bundle which "owns" the class being serialized. This name needs to be included in the xml data you store. Then on deserialization, loadClass looks up the bundle by the name specified, then uses that bundle to load the class.

This code assumes that there will be only one version of the bundle with a given name present, but it would be fairly straightforward to extend this to look at bundle versions if needed...

HTH...

Kris

On Dec 1, 2009, at 10:44 AM, Ziegenlippe wrote:

Hello,

I cannot find a satisfying solution to a simple looking problem for quite a while.

The condensation of the problem:
Bundle A
the producer stores objects as xml files (e.g. with XStream)
it exports the /bundle.a/ package which contains the interface / Item/ it contains a private implementation package /bundle.a.impl/ which contains the item implementation class /ItemImpl/

Bundle B
the consumer needs to load and process /Item/ instances
therefor it imports package /bundle.a/ which contains the /Item/ interface

Problem: loading Item instances in bundle B leads always to ClassNotFoundExceptions

Even working with the last resort /DynamicImport-Package: * / does not solve the problem since it imports only exported packages.

From some hints in the internet I got the feeling that this issue is not really solved for OSGI. Is there any solution known? How deal others with that issue e.g. ActiveMq which claims to be OSGI compliant.
Or am I just on the wrong way?

Thank you in advance,
Andy



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to