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

Import / Export component from one plugin to another

Nahn Yanootz
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.
March 26, 2013

I want to have 2 different JIRA plugins communicate with each other using Java code.

I have read about the "component import plugin module", and it seems to be a good solution:

https://developer.atlassian.com/display/JIRADEV/Component+Import+Plugin+Module

The documentation seems to be incomplete, though. It doesn't say how to access the imported component's methods from a Java class.

Let's say, using the example mentioned in the documentation, that we have the following 2 plugins: "Hello World Provider", and "Hello World".

Here's the atlassian-plugin.xml for "Hello World Provider":

<atlassian-plugin name="Hello World Provider" key="example.plugin.helloworld.provider" plugins-version="2">
    <plugin-info>
        <description>A basic component module test</description>
        <vendor name="Atlassian Software Systems" url="http://www.atlassian.com"/>
        <version>1.0</version>
    </plugin-info>
 
    <component key="helloWorldService" class="com.myapp.internal.MyHelloWorldService" public="true">
        <interface>com.myapp.HelloWorldService</interface>
    </component>
</atlassian-plugin>

Here are what I would think the Java classes would look like:

HelloWorldService.java:

package com.myapp;

public interface HelloWorldService
{
    String sayHello();
}

MyHelloWorldService.java:

package com.myapp.internal;

public class MyHelloWorldService implements HelloWorldService
{
    public String sayHello()
    {
        return "Hello from another plugin";
    }
}

Here's the atlassian-plugin.xml for "Hello World" (the consumer):

<atlassian-plugin name="Hello World" key="example.plugin.helloworld" plugins-version="2">
    <plugin-info>
        <description>A basic component import module test</description>
        <vendor name="Atlassian Software Systems" url="http://www.atlassian.com"/>
        <version>1.0</version>
    </plugin-info>
 
    <component-import key="helloWorldService">
        <interface>com.myapp.HelloWorldService</interface>
    </component-import>
</atlassian-plugin>

I would think that, in order for the "Hello World" plugin to have access to the "Hello World Provider" plugin's exported component, it would have to be added as a dependency in the pom.xml as well (not mentioned in the documentation).

Now that I have the component from "Hello World Provider" imported into "Hello World", how do I access it from my Java classes?

Thanks

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
SuhailM March 26, 2013

1.For me its works with "ComponentManager"

2.

Add HelloWorldService as paramenter in the constructor of your class along with other parameters...

For examle you class name is MyWorldClass.java

//declare

private HelloWorldService helloWorldService;

//Constructor

pulbic MyWorldClass(...,HelloWorldService helloworld,..){

helloWorldService= helloworld;

}

Nahn Yanootz
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.
March 26, 2013

Yes, it works using dependency injection in the constructor.

It seems that this is handled by "PicoContainer":

https://developer.atlassian.com/display/JIRADEV/PicoContainer+and+JIRA

My problem was that I was trying to access the imported component from an action class, and I didn't make a constructor for it.

I still like the first solution, though, because it's more flexible.

Anyway, this information should have been in the "component import plugin module" documentation.

I'm thankful for the very cool atlassian answers contributors (like yourself).

1 vote
SuhailM March 26, 2013

Use this code to access your object-

ComponentManager.getOSGiComponentInstanceOfType(HelloWorldService.class)

OR you can also inject HelloWorldService in the constructor.

Nahn Yanootz
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.
March 26, 2013

I tried your first solution, and with a little modification ("ComponentAccessor" instead of "ComponentManager"), it works. Awesome!!!

How would I do it using your 2nd solution (injecting HelloWorldService in the constructor)?

I've seen some examples where this was done in a simple java class, but accessing that class from somewhere else and making an instance of it was impossible without providing the constructor's argument.

Thanks a lot.

0 votes
Adrien Ragot 2
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.
March 29, 2013

Hi,

There might be a syntax mistake in your component-import. As far as I've seen, it should be:

<component-importkey="helloWorldService" interface="com.myapp.HelloWorldService"/>

'interface' isn't a child but an attribute.

Cheers

Nahn Yanootz
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 7, 2013

Thanks for your comment, but I checked and it works both ways, as an attribute and as a child.

In the documentation mentioned above, the author has the interface as a child element.

TAGS
AUG Leaders

Atlassian Community Events