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
Community moderators have prevented the ability to post new answers.
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;
}
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).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Use this code to access your object-
ComponentManager.getOSGiComponentInstanceOfType(HelloWorldService.class)
OR you can also inject HelloWorldService in the constructor.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
There might be a syntax mistake in your component-import. As far as I've seen, it should be:
<component-
import
key=
"helloWorldService" interface="
com.myapp.HelloWorldService"/>
'interface' isn't a child but an attribute.
Cheers
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.