Reputation: 3674
I've OSGI bundle (say A) which depends on Non-osgi library (Say B). B is using Class.forName to load one of the class (ClassA from library A is of type ClassB from library B). I've wrapped the library B and made it osgi bundle and imported the packages which is needed in library A but I'm not able to load the class using Class.forName. Note that library B is third party library and i don't have any control on this.
Here is the manifest file of library B which i made OSGI enabled library -
Manifest-Version: 1.0
Bnd-LastModified: 1420745798993
Build-Jdk: 1.6.0_51
Built-By: xyz
Bundle-ManifestVersion: 2
Bundle-Name: dapclient
Bundle-SymbolicName: dapclient
Bundle-Vendor: dapclient
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
DynamicImport-Package: com.xxx.zzz.wi.shared.datacache.model
Export-Package: com.xxx.platform.yyy.persistence.parser.entity;versio n="2.0.2.8",com.intuit.xxx.yyy.persistence.utils;version="2.0.2.8" ,com.xxx.platform.yyy.persistence.parser.domain;version="2.0.2.8",co m.xxx.platform.yyy.persistence;version="2.0.2.8",com.xxx.platform .yyy.persistence.types;version="2.0.2.8",com.xxx.platform.yyy.persis tence.annotations;version="2.0.2.8",com.xxx.platform.yyy.persistence .parser;version="2.0.2.8"
Tool: Bnd-2.1.0.20130426-122213
In this library, we're loading this package using ClasspathHelper.forPackage(packageName) where packageName is "com.xxx.zzz.wi.shared.datacache.model". ClasspatheHelper is used from reflections library.
Upvotes: 0
Views: 713
Reputation: 111
classForName uses 'the defining class loader of the current class', so you might have to import the package in A and B to make this work. So in general, ClassA must be in a separate bundle. But if you are that tightly coupled to the lib, it might be better to embeed 'B' int Bundle A.
Beside that, you can open a ticket to the creator of B to allow more generic classloading, like providing the class itself instead of a string, support some kind of resolver-hooks or at least use context-class-loading in which case you can use something like
Thread thread = Thread.currentThread();
ClassLoader oldLoader = thread.getContextClassLoader();
try {
thread.setContextClassLoader(getClass().getClassLoader());
//call lib here that supports context-class-loading
} finally {
thread.setContextClassLoader(oldLoader);
}
Upvotes: 1