Skip to content
New issue

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

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

Already on GitHub? # to your account

Question about ClassLoader origin #215

Open
Jan-Thes opened this issue Sep 7, 2023 · 0 comments
Open

Question about ClassLoader origin #215

Jan-Thes opened this issue Sep 7, 2023 · 0 comments

Comments

@Jan-Thes
Copy link

Jan-Thes commented Sep 7, 2023

Hi,

is there a specific reason why ServiceLoaderUtil is using the ClassLoader from the current Thread instead of from its own class object?
See:

return Thread.currentThread().getContextClassLoader();

I guess since its possible to set the Thread classLoader, its possible to workaround, however i spent the last two weeks analyzing a situation where:

  • we have a WAR which happens to have CXF on its classpath fror whatever reason
  • the WAR has a "dependency" on different project of ours, however we only depend on an API (some interfaces) for that project, not the implementation
  • the implemenation of that dependency (and its classpath) is actually supplied at runtime by a 3rd party framework that loads the dependencies implementation through its own 3rd party classloader
  • (the 3rd party ClassLoader is "child first" so it picks up libraries from its own ClassPath first apparently)
  • the dependency that is loaded through this ClassLoader happens to require a specifc CXF transport extension library (which is on the 3rd party's classpath just like the main CXF lib, however not on the WAR classpath)
  • the WAR code happens to run the classes loaded from the different ClassLoader without creating a separate thread for them
  • those classes initiate a SOAP client
  • ServiceLoaderUtil then tries to find the CXF implementation on the wrong ClassLoader, because it is getting the ClassLoader from the WAR thread, instead of using the ClassLoader of its own class which should be the Third-Party-ClassLoader at that point
  • ServiceLoaderUtil then finds the unrelated CXF dependency from the WAR which does not come with the correct transport extenions, instead of finding the CXF with correct transports from the Third-Party Classpath
  • CXF fails to find the correct extension and throws an Exception

Maybe ClassLoaders are not meant to be used this way, which is entirely possible, because this 3rd party framework is quite old and certainly not following best practices in all regards.

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

No branches or pull requests

1 participant