hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
185 stars 73 forks source link

Allow TransportBuilder.getTransportProvider() to specify custom class loader #210

Closed cthbleachbit closed 1 year ago

cthbleachbit commented 1 year ago

I am integrating this library into a Minecraft Bukkit plugin and it seems like TransportBuilder is unable to find any transport providers despite the fact that two of them are listed in the service definitions inside the jar.

After some investigation, it looks like plugin class loader instance is needed for ServiceLoader to correctly find provider classes...

This works:

@Override
public void onEnable() {
    ServiceLoader<ITransportProvider> spiLoader = ServiceLoader.load(ITransportProvider.class, this.getClass().getClassLoader());
}

This doesn't:

@Override
public void onEnable() {
    ServiceLoader<ITransportProvider> spiLoader = ServiceLoader.load(ITransportProvider.class);
}

Currently field TransportBuilder#PROVIDERS is to be initialized with static life time. This leaves no way to properly initialize this field from a bukkit plugin even with reflection as attempting to reflect-assign this field causes lazy initialization and thus invoke getTransportProvider() (which will call ServiceLoader without a correct class loader).

hypfvieh commented 1 year ago

I changed the TransportBuilder like you suggested and also adjusted AbstractTransport (so loading of IMessageReader/Writer will also work in your case).

cthbleachbit commented 1 year ago

This seems to work now. Many thanks.