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....

6 comments:

  1. There is another way... sort of. Just install a new bundle using the OSGi API, and then get PackageAdmin to resolve it. The extension registry will pick up any plugin.xml that it finds therein and install the extensions.

    You could even dynamically generate the bundle contents including plugin.xml on the fly!

    Of course this is a bit of a heavyweight solution, but it might still be useful depending on your requirements.

    ReplyDelete
  2. The title of this post is misleading. The registry IS dynamic. It does react properly to bundles being added and removed.
    You are disappointed because the granularity at which the dynamicity is available is not suitable for your use case, which is another problem.

    ReplyDelete
  3. Le Scal,

    well, the API suggests that you can add extensions (contributions) dynamically (the method is called addContribution). But the reality is that it is not possible uising that API. I now learned that OSGi would allow me to add bundles at runtime. But I have to investigate this further....

    ReplyDelete
  4. What about escript?

    From the website:
    http://www.eclipsefaq.org/escript/

    "In an alternative execution mode, an eScript file can be presented to a running Eclipse in the form of text, and the plugin will be installed dynamically. In this manner, plugins can be installed and run quite easily from any source that can generate a text string. For instance, a plugin could contain multiple plugins inside itself and activate certain ones on demand. Furthermore, plugins could be extracted quite easily from a web site by using an http connection to download the specification as a text file, customize it, and instantiate using the eSDE runtime."

    ReplyDelete
  5. Eclipse is a very powerful and extensible IDE but this tips looks little tough for normal developer to me.

    Thanks
    java remote debugging in Eclipse

    ReplyDelete