From 0d4bc5284f9fd633b8191cedd953c770238eaf32 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Fri, 24 Feb 2023 00:18:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=8A=A8=E6=80=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=B1=BB=E4=BE=9D=E8=B5=96jar=20in=20jar=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E6=8A=A5=E6=89=BE=E4=B8=8D=E5=88=B0=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/aop/compiler/AsgcCompiler.java | 15 +++++++++- .../internal/ExecutableArchiveLauncher.java | 14 ++++----- .../internal/SpringBootJarParser.java | 30 +++++++++++-------- .../asgc/neutrino/core/base/GlobalConfig.java | 12 ++++++++ .../asgc/neutrino/core/util/SystemUtil.java | 17 +++++++---- 5 files changed, 61 insertions(+), 27 deletions(-) diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java index d44ef4b0..1ff47f58 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java @@ -22,6 +22,7 @@ package fun.asgc.neutrino.core.aop.compiler; import com.google.common.collect.Lists; +import fun.asgc.neutrino.core.aop.compiler.internal.JarLauncher; import fun.asgc.neutrino.core.base.GlobalConfig; import fun.asgc.neutrino.core.util.CollectionUtil; import fun.asgc.neutrino.core.util.FileUtil; @@ -73,11 +74,23 @@ public class AsgcCompiler { if (null == javaCompiler) { throw new RuntimeException("Can not load JavaCompiler from javax.tools.ToolProvider#getSystemJavaCompiler(),\n please confirm the application running in JDK not JRE."); } + ClassLoader parent = classLoader; + if (SystemUtil.isStartupFromJar() && GlobalConfig.isIsUseSpringBootJar()) { + try { + log.debug("使用LaunchedURLClassLoader jar路径:{}", SystemUtil.getCurrentJarFilePath()); + JarLauncher jarLauncher = new JarLauncher(parent, SystemUtil.getCurrentJarFilePath()); + parent = jarLauncher.createClassLoader(); + log.debug("使用LaunchedURLClassLoader instance:{}", parent); + } catch (Exception e) { + // ignore + log.error("使用LaunchedURLClassLoader异常", e); + } + } this.collector = new DiagnosticCollector<>(); this.standardJavaFileManager = javaCompiler.getStandardFileManager(collector, null, null); this.isSaveClassFile = false; this.generatorCodeSavePath = GlobalConfig.getGeneratorCodeSavePath(); - this.dynamicClassLoader = new DynamicClassLoader(classLoader, this); + this.dynamicClassLoader = new DynamicClassLoader(parent, this); addOption("-Xlint:unchecked"); addOption("-implicit:class"); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/internal/ExecutableArchiveLauncher.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/internal/ExecutableArchiveLauncher.java index 56d81c11..b475769c 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/internal/ExecutableArchiveLauncher.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/internal/ExecutableArchiveLauncher.java @@ -33,13 +33,13 @@ public abstract class ExecutableArchiveLauncher extends Launcher { private ClassPathIndexFile classPathIndex; public ExecutableArchiveLauncher() { - try { - this.archive = createArchive(); - this.classPathIndex = getClassPathIndex(this.archive); - } - catch (Exception ex) { - throw new IllegalStateException(ex); - } +// try { +// this.archive = createArchive(); +// this.classPathIndex = getClassPathIndex(this.archive); +// } +// catch (Exception ex) { +// throw new IllegalStateException(ex); +// } } protected ExecutableArchiveLauncher(Archive archive) { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/internal/SpringBootJarParser.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/internal/SpringBootJarParser.java index cfff65b2..6f0a380a 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/internal/SpringBootJarParser.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/internal/SpringBootJarParser.java @@ -23,21 +23,25 @@ public final class SpringBootJarParser { */ public static InputStream getInputStream(URI uri) { try { - if (uri.getScheme().equals("jar")) { - URL url = uri.toURL(); - String path = url.getPath(); - int index = path.indexOf("!"); - if (index >= 0) { - path = path.substring(0, index); + return uri.toURL().openStream(); + } catch (Exception e) { + try { + if (uri.getScheme().equals("jar")) { + URL url = uri.toURL(); + String path = url.getPath(); + int index = path.indexOf("!"); + if (index >= 0) { + path = path.substring(0, index); + } + if (path.startsWith("file:")) { + path = path.substring(5); + } + JarFile jarFile = new JarFile(new File(path)); + return JarURLConnection.get(url, jarFile).getInputStream(); } - if (path.startsWith("file:")) { - path = path.substring(5); - } - JarFile jarFile = new JarFile(new File(path)); - return JarURLConnection.get(url, jarFile).getInputStream(); + } catch (Exception e1) { + // ignore } - } catch (Exception e1) { - // ignore } return null; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/GlobalConfig.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/GlobalConfig.java index 755b3215..8ad8d938 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/GlobalConfig.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/base/GlobalConfig.java @@ -44,6 +44,10 @@ public class GlobalConfig { * 当程序是以jar方式运行时,强制保存生成的代码 */ private static volatile boolean isSaveGeneratorCode = false; + /** + * 是否使用SpringBoot方式的jar + */ + private static volatile boolean isUseSpringBootJar = true; /** * 自动生成代码保存路径 */ @@ -88,4 +92,12 @@ public class GlobalConfig { public static String getMapperXmlFileBasePath() { return mapperXmlFileBasePath; } + + public static boolean isIsPrintGeneratorCode() { + return isPrintGeneratorCode; + } + + public static boolean isIsUseSpringBootJar() { + return isUseSpringBootJar; + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/SystemUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/SystemUtil.java index e242d497..9f798060 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/SystemUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/SystemUtil.java @@ -23,6 +23,7 @@ package fun.asgc.neutrino.core.util; import fun.asgc.neutrino.core.base.CodeBlock; +import lombok.extern.slf4j.Slf4j; import java.util.Objects; @@ -31,6 +32,7 @@ import java.util.Objects; * @author: aoshiguchen * @date: 2022/6/16 */ +@Slf4j public class SystemUtil { public static void addShutdownHook(Runnable runnable) { @@ -98,14 +100,17 @@ public class SystemUtil { if (!isStartupFromJar()) { return null; } - String url = SystemUtil.class.getResource("").getPath(); - if (url.startsWith("file:")) { - url = url.substring(5); + String path = SystemUtil.class.getResource("").getPath(); + String res = path; + if (res.startsWith("file:")) { + res = res.substring(5); } - if (url.contains("!")) { - url = url.substring(0, url.indexOf("!")); + int index = res.indexOf("!"); + if (index >= 0) { + res = res.substring(0, index); } - return url; + log.debug("getCurrentJarFilePath path:{} index:{} url:{}", path, index, res); + return res; } public static boolean isWindows() {