SOLR-14151: refactor to avoid code duplicate

This commit is contained in:
noblepaul 2020-10-07 16:04:35 +11:00
parent 247cea1011
commit 2a8136b3fd
2 changed files with 19 additions and 13 deletions

View File

@ -694,17 +694,24 @@ public class SolrResourceLoader implements ResourceLoader, Closeable, SolrClassL
}
for (ResourceLoaderAware aware : arr) {
CURRENT_AWARE.set(aware);
try{
aware.inform(loader);
} finally {
CURRENT_AWARE.remove();
}
informAware(loader, aware);
}
}
}
/**
* Set the current {@link ResourceLoaderAware} object in thread local so that appropriate classloader can be used for package loaded classes
*/
public static void informAware(ResourceLoader loader, ResourceLoaderAware aware) throws IOException {
CURRENT_AWARE.set(aware);
try{
aware.inform(loader);
} finally {
CURRENT_AWARE.remove();
}
}
/**
* Register any {@link SolrInfoBean}s
*
@ -883,6 +890,6 @@ public class SolrResourceLoader implements ResourceLoader, Closeable, SolrClassL
}
//This is to verify if this requires to use the schema classloader for classes loaded from packages
public static final ThreadLocal<ResourceLoaderAware> CURRENT_AWARE = new ThreadLocal<>();
private static final ThreadLocal<ResourceLoaderAware> CURRENT_AWARE = new ThreadLocal<>();
}

View File

@ -79,6 +79,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import static org.apache.solr.core.SolrResourceLoader.informAware;
/** Solr-managed schema - non-user-editable, but can be mutable via internal and external REST API requests. */
public final class ManagedIndexSchema extends IndexSchema {
@ -1324,7 +1326,7 @@ public final class ManagedIndexSchema extends IndexSchema {
for (CharFilterFactory next : charFilters) {
if (next instanceof ResourceLoaderAware) {
try {
((ResourceLoaderAware) next).inform(loader);
informAware(loader, (ResourceLoaderAware) next);
} catch (IOException e) {
throw new SolrException(ErrorCode.SERVER_ERROR, e);
}
@ -1334,7 +1336,7 @@ public final class ManagedIndexSchema extends IndexSchema {
TokenizerFactory tokenizerFactory = chain.getTokenizerFactory();
if (tokenizerFactory instanceof ResourceLoaderAware) {
try {
((ResourceLoaderAware) tokenizerFactory).inform(loader);
informAware(loader, (ResourceLoaderAware) tokenizerFactory);
} catch (IOException e) {
throw new SolrException(ErrorCode.SERVER_ERROR, e);
}
@ -1343,13 +1345,10 @@ public final class ManagedIndexSchema extends IndexSchema {
TokenFilterFactory[] filters = chain.getTokenFilterFactories();
for (TokenFilterFactory next : filters) {
if (next instanceof ResourceLoaderAware) {
SolrResourceLoader.CURRENT_AWARE.set((ResourceLoaderAware) next);
try {
((ResourceLoaderAware) next).inform(loader);
informAware(loader, (ResourceLoaderAware) next);
} catch (IOException e) {
throw new SolrException(ErrorCode.SERVER_ERROR, e);
} finally {
SolrResourceLoader.CURRENT_AWARE.remove();
}
}
}