Detecting the Presence or Absence of Classes in Different OSGI Bundles
Occasionally you will need to write a plugin that behaves differently depending on the presence or absence of another plugin. An example where we at Atlassian confront this problem is with the AppLinks plugin: if the AppLinks plugin is available, we want to use it to auto-detect known servers and connect to them automatically, but we still want our plugin to behave normally if AppLinks is not present.
There are a few ways to detect whether a specific plugin is present in your host application or not. On this page, we will show you how to add a listener to track when the availability of that plugin changes.
To start with, there is a bit of housekeeping. Add a dependency on spring-extender in your
Add a DynamicImport-Package element to the plugin-info element in
Wire up the class you will use, in
Now to the meat of it all. The class should implement BundleContextAware and DisposableBean as follows:
Just query the method isExamplePresent to find out if the example dependency is available, and execute different code based on the result.