Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Object Initialization/Dependency Injection Inside a Jira Service

Edited

Hi everyone, I'm writing a custom Jira plugin of type Service.

I have

@Named
@ExportAsService
public class ENGSLAService extends AbstractService {

@JiraImport
private JqlQueryParser jqlQueryParser;

@JiraImport
private SearchService searchService;

private InsightReader insightReader;

@Inject
public ENGSLAService(JqlQueryParser jqlQueryParser, SearchService searchService) {

this.jqlQueryParser = jqlQueryParser;
this.searchService = searchService;
this.insightReader = ComponentAccessor.getOSGiComponentInstanceOfType(InsightReader.class);
log.warn("insightReader is " + insightReader);
}

...

}
@JiraComponent
public class InsightReader {

@JiraImport
private ObjectFacade objectFacade;
@JiraImport
private IQLFacade iqlFacade;

...

}

Basically I don't know how I can initialize a InsightReader's object inside my ENGSLAService constructor. I tryied different arrangements with annotations and non-empty constructor in InsightReader's class but nothing seems to work. Sometimes I get a PicoContainer Exception, sometimes (like for the above code) the object of type InsightReader is null after invoking ComponentAccessor.getOSGiComponentInstanceOfType. I probably messed up with Spring/OSGi annotations. Could you please suggest how to make it work?

Thank you

 

1 answer

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 15, 2021

Hi @Giorgia Fineschi did you try to annotate InsightReader wIth @Named annotation? 

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 19, 2021

@Giorgia Fineschi did you solve the problem? :)

Hi Martin,

I did it another way:

@Autowired
public ENGSLAService(JqlQueryParser jqlQueryParser, SearchService searchService) {

this.jqlQueryParser = jqlQueryParser;
this.searchService = searchService;
this.pluginSettings = null;

try {
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().loadClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().loadClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade");
ObjectFacade objectFacade = (ObjectFacade) ComponentAccessor.getOSGiComponentInstanceOfType (objectFacadeClass);
IQLFacade iQLFacade = (IQLFacade) ComponentAccessor.getOSGiComponentInstanceOfType (iqlFacadeClass);
this.insightReader = new InsightReader (objectFacade,iQLFacade);
} catch (ClassNotFoundException e) {
log.error("Cannot initialize InsightReader Object", e);
}

try {
Class pluginSettingsFactoryClass = ComponentAccessor.getPluginAccessor().getClassLoader().loadClass("com.atlassian.sal.api.pluginsettings.PluginSettingsFactory");
pluginSettingsFactory = (PluginSettingsFactory) ComponentAccessor.getOSGiComponentInstanceOfType (pluginSettingsFactoryClass);
pluginSettings = pluginSettingsFactory.createSettingsForKey(ENGSLARESTServiceConfig.class.getName());
}

catch (ClassNotFoundException e) {
log.error("Cannot initialize PluginSettingsFactory Object", e);
}

}


Of course after adding this


public InsightReader(ObjectFacade objectFacade, IQLFacade iqlFacade) {
this.objectFacade = objectFacade;
this.iqlFacade = iqlFacade;

}

To InsightReader.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 24, 2021

@Giorgia Fineschi ok, I hope it works for you. My best practice is to not to use ComponentAccessor if it is not necessary.

In my opinion, you should create InsightReader service as "service" or "component" with objectFacade and iqlFacade to be injected by OSGi.

It should be someting like:

import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class InsightReader{

private final ObjetFacade objectFacade;
private final IQLFacade iqlFacade;

@Autowired
public InsightReader(@ComponentImport ObjectFacade objectFacade, @ComponentImport IQLFacade iqlFacade){
this.objectFacade = objectFacade;
this.iqlFacade = iqlFacade;
}

}

I know it's bad. I don't like using ComponentAccessor either but I had to make my code work and that workaround seems ok.

I'm still working on this project so I can refactor. However it will take me some time since I have to complete another part before making these changes. I'll let you know in the next weeks.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 25, 2021

Sure, no problem :). Good luck with the project :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events