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