equinox的框架的类加载器

来源:互联网 发布:mysql安装步骤 编辑:程序博客网 时间:2024/05/16 14:27

不同的类加载器加载的类,会有不同的命名空间,不同命名空间的类不能够感受到彼此的存在,并且不同的命名空间中,类型的数据是不同的,如下图所示:



所以对于同一个类,由不同的类加载器加载,即使是static的成员变量,也不是公用的,每一个类都有一份自己的static成员变量的数据。

所以为了达到这种粒度的类的隔离,可以用不同的类加载器进行加载。


equinox框架加载整个框架的代码为

 private Class<?> getImplClass() throws ClassNotFoundException {        ClassLoader thisCL = this.getClass().getClassLoader();        //useSeparateCL在当FrameworkProperties已经设置了属性的时候为true        if (!(useSeparateCL && (thisCL instanceof URLClassLoader)))            return Class.forName(implName);        URL[] cp = getFrameworkURLs((URLClassLoader) thisCL);        //用框架的classLoader加载整个框架        EquinoxFWClassLoader fwCL = new EquinoxFWClassLoader(cp, thisCL);        return fwCL.loadClass(implName);    }

从代码中可以看出,用新的ClassLoader加载框架的前提是,发现框架的配置FrameworkProperties中已经被设置的属性,如果框架使用这个配置,将影响主程序,并且框架也会受到主程序的影响,所以使用新的ClassLoader,可以避免相互的影响。


框架ClassLoader的实现:

/******************************************************************************* * Copyright (c) 2008, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html *  * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.osgi.launch;import java.net.URL;import java.net.URLClassLoader;class EquinoxFWClassLoader extends URLClassLoader {private static final String[] DELEGATE_PARENT_FIRST = {"java.", "org.osgi.", "org.eclipse.osgi.launch.", "org.eclipse.osgi.service.", "org.eclipse.osgi.framework.log", "org.eclipse.osgi.framework.adaptor", "org.eclipse.osgi.framework.internal.core.ReferenceInputStream"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$private static final String[] DELEGATE_CHILD_FIRST = new String[0]; // nothing right now is skippedprivate final ClassLoader parent;public EquinoxFWClassLoader(URL[] urls, ClassLoader parent) {super(urls, parent);this.parent = parent;}protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {Class<?> clazz = findLoadedClass(name);if (clazz != null)return clazz;boolean childFirst = childFirst(name);ClassNotFoundException cnfe = null;if (childFirst)try {clazz = findClass(name);} catch (ClassNotFoundException e) {// continuecnfe = e;}if (clazz == null)try {clazz = parent.loadClass(name);} catch (ClassNotFoundException e) {// continue}if (clazz == null && cnfe != null)throw cnfe;if (clazz == null && !childFirst)clazz = findClass(name);if (resolve)resolveClass(clazz);return clazz;}private boolean childFirst(String name) {for (int i = DELEGATE_CHILD_FIRST.length - 1; i >= 0; i--)if (name.startsWith(DELEGATE_CHILD_FIRST[i]))return true;for (int i = DELEGATE_PARENT_FIRST.length - 1; i >= 0; i--)if (name.startsWith(DELEGATE_PARENT_FIRST[i]))return false;return true;}}


调用方在初始化的时候,构造器的urls,使用的是AppClassLoader的类路径;parent传入的是AppClassLoader。

框架的classLoader实现非常简单,继承URLClassLoader,发现如果是需要优先使用父ClassLoader的情况,会先使用AppClassLoader进行加载,如果没有加载到再使用自身进行加载,整个过程也是使用的双亲委派模型。

0 0