Skip to content

Apache Lucene open-source search software

LUCENE-4713: The SPI components used to load custom codecs or analysis components were fixed to also scan the Lucene ClassLoader in addition to the context ClassLoader, so Lucene is always able to find its own codecs. The special case of a null context ClassLoader is now also supported.

Uwe Schindlerbd1a1e5ffc70
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 202d5c982da..d23e4b4ddb9 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -74,6 +74,12 @@ Bug Fixes
* LUCENE-4826: PostingsHighlighter was not returning the top N best
scoring passages. (Robert Muir, Mike McCandless)
+* LUCENE-4713: The SPI components used to load custom codecs or analysis
+ components were fixed to also scan the Lucene ClassLoader in addition
+ to the context ClassLoader, so Lucene is always able to find its own
+ codecs. The special case of a null context ClassLoader is now also
+ supported. (Christian Kohlschütter, Uwe Schindler)
+
======================= Lucene 4.2.0 =======================
Changes in backwards compatibility policy
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AnalysisSPILoader.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AnalysisSPILoader.java
index f65f242be9c..28074843cee 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AnalysisSPILoader.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/util/AnalysisSPILoader.java
@@ -52,6 +52,11 @@ final class AnalysisSPILoader<S extends AbstractAnalysisFactory> {
public AnalysisSPILoader(Class<S> clazz, String[] suffixes, ClassLoader classloader) {
this.clazz = clazz;
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 (clazzClassloader != null && !SPIClassIterator.isParentClassLoader(clazzClassloader, classloader)) {
+ reload(clazzClassloader);
+ }
reload(classloader);
}
diff --git a/lucene/core/src/java/org/apache/lucene/util/NamedSPILoader.java b/lucene/core/src/java/org/apache/lucene/util/NamedSPILoader.java
index b831c97da8c..40caaf95cc9 100644
--- a/lucene/core/src/java/org/apache/lucene/util/NamedSPILoader.java
+++ b/lucene/core/src/java/org/apache/lucene/util/NamedSPILoader.java
@@ -39,6 +39,11 @@ public final class NamedSPILoader<S extends NamedSPILoader.NamedSPI> implements
public NamedSPILoader(Class<S> clazz, ClassLoader classloader) {
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 (clazzClassloader != null && !SPIClassIterator.isParentClassLoader(clazzClassloader, classloader)) {
+ reload(clazzClassloader);
+ }
reload(classloader);
}
diff --git a/lucene/core/src/java/org/apache/lucene/util/SPIClassIterator.java b/lucene/core/src/java/org/apache/lucene/util/SPIClassIterator.java
index b9c136a7550..0cfa85194ea 100644
--- a/lucene/core/src/java/org/apache/lucene/util/SPIClassIterator.java
+++ b/lucene/core/src/java/org/apache/lucene/util/SPIClassIterator.java
@@ -54,16 +54,27 @@ public final class SPIClassIterator<S> implements Iterator<Class<? extends S>> {
return new SPIClassIterator<S>(clazz, loader);
}
+ /** Utility method to check if some class loader is a (grand-)parent of or the same as another one.
+ * This means the child will be able to load all classes from the parent, too. */
+ public static boolean isParentClassLoader(final ClassLoader parent, ClassLoader child) {
+ while (child != null) {
+ if (child == parent) {
+ return true;
+ }
+ child = child.getParent();
+ }
+ return false;
+ }
+
private SPIClassIterator(Class<S> clazz, ClassLoader loader) {
- if (loader == null)
- throw new IllegalArgumentException("You must provide a ClassLoader.");
this.clazz = clazz;
- this.loader = loader;
try {
- this.profilesEnum = loader.getResources(META_INF_SERVICES + clazz.getName());
+ final String fullName = META_INF_SERVICES + clazz.getName();
+ this.profilesEnum = (loader == null) ? ClassLoader.getSystemResources(fullName) : loader.getResources(fullName);
} catch (IOException ioe) {
throw new ServiceConfigurationError("Error loading SPI profiles for type " + clazz.getName() + " from classpath", ioe);
}
+ this.loader = (loader == null) ? ClassLoader.getSystemClassLoader() : loader;
this.linesIterator = Collections.<String>emptySet().iterator();
}