Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cthbleachbit opened this issue May 9, 2023 · 2 comments

Comments

@cthbleachbit
Copy link
Contributor

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
Copy link
Owner

hypfvieh commented May 9, 2023

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

@cthbleachbit
Copy link
Contributor Author

This seems to work now. Many thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants