Jira Server/Data Center app: How to add an OOB condition to the web-item module in spring scanner

In this article I will show you how you can add an out of the box Jira condition to a web-item in a Jira Server/Data Center app which uses spring scanner.

You will not have this problem if you do not use spring scanner, but you will if you use. And I will show you two ways how to solve this problem.

Problem

I created a Jira Server/Data Center app.

Then I added a new web-item in the atlassian-plugin.xml

<web-item name="my-web-item" i18n-name-key="my--web--item.name" key="my--web--item" section="operations-operations" weight="1000">
<description key="my--web--item.description">The my-web-item Plugin</description>
<label key="my--web--item.label"></label>
<condition class="com.atlassian.jira.plugin.webfragment.conditions.IsAdminCondition"/>
<link linkId="my--web--item-link">google.com</link>
</web-item>

This code means that I want to have my-web-item in the view issue screen in the More menu:

Screenshot 2020-09-11 at 19.06.23.png

And also I added a condition to show it only for admins. Here is this line in the atlassian-plugin.xml file:

<condition class="com.atlassian.jira.plugin.webfragment.conditions.IsAdminCondition"/>

You can find the complete code for this part here.

Let's run our app.

And we will see that our app is not installed with the following error:

Caused by: java.lang.ClassNotFoundException: com.atlassian.jira.plugin.webfragment.conditions.IsAdminCondition not found by ru.matveev.alexey.atlassian.jira.condition.tutorial.jira-springscanner-webitem-condition-tutorial [231]

Ok. Let's fix it.

Fix ClassNotFoundException

To fix this error we need to add the com.atlassian.jira.plugin.webfragment.conditions package to the import-package in the pom.xml file.

Now our import-package looks like this:

<Import-Package>
com.atlassian.jira.plugin.webfragment.conditions,
org.springframework.osgi.*;resolution:="optional",
org.eclipse.gemini.blueprint.*;resolution:="optional",
*
</Import-Package>

Now let's run our app again.

And we will see another error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.atlassian.jira.security.JiraAuthenticationContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

It means that the condition class could not inject required beans.

We can fix this error with two methods: add a bean which will imports required beans and use osgi:reference to import required beans.

Method 1: Add a bean which imports required beans

The required beans for our condition are JiraAuthenticationContext and GlobalPermissionService.

That is why I will create src/main/java/ru/matveev/alexey/atlassian/jira/condition/tutorial/impl/BeanInjecter.java:

@Named
public class BeanInjecter {

@Inject
public BeanInjecter(@ComponentImport JiraAuthenticationContext jiraAuthenticationContext,
@ComponentImport GlobalPermissionManager globalPermissionManager) {
}

}

This bean just injects required beans and it will make those beans visible for our condition as well.

You can find the complete code for this part here.

Now run the app and everything will work.

Method 2: osgi:reference

Another method to use osgi:reference in src/main/resources/META-INF/spring/plugin-context.xml.

I deleted the BeanInjecter.java file which I created earlier.

I added com.atlassian.jira.security to import-package in pom.xml.

Then I changed src/main/resources/META-INF/spring/plugin-context.xml to the following code:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:atlassian-scanner="http://www.atlassian.com/schema/atlassian-scanner/2"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.atlassian.com/schema/atlassian-scanner/2/
http://www.atlassian.com/schema/atlassian-scanner/2/atlassian-scanner.xsd
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<atlassian-scanner:scan-indexes/>
<osgi:reference id="globalPermissionManager"
interface="com.atlassian.jira.security.GlobalPermissionManager"/>

<osgi:reference id="jiraAuthenticationContext"
interface="com.atlassian.jira.security.JiraAuthenticationContext"/>

</beans>

What I changed there?

I added xmlns:osgi="http://www.springframework.org/schema/osgi" and http://www.springframework.org/schema/osgi/spring-osgi.xsd to be able to work with osgi:reference tag. And then I added two osgi:reference tags to import required beans:

 <osgi:reference id="globalPermissionManager"
interface="com.atlassian.jira.security.GlobalPermissionManager"/>

<osgi:reference id="jiraAuthenticationContext"
interface="com.atlassian.jira.security.JiraAuthenticationContext"/>

That is all.

You can find the complete code for this part here.

Let's run the app and everything works as expected!

1 comment

Comment

Log in or Sign up to comment
M Amine
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 4, 2021

Great article

TAGS
AUG Leaders

Atlassian Community Events