From 0b3ffcf73cc7d9706d6fe0a14005463498da4c2e Mon Sep 17 00:00:00 2001 From: pyh Date: Fri, 6 Sep 2024 11:15:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=AF=E8=83=BD=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E7=A1=AE=E4=BF=9DIO=E8=B5=84=E6=BA=90=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E5=85=B3=E9=97=AD=EF=BC=8C=E6=94=B9=E7=94=A8ProtectionDomain?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E8=B5=84=E6=BA=90=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/DataEaseDatasourcePlugin.java | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/plugin/DataEaseDatasourcePlugin.java b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/plugin/DataEaseDatasourcePlugin.java index 2b4d783d89..35a078e574 100644 --- a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/plugin/DataEaseDatasourcePlugin.java +++ b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/plugin/DataEaseDatasourcePlugin.java @@ -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 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 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 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 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) {