Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting OSGI Bundle from Eclipse IConfigurationElement

Tags:

eclipse

osgi

I am looking for extensions that implement a specific extension point, and am using the following acceptable method to do this:

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

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

I then would like to get the version of the defining bundle. I would use the following API, but the API for PluginVersionIdentifier is deprecated:

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

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

I could not find an alternative in the new API - i.e. from a IConfigurationElement, how do I get the version id descriptor of the bundle. Obviously, from the Bundle I can get the version using the Bundle.getHeaders(), getting the Bundle-Version value - but how do I get the Bundle in the first place??? Platform.getBundle(bundleId) is not enough since I might have multiple versions of same bundle installed, and I need to know who I am. At the moment I have a chicken & egg situation, and the only solution I have is the above deprecated API.

like image 259
Hayden Marchant Avatar asked Oct 15 '22 04:10

Hayden Marchant


1 Answers

All this information is based on Eclipse 3.6:

Your IContributor will be an instance of RegistryContributor if you are in the osgi environment which of course you are or you wouldn't be having this issue.

RegistryContributor gives you two methods: getID() and getActualID(). getID() may return the host bundle if this was loaded from a fragment. getActualID() always loads the id of the fragment/bundle the contributor represents. You can use this id in your BundleContext.getBundle(long id) method. Here is a snippet:

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());          
}

I use a fall through method that will degrade gracefully to a non-version aware solution if IContributor gets a new default implementation in the future. The bundle id is unique to an instance of OSGi so it will load the correct version of the bundle.

like image 52
rancidfishbreath Avatar answered Jan 04 '23 07:01

rancidfishbreath