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

Access components from other plugins without using component-import?

Candid Dauth _K15t_ January 22, 2014

We are writing a Java library that is meant to be used by Confluence and Jira plugins by referencing it in the pom.xml.

Among other things, this library provides a Servlet class that can be used by those plugins by adding a <servlet> tag to their atlassian-plugin.xml. This servlet class requires several components that are not automatically available in Confluence/Jira because they are provided by another plugin, such as the UserManager and LoginUriProvider classes.

Because we need to access those classes in our Servlet class, at the moment we need our users to add them to their atlassian-plugin.xml using <component-import> so that they can be autowired with our Servlet class. This is unfortunate, as it is a source of error, unnecessary work, and will be particularly confusing when the dependencies of our Servlet change.

Is there any way to access the components of other plugins without needing to use <component-import>?

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Candid Dauth _K15t_ January 22, 2014

I believe I have found the answer. My servlet now implements the interface org.springframework.osgi.context.BundleContextAware (part of Maven package org.springframework.osgi:spring-osgi-core). This is the code that I have added to access UserManager:

private ServiceTracker userManagerTracker;

@Override
public void setBundleContext(BundleContext bundleContext) {
    userManagerTracker = new ServiceTracker(bundleContext, UserManager.class.getName(), null);
    userManagerTracker.open();
}

@Override
public void destroy() {
    super.destroy();

    userManagerTracker.close();
}

protected UserManager getUserManager() {
    Object proxy = userManagerTracker.getService();
    if ((proxy != null) &amp;&amp; (proxy instanceof UserManager)) {
        return (UserManager) userManagerTracker.getService();
    } else {
        throw new RuntimeException("Could not get a valid UserManager proxy.");
    }
}

0 votes
Michael Ammann March 13, 2014

There is another solution to access a osgi component, declared in a different plugin / core:

get the "osgiContainerManager" - injected or from Spring ContainerManager.getComponent("osgiContainerManager")

you can then get the ServiceTracker from there:

osgiContainerManager.getServiceTracker("yourClassName")

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.
January 22, 2014
  • Your library should be a plugin that you embed in both other plugins. The other plugins will be a zip of both plugins, with a few more manifests and the extensions OBR. See Bundling in an OBR for details. This is what I've done for Play SQL Spreadsheets, which embeds Play SQL Base.
  • Since that plugin will be a cross-product plugin, you will have to declare the following on your servlet module: application="confluence". See "Application-Specific Modules" on the Modules page.
Candid Dauth _K15t_ January 22, 2014

Thank you for your response. We have done something similar in the past, but have for now decided against bundled plugins for various reasons. Also, I think it wouldn’t help in this particular case, as the servlet module would still be part of the plugin using our library, so the <component-import>s would still need to be configured for that plugin.

TAGS
AUG Leaders

Atlassian Community Events