2012-03-13 53 views
2

是否有API方法來獲取當前插件的ID?如何在eclipse rcp應用程序中找到插件ID

注:我的RCP應用程序包含一個以上的插件項目,其中一些不包含插件類(Activator類)

,所以我不能讓插件ID使用這樣的方法:

Plugin.getBundle().getSymbolicName(); 

回答

0

在每個插件類中,您必須擁有您的插件的私有靜態實例變量,並且您應該實現getInstance()方法以將引用返回給實例,我相信您必須已經在執行此操作,如果沒有看到插件類的參考docomentaion。在YorPluginClass1.getInstance().getBundle().getSymbolicName();YorPluginClass2.getInstance().getBundle().getSymbolicName();之後應該返回它們各自的插件的符號名稱。

0

我用下面的代碼在我Activators之一:

/** 
* Returns the bundle id of the bundle that contains the provided object, or <code>null</code> 
* if the bundle could not be determined. 
* 
* @param clazz the object to test 
* @return the build ID or <code>null</code> 
*/ 
public String getBundleId(Class<? extends Object> clazz) { 
    if (clazz == null) return null; 

    final PackageAdmin packageAdmin = getBundleAdmin(); 
    if (packageAdmin == null) return null; 

    final Bundle source = packageAdmin.getBundle(clazz); 
    if (source == null || source.getSymbolicName() == null) return null; 

    return source.getSymbolicName(); 
} 

/** 
* Returns the OSGi Package Admin service, if available. 
*/ 
public PackageAdmin getBundleAdmin() { 
    if (bundleTracker == null) { 
     bundleTracker = new ServiceTracker(getContext(), PackageAdmin.class.getName(), null); 
     bundleTracker.open(); 
    } 
    return (PackageAdmin) bundleTracker.getService(); 
} 

是的,我知道了PackageAdmin已過時,但我還沒有更新它的時間...

相關問題