2010-06-15 43 views
5

我寻找实现特定扩展点的扩展,并且正在使用以下可以接受的方法来做到这一点:在Eclipse IConfigurationElement得到OSGI包

IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();如果(extensionRegistry == null){ return TEMPLATES; }

IConfigurationElement [] config = extensionRegistry.getConfigurationElementsFor(“com.ibm.im.launchpoint.templates.template”);

然后我想要获取定义包的版本。我将使用以下API,但不推荐使用API​​ PluginVersionIdentifier:

(IConfigurationElement e:config)BlueprintTemplate template = new BlueprintTemplate();

IExtension declaringExtension = e.getDeclaringExtension(); PluginVersionIdentifier versionIdentifier = declaringExtension.getDeclaringPluginDescriptor()。getVersionIdentifier();

我无法在新的API中找到替代方案 - 即从IConfigurationElement中,我如何获取捆绑的版本ID描述符。显然,从Bundle中我可以使用Bundle.getHeaders()获得版本,获得Bundle-Version值 - 但是我怎样才能获得Bundle呢? Platform.getBundle(bundleId)是不够的,因为我可能安装了同一个bundle的多个版本,我需要知道我是谁。目前,我有一个鸡蛋情况,我唯一的解决方案是上面的弃用API。

回答

0

我建议浏览一下Javadoc弃用说明,替换记录。我发现下面的代码,但没有测试它。

String contributor = e.getDeclaringExtension().getContributor().getName(); 
Bundle bundle = Platform.getBundle(contributor); 
Version versionInfo = bundle.getVersion(); 

出于好奇:为什么你需要获得扩展插件的版本?据我所知,扩展点机制的目标是分离关于扩展器的特定信息,并且只需要扩展(plugin.xml)中描述的信息或引用的代码。

+0

再次,这并没有真正帮助我,因为可能会有多个包含该ID的捆绑包,但只有其中一个包含了该扩展的定义。 另一点,Bundle没有getVersion方法。 我需要的原因是我正在注册一个'文档模板'作为扩展点,并且可能有此模板的不同版本。所以当模板加载时,我想知道它是哪个版本,用于记号等等。现在,我在模式中为扩展点使用了一个额外的属性,这是一个耻辱。 – 2010-06-21 06:06:34

+0

我认为,这个额外的属性是更好的解决方案。通过这种方式,可以在不更新扩展版本的情况下更新插件,也无需修改接收器即可更新新版本。 有趣的是,该包不包含getVersion()方法。也许这是Eclipse 3.6中的一个新方法,我在研究贡献者时使用了它。 – 2010-06-21 07:55:04

3

所有这些信息是基于Eclipse 3.6:如果你是在OSGi环境,当然你或你不会有这个问题

IContributor将是RegistryContributor一个实例。

RegistryContributor给你两种方法:getID()getActualID()getID()可能会返回主机包,如果这是从一个片段加载。 getActualID()始终加载贡献者代表的片段/包的ID。您可以在BundleContext.getBundle(long id)方法中使用此ID。这里是一个片段:

Bundle bundle; 
if (contributor instanceof RegistryContributor) { 
    long id = Long.parseLong(((RegistryContributor) contributor).getActualId()); 
    Bundle thisBundle = FrameworkUtil.getBundle(getClass()); 
    bundle = thisBundle.getBundleContext().getBundle(id); 
} else { 
    bundle = Platform.getBundle(contributor.getName());   
} 

我使用告吹方法,将优雅降级到非版本感知解决方案,如果IContributor获得在未来的一个新的默认实现。捆绑ID对于OSGi的实例是唯一的,因此它将加载捆绑的正确版本。