How to handle optional plugin dependency

What I want to do:
I have a common interface which is implemented by plugin A. Plugin B asks for a component implementing the common interfaced. If such a component is found, then plugin B calls some methods on it. If no such component is found (i.e. plugin A is not installed), then those calls are omitted.

That's what I have so far:
The interface is not common but belongs to plugin A. Plugin B gets access to the classes of plugin A and can therefore reference the interface. But only if plugin A is installed!

The problem:
If plugin A is not installed, then instead of not finding a suitable component I get a NoClassDefFoundError when referencing the interface. Adding the interface to plugin B too doesn't work either, since then it isn't the same interface anymore (one is loaded by classloader A and the other by classloader B).

How do others handle this problem?

4 comments

(I know that I can extract all references to the interface into a separate class and encapsulate everything with try/catch blocks. But that's not really the nice solution I'm heading for. Then I could as well use reflection to call any methods on the other plugin.)

0

Until 6.0.2, plugin dependencies didn't work at all because they were not supported by the plugin manager and unresolved optional dependencies were totally broken as well, so I guess there aren't many people around who have any experience with that.

With my rather bad experience so far I'd avoid any kind of dependency like the plague. Right now, I've got a "soft" dependency from one plugin to another which I handle via Application.getComponent(name) and some reflection. This works well as long as I don't need to supply instances or implementations of "foreign" classes. In that case, I'd probably indeed wrap the optional into try/catch blocks because I don't see any better way actually.

Sascha

0

Actually even providing instances (implementations) of foreign classes is possible in a quite elegant (at least I think it is) way using CGLIB. Check out the class XPathSupportProxy from the IntelliLang plugin.

Sascha

0

I'll take a look at it, thanks.

0

Please sign in to leave a comment.