其实在google或者http://www.eclipse.org/forums/就能够找到这个问题的答案。
搜索关键字:rcp install pluginsundles 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。
当然以上两种方法是等效的。