DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Resource.getResource is getting the classloader in a potentially undesired way for web applications #1448

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
com.google.common.io.Resource.getResource is using 

 Resources.class.getClassLoader()

to get the classloader, however the com.google.common.io.Resource class might 
be loaded by a different classloader than the calling code, e.g. in a web 
application scenario, which may result in not finding the desired resource.

This should either be documented as a warning, or replaced by default to the 
bit "safer": 

  Thread.currentThread().getContextClassLoader()

Or even adding another overload that supports web scenario (with a lack of a 
better idea something like getWebResource although I'm sure there are better 
names)

It's a small thing, but if someone is using it in a Web scenario, they might be 
surprised to get inconsistent results based on the container classloading 
architecture.

see also: 
http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an
-inputstream

Original issue reported on code.google.com by Ehrann.M...@gmail.com on 13 Jun 2013 at 8:43

GoogleCodeExporter commented 9 years ago
This sounds like issue 588, though I'm fairly ignorant of ClassLoader magic, so 
please let me know if that's incorrect.

Original comment by cpov...@google.com on 13 Jun 2013 at 8:45

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08