tohokuaikiのチラシの裏

技術的ネタとか。

Confluenceの自分自身のPluginUninstallEventをキャッチする方法

EventLitenerの使い方自体はこちらに過去書きましたが。。。

tohokuaiki.hateblo.jp

プラグインの自分自身のアンインストール(あるいは無効化)のEventはキャッチできない

・・・なんか、あたりまえっていう気もしますが、アンインストール時に何か処理をしたいっていう場合は致命的です。

ここにも書いてあります。
Event Listener Module - Atlassian Developers

Plugin Events
It is possible to listen for plugin install/uninstall/enable/disable events, however this will be unreliable when trying to listen for events about your own plugin. You will not receive a PluginDisableEvent or PluginUninstallEvent for the plugin itself. To trigger actions for these events, one (or more) of your modules (macro, event listener, etc.) should implement the Making your Plugin Modules State Aware interface instead.

ではどうするか?

ということで、このページ
Making your Plugin Modules State Aware - Atlassian Developers
によると、

The Component Module type for OSGi (version 2) plugins doesn't support the StateAware interface. To achieve the same effect, you can use the two Spring lifecycle interfaces: InitializingBean and DisposableBean. The afterPropertiesSet() and destroy() methods on these interfaces will be called when the module is enabled or disabled, exactly like StateAware.
Making this change to a component in an existing plugin will be backwards compatible. That is, a component module in a legacy plugin which implements InitializingBean will have its init() method called when it is enabled, exactly the same as such a component in an OSGi plugin.

InitializingBean と DisposableBean をimplementして、afterPropertiesSet() と destroy()を使いなさいっていう事ですね。
ちなみに、DisposableBeanは必須です。なぜなら、アンインストール時にはJVM内のEventListenerインスタンスを自動で消してくれないので、これを実装しないとインストールごとにEventListenerが追加され、1回のトリガーにつき複数回処理が起こる可能性があります。

ということで、まとめてみます。

イベントリスナー(or method) 捕捉可能 備考
PluginInstallEvent ただし、他のプラグインインストールイベントにも反応してしまうので、event.getPluginKey()でチェック
PluginUninstallEvent × -
PluginEnableEvent ただし、他のプラグインEnableEventにも反応
PluginDisableEvent × -
destroy() DisposableBeanを実装。自分自身の時にしか反応しない。プラグインを「無効化」「アンインストール」時に実行。
afterPropertiesSet() InitializingBeanを実装。自分自身の時にしか反応しない。プラグインを「有効化」「インストール」時に実行。


これ、困ったなーっていうのが、destroy()は無効化でも走っちゃうこと。
アンインストール時にBandanaManagerとかで保存した情報を削除したかったのだけど、これだと無効化時にも削除されちゃう。。。。まぁ、仕方ないか。

あと、プラグインのアップデートは削除→インストールってやるからBandanaManager情報を削除しちゃうとアップデート時にも削除されちゃうからダメだろうな。。。。