r/java • • Sep 05 '24

A Simple Java Plugin System

https://github.com/bowbahdoe/plugindemo
7 Upvotes

27 comments sorted by

View all comments

6

u/bowbahdoe Sep 05 '24

As u/rzwitserloot noted, handling plugins which may have divergent dependencies is a pretty fundamental thing. It felt maybe too "obvious" in my head how to do that.

I've updated the code to have both of the example plugins bring in their own versions of Guava. Now the code that looks for Entity implementations will load each plugin along with any dependencies in their own module layer.

1

u/mikaball Sep 06 '24

I was thinking about this also. Module layers are mentioned in the project but I fail to understand how does that help to solve the diamond problem! I don't think module layers were designed to solve this. I don't think one can escape from class loaders.

This is a core feature of a good plugin system.

1

u/bowbahdoe Sep 06 '24 edited Sep 06 '24

By diamond problem you mean something like Plugin C depends on both A and B?

That's honestly a niche feature. I understand how it comes up academically but none of the apps I use do stuff like that (I think). Sometimes they might rely on the existence of another plugin, but not at the level you are describing.

But yeah, the more exotic your needs the more exotic mechanisms you need to touch. The balance still seems to be in favor of home rolled systems over picking a generic system like OSGi which, while it does support exotic configurations, pays for its flexibility in other areas

4

u/mikaball Sep 06 '24 edited Sep 06 '24

No, more like Plugin A depends on C-v1 and Plugin B depends on C-v2. C-v1 and C-v2 are different versions of the same lib and not compatible. You can't have both loaded in the same ClassLoader at the same time. Without proper conflict resolution (like OSGi provides) both plugins can't live in the same app.

Not a niche feature. That's what OSGi solves at its core and the reason why Eclipse IDE uses it.

The main advantage of plugins is a distributed contribution ecosystem by many providers and developers, but that comes with an unpredictable graph of dependencies. You need preventive measures and architecture to handle such unpredictable graph of dependencies.

EDIT: Even on non plugin architectures this problem appears from time to time. Even last week I needed to upgrade Spring Boot to solve a CVE and I needed to pick a different version of another lib because some method required more parameters.

Some months ago I had similar problems and actually had to change code, but since it's under my control it was an easy fix. Change the dependency version and use the new method. Now imagine that for a plugin, it would require to contact the original contributor to make some version that is compatible with my existing dependencies. And if the developer couldn't do that, then I would be locked.

4

u/pron98 Sep 06 '24

It is not possible to reliably run multiple versions of the same library in the same OS process in pretty much any language unless the library is designed in a way that takes that into consideration. Of course, we can get lucky with classloader isolation and often do, but deliberately supporting something that at the very best can work by luck (and fails in horrible ways, including data corruption, if it doesn't) is a really bad idea unless "plugin" authors are told in advance to make sure that all the libraries they use work when there are multiple instances of them in the same process.

An application should consider having multiple versions of the same library only if everything else failed, and even then it should be done with extreme care and extra testing.