SOLR-14481: use classloader to open resource

This commit is contained in:
noble 2020-06-29 14:03:17 +10:00
parent 94684cc014
commit d883cd69fc
2 changed files with 15 additions and 51 deletions

View File

@ -18,27 +18,28 @@
package org.apache.solr.pkg;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.solr.common.MapWriter;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.util.SimplePostTool;
import org.apache.zookeeper.server.ByteBufferInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -343,45 +344,8 @@ public class PackageLoader implements Closeable {
}
@Override
public InputStream openResource(String resource) throws IOException {
for (Path path : paths) {
try(FileInputStream in = new FileInputStream(path.toFile())) {
ZipInputStream zis = new ZipInputStream(in);
try {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (resource == null || resource.equals(entry.getName())) {
SimplePostTool.BAOS out = new SimplePostTool.BAOS();
byte[] buffer = new byte[2048];
int size;
while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, size);
}
out.close();
return new ByteBufferStream(out.getByteBuffer());
}
}
} finally {
zis.closeEntry();
}
}
}
return null;
}
}
private static class ByteBufferStream extends ByteBufferInputStream implements Supplier<ByteBuffer> {
private final ByteBuffer buf ;
public ByteBufferStream(ByteBuffer buf) {
super(buf);
this.buf = buf;
}
@Override
public ByteBuffer get() {
return buf;
public InputStream openResource(String resource) {
return getClassLoader().getResourceAsStream(resource);
}
}

View File

@ -22,9 +22,9 @@ import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.IOUtils;
import org.apache.lucene.analysis.util.ResourceLoader;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
import org.apache.solr.api.Command;
@ -270,9 +270,9 @@ public class TestContainerPlugin extends SolrCloudTestCase {
this.resourceLoader = (SolrResourceLoader) loader;
try {
InputStream is = resourceLoader.openResource("org/apache/solr/handler/MyPlugin.class");
if (is instanceof Supplier) {
classData = ((Supplier<ByteBuffer>) is).get();
}
byte[] buf = new byte[1024*5];
int sz = IOUtils.read(is, buf);
classData = ByteBuffer.wrap(buf, 0,sz);
} catch (IOException e) {
//do not do anything
}