Merge pull request #12060 from Pugo66/feature/resource-leak-fix

修复可能的内存泄露问题,确保IO资源正确关闭,改用ProtectionDomain进行资源加载
This commit is contained in:
Junjun 2024-09-06 16:09:50 +08:00 committed by GitHub
commit 1bf51ca229
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,8 +12,9 @@ import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URI;
import java.nio.file.Paths;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@ -38,25 +39,27 @@ public abstract class DataEaseDatasourcePlugin extends Provider implements DataE
private void loadDriver() throws Exception {
XpackPluginsDatasourceVO config = getConfig();
String localPath = StringUtils.isEmpty(config.getDriverPath()) ? DEFAULT_FILE_PATH : config.getDriverPath();
ClassLoader classLoader = this.getClass().getClassLoader();
URL[] urls = ((URLClassLoader) classLoader).getURLs();
String jarPath = urls[0].getPath();
JarFile jarFile = new JarFile(jarPath);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry) entries.nextElement();
String name = entry.getName();
if (StringUtils.endsWith(name, ".jar")) {
InputStream inputStream = jarFile.getInputStream(entry);
File file = new File(localPath, name.substring(name.indexOf("/") + 1));
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = inputStream.read(bytes)) >= 0) {
outputStream.write(bytes, 0, length);
ProtectionDomain protectionDomain = this.getClass().getProtectionDomain();
URI uri = protectionDomain.getCodeSource().getLocation().toURI();
try(JarFile jarFile = new JarFile(new File(uri))) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (StringUtils.endsWith(name, ".jar")) {
File file = new File(localPath, Paths.get(name).getFileName().toString());
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try(InputStream inputStream = jarFile.getInputStream(entry);
FileOutputStream outputStream = new FileOutputStream(file)){
byte[] bytes = new byte[1024];
int length;
while ((length = inputStream.read(bytes)) >= 0) {
outputStream.write(bytes, 0, length);
}
}
}
}
}
@ -78,17 +81,17 @@ public abstract class DataEaseDatasourcePlugin extends Provider implements DataE
@Override
public void unloadPlugin() {
try {
ClassLoader classLoader = this.getClass().getClassLoader();
URL[] urls = ((URLClassLoader) classLoader).getURLs();
String jarPath = urls[0].getPath();
JarFile jarFile = new JarFile(jarPath);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry) entries.nextElement();
String name = entry.getName();
if (StringUtils.endsWith(name, ".jar")) {
File file = new File(DEFAULT_FILE_PATH, name.substring(name.indexOf("/") + 1));
file.delete();
ProtectionDomain protectionDomain = this.getClass().getProtectionDomain();
URI uri = protectionDomain.getCodeSource().getLocation().toURI();
try(JarFile jarFile = new JarFile(new File(uri))) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (StringUtils.endsWith(name, ".jar")) {
File file = new File(DEFAULT_FILE_PATH, Paths.get(name).getFileName().toString());
file.delete();
}
}
}
} catch (Exception e) {