In the Equinox environment, each bundle is loaded by an independent classloader to implement classes. In osgi framework, bundle is a modular Management Unit. All applications and resources must use bundle as the carrier. Each bundle has its own class loader. Different bundle (in the same VM) can share or hide the package through the import and export mechanisms. Class Loader establishes a loading class proxy model to share or hide resources.
The following is the code for loading classes implemented by equinox:
Class findclass (string name, Boolean checkparent) throws classnotfoundexception {
String pkgname = getpackagename (name );
// Follow the osgi Delegation Model
If (checkparent & parent! = NULL ){
If (name. startswith (java_package ))
// 1) If startswith "Java." delegate to parent and terminate search
// We Want to throw classnotfoundexceptions if a java. * class cannot be loaded from the parent.
Return parent. loadclass (name );
Else if (isbootdelegationpackage (pkgname ))
// 2) if part of the bootdelegation list then delegate to parent and continue of failure
Try {
Return parent. loadclass (name );
} Catch (classnotfoundexception cnfe ){
// We want to continue
}
}
Class result = NULL;
// 3) search the imported packages
Packagesource source = findimportedsource (pkgname );
If (source! = NULL ){
// 3) found import source Terminate search at the source
Result = source. loadclass (name );
If (result! = NULL)
Return result;
Throw new classnotfoundexception (name );
}
// 4) search the required Bundles
Source = findrequiredsource (pkgname );
If (source! = NULL)
// 4) attempt to load from source but continue on Failure
Result = source. loadclass (name );
// 5) search the local bundle
If (result = NULL)
Result = findlocalclass (name );
If (result! = NULL)
Return result;
// 6) attempt to find a dynamic import source; only do this if a required source was not found
If (Source = NULL ){
Source = finddynamicsource (pkgname );
If (source! = NULL)
Result = source. loadclass (name );
}
// Do buddy policy Loading
If (result = NULL & Policy! = NULL)
Result = policy. dobuddyclassloading (name );
// Last resort; do class context trick to work around VM bugs
If (result = NULL & findparentresource (name ))
Result = parent. loadclass (name );
If (result = NULL)
Throw new classnotfoundexception (name );
Return result;
}
What can I see from the above Code?
1. Explains why javax. * packages need to be introduced instead of Java. * packages in equinox bundle.
See equinox code explanation for implementing the Class Loader mechanism (2)
2. Explains what will happen if a package name is the same as the package name in the current bundle.
See equinox code explanation for implementing the Class Loader mechanism (3)
3. explained the impact of re-import of the same package after dynamic introduction.
See the following series of articles.