On JIRA starting, DefaultIssueTypeManager.creationLock is null when PluginEnabledEvent is published

I’m developing a jira plugin using jira version 7.13.0. I want to create an issuetype on plugin enabled. Here is my code:

    @EventListener
    public void onPluginEnabled(PluginEnabledEvent event) throws GenericEntityException, IOException {
        if(PLUGIN_KEY.equals(event.getPlugin().getKey())){
            log.info("Enabling plugin");
            Optional<IssueType> issueType = issueTypeManager.getIssueTypes().stream()
                    .filter(i -> ISSUETYPE_NAME.equals(i.getName())).findAny();
            String issueTypeId;
            if (issueType.isPresent()) {
                log.info("IssueType exists");
                issueTypeId = issueType.get().getId();
            } else {
                log.info("Creating issueType");
                Avatar avatar = avatarManager.create(AvatarImpl.createCustomAvatar(
                        "my.png","image/png", PLUGIN_KEY, IconType.ISSUE_TYPE_ICON_TYPE),
                        getClass().getResourceAsStream("/images/my.png"), null);
                IssueType is = issueTypeManager.createIssueType(ISSUETYPE_NAME, ISSUETYPE_DESC, avatar.getId());
                issueTypeId = is.getId();
            }
            // do other stuff
            ...
        }
    }

However, when I atlas-run, a NullPointerException is thrown. Here is stacktrace:

[INFO] [talledLocalContainer] 2019-06-16 15:55:07,292 localhost-startStop-1 ERROR      [c.a.event.internal.AsynchronousAbleEventDispatcher] There was an exception thrown trying to dispatch event [com.atlassian.plugin.event.events.PluginEnabledEvent for com.sify.jiraPlugin] from the invoker [SingleParameterMethodListenerInvoker{method=public void com.sify.PluginInstalledListener.onPluginEnabled(com.atlassian.plugin.event.events.PluginEnabledEvent) throws org.ofbiz.core.entity.GenericEntityException,java.io.IOException, listener=com.sify.PluginInstalledListener@5da9bbe6}]
[INFO] [talledLocalContainer] java.lang.RuntimeException: Listener: com.sify.PluginInstalledListener event: com.atlassian.plugin.event.events.PluginEnabledEvent
[INFO] [talledLocalContainer] 	at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:50)
[INFO] [talledLocalContainer] 	at com.atlassian.event.internal.AsynchronousAbleEventDispatcher$1$1.run(AsynchronousAbleEventDispatcher.java:38)
[INFO] [talledLocalContainer] 	at com.google.common.util.concurrent.MoreExecutors$DirectExecutorService.execute(MoreExecutors.java:299)
[INFO] [talledLocalContainer] 	at com.atlassian.event.internal.AsynchronousAbleEventDispatcher.dispatch(AsynchronousAbleEventDispatcher.java:88)
[INFO] [talledLocalContainer] 	at com.atlassian.event.internal.EventPublisherImpl.invokeListeners(EventPublisherImpl.java:204)
[INFO] [talledLocalContainer] 	at com.atlassian.event.internal.EventPublisherImpl.publish(EventPublisherImpl.java:115)
[INFO] [talledLocalContainer] 	at com.atlassian.plugin.event.impl.DefaultPluginEventManager.broadcast(DefaultPluginEventManager.java:83)
[INFO] [talledLocalContainer] 	at com.atlassian.plugin.manager.DefaultPluginManager.broadcastIgnoreError(DefaultPluginManager.java:2129)
[INFO] [talledLocalContainer] 	at com.atlassian.plugin.manager.DefaultPluginManager.enableDependentPlugins(DefaultPluginManager.java:1264)
[INFO] [talledLocalContainer] 	at com.atlassian.plugin.manager.DefaultPluginManager.addPlugins(DefaultPluginManager.java:1218)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.plugin.JiraPluginManager.addPlugins(JiraPluginManager.java:165)
[INFO] [talledLocalContainer] 	at com.atlassian.plugin.manager.DefaultPluginManager.earlyStartup(DefaultPluginManager.java:597)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.plugin.JiraPluginManager.earlyStartup(JiraPluginManager.java:125)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.component.pico.ComponentManager$PluginSystem.earlyStartup(ComponentManager.java:781)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.component.pico.ComponentManager.quickStart(ComponentManager.java:213)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.component.pico.ComponentManager.start(ComponentManager.java:182)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.upgrade.PluginSystemLauncher.start(PluginSystemLauncher.java:45)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.startup.DefaultJiraLauncher.lambda$postDbLaunch$2(DefaultJiraLauncher.java:143)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.config.database.DatabaseConfigurationManagerImpl.doNowOrEnqueue(DatabaseConfigurationManagerImpl.java:304)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.config.database.DatabaseConfigurationManagerImpl.doNowOrWhenDatabaseActivated(DatabaseConfigurationManagerImpl.java:199)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.startup.DefaultJiraLauncher.postDbLaunch(DefaultJiraLauncher.java:135)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.startup.DefaultJiraLauncher.lambda$start$0(DefaultJiraLauncher.java:102)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.util.devspeed.JiraDevSpeedTimer.run(JiraDevSpeedTimer.java:31)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.startup.DefaultJiraLauncher.start(DefaultJiraLauncher.java:100)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.startup.LauncherContextListener.initSlowStuff(LauncherContextListener.java:154)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.startup.LauncherContextListener.initSlowStuffInBackground(LauncherContextListener.java:139)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.startup.LauncherContextListener.contextInitialized(LauncherContextListener.java:101)
[INFO] [talledLocalContainer] 	... 8 filtered
[INFO] [talledLocalContainer] 	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[INFO] [talledLocalContainer] 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[INFO] [talledLocalContainer] 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[INFO] [talledLocalContainer] 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[INFO] [talledLocalContainer] 	at java.lang.Thread.run(Thread.java:748)
[INFO] [talledLocalContainer] Caused by: java.lang.NullPointerException
[INFO] [talledLocalContainer] 	at com.atlassian.jira.config.DefaultIssueTypeManager.createIssueTypeAndAddToDefaultScheme(DefaultIssueTypeManager.java:136)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.config.DefaultIssueTypeManager.createIssueTypeAndAddToDefaultScheme(DefaultIssueTypeManager.java:129)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.config.DefaultIssueTypeManager.createIssueType(DefaultIssueTypeManager.java:103)
[INFO] [talledLocalContainer] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[INFO] [talledLocalContainer] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[INFO] [talledLocalContainer] 	at java.lang.reflect.Method.invoke(Method.java:498)
[INFO] [talledLocalContainer] 	at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
[INFO] [talledLocalContainer] 	at com.sun.proxy.$Proxy289.createIssueType(Unknown Source)
[INFO] [talledLocalContainer] 	... 3 filtered
[INFO] [talledLocalContainer] 	at java.lang.reflect.Method.invoke(Method.java:498)
[INFO] [talledLocalContainer] 	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
[INFO] [talledLocalContainer] 	at org.eclipse.gemini.blueprint.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:56)
[INFO] [talledLocalContainer] 	at org.eclipse.gemini.blueprint.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.java:60)
[INFO] [talledLocalContainer] 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
[INFO] [talledLocalContainer] 	at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
[INFO] [talledLocalContainer] 	at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
[INFO] [talledLocalContainer] 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
[INFO] [talledLocalContainer] 	at org.eclipse.gemini.blueprint.service.util.internal.aop.ServiceTCCLInterceptor.invokeUnprivileged(ServiceTCCLInterceptor.java:70)
[INFO] [talledLocalContainer] 	at org.eclipse.gemini.blueprint.service.util.internal.aop.ServiceTCCLInterceptor.invoke(ServiceTCCLInterceptor.java:53)
[INFO] [talledLocalContainer] 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
[INFO] [talledLocalContainer] 	at org.eclipse.gemini.blueprint.service.importer.support.LocalBundleContextAdvice.invoke(LocalBundleContextAdvice.java:57)
[INFO] [talledLocalContainer] 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
[INFO] [talledLocalContainer] 	at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
[INFO] [talledLocalContainer] 	at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
[INFO] [talledLocalContainer] 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
[INFO] [talledLocalContainer] 	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
[INFO] [talledLocalContainer] 	at com.sun.proxy.$Proxy870.createIssueType(Unknown Source)
[INFO] [talledLocalContainer] 	at com.sify.PluginInstalledListener.onPluginEnabled(PluginInstalledListener.java:99)
[INFO] [talledLocalContainer] 	... 2 filtered
[INFO] [talledLocalContainer] 	at java.lang.reflect.Method.invoke(Method.java:498)
[INFO] [talledLocalContainer] 	at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:40)
[INFO] [talledLocalContainer] 	... 39 more
[INFO] [talledLocalContainer] 2019-06-16 15:55:07,450 localhost-startStop-1 INFO      [c.a.plugin.manager.DefaultPluginManager] Plugin system earlyStartup ended

I debugged and found that createIssueTypeAndAddToDefaultScheme will first call this.creationLock.lock(). But this.creationLock is not initialized at that time, which causes NullPointerException. What can I do to get around this problem?

Answering my own question for others who come across same problem with me. I moved the logic from PluginEnabledEvent to LifecyleAware’s start() method, at which time all Startable components should have been initialized.

1 Like