LUCENE-6921: Prevent NPE if stupid context classloader is null

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1718113 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2015-12-05 18:31:50 +00:00
parent 889bf7f953
commit 847a8af93b
3 changed files with 15 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.ServiceConfigurationError;
@ -54,6 +55,9 @@ final class AnalysisSPILoader<S extends AbstractAnalysisFactory> {
this.suffixes = suffixes;
// if clazz' classloader is not a parent of the given one, we scan clazz's classloader, too:
final ClassLoader clazzClassloader = clazz.getClassLoader();
if (classloader == null) {
classloader = clazzClassloader;
}
if (clazzClassloader != null && !SPIClassIterator.isParentClassLoader(clazzClassloader, classloader)) {
reload(clazzClassloader);
}
@ -72,6 +76,7 @@ final class AnalysisSPILoader<S extends AbstractAnalysisFactory> {
* of new service providers on the given classpath/classloader!</em>
*/
public synchronized void reload(ClassLoader classloader) {
Objects.requireNonNull(classloader, "classloader");
final LinkedHashMap<String,Class<? extends S>> services =
new LinkedHashMap<>(this.services);
final SPIClassIterator<S> loader = SPIClassIterator.get(clazz, classloader);

View File

@ -20,6 +20,7 @@ package org.apache.lucene.util;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.ServiceConfigurationError;
@ -41,6 +42,9 @@ public final class NamedSPILoader<S extends NamedSPILoader.NamedSPI> implements
this.clazz = clazz;
// if clazz' classloader is not a parent of the given one, we scan clazz's classloader, too:
final ClassLoader clazzClassloader = clazz.getClassLoader();
if (classloader == null) {
classloader = clazzClassloader;
}
if (clazzClassloader != null && !SPIClassIterator.isParentClassLoader(clazzClassloader, classloader)) {
reload(clazzClassloader);
}
@ -59,6 +63,7 @@ public final class NamedSPILoader<S extends NamedSPILoader.NamedSPI> implements
* of new service providers on the given classpath/classloader!</em>
*/
public void reload(ClassLoader classloader) {
Objects.requireNonNull(classloader, "classloader");
final LinkedHashMap<String,S> services = new LinkedHashMap<>(this.services);
final SPIClassIterator<S> loader = SPIClassIterator.get(clazz, classloader);
while (loader.hasNext()) {

View File

@ -50,7 +50,11 @@ public final class SPIClassIterator<S> implements Iterator<Class<? extends S>> {
/** Creates a new SPI iterator to lookup services of type {@code clazz} using the context classloader. */
public static <S> SPIClassIterator<S> get(Class<S> clazz) {
return new SPIClassIterator<>(clazz, Thread.currentThread().getContextClassLoader());
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = clazz.getClassLoader();
}
return new SPIClassIterator<>(clazz, cl);
}
/** Creates a new SPI iterator to lookup services of type {@code clazz} using the given classloader. */