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>?
Community moderators have prevented the ability to post new answers.
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) && (proxy instanceof UserManager)) { return (UserManager) userManagerTracker.getService(); } else { throw new RuntimeException("Could not get a valid UserManager proxy."); } }
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")
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.
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.
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.