lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Kohlschütter (JIRA) <>
Subject [jira] [Commented] (LUCENE-4713) SPI: Allow fallback to default ClassLoader if Thread#getContextClassLoader fails
Date Tue, 12 Mar 2013 12:11:15 GMT


Christian Kohlschütter commented on LUCENE-4713:

Overlap and coverage, Uwe :)

One thing is still unclear to me. Given loader is null, in SPIClassIterator line 143 we call
Class.forName with a null ClassLoader.

However (at least in the Oracle 1.7 JDK) Class#forName(String,boolean,ClassLoader) does not
use ClassLoader#getSystemClassLoader but ClassLoader#getCallerClassLoader instead (which IMHO
contradicts the JavaDocs description, where they claim to use the bootstrap classloader...)

Given that it is very unlikely that we're running into any problems with bootstrap resources,
I would actually just check for loader==null in SPIClassIterator and assign loader=ClassLoader.getSystemClassLoader()
in this case. This will use the System ClassLoader by default and only falls back to getCallerClassLoader
if there is no System ClassLoader.
> SPI: Allow fallback to default ClassLoader if Thread#getContextClassLoader fails
> --------------------------------------------------------------------------------
>                 Key: LUCENE-4713
>                 URL:
>             Project: Lucene - Core
>          Issue Type: Improvement
>    Affects Versions: 4.0, 4.1, 4.2
>            Reporter: Christian Kohlschütter
>            Assignee: Uwe Schindler
>            Priority: Minor
>              Labels: ClassLoader, Thread
>             Fix For: 4.3
>         Attachments: LUCENE-4713.patch, LUCENE-4713.patch, LUCENE-4713.patch, LUCENE-4713.patch,
LUCENE-4713.patch, LUCENE-4713.patch, LuceneContextClassLoader.patch
> NOTE: This issue has been renamed from:
> "Replace calls to Thread#getContextClassLoader with the ClassLoader of the current class"
> because the revised patch provides a clean fallback path.
> I am not sure whether it is a design decision or if we can indeed consider this a bug:
> In core and analysis-common some classes provide on-demand class loading using SPI. In
NamedSPILoader, SPIClassIterator, ClasspathResourceLoader and AnalysisSPILoader there are
constructors that use the Thread's context ClassLoader by default whenever no particular other
ClassLoader was specified.
> Unfortunately this does not work as expected when the Thread's ClassLoader can't see
the required classes that are instantiated downstream with the help of Class.forName (e.g.,
Codecs, Analyzers, etc.).
> That's what happened to us here. We currently experiment with running Lucene 2.9 and
4.x in one JVM, both being separated by custom ClassLoaders, each seeing only the corresponding
Lucene version and the upstream classpath.
> While NamedSPILoader and company get successfully loaded by our custom ClassLoader, their
instantiation fails because our Thread's Context-ClassLoader cannot find the additionally
required classes.
> We could probably work-around this by using Thread#setContextClassLoader at construction
time (and quickly reverting back afterwards), but I have the impression this might just hide
the actual problem and cause further trouble when lazy-loading classes later on, and potentially
from another Thread.
> Removing the call to Thread#getContextClassLoader would also align with the behavior
of AttributeSource.DEFAULT_ATTRIBUTE_FACTORY, which in fact uses Attribute#getClass().getClassLoader()
> A simple patch is attached. All tests pass.

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message