RCP中如何使用代码安装、运行plugins
其实在google或者http://www.eclipse.org/forums/就能够找到这个问题的答案。
搜索关键字:rcp install plugins\bundles programmatically
大部分的操作都能在org.osgi.framework.Bundle这个接口里找到,比如start、stop、uninstall等等。
如果对这几个术语不熟悉,请找一本《OSGI最佳原理与实践》,详细阅读OSGI的生命周期相关内容。
Bundle接口也维护了所有的生命周期的状态常量比如:
int UNINSTALLED = 0x00000001;
Bundle的获取代码如下所示:
org.eclipse.core.runtime.Platform.getBundle(String symbolicName)
Platform是eclipse平台运行时的核心类,它是静态的不允许继承的,它的内部方法全部是静态方法。即是说,只要你依赖了org.eclipse.core.runtime插件,你可以在任何地方无条件的使用该类的所有公开静态方法。
symbolicName是指插件的插件名,比如org.eclipse.core.runtime_3.7.0.v20110110的插件名即是org.eclipse.core.runtime。
获取Bundle之后,你就可以对该bundle做任何你爱做的事,但是,不包括安装!
我们再看回Bundle插件,注意它的类注释的第一条:
“An installed bundle in the Framework.”
这一句说明,Bundle接口是已经安装的bundle实体,在一个插件jar包还没有被安装的时候,自然不构成Bundle。
我们要如何安装它呢?
找到org.osgi.framework.BundleContext接口。
BundleContext methods allow a bundle to:
Subscribe to events published by the Framework.
Register service objects with the Framework service registry.
Retrieve ServiceReferences from the Framework service registry.
Get and release service objects for a referenced service.
Install new bundles in the Framework.
Get the list of bundles installed in the Framework.
Get the Bundle object for a bundle.
Create File objects for files in a persistent storage area provided for the bundle by the Framework.
看到红色字体了吗?
为此,BundleContext提供了两个方法:
Bundle installBundle(String location, InputStream input) throws BundleException; Bundle installBundle(String location) throws BundleException;
具体的使用请自行参考注释。
如何获取BundleContext呢?
你可以使用一个现存的Bundle来getBundleContext,也可以在你的插件激活器(Activator,如果这个不清楚是什么,请回炉)中直接使用start方法的参数BundleContext。
当然以上两种方法是等效的。
标签:
动态安装bunlde
, 动态安装plugins
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)