3

is there a API method to get current plugin's id?

note : my rcp application contains more than one plugin project, some of them don't contains a Plugin class(the Activator class)

so I can't get plugin id use such method:

Plugin.getBundle().getSymbolicName();
CaiNiaoCoder
  • 3,269
  • 9
  • 52
  • 82

2 Answers2

0

In every plugin class you must have your private static instance variable for your plugin and and you should implement a getInstance() method to return the reference to the instance, i believe you must already be doing this, if not see the reference docomentaion for the Plugin class. After YorPluginClass1.getInstance().getBundle().getSymbolicName(); and YorPluginClass2.getInstance().getBundle().getSymbolicName(); should give the should return their respective plugin's symbolic names.

Viral
  • 245
  • 1
  • 2
  • 8
0

I use the following code in one of my 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();
}

And yes, I do know the PackageAdmin is deprecated, but I haven't had the time to update it...

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70