Code explanation for implementing the Class Loader Mechanism Using equinox (1)

Source: Internet
Author: User

 

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.