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
- equinox的框架的类加载器
- Equinox加载java.*以外的包
- Equinox加载Bundle Class的实现
- Equinox 框架进行 OSGi 环境下的 Web 开发
- 使用 Equinox 框架进行 OSGi 环境下的 Web 开发
- 使用Equinox框架进行OSGi环境下的Web开发
- 使用 Equinox 框架进行 OSGi 环境下的 Web 开发
- Eclipse的核心 - Equinox
- equinox的DS
- equinox的osgi命令
- Equinox容器的搭建
- equinox实现osgi类加载机制
- equinox 的介绍以及应用。
- equinox的plugin fragment开发(实现runtime加载tools.jar)
- OSGI之equinox框架的Abundle无法导入Bbundle导出的包
- 在ideal中以equinox-SDK-Neon框架输出一个基于osgi的helloworld例子
- Cobertura统计Equinox的bundles的覆盖率
- org.eclipse.equinox.http.helper 的获得
- 基于c# 类、接口、结构的联系与区别详解
- poj 2299Ultra-QuickSort
- 连载《一个程序猿的生命周期》- 30、争取内蒙区的市场销售
- 杭电1005-Number Sequence
- ubuntu grub引导
- equinox的框架的类加载器
- 背景图编辑
- JVM调优总结 -Xms -Xmx -Xmn -Xss
- Android 起步之路
- 嵌套循环连接(Nested Loops), 合并联接(Merge), 哈希联接(Hash)的适用情况
- Servlet 的生命周期
- 如何根治安卓卡顿短板 三星这次有办法
- LaunchImage设置失败的可能原因之一
- 两个线程按顺序打印1~10