2010-06-15 42 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的實例是唯一的,因此它將加載捆綁的正確版本。