Wednesday, February 21, 2007

No real support for dynamic extensions (aka dynamic plugin.xml)

Eclipse allows (theoretically) to add extensions dynamically (see "Leave Eclipse plug-in headaches behind" or "Contributing items to a toolbar dynamically"). Unfortunately, there's no way a normal plugin can do this without using internal classes :-(. Here's the proposed hack how to do it:

IExtensionRegistry reg = RegistryFactory.getRegistry();
// ExtensionRegistry is internal!!!!
Object key = ((ExtensionRegistry) reg).getTemporaryUserToken();
Bundle bundle = Activator.getDefault().getBundle();
IContributor contributor = ContributorFactoryOSGi.createContributor(bundle);
try {
// I have the content of my dynamic plugin in a file
// called dynamicplugin.xml
InputStream is = FileLocator.openStream(bundle,new Path("dynamicplugin.xml"), false);
reg.addContribution(is, contributor, false, null, null, key);
} catch (IOException e) {

Unfortunately ExtensionRegistry is an internal class and getTemporaryUserToken() is not exposed in any official way. Bug 112954 asks for a way to get to getTemporaryUserToken(). The solution is to use null as the key (token). Well, almost. In order to make this work, the system property eclipse.registry.nulltoken has to be set to true (Bug 112954 comment 25). But a normal plugin cannot set a system property before OSGi starts....

This effectively means: there is no way a normal well behaving plugin can provide a dynamic extension. Therefore I filed bug 174967 to fix that problem....