In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to carry out Equinox loading Bundle Class implementation, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.
When Equinox creates the ClassLoader of Bundle, it first gets the classpath of bundle, and then executes the createBCLPrevileged method, which passes on to BaseData to create the ClassLoader.
The key code snippet for BaseDate to create ClassLoader is:
ClassLoadingHook [] hooks = adaptor.getHookRegistry (). GetClassLoadingHooks (); ClassLoader parent = adaptor.getBundleClassLoaderParent (); BaseClassLoader cl = null; for (int I = 0; I < hooks.length & & cl = = null; iTunes +) cl = hooks [I] .createClassLoader (parent, delegate, domain, this, bundleclasspath); if (cl = null) cl = new DefaultClassLoader (parent, delegate, domain, this, bundleclasspath); return cl
In Equinox, adaptor.getBundleClassLoaderParent returns bootstrap classloader by default. You can change this parent classloader by modifying the started osgi.parentClassLoader.
There are four optional values for osgi.parentClassLoader, which are:
L boot: default
L app: SystemClassLoader
L ext: parent of SystemClassLoader
L fwk: the ClassLoader that starts Equinox
ClassLoadingHook does not take any action when it comes to createClassLoader, so * ClassLoader is built by creating DefaultClassLoader objects, where the parameter parent is the null,delegate instance and the bundleclasspath parameter is the classpath of bundle.
After the above steps, the ClassLoader is created and you can start loading the class. According to the above, the Class of the Bundle is completed by DefaultClassLoader.
Looking at the loadClass code of DefaultClassLoader, it is found that the real process of loading class is completed by calling the findClass of delegate, and the delegate parameter corresponds to the BundleLoader instance and to the findClass method of tracking BundleLoader.
Code snippet of BundleLoader's findClass method:
If (checkParent & & parentCL! = null & & name.startsWith (JAVA_PACKAGE)) return parentCL.loadClass (name)
From the above code snippet, you can see that Equinox will java. The first class is handed over to parent classloader to load, which means that there is no need to provide external export java in the system. The package at the beginning.
If it wasn't for java. The first class is loaded by the findClassInternal method.
The findClassInternal method follows the loading order of the Class defined in the OSGi specification, but with slight changes:
1) determine whether to leave it to parent classloader to complete the loading
When starting Equinox, Equinox reads the org.osgi.framework.bootdelegation attribute, which corresponds to the package that needs to be loaded from the parent classloader. If the value is *, all are loaded from the parent classloader. If the value is configured as a specific package, then put into the bootDelegation collection; if the configuration is a package with wildcards, then put into the bootDelegationStems collection.
When judging, Equinox first determines whether everything is loaded from parent classloader, if so, from parent classloader.
If the package of the class that needs to be loaded is in the bootDelegation or bootDelegationStems collection, it is also loaded from parent classloader.
If you do not load from parent classloader, proceed to the following steps.
2) try to call the extension of ClassLoaderDelegateHook provided by Equinox to load the
Equinox provides the interface extension of ClassLoaderDelegateHook. You can write the implementation of ClassLoaderDelegateHook and register it in Framework, so you will be notified when there are actions such as Class loading.
By default, there is no implementation of ClassLoaderDelegateHook in Equinox, so continue with the following steps.
3) determine whether it is in import-package, if so, leave it to the corresponding PackageSource to load.
According to the import-package configured by Bundle, determine whether the class to be loaded is in import-package. If so, it will be loaded by the corresponding PackageSource. When the PackageSource is loaded, it will be directly loaded by the classloader of the corresponding Bundle. If the package of the loaded class is in import-package, but the Class is still not found after loading, the ClassNotFoundException will be thrown directly. If loaded, it will be returned directly.
If the package of the class you need to load is not in import-package, continue with the following steps.
4) try to load from require-bundle
Try to use require-bundle to load, if loaded into, return directly, if not, continue with the following steps.
5) try to load from the current Bundle
It is not attempted to load from the current Bundle until the attempt of the above steps. The current Bundle loading method is to find the class file with the corresponding name from the Fragment of the Bundle-Classpath or the current Bundle, and read the file to load it. If the class file has been loaded, it will be cached. When loading again, there is no need to find and parse the class file.
If the desired class is still not found from the current Bundle, proceed to the following steps.
6) try to load from DynamicImport-Package
Determine whether the package of the class you are looking for is in DynamicImport-Package, and if so, leave it to the corresponding PackageSource to load. If it cannot be loaded in PackageSource, throw ClassNotFoundException; if it is not in DynamicImport-Package, then continue with the following steps.
7) try again to call the extension of ClassLoaderDelegateHook provided by Equinox to load
This step is the same as step 2), so continue with the following steps by default.
8) try to use eclipse's buddy mechanism to load
The Buddy mechanism is an extension of Eclipse and does not conform to the OSGi specification, so there is no in-depth analysis here.
9) judge certain conditions and load them from parent classloader if they are met.
The conditions for judging are: parent classloader is not null, does not load from parent classloader, the backward compatibility property (osgi.compatibility.bootdelegation) of Equinox is true, and bug class of jvm. If the above conditions are met, try to load from parent classloader.
If, after all the steps above, the class that needs to be loaded is still not found, the ClassNotFoundException is thrown.
From the above code analysis, the loading of Class from outside Bundle ClassLoader can be controlled by osgi.parentClassLoader and org.osgi.framework.bootdelegation in Equinox, which is very useful for integrating other containers of Equinox. In addition, the loading of Class can be changed by implementing ClassLoaderDelegateHook.
The above is how to implement Equinox loading Bundle Class. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.