Dynamically loading plugin from another plugin [message #1761265] |
Tue, 09 May 2017 12:22  |
Eclipse User |
|
|
|
I need to load plugin from another plugin (loader). The plugin has menus defined in plugin.xml . When I load the plugin the first time it looks and works OK. When I load it the second time all UI items are present (from the loader and from the plugin) and UI is behaving in a strange way (some windows do not open, etc.). When I rename the plugin everything works fine again.
I need only loader UI to be present. I cannot figure out how to achieve it. It looks like something is cached, but I am not sure how to clear the cache automatically.
Any ideas?
|
|
|
|
Re: Dynamically loading plugin from another plugin [message #1761413 is a reply to message #1761391] |
Wed, 10 May 2017 17:24   |
Eclipse User |
|
|
|
It needs to work on 3 last versions of Eclipse. I think I found a working solution:
in Activator of the loader (in stop and start callbacks) I unload the dynamically loaded plugin. Now all menus are gone and everything works fine.
This is unload code:
public static void UninstallPlugin()
{
Bundle plugin = Platform.getBundle("bb.aa.cc.dd");
if(plugin == null) return;
try {
Bundle bundle = plugin.getBundleContext().getBundle(0);
FrameworkWiring adapt = bundle.adapt(FrameworkWiring.class);
ExtensionRegistry registry = (ExtensionRegistry) Platform.getExtensionRegistry();
Field privateStringField = ExtensionRegistry.class.getDeclaredField("masterToken"); //$NON-NLS-1$
privateStringField.setAccessible(true);
Object masterToken = privateStringField.get(registry);
IContributor contributor = ContributorFactoryOSGi.createContributor(plugin);
IExtension[] points = registry.getExtensions(contributor);
for (IExtension point : points) {
point.getExtensionPointUniqueIdentifier();
Platform.getExtensionRegistry().removeExtension(point, masterToken);
}
adapt.refreshBundles(adapt.getRemovalPendingBundles());
plugin.uninstall();
Thread.sleep(5000);
//update menus
IWorkbenchWindow window = Workbench.getInstance().getActiveWorkbenchWindow();
MenuManager menuManager = ((WorkbenchWindow)window).getMenuManager();
//menuManager.update();
menuManager.updateAll(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
|
|
|
Re: Dynamically loading plugin from another plugin [message #1761828 is a reply to message #1761413] |
Thu, 11 May 2017 15:25  |
Eclipse User |
|
|
|
You should be able to use the OSGi-level APIs to load and unload the bundles directly, and the Extension Registry will react automatically. But you will still need to refresh the window menu manager (and be aware that you may have multiple windows and will need to update them all). The support for cleaning up from dynamically added or removed extension contributions is uneven and though much improved in Neon.
|
|
|
Powered by
FUDForum. Page generated in 0.02403 seconds