How to handle the uninstall event from within a plugin?

marc raiser September 8, 2011

Hi,

Was wondering how to catch the uninstall event so that the plugin could clean up after itself (like remove plugin specific properties etc...)

Had a brief look around but didn't find anything, so appologies if this has already been dealt with elsewhere.

Cheers

3 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Igor Sereda [ALM Works]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 8, 2011

Here's a utility class we use for this purpose:

public final class StopListener {
  private final PluginEventManager myPluginEventManager;
  private final String myModuleKey;
  private final Runnable myStop;
 
  private StopListener(PluginEventManager pluginEventManager, String moduleKey, Runnable stop) {
    myPluginEventManager = pluginEventManager;
    myModuleKey = moduleKey;
    myStop = stop;
  }
 
  public static void install(PluginEventManager pluginEventManager, String moduleKey, Runnable stop) {
    pluginEventManager.register(new StopListener(pluginEventManager, moduleKey, stop));
  }
 
  public void stop() {
    myPluginEventManager.unregister(this);
    if (myStop != null) myStop.run();
  }
 
  @PluginEventListener
  public void onShutdown(PluginFrameworkShutdownEvent event) {
    stop();
  }
 
  @PluginEventListener
  public void onPluginDisabled(PluginDisabledEvent event) {
    if (event == null) return;
    stopIfMe(event.getPlugin());
  }
 
  @PluginEventListener
  public void onFrameworkRestarting(PluginFrameworkWarmRestartingEvent event) {
    stop();
  }
 
  @PluginEventListener
  public void onModuleDisabled(PluginModuleDisabledEvent event) {
    if (event == null) return;
    ModuleDescriptor module = event.getModule();
    if (module == null) return;
    Plugin plugin = module.getPlugin();
    if (plugin == null) return;
    if (Util.STRUCTURE_PLUGIN_KEY.equals(plugin.getKey()) && myModuleKey.equals(module.getKey())) {
      stop();
    }
  }
 
  @PluginEventListener
  public void onPluginUninstalledEvent(PluginUninstalledEvent event) {
    if (event == null) return;
    stopIfMe(event.getPlugin());
  }
 
  @PluginEventListener
  public void onPluginRefreshedEvent(PluginRefreshedEvent event) {
    if (event == null) return;
    stopIfMe(event.getPlugin());
  }
 
  protected void stopIfMe(Plugin plugin) {
    if (plugin == null) return;
    if (Util.STRUCTURE_PLUGIN_KEY.equals(plugin.getKey())) {
      stop();
    }
  }
}
 
Igor Sereda [ALM Works]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 8, 2011

Replace Util.STRUCTURE_PLUGIN_KEY with your own plugin key.

Typical use of the class:

public MyComponent(PluginEventManager pluginEventManager, ....) {

initStuff();

StopListener.install(pluginEventManager, "my-module-key", new Runnable() {

public void run() {

cleanupStuff();

}

});

}

marc raiser September 11, 2011

Thank you Igor, I'll give that a try.

0 votes
Raju
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 2, 2011

You can define a compoent in your plugin which implements DisposableBean interface.

method destroy() from this interface gets called when plugin is disabled or removed.

hope this helps,

Raju

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 8, 2011

Not an answer because of course this is not authoritative, but I don't think this is possible. You can catch enabled and disabled events but that's it. I don't think it's possible to reliably know when a plugin is installed, eg it could be removed from installed-plugins and .osgi-plugins and then jira restarted.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events