Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

how to catch Plugin Enabled event in JIRA?

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.
October 27, 2011

I need to perform few auto configuration steps after plugin is enabled. Which method in Jira will help me to allow me to do these configuration methods after Plugin is fully loaded by JIRA and available to use.

Right now I've tried to use onStart() method made available by LifecycleAware interface. But it seems to be getting called before plugin is enabled as I get nullpointer exception when tried to access modules declared in this Plugin.

AfterPropertiesSet() available from interface InitializingBean also doesnt provide much help.

Any other suggestions on how one can perform the operations within Plugin after it is successfully loaded?

thanks,

Raju

4 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Diego Ferreira
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.
April 28, 2017

I know this is old, but if anyone stumbles with this, this is how I register a plugin enabled listener on the plugin itself and handle the configuration on onPluginEnabled method.

Hope this helps anybody.

@ExportAsService({PluginEnabledHandler.class})
@Named("pluginEnabledHandler")
@Scanned
public class PluginEnabledHandlerImpl implements InitializingBean, DisposableBean, PluginEnabledHandler {

    @ComponentImport
    private final EventPublisher eventPublisher;

    @Inject
    public PluginEnabledHandlerImpl(EventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public void afterPropertiesSet() throws Exception {
        eventPublisher.register(this);
    }

    public void destroy() throws Exception {
        eventPublisher.unregister(this);
    }

    @EventListener
    public void onPluginEnabled(PluginEnabledEvent event) throws Exception {
        Plugin plugin = event.getPlugin();
        if ("myplugin.key".equals(plugin.getKey())) {
           // configure on enable
        }
    }
}
0 votes
AlexH
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 11, 2011

I've recently written a CustomField plugin that requires loading some configuration in order to properly display its value.

We solved our needs by writing a Singleton that fetches a Config.class that is used during the calculation/render/display of the customfield (i.e. when an issue is loaded). If it's the first time the Singleton is requested it performs the configuration tasks at that time. Any time after that it simply returns the pre-loaded/configured Config.class.

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 15, 2011

Wojciech, Alex: Thanks for your comments.

Using Jira's PropertySet API, I'm already making sure configuration is done only once. Customfield that we are creating needs to be available once Plugin is loaded and enabled.

So far I don't have no luck in capturing the event that will help to make the modules declared within Plugin to be available to do action like creating custom field using Custom Field Declaration available within module inside this Plugin.

Currently I've got the workaround by creating this custom field when user adds the license (by this time plugin is ready and all modules are available to call from anywhere!).

Will lookin to you your suggestions as I get time to explore this problem again. Thanks again for your input.

cheers,

Raju

AlexH
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 15, 2011

Actually I think I misunderstood your question. When you said "Plugin Enabled" I thought you meant "plugin loaded" as in, each time JIRA is restarted the plugin has to be reloaded.

Instead it sounds like you're talking about a configuration step that occurs only the first time a plugin is installed.

My answer was geared towards the former problem.

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

Updates:

a) Haven't tried this option. But Ive found tutorial [Scheduling Events via SAL], that may help to write a Job as you suggested.

b)This option didn't worked for me. Since I'm trying to create a custom field, I've injected Custom Field Type into Component responsible for Initialization. But got following error: Unsatisfied dependency of type [class CustomFieldType]: expected at least 1 matching bean

Not sure if I can CustomfieldType is treated as a component module or not and hence might be the error!

There is a third option which currently I'm exploring upon:

In this plugin initalization happens only when JIRA Administrator triggers an event, for example clicking on button "Initalize this plugin".

This doesn't sound fancy like the auto initialization during JIRA startup, but here we have 100% assurance that plugin is fully enabled and available in JIRA.

Thanks again for your input.

cheers,

Raju

Wojciech Seliga
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 13, 2011

Ad b) I guess you misunderstood me and injected it other way around. IIRC you cannot inject custom field type classes into something else, but you can inject components almost everwhere.

To clarify one more time this option:

a) define in atlassian-plugin.xml a component (e.g. com.example.MyInitializingComponent) and create a java class for it. In the constructor of this class inject all dependencies you want to be instantiated before you can perform your initialization. Often this list may include zero of your dependencies :). You should not inject here your CustomFieldType classes.

b) then in the constructor perform the initialization.

c) inject your MyInitalizingComponent to other classes which need the stuff to be initialized before they are used (probably to your CustomFieldType class too).

If you cannot reliably inject your MyInitalizingComponent in the constructor (for some classes it's not supported as they are instantiated by JIRA using Class.newInstance(), then you may also try out the singleton pattern proposed by Alex - where the inialization happens upon first retrieval of singleton instance (lazy initialization).

Cheers,

Wojtek

0 votes
Wojciech Seliga
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.
October 27, 2011

That's a very good question.

Unfortunately JIRA does not provide very well-defined and well-behaving mechanism to perform such actions. There are several options (you mentioned two), but all have some drawback.

I would recommend the following:

a) in your LifecycleAware component in onStart() register a one-time job using SAL PluginScheduler and when this job is executed (should be executed only after the application is fully started) perform there all needed steps. That would be asynchronous of course.

b) (that's what I do usually) create a component responsible for initialization (this auto-configuration in your case) and inject there all components which you would like to be instatiated before you can peform your auto-configuration. Then perform your stuff in the constructor of this special component. That worked reliably for me.

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.
October 27, 2011

Regarding

a) Good idea. Need to try on this.

b) Will it be possible to share a example on this? Not sure if you can attach a files to this thread. Or you can email me at raju.kadam -> gmail.com

thanks,

Raju

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