原网址:http://blog.csdn.net/zajin/article/details/9450883
一、概述
一直关注我blog的朋友会看到,最近我一直在关注Eclipse源代码的编译工作,但除了简单的编译设置和烦琐而极具耐心的源代码剥离,似乎还没有什么 太有趣的东西出现。现在,经过了前面一系列的热身运动,我将和大家一起走入Eclipse的内核,看看它到底是怎么工作的?
1、Eclipse源代码 版本:3.1.1 大小:63.2MB
eclipse-sourceBuild-srcIncluded-3.1.1.zip
下载地址:http://download.eclipse.org/eclipse/downloads
2、源代码阅读工具 Source Insight V3.5
它其实是一个代码编辑软件,因为有强大的代码分析工具,可以很方便地跟踪代码的相关性,所以常用来作为阅读代码的工具。
下载地址:http://sourceinsight.com/down35.html
为了方便代码的分析,我们只提取以下几个插件的代码:
org.eclipse.platform org.eclipse.platform_3.1.1.jar
org.eclipse.core.commands org.eclipse.core.commands_3.1.0.jar
org.eclipse.core.expressions org.eclipse.core.expressions_3.1.0.jar
org.eclipse.core.runtime org.eclipse.core.runtime_3.1.1.jar
org.eclipse.help org.eclipse.help_3.1.0.jar
org.eclipse.jface org.eclipse.jface_3.1.1.jar
org.eclipse.osgi org.eclipse.osgi_3.1.1.jar
org.eclipse.swt.win32.win32.x86 org.eclipse.swt.win32.win32.x86_3.1.1.jar
org.eclipse.swt org.eclipse.swt_3.1.0.jar
org.eclipse.ui.workbench org.eclipse.ui.workbench_3.1.1.jar
org.eclipse.ui org.eclipse.ui_3.1.1.jar
org.eclipse.update.configurator org.eclipse.update.configurator_3.1.0.jar
将这些代码解压缩到一个空目录里,然后导入到Source Insight的Project里。
二、Eclipse启动过程
首先我们从Eclipse的启动过程开始分析。
1、eclipse.exe
它是Eclipse的启动文件,是与平台相关的可执行文件。它的功能比较简单,主要是加载startup.jar文件,代码在Eclipse源代码的 /features/org.eclipse.platform.launchers/library目录下,对应多个平台。对于win32平台,你可以 直接运行win32目录下的build.bat文件来编译得到它(需要安装C编译器)。
2、startup.jar
这个是Eclipse真正的启动文件,你可以在命令行下运行java -jar startup.jar命令来启动Eclipse。它的入口是org.eclipse.core.launcher.Main,它对应的源代码在 org.eclipse.platform/src目录的子目录下的Main.java。我们从main函数往后跟踪,找到basicRun,这个是启动 的主要部分。
protected void basicRun(String[] args) throws Exception {
......
setupVMProperties(); //设置VM属性
processConfiguration(); //读取configuration/config.ini配置文件
......
// need to ensure that getInstallLocation is called at least once to initialize the value.
// Do this AFTER processing the configuration to allow the configuration to set
// the install location.
getInstallLocation();
// locate boot plugin (may return -dev mode variations)
URL[] bootPath = getBootPath(bootLocation);
setSecurityPolicy(bootPath); //设置执行权限
// splash handling is done here, because the default case needs to know
// the location of the boot plugin we are going to use
handleSplash(bootPath);
invokeFramework(passThruArgs, bootPath); //启动Eclipse内核
}
这个函数前面部分是设置一些属性,最关键的是最后invokeFramework函数,它是启动Eclipse的核心。下面我们看看invokeFramework函数的具体内容。
private void invokeFramework(String[] passThruArgs, URL[] bootPath) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, Error, Exception, InvocationTargetException {
......
URLClassLoader loader = new StartupClassLoader(bootPath, parent);
Class clazz = loader.loadClass(STARTER); //加载 String STARTER = "org.eclipse.core.runtime.adaptor.EclipseStarter";
Method method = clazz.getDeclaredMethod("run", new Class[] {String[].class, Runnable.class}); //获得run方法
......
method.invoke(clazz, new Object[] {passThruArgs, endSplashHandler}); //调用run方法
......
}
首先创建加载器loader,它是一个URLClassLoader类型。接着加载类"org.eclipse.core.runtime.adaptor.EclipseStarter",获得其run方法,然后调用此方法。
3、OSGI启动
"org.eclipse.core.runtime.adaptor.EclipseStarter"类的源代码位于/plugins /org.eclipse.osgi/eclipseAdaptor/src/org/eclipse/core/runtime/adaptor。可见 它已经位于OSGI包内,它是OSGI的启动类。
public static void startup(String[] args, Runnable endSplashHandler) throws Exception {
......
adaptor = createAdaptor(); //建立适配器
......
OSGi osgi = new OSGi(adaptor); //建立OSGI对象,这就是我们要找的东西
......
osgi.launch(); //启动OSGI
......
context = osgi.getBundleContext(); //获得已加载的Bundle的执行上下文
......
Bundle[] startBundles = loadBasicBundles(); //加载Bundle
setStartLevel(getStartLevel()); //设置启动级别
......
}
相关推荐
OSGI 实例 eclipse 插件开发
《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自执笔,并得到了Eclipse官方技术社区的强烈推荐,权威性毋庸置疑!内容全面,系统讲解了利用Eclipse RCP和Spring OSGi开发大规模Java应用的...
BlueDavy的opendoc《OSGi实战》所涉及实例的源代码。OSGi入门的经典教程。
工作中用到了osgi,又需要阅读或者修改osgi的源代码,好不容易找到了3.71版的,其实我需要的只是org.eclipse.osgi.source_3.7.1.R37x_v20110808-1106.jar,里面有osgi类加载相关的代码,它位于压缩包的equinox-SDK-...
OSGi进阶源代码,OSGi集成webwork + spring + hibernate
深入理解OSGi:Equinox原理、应用与最佳实践源代码,同时附带equinox-SDK-3.8源代码。
这个是eclipse OSGI 实战,里面介绍了很多osgi的知识,还有一个进行osgi编程的例子,发出来给大家共享一下,希望有帮助
深入理解OSGi:Equinox原理、应用与最佳实践,书本源代码
osgi插件化开发流程
org.eclipse.osgi_3.7.0.v20110613以及源码 。
Eclipse-OSGi内核源码分析
深入理解OSGi:Equinox原理、应用与最佳实践.pdfOSGi应用开发
Eclipse 3.0采用OSGI作为其插件体系结构
From its inception Eclipse was mainly designed to be a tooling platform, but with Version 3.0, Eclipse is now evolving toward a Rich Client Platform (RCP). This change, driven by the open-source ...
介绍了eclipse插件开发基本理论、插件项目结构及开发步骤,最后再给出两个插件开发实例。结构如下: 1.介绍eclipse内核结构、扩展点机制; 2.介绍SWT的原理,控件设计及事件响应机制 3.介绍OSGi 4.介绍RCP(Rich ...
包含书中的用例源代码供大家研究学习。 OSGi国内推广者林昊多年经验的结晶 涵盖OSGi从入门到深入的知识体系 引领OSGi国内研究和普及 本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入的知识体系,从...
1.一篇實例教程,配图分部讲解 2.SpringDM实例实现OSGI源码 3.纯OSGI实现源码
解答如何基于OSGi怎么来设计/实现/部署/测试项目和产品