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。

当然以上两种方法是等效的。

 

 

 

 

 

 
 
 
posted @   荒土  阅读(486)  评论(0编辑  收藏  举报
编辑推荐:
· 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)
点击右上角即可分享
微信分享提示