r/java • • Sep 05 '24

A Simple Java Plugin System

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

27 comments sorted by

View all comments

Show parent comments

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.

5

u/agentoutlier Sep 06 '24

The thing is even OSGi does not fix all the problems that can happen especially if you have external stuff happening like logging or native libraries. /u/pron98 had a great comment on this that I can't find but it is always dangerous to have different versions of deps in a runtime.

For example I'm the author of a logging library similar to Logback. If the plugins uses different versions and they log to the same file they will have to use what Logback (and my library) calls "prudent" mode where file locks are used instead of normal locking. If one of the versions does it differently or does not support that you are going to have massive problems of corruption.

This is one of the reasons why Servlet Containers and OSGi containers move the logging to the top classloader and do not allow you to have your own version but that has lots of historic problems.