I think the key to your solution is the following I read in another post, Each Eclipse Plugin maintains it's own classloader.
So I can tell you what my Eclipse (3.7) plugin is doing for dynamic loading, I'm not sure if you can "fit" it into your needs. Basically, my plugin does encryption/decryption. As a new enhancement, I allow the user to load one or more Jar files via a FileDialog, where each jar file is loaded using a URLClassLoader. I create my "custom" classloader with the following line :
this.FQCNLoader = new URLClassLoader ( getFQCNUrls(), this.getClass ().getClassLoader () );
The key was to pass in the Eclipse plugin's ClassLoader as the second argument...This means your "custom" classloader has the CLASSPATH of the Eclipse plugin plus whatever URLs you are adding (the getter getFQCNUrls() returns a URL[]).
The other key piece, was I maintain the "custom" classloader in a View that I know persists for the entire life of the Eclipse plugin. The View resides above all code that needs to reference the dynamic *.jar classes.
So, you'll need to do something along the same lines. Your "custom" classloader would need to live in a Class above both pluginA and pluginB. Also, i would think because each plugin has it's own classloader, you're actually going to need to build a "custom" classloader that does something like the following :
1.) Let pluginB load dynamic *.jar/classes. Convert into URL[].
2.) Create "custom" loader, pass in pluginB URL[] PLUS pluginA classloader.
Here is the addURL() method in my View class that builds the "custom" classloader...it's a little ugly, because I wrote it in a hurry and it rebuilds the "custom" classloader each time the user loads a *.jar, but it does work in Eclipse and without OSGi :
private int URLCount = 0;
private URL[] FQCNUrls = new URL[10];
private ClassLoader FQCNLoader = new URLClassLoader ( this.FQCNUrls );
...Lots of code...
public void addURL ( URL theURL ) throws IOException {
//CPTest lclsTest = new CPTest();
if ( getURLCount () == 0 ) {
getFQCNUrls()[getURLCount()] = theURL;
setURLCount ( 1 );
}
else {
boolean lisThere = false;
for (int i = 0; i < getURLCount(); i++) {
if (getFQCNUrls()[i].toString().equalsIgnoreCase(theURL.toString())) {
lisThere = true;
}
}
if ( lisThere ) {
System.out.println ( "File URL [" + theURL.toString () + "] already on the CLASSPATH!" );
}
else {
getFQCNUrls()[getURLCount()] = theURL;
setURLCount ( getURLCount ()+1 );
}
}
// CLEAR : Null out the classloader...
this.FQCNLoader = null;
// BUILD/RE-BUILD : the classloader...
this.FQCNLoader = new URLClassLoader ( getFQCNUrls(), this.getClass ().getClassLoader () );
//try {
// System.out.println ( "---------------------------------------------------------------------------" );
// System.out.println ( "Current Working Directory.............[" + System.getProperty ( "user.dir" ) + "]" );
// System.out.println ( "---------------------------------------------------------------------------" );
// System.out.println ( "this.classes []! " );
// lclsTest.dumpClasses ( this.getClass ().getClassLoader () );
// System.out.println ( "---------------------------------------------------------------------------" );
//
// System.out.println ( "---------------------------------------------------------------------------" );
// System.out.println ( "File URL [" + theURL.toString () + "] added! " );
// lclsTest.dumpClasses ( this.FQCNLoader );
// System.out.println ( "---------------------------------------------------------------------------" );
// System.out.println ( "---------------------------------------------------------------------------" );
// System.out.println ( "ClassLoader.getSystemClassLoader() " );
// lclsTest.dumpClasses ( ClassLoader.getSystemClassLoader() );
// System.out.println ( "---------------------------------------------------------------------------" );
// Class cls = this.FQCNLoader.loadClass ( "com.lmig.RRFECF.pso.security.nonproduction.CM_RRFECF_development_securitykey" );
// Class cls = Class.forName(theFQCN, false, theClsLoader);
// theObjectKey = ( Object ) theClsLoader.loadClass(theFQCN);
//}
//catch ( Exception e ) {
//// TODO Auto-generated catch block
// e.printStackTrace();
//}
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(getFQCNLoader(), new Object[] {
theURL
});
}
catch (Throwable t) {
t.printStackTrace();
throw new IOException(
"Error, could not add URL to system classloader");
}
}
BTW, I encourage you to grab that CPTest class commented out in the first line, it was invaluable to me on getting insight into the different classloaders and what was in them. I left the Sysout lines in the code so you can see how to dump all the classes in a classloader...