From a925aa6064460e8f15d3db9b895106f2de25e02c Mon Sep 17 00:00:00 2001 From: StarBlues Date: Sat, 17 Sep 2022 12:27:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E4=B8=BAjar-outer=E7=B1=BB=E5=9E=8B=E5=8A=A0=E8=BD=BD=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootstrap/ConfigurePluginEnvironment.java | 12 +++-- .../com/gitee/starblues/utils/FilesUtils.java | 46 +++++++++++++------ .../launcher/SpringMainProdBootstrap.java | 14 ------ .../AbstractPluginDescriptorLoader.java | 14 ++---- .../ProdPackagePluginDescriptorLoader.java | 10 ++++ 5 files changed, 52 insertions(+), 44 deletions(-) diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java index 536b4e3..272d6a1 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java @@ -39,7 +39,8 @@ import java.util.Map; /** * 插件环境配置 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ public class ConfigurePluginEnvironment { private final Logger logger = LoggerFactory.getLogger(ConfigurePluginEnvironment.class); @@ -100,11 +101,12 @@ public class ConfigurePluginEnvironment { } private String getConfigFileLocation(String configFileLocation){ - String s = FilesUtils.resolveRelativePath(new File("").getAbsolutePath(), configFileLocation); - if(s.endsWith("/") || s.endsWith(File.separator)){ - return s; + String path = FilesUtils.resolveRelativePath(new File("").getAbsolutePath(), configFileLocation); + // 拼接最后字符斜杠 + if(path.endsWith(FilesUtils.SLASH) || path.endsWith(File.separator)){ + return path; } else { - return s + File.separator; + return path + File.separator; } } diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java b/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java index a057e15..0e2741c 100644 --- a/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java +++ b/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java @@ -31,6 +31,22 @@ import java.io.IOException; */ public class FilesUtils { + /** + * 正斜杠 + */ + public static final String SLASH = "/"; + + /** + * 双正斜杠 + */ + public static final String DOUBLE_SLASH = "//"; + + /** + * 反斜杠 + */ + public static final String BACKSLASH = "\\"; + + /** * 获取存在的文件 * @@ -77,17 +93,17 @@ public class FilesUtils { continue; } if (i < length - 1) { - if (path.endsWith("/")) { - path = path.replace("/", ""); - } else if (path.endsWith("\\")) { - path = path.replace("\\", ""); - } else if (path.endsWith("//")) { - path = path.replace("//", ""); + if (path.endsWith(SLASH)) { + path = path.replace(SLASH, ""); + } else if (path.endsWith(BACKSLASH)) { + path = path.replace(BACKSLASH, ""); + } else if (path.endsWith(DOUBLE_SLASH)) { + path = path.replace(DOUBLE_SLASH, ""); } } if (i > 0) { - if (path.startsWith(File.separator) || path.startsWith("/") || - path.startsWith("\\") || path.startsWith("//")) { + if (path.startsWith(File.separator) || path.startsWith(SLASH) || + path.startsWith(DOUBLE_SLASH) || path.startsWith(BACKSLASH)) { stringBuilder.append(path); } else { stringBuilder.append(File.separator).append(path); @@ -119,12 +135,12 @@ public class FilesUtils { continue; } if(i < length - 1){ - if(path.endsWith("/")){ - path = path.replace("/", ""); - } else if(path.endsWith("\\")){ - path = path.replace("\\", ""); - } else if(path.endsWith("//")){ - path = path.replace("//", ""); + if(path.endsWith(SLASH)){ + path = path.replace(SLASH, ""); + } else if(path.endsWith(BACKSLASH)){ + path = path.replace(BACKSLASH, ""); + } else if(path.endsWith(DOUBLE_SLASH)){ + path = path.replace(DOUBLE_SLASH, ""); } } if(i > 0){ @@ -162,7 +178,6 @@ public class FilesUtils { } } - /** * 解决相对路径 * @param rootPath 根路径 @@ -192,4 +207,5 @@ public class FilesUtils { return path.startsWith(Constants.RELATIVE_SIGN); } + } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainProdBootstrap.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainProdBootstrap.java index 8558224..6e34e2b 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainProdBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainProdBootstrap.java @@ -17,21 +17,7 @@ package com.gitee.starblues.loader.launcher; import com.gitee.starblues.loader.jar.JarFile; -import com.gitee.starblues.loader.launcher.isolation.IsolationJarOuterLauncher; -import com.gitee.starblues.loader.launcher.isolation.IsolationFastJarLauncher; -import com.gitee.starblues.loader.launcher.runner.MethodRunner; -import com.gitee.starblues.loader.utils.ObjectUtils; -import java.io.File; -import java.net.URI; -import java.net.URISyntaxException; -import java.security.CodeSource; -import java.security.ProtectionDomain; -import java.util.Objects; -import java.util.jar.Attributes; -import java.util.jar.Manifest; - -import static com.gitee.starblues.loader.LoaderConstant.*; /** * 主程序生成环境启动引导器 * @author starBlues diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java index efeb3a0..f72ff38 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java @@ -187,27 +187,21 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor } protected String getLibDir(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir){ - if(FilesUtils.existFile(configPluginLibDir)){ + if(FilesUtils.existFile(configPluginLibDir) || !FilesUtils.isRelativePath(configPluginLibDir)){ return configPluginLibDir; } // 先检查插件相对目录 - String resolveRelativePath = null; - if(FilesUtils.isRelativePath(configPluginLibDir)){ - // 先相对当前插件目录 - resolveRelativePath = FilesUtils.resolveRelativePath(descriptor.getPluginPath(), configPluginLibDir); - } else { - resolveRelativePath = FilesUtils.joiningFilePath(descriptor.getPluginPath(), configPluginLibDir); - } + String resolveRelativePath = FilesUtils.resolveRelativePath(descriptor.getPluginPath(), configPluginLibDir); if(FilesUtils.existFile(resolveRelativePath)){ return resolveRelativePath; } // 再相对插件存放目录 - resolveRelativePath = FilesUtils.joiningFilePath(new File(descriptor.getPluginPath()).getParent(), configPluginLibDir); + resolveRelativePath = FilesUtils.resolveRelativePath(new File(descriptor.getPluginPath()).getParent(), configPluginLibDir); if(FilesUtils.existFile(resolveRelativePath)){ return resolveRelativePath; } // 最后相对主程序目录 - resolveRelativePath = FilesUtils.joiningFilePath(new File("").getAbsolutePath(), configPluginLibDir); + resolveRelativePath = FilesUtils.resolveRelativePath(new File("").getAbsolutePath(), configPluginLibDir); if(FilesUtils.existFile(resolveRelativePath)){ return resolveRelativePath; } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPackagePluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPackagePluginDescriptorLoader.java index a52b8a6..7a4d0b9 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPackagePluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPackagePluginDescriptorLoader.java @@ -85,6 +85,16 @@ public class ProdPackagePluginDescriptorLoader extends AbstractPluginDescriptorL protected String getLibDir(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir) { if(PluginType.isNestedPackage(descriptor.getType())){ return descriptor.getPluginLibDir(); + } else if(PluginType.isOuterPackage(descriptor.getType())){ + String pluginLibDir = descriptor.getPluginLibDir(); + if(ObjectUtils.isEmpty(pluginLibDir)){ + return super.getLibDir(descriptor, configPluginLibDir); + } + if(FilesUtils.isRelativePath(pluginLibDir)){ + return super.getLibDir(descriptor, configPluginLibDir); + } else { + return descriptor.getPluginLibDir(); + } } else { return super.getLibDir(descriptor, configPluginLibDir); } -- Gitee From 1e4d15a6c131ce3edf2f79c9f73134e89b51d4ae Mon Sep 17 00:00:00 2001 From: StarBlues Date: Sat, 17 Sep 2022 16:47:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E4=B8=BAjar-outer=E7=B1=BB=E5=9E=8B=E5=8A=A0=E8=BD=BD=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gitee/starblues/utils/FilesUtils.java | 27 ++++++++++++++++++- .../AbstractPluginDescriptorLoader.java | 22 ++++++++------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java b/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java index 0e2741c..7235739 100644 --- a/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java +++ b/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java @@ -189,12 +189,37 @@ public class FilesUtils { return relativePath; } if(isRelativePath(relativePath)){ - return joiningFilePath(rootPath, relativePath.replaceFirst(Constants.RELATIVE_SIGN, "")); + String resolveRelativePath = relativePath.replaceFirst(Constants.RELATIVE_SIGN, ""); + return joiningFilePath(rootPath, resolveRelativePath); } else { return relativePath; } } + /** + * 解决存在的相对路径 + * @param rootPath 根路径 + * @param path 以 ~ 开头的相对路径或者完整路径 + * @return File 或者 null(不存在) + */ + public static File resolveExistRelativePathFile(String rootPath, String path){ + if(ObjectUtils.isEmpty(path)){ + return null; + } + if(isRelativePath(path)){ + String resolveRelativePath = path.replaceFirst(Constants.RELATIVE_SIGN, ""); + String joiningFilePath = joiningFilePath(rootPath, resolveRelativePath); + return getExistFile(joiningFilePath); + } else { + File existFile = getExistFile(path); + if(existFile != null){ + return existFile; + } + String joiningFilePath = joiningFilePath(rootPath, path); + return getExistFile(joiningFilePath); + } + } + /** * 是否是相对路径 * @param path 路径 diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java index f72ff38..49e3cda 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java @@ -187,23 +187,25 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor } protected String getLibDir(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir){ - if(FilesUtils.existFile(configPluginLibDir) || !FilesUtils.isRelativePath(configPluginLibDir)){ + if(FilesUtils.existFile(configPluginLibDir)){ return configPluginLibDir; } // 先检查插件相对目录 - String resolveRelativePath = FilesUtils.resolveRelativePath(descriptor.getPluginPath(), configPluginLibDir); - if(FilesUtils.existFile(resolveRelativePath)){ - return resolveRelativePath; + File libDirFile = FilesUtils.resolveExistRelativePathFile(descriptor.getPluginPath(), configPluginLibDir); + if(libDirFile != null){ + return libDirFile.getPath(); } // 再相对插件存放目录 - resolveRelativePath = FilesUtils.resolveRelativePath(new File(descriptor.getPluginPath()).getParent(), configPluginLibDir); - if(FilesUtils.existFile(resolveRelativePath)){ - return resolveRelativePath; + libDirFile = FilesUtils.resolveExistRelativePathFile( + new File(descriptor.getPluginPath()).getParent(), configPluginLibDir); + if(libDirFile != null){ + return libDirFile.getPath(); } // 最后相对主程序目录 - resolveRelativePath = FilesUtils.resolveRelativePath(new File("").getAbsolutePath(), configPluginLibDir); - if(FilesUtils.existFile(resolveRelativePath)){ - return resolveRelativePath; + libDirFile = FilesUtils.resolveExistRelativePathFile(new File("").getAbsolutePath(), + configPluginLibDir); + if(libDirFile != null){ + return libDirFile.getPath(); } return null; } -- Gitee