diff --git a/pom.xml b/pom.xml index 6e6cdb651d900511884a6b97dbc72f5af3d71e10..c715765c02f61c6d1161020fce158564bba6115c 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.gitee.starblues spring-brick-parent pom - 3.0.1 + 3.0.2 spring-brick-common diff --git a/spring-brick-bootstrap/pom.xml b/spring-brick-bootstrap/pom.xml index 5e1c5c6a4ce96373b51e0f22ce232fe3f82cddc9..730adc0fc110be1b5e129d048858d46bf3b76dfb 100644 --- a/spring-brick-bootstrap/pom.xml +++ b/spring-brick-bootstrap/pom.xml @@ -7,7 +7,7 @@ spring-brick-parent com.gitee.starblues - 3.0.1 + 3.0.2 spring-brick-bootstrap diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/EmptyMainApplicationContext.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/EmptyMainApplicationContext.java index 35eb69509929b109232c4e7d8ffe141ba52c343c..0321276c98ebb22c5e19fe62a551f7e2ff7018af 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/EmptyMainApplicationContext.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/EmptyMainApplicationContext.java @@ -46,10 +46,4 @@ public class EmptyMainApplicationContext implements MainApplicationContext { return Collections.emptyMap(); } - @Override - public ClassLoader getClassLoader() { - return EmptyMainApplicationContext.class.getClassLoader(); - } - - } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginSpringApplication.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginSpringApplication.java index c46e5f620dc5cd40e42cca43844a988b117df61f..59a700261526741a46a6065cbf5bcf4ebe4cd30b 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginSpringApplication.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginSpringApplication.java @@ -103,7 +103,7 @@ public class PluginSpringApplication extends SpringApplication { return super.run(args); } catch (Exception e) { pluginProcessor.failure(processorContext); - logger.error("启动插件[{}]失败. {}", + logger.debug("启动插件[{}]失败. {}", processorContext.getPluginDescriptor().getPluginId(), e.getMessage(), e); throw new RuntimeException(e); diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginInterceptorsProcessor.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginInterceptorsProcessor.java index 48bb8fb2c11b3db0d71ef30f5f42a4540ec4c764..1ecd40b72597b378be61c274161f94a127f3e38e 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginInterceptorsProcessor.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginInterceptorsProcessor.java @@ -24,19 +24,20 @@ import com.gitee.starblues.bootstrap.processor.interceptor.PluginInterceptorRegi import com.gitee.starblues.bootstrap.utils.SpringBeanUtils; import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.spring.MainApplicationContext; -import com.gitee.starblues.utils.ClassUtils; -import com.gitee.starblues.utils.OrderUtils; -import com.gitee.starblues.utils.PluginConfigUtils; -import com.gitee.starblues.utils.SpringBeanCustomUtils; +import com.gitee.starblues.utils.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.web.context.request.WebRequestInterceptor; import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.handler.AbstractHandlerMapping; import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 插件拦截器处理者 @@ -48,67 +49,79 @@ public class PluginInterceptorsProcessor implements SpringPluginProcessor { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final static String INTERCEPTORS = "pluginHandlerInterceptors"; - private AbstractHandlerMapping handlerMapping; + private List handlerMappings; @Override public void initialize(ProcessorContext context) throws ProcessorException { MainApplicationContext applicationContext = context.getMainApplicationContext(); - handlerMapping = SpringBeanCustomUtils.getExistBean(applicationContext, + handlerMappings = SpringBeanCustomUtils.getBeans(applicationContext, AbstractHandlerMapping.class); - if(handlerMapping == null){ + if(handlerMappings.isEmpty()){ logger.warn("Not found AbstractHandlerMapping, Plugin interceptor can't use"); } } @Override public void refreshAfter(ProcessorContext context) throws ProcessorException { - if(handlerMapping == null){ + if(handlerMappings.isEmpty()){ return; } List interceptorRegisters = SpringBeanUtils.getBeans( context.getApplicationContext(), PluginInterceptorRegister.class); - List interceptorsObjects = new ArrayList<>(); - List adaptedInterceptors = getAdaptedInterceptors(); - if(adaptedInterceptors == null){ + + Map> handlerInterceptorMap = new HashMap<>(); + for (AbstractHandlerMapping handlerMapping : handlerMappings) { + List adaptedInterceptors = getAdaptedInterceptors(handlerMapping); + if(!ObjectUtils.isEmpty(adaptedInterceptors)){ + handlerInterceptorMap.put(handlerMapping, adaptedInterceptors); + } + } + if(handlerInterceptorMap.isEmpty()){ + logger.debug("handlerInterceptorMap is empty"); return; } IntegrationConfiguration configuration = context.getConfiguration(); String pluginId = context.getPluginDescriptor().getPluginId(); String pluginRestPrefix = PluginConfigUtils.getPluginRestPrefix(configuration, pluginId); + + List storeInterceptors = new ArrayList<>(); for (PluginInterceptorRegister interceptorRegister : interceptorRegisters) { PluginInterceptorRegistry interceptorRegistry = new PluginInterceptorRegistry(pluginRestPrefix); interceptorRegister.registry(interceptorRegistry); - List interceptors = interceptorRegistry.getInterceptors(); - if(interceptors == null || interceptors.isEmpty()){ + List registryInterceptors = interceptorRegistry.getInterceptors(); + if(registryInterceptors == null || registryInterceptors.isEmpty()){ continue; } - for (Object interceptor : interceptors) { + for (Object interceptor : registryInterceptors) { HandlerInterceptor handlerInterceptor = adaptInterceptor(interceptor); - adaptedInterceptors.add(handlerInterceptor); - interceptorsObjects.add(handlerInterceptor); + for (List value : handlerInterceptorMap.values()) { + value.add(handlerInterceptor); + } + storeInterceptors.add(handlerInterceptor); } } - context.addRegistryInfo(INTERCEPTORS, interceptorsObjects); + context.addRegistryInfo(INTERCEPTORS, storeInterceptors); } @Override public void close(ProcessorContext context) throws ProcessorException { - if(handlerMapping == null){ + if(handlerMappings.isEmpty()){ return; } - List interceptorsObjects = context.getRegistryInfo(INTERCEPTORS); - if(interceptorsObjects == null || interceptorsObjects.isEmpty()){ + List storeInterceptors = context.getRegistryInfo(INTERCEPTORS); + if(ObjectUtils.isEmpty(storeInterceptors)){ return; } - List adaptedInterceptors = getAdaptedInterceptors(); - if(adaptedInterceptors == null){ - return; - } - for (HandlerInterceptor interceptor : interceptorsObjects) { - adaptedInterceptors.remove(interceptor); + for (HandlerInterceptor storeInterceptor : storeInterceptors) { + for (AbstractHandlerMapping handlerMapping : handlerMappings) { + List adaptedInterceptors = getAdaptedInterceptors(handlerMapping); + if(!ObjectUtils.isEmpty(adaptedInterceptors)){ + adaptedInterceptors.remove(storeInterceptor); + } + } } } @@ -119,9 +132,10 @@ public class PluginInterceptorsProcessor implements SpringPluginProcessor { /** * 得到拦截器存储者 + * @param handlerMapping AbstractHandlerMapping * @return List */ - private List getAdaptedInterceptors(){ + private List getAdaptedInterceptors(AbstractHandlerMapping handlerMapping){ try { return ClassUtils.getReflectionField(handlerMapping, "adaptedInterceptors", List.class); } catch (IllegalAccessException e) { diff --git a/spring-brick-common/pom.xml b/spring-brick-common/pom.xml index a0e3e5a5f1b8d3eaec38fda773810fca3ae42043..f5213b9e0b5e85bb6ac23dba4d71ae5ac0fbea1a 100644 --- a/spring-brick-common/pom.xml +++ b/spring-brick-common/pom.xml @@ -7,7 +7,7 @@ spring-brick-parent com.gitee.starblues - 3.0.1 + 3.0.2 spring-brick-common diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/ManifestKey.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/ManifestKey.java index ae91f444f45f4f452201841d1558cc024faffddc..b4ffd0958ed1f7bfcc76e4395842addbc04c36ff 100644 --- a/spring-brick-common/src/main/java/com/gitee/starblues/common/ManifestKey.java +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/ManifestKey.java @@ -67,7 +67,21 @@ public class ManifestKey { */ public static final String CLASS_PATH = "Class-Path"; + /** + * jar main lib path + */ + public static final String MAIN_LIB_DIR = "Lib-Dir"; + + /** + * jar main lib indexes + */ + public static final String MAIN_LIB_INDEXES = "Lib-Indexes"; + + /** + * main package type + */ + public static final String MAIN_PACKAGE_TYPE = "Main-Package-Type"; /** * 获取值 diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/PackageStructure.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/PackageStructure.java index 95907fdc05e0962ae09150601213cd8839dce696..3a02c907df7efe3e4ee17a9e25ce70939a7b16f0 100644 --- a/spring-brick-common/src/main/java/com/gitee/starblues/common/PackageStructure.java +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/PackageStructure.java @@ -67,6 +67,7 @@ public abstract class PackageStructure { public static final String PROD_LIB_PATH = LIB_NAME + SEPARATOR; + public static String resolvePath(String path){ if(path == null || "".equals(path)){ return path; diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDescriptorKey.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDescriptorKey.java index 11bd32e5c25128165fd9f592b8ec70c37385a4cb..fab32471c14d9c1e61c1a03bbaf81d1c582b4d39 100644 --- a/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDescriptorKey.java +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDescriptorKey.java @@ -39,6 +39,7 @@ public class PluginDescriptorKey { public static final String PLUGIN_CONFIG_FILE_NAME = "plugin.configFileName"; public static final String PLUGIN_CONFIG_FILE_LOCATION = "plugin.configFileLocation"; public static final String PLUGIN_ARGS = "plugin.args"; + public static final String PLUGIN_LIB_DIR = "plugin.libDir"; /** System create prop **/ public static final String PLUGIN_PATH = "plugin.system.path"; 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 8f7bfa01c7f1c99a86c417f1024c53e25ffb22d4..34b06d33ba6ada2cc2a8570745c77fe5cdfd992b 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 @@ -20,13 +20,12 @@ import com.gitee.starblues.common.Constants; import java.io.File; import java.io.IOException; -import java.util.function.Supplier; /** * 文件工具类 * * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ public class FilesUtils { @@ -106,11 +105,23 @@ public class FilesUtils { if(ObjectUtils.isEmpty(relativePath)){ return relativePath; } - if(relativePath.startsWith(Constants.RELATIVE_SIGN)){ + if(isRelativePath(relativePath)){ return joiningFilePath(rootPath, relativePath.replaceFirst(Constants.RELATIVE_SIGN, "")); } else { return relativePath; } } + /** + * 是否是相对路径 + * @param path 路径 + * @return true 为相对路径, false 未非相对路径 + */ + public static boolean isRelativePath(String path){ + if(ObjectUtils.isEmpty(path)){ + return false; + } + return path.startsWith(Constants.RELATIVE_SIGN); + } + } diff --git a/spring-brick-loader/pom.xml b/spring-brick-loader/pom.xml index 8fc2cb2e2f300a787bc32672b253f99c7630e6de..2df42130e1ba20beabfa221ab97c4c903d945313 100644 --- a/spring-brick-loader/pom.xml +++ b/spring-brick-loader/pom.xml @@ -5,7 +5,7 @@ spring-brick-parent com.gitee.starblues - 3.0.1 + 3.0.2 4.0.0 diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/LoaderConstant.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/LoaderConstant.java new file mode 100644 index 0000000000000000000000000000000000000000..4631f5566c84a77a096a8897f266b64dc9652614 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/LoaderConstant.java @@ -0,0 +1,52 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.loader; + +/** + * 常量统一定义 + * + * @author starBlues + * @version 3.0.2 + */ +public class LoaderConstant { + + public static final String PROD_CLASSES_PATH = "classes/"; + public static final String PROD_CLASSES_URL_SIGN = "/classes!/"; + public static final String PROD_LIB_PATH = "lib/"; + + /** + * 相对路径符号标志 + */ + public final static String RELATIVE_SIGN = "~"; + + + /** + * ================= Manifest Key ===================== + */ + public static final String MAIN_LIB_DIR = "Lib-Dir"; + public static final String MAIN_LIB_INDEXES = "Lib-Indexes"; + public static final String MAIN_LIB_INDEXES_SPLIT = " "; + + public static final String START_CLASS = "Start-Class"; + public static final String MAIN_PACKAGE_TYPE = "Main-Package-Type"; + + public static final String MAIN_PACKAGE_TYPE_JAR = "jar"; + public static final String MAIN_PACKAGE_TYPE_JAR_OUTER = "jar-outer"; + + + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/MainJarResourceLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/MainJarResourceLoader.java new file mode 100644 index 0000000000000000000000000000000000000000..c1675b0fe550d0e93d3a91f9a3d5a5cebb9ee77b --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/MainJarResourceLoader.java @@ -0,0 +1,39 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.loader.classloader.resource.loader; + +import java.net.URL; + +/** + * 主程序 jar 启动时资源加载者 + * + * @author starBlues + * @version 3.0.2 + */ +public class MainJarResourceLoader extends JarResourceLoader { + + private static final String PROD_CLASSES_PATH = "classes/"; + + public MainJarResourceLoader(URL url) throws Exception { + super(url); + } + + @Override + protected String resolveName(String name) { + return name.replace(PROD_CLASSES_PATH, ""); + } +} \ No newline at end of file diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/AbstractLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/AbstractLauncher.java index e1d1c35208cc2c21be3aeb45fa54f447b3872fb5..a44ab55492ec712354471e79004fbfb499db62de 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/AbstractLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/AbstractLauncher.java @@ -19,7 +19,7 @@ package com.gitee.starblues.loader.launcher; /** * 抽象的启动引导者 * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ public abstract class AbstractLauncher implements Launcher { @@ -27,8 +27,13 @@ public abstract class AbstractLauncher implements Launcher { public R run(String... args) throws Exception { ClassLoader classLoader = createClassLoader(args); Thread thread = Thread.currentThread(); - thread.setContextClassLoader(classLoader); - return launch(classLoader, args); + ClassLoader oldClassLoader = thread.getContextClassLoader(); + try { + thread.setContextClassLoader(classLoader); + return launch(classLoader, args); + } finally { + thread.setContextClassLoader(oldClassLoader); + } } /** diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/AbstractMainLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/AbstractMainLauncher.java new file mode 100644 index 0000000000000000000000000000000000000000..f68fbd37bb69a5103a49323a7067b4587275a8f6 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/AbstractMainLauncher.java @@ -0,0 +1,40 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.loader.launcher; + +/** + * 抽象的启动引导者 + * @author starBlues + * @version 3.0.2 + */ +public abstract class AbstractMainLauncher extends AbstractLauncher { + + @Override + public R run(String... args) throws Exception { + ClassLoader classLoader = createClassLoader(args); + Thread thread = Thread.currentThread(); + ClassLoader oldClassLoader = thread.getContextClassLoader(); + try { + thread.setContextClassLoader(classLoader); + LauncherContext.setMainClassLoader(classLoader); + return launch(classLoader, args); + } finally { + thread.setContextClassLoader(oldClassLoader); + } + } + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/LauncherContext.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/LauncherContext.java new file mode 100644 index 0000000000000000000000000000000000000000..6f3f1c8d4a1db7ecf8008d7206245201b6ad55eb --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/LauncherContext.java @@ -0,0 +1,52 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.loader.launcher; + +/** + * LauncherContext + * + * @author starBlues + * @version 3.0.2 + */ +public class LauncherContext { + + private static volatile ClassLoader mainClassLoader = null; + + /** + * 获取主程序的ClassLoader + * @return 主程序ClassLoader + */ + public static ClassLoader getMainClassLoader(){ + return mainClassLoader; + } + + /** + * 设置主程序的ClassLoader + * @param classLoader 主程序ClassLoader + */ + static void setMainClassLoader(ClassLoader classLoader){ + if(mainClassLoader == null){ + synchronized (AbstractLauncher.class){ + if(mainClassLoader == null){ + mainClassLoader = classLoader; + } + } + } + } + + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarOuterProgramLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarOuterProgramLauncher.java new file mode 100644 index 0000000000000000000000000000000000000000..22249d6208c5dbb2e1a410da8c55c41bd97fb3ce --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarOuterProgramLauncher.java @@ -0,0 +1,141 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.loader.launcher; + +import com.gitee.starblues.loader.archive.Archive; +import com.gitee.starblues.loader.archive.ExplodedArchive; +import com.gitee.starblues.loader.archive.JarFileArchive; +import com.gitee.starblues.loader.classloader.GenericClassLoader; +import com.gitee.starblues.loader.classloader.resource.loader.MainJarResourceLoader; +import com.gitee.starblues.loader.launcher.runner.MethodRunner; +import com.gitee.starblues.loader.utils.FilesUtils; +import com.gitee.starblues.loader.utils.ObjectUtils; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.net.URL; +import java.util.*; +import java.util.jar.Manifest; + +import static com.gitee.starblues.loader.LoaderConstant.*; + + +/** + * 主程序jar-outer 模式启动者 + * + * @author starBlues + * @version 3.0.2 + */ +public class MainJarOuterProgramLauncher extends MainProgramLauncher{ + + + private final static Archive.EntryFilter ENTRY_FILTER = (entry)->{ + String name = entry.getName(); + return name.startsWith(PROD_CLASSES_PATH); + }; + + private final static Archive.EntryFilter INCLUDE_FILTER = (entry) -> { + if (entry.isDirectory()) { + return entry.getName().equals(PROD_CLASSES_PATH); + } + return false; + }; + + private final File rootJarFile; + + public MainJarOuterProgramLauncher(MethodRunner methodRunner, File rootJarFile) { + super(methodRunner); + this.rootJarFile = Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); + } + + @Override + protected void addResource(GenericClassLoader classLoader) throws Exception { + super.addResource(classLoader); + Archive archive = getArchive(); + Iterator archiveIterator = archive.getNestedArchives(ENTRY_FILTER, INCLUDE_FILTER); + addEntryResource(archiveIterator, classLoader); + addLibResource(archive, classLoader); + } + + private Archive getArchive() throws IOException { + return (rootJarFile.isDirectory() ? new ExplodedArchive(rootJarFile) : new JarFileArchive(rootJarFile)); + } + + private void addEntryResource(Iterator archives, GenericClassLoader classLoader) throws Exception { + while (archives.hasNext()){ + Archive archive = archives.next(); + URL url = archive.getUrl(); + String path = url.getPath(); + if(path.contains(PROD_CLASSES_URL_SIGN)){ + classLoader.addResource(new MainJarResourceLoader(url)); + } + } + } + + private void addLibResource(Archive archive, GenericClassLoader classLoader) throws Exception { + Manifest manifest = archive.getManifest(); + String libDir = manifest.getMainAttributes().getValue(MAIN_LIB_DIR); + String relativePath = rootJarFile.isDirectory() ? rootJarFile.getPath() : rootJarFile.getParent(); + libDir = FilesUtils.resolveRelativePath(relativePath, libDir); + File libJarDir = new File(libDir); + if(libJarDir.exists()){ + List libIndexes = getLibIndexes(manifest); + addLibJarFile(libJarDir, libIndexes, classLoader); + } else { + throw new IllegalStateException("主程序依赖目录不存在: " + libDir); + } + } + + private List getLibIndexes(Manifest manifest){ + String libIndexes = manifest.getMainAttributes().getValue(MAIN_LIB_INDEXES); + if(ObjectUtils.isEmpty(libIndexes)){ + return Collections.emptyList(); + } + String[] indexSplit = libIndexes.split(MAIN_LIB_INDEXES_SPLIT); + List indexes = new ArrayList<>(indexSplit.length); + for (String index : indexSplit) { + if(ObjectUtils.isEmpty(index)){ + continue; + } + indexes.add(index); + } + if(indexes.isEmpty()){ + throw new IllegalStateException("主程序依赖包未发现!"); + } + return indexes; + } + + private void addLibJarFile(File rootFile, List libIndexes, GenericClassLoader classLoader) throws Exception { + Set linIndexes = new HashSet<>(libIndexes); + File[] listFiles = rootFile.listFiles(new FileFilter() { + @Override + public boolean accept(File pathname) { + return linIndexes.contains(pathname.getName()); + } + }); + if(listFiles == null || listFiles.length == 0){ + return; + } + for (File listFile : listFiles) { + classLoader.addResource(listFile); + } + } + + + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarProgramLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarProgramLauncher.java index f9bd4e95fc6bd182ac14c65796e5e63899372ea4..c423b92636ca0caf45c6073f6338385188a62e20 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarProgramLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarProgramLauncher.java @@ -21,7 +21,7 @@ import com.gitee.starblues.loader.archive.ExplodedArchive; import com.gitee.starblues.loader.archive.JarFileArchive; import com.gitee.starblues.loader.classloader.GenericClassLoader; import com.gitee.starblues.loader.classloader.resource.loader.JarResourceLoader; -import com.gitee.starblues.loader.classloader.resource.storage.ResourceStorage; +import com.gitee.starblues.loader.classloader.resource.loader.MainJarResourceLoader; import com.gitee.starblues.loader.launcher.runner.MethodRunner; import java.io.File; @@ -30,18 +30,15 @@ import java.net.URL; import java.util.Iterator; import java.util.Objects; +import static com.gitee.starblues.loader.LoaderConstant.*; + /** * 主程序jar in jar 模式启动者 * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ public class MainJarProgramLauncher extends MainProgramLauncher{ - private static final String PROD_CLASSES_PATH = "classes/"; - private static final String PROD_CLASSES_URL_SIGN = "/classes!/"; - - private static final String PROD_LIB_PATH = "lib/"; - private final static Archive.EntryFilter ENTRY_FILTER = (entry)->{ String name = entry.getName(); return name.startsWith(PROD_CLASSES_PATH) || name.startsWith(PROD_LIB_PATH); @@ -86,17 +83,4 @@ public class MainJarProgramLauncher extends MainProgramLauncher{ } } - private static class MainJarResourceLoader extends JarResourceLoader { - - public MainJarResourceLoader(URL url) throws Exception { - super(url); - } - - @Override - protected String resolveName(String name) { - return name.replace(PROD_CLASSES_PATH, ""); - } - } - - } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainProgramLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainProgramLauncher.java index 3c9a1bda6aae04e4a4d61ebf0e7bacaf5c6e21cd..00ce88a3ddc352bd49c6945c7e86852240ea0399 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainProgramLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainProgramLauncher.java @@ -31,7 +31,7 @@ import java.net.URLClassLoader; * @author starBlues * @version 3.0.0 */ -public class MainProgramLauncher extends AbstractLauncher{ +public class MainProgramLauncher extends AbstractMainLauncher{ public static final String MAIN_CLASS_LOADER_NAME = "MainProgramLauncherClassLoader"; 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 5e79f5af1c80f5171010394fcc7aecc458920003..47fc054e8ec43cabcfaf691b9478404907798387 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 @@ -25,8 +25,11 @@ 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 @@ -34,7 +37,6 @@ import java.util.jar.Manifest; */ public class SpringMainProdBootstrap { - private static final String START_CLASS = "Start-Class"; public static void main(String[] args) throws Exception { JarFile.registerUrlProtocolHandler(); @@ -44,19 +46,27 @@ public class SpringMainProdBootstrap { private void run(String[] args) throws Exception{ File rootJarFile = getRootJarFile(); String startClass = null; + String mainPackageType; try (JarFile jarFile = new JarFile(rootJarFile)){ Manifest manifest = jarFile.getManifest(); IllegalStateException exception = new IllegalStateException("当前启动包非法包!"); if(manifest == null || manifest.getMainAttributes() == null){ throw exception; } - startClass = manifest.getMainAttributes().getValue(START_CLASS); + Attributes mainAttributes = manifest.getMainAttributes(); + startClass = mainAttributes.getValue(START_CLASS); if (ObjectUtils.isEmpty(startClass)) { throw exception; } + mainPackageType = mainAttributes.getValue(MAIN_PACKAGE_TYPE); } MethodRunner methodRunner = new MethodRunner(startClass, SpringMainBootstrap.SPRING_BOOTSTRAP_RUN_METHOD, args); - Launcher launcher = new MainJarProgramLauncher(methodRunner, rootJarFile); + Launcher launcher; + if(Objects.equals(mainPackageType, MAIN_PACKAGE_TYPE_JAR_OUTER)){ + launcher = new MainJarOuterProgramLauncher(methodRunner, rootJarFile); + } else { + launcher = new MainJarProgramLauncher(methodRunner, rootJarFile); + } launcher.run(args); } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/FilesUtils.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/FilesUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..1b17a8b2434e8d85c9d02e926f613e5db6297da4 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/FilesUtils.java @@ -0,0 +1,127 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.loader.utils; + +import com.gitee.starblues.loader.LoaderConstant; + +import java.io.File; +import java.io.IOException; + +/** + * 文件工具类 + * + * @author starBlues + * @version 3.0.2 + */ +public class FilesUtils { + + /** + * 获取存在的文件 + * + * @param pathStr 文件路径 + * @return File + */ + public static File getExistFile(String pathStr){ + File file = new File(pathStr); + if(file.exists()){ + return file; + } + return null; + } + + + /** + * 拼接file路径 + * + * @param paths 拼接的路径 + * @return 拼接的路径 + */ + public static String joiningFilePath(String ...paths){ + if(paths == null || paths.length == 0){ + return ""; + } + StringBuilder stringBuilder = new StringBuilder(); + int length = paths.length; + for (int i = 0; i < length; i++) { + String path = paths[i]; + if(ObjectUtils.isEmpty(path)) { + continue; + } + if(i > 0){ + if(path.startsWith(File.separator) || path.startsWith("/") || + path.startsWith("\\") || path.startsWith("//")){ + stringBuilder.append(path); + } else { + stringBuilder.append(File.separator).append(path); + } + } else { + stringBuilder.append(path); + } + } + + return stringBuilder.toString(); + } + + public static File createFile(String path) throws IOException { + try { + File file = new File(path); + File parentFile = file.getParentFile(); + if(!parentFile.exists()){ + if(!parentFile.mkdirs()){ + throw new IOException("Create " + parentFile + " dir error"); + } + } + if(file.createNewFile()){ + return file; + } + throw new IOException("Create " + path + " file error"); + } catch (Exception e){ + throw new IOException("Create " + path + " file error"); + } + } + + + /** + * 解决相对路径 + * @param rootPath 根路径 + * @param relativePath 以 ~ 开头的相对路径 + * @return 处理后的路径 + */ + public static String resolveRelativePath(String rootPath, String relativePath){ + if(ObjectUtils.isEmpty(relativePath)){ + return relativePath; + } + if(isRelativePath(relativePath)){ + return joiningFilePath(rootPath, relativePath.replaceFirst(LoaderConstant.RELATIVE_SIGN, "")); + } else { + return relativePath; + } + } + + /** + * 是否是相对路径 + * @param path 路径 + * @return true 为相对路径, false 未非相对路径 + */ + public static boolean isRelativePath(String path){ + if(ObjectUtils.isEmpty(path)){ + return false; + } + return path.startsWith(LoaderConstant.RELATIVE_SIGN); + } + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/ResourceUtils.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/ResourceUtils.java index 30dceae5a6153490ea8d2b46d91c68fe309dbba8..d128670638488f86f1c97e112ef390730c52e6c3 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/ResourceUtils.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/ResourceUtils.java @@ -16,6 +16,7 @@ package com.gitee.starblues.loader.utils; +import java.io.File; import java.net.URL; /** @@ -49,6 +50,15 @@ public class ResourceUtils { || (URL_PROTOCOL_JAR_FILE.equals(protocol) || extensionIsJar); } + /** + * 是否为jar文件 + * @param file file + * @return boolean + */ + public static boolean isJarFile(File file) { + return file.getName().toLowerCase().endsWith(JAR_FILE_EXTENSION); + } + /** * 是否为zip文件 * @param url url diff --git a/spring-brick-maven-packager/pom.xml b/spring-brick-maven-packager/pom.xml index 1037304504d7c5bc91621fed7d3757c7a4eece2f..10d4c6b7621bbea7e018886b9389165abda98a05 100644 --- a/spring-brick-maven-packager/pom.xml +++ b/spring-brick-maven-packager/pom.xml @@ -7,7 +7,7 @@ spring-brick-parent com.gitee.starblues - 3.0.1 + 3.0.2 spring-brick-maven-packager diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/AbstractPackagerMojo.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/AbstractPackagerMojo.java index f169726e23a54ab8a0ac626da84f0588b15f66e3..b24c52523d6a5463f6ec4ca63203420ed9d077d3 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/AbstractPackagerMojo.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/AbstractPackagerMojo.java @@ -54,6 +54,9 @@ public abstract class AbstractPackagerMojo extends AbstractDependencyFilterMojo{ @Parameter(property = "spring-brick-packager.loadMainResourcePattern", required = false) private LoadMainResourcePattern loadMainResourcePattern; + @Parameter(property = "spring-brick-packager.includeSystemScope", defaultValue = "true", required = false) + private Boolean includeSystemScope; + @Override public final void execute() throws MojoExecutionException, MojoFailureException { if(Constant.isPom(this.getProject().getPackaging())){ diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/BasicRepackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/BasicRepackager.java index ae6946000d87c1b95cce07c4c2432e50021c2cd7..bb1f79012dfec24951f3ea77c9c2bd8c19fa2ce6 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/BasicRepackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/BasicRepackager.java @@ -16,19 +16,16 @@ package com.gitee.starblues.plugin.pack; -import com.gitee.starblues.common.AbstractDependencyPlugin; -import com.gitee.starblues.common.ManifestKey; -import com.gitee.starblues.common.PackageStructure; -import com.gitee.starblues.common.PackageType; -import com.gitee.starblues.plugin.pack.dev.DevConfig; -import com.gitee.starblues.plugin.pack.utils.CommonUtils; +import com.gitee.starblues.common.*; import com.gitee.starblues.utils.FilesUtils; import com.gitee.starblues.utils.ObjectUtils; import lombok.Getter; import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; import java.io.File; import java.io.FileOutputStream; @@ -187,7 +184,7 @@ public class BasicRepackager implements Repackager{ properties.put(PLUGIN_VERSION, pluginInfo.getVersion()); properties.put(PLUGIN_PATH, getPluginPath()); - String resourcesDefineFilePath = writeResourcesDefineFile(); + String resourcesDefineFilePath = writeResourcesDefineFile(getResourcesDefineContent()); if(!ObjectUtils.isEmpty(resourcesDefineFilePath)){ properties.put(PLUGIN_RESOURCES_CONFIG, resourcesDefineFilePath); } @@ -264,10 +261,9 @@ public class BasicRepackager implements Repackager{ return repackageMojo.getProject().getBuild().getOutputDirectory(); } - protected String writeResourcesDefineFile() throws Exception{ + protected String writeResourcesDefineFile(String resourcesDefineContent) throws Exception{ resourcesDefineFile = createResourcesDefineFile(); - writeDependenciesIndex(); - writeLoadMainResources(); + FileUtils.write(resourcesDefineFile, resourcesDefineContent, CHARSET_NAME, true); return resourcesDefineFile.getPath(); } @@ -276,15 +272,44 @@ public class BasicRepackager implements Repackager{ return FilesUtils.createFile(path); } - protected void writeDependenciesIndex() throws Exception { + protected String getResourcesDefineContent() throws Exception { + String dependenciesIndex = getDependenciesIndex(); + String loadMainResources = getLoadMainResources(); + boolean indexIsEmpty = ObjectUtils.isEmpty(dependenciesIndex); + boolean resourceIsEmpty = ObjectUtils.isEmpty(loadMainResources); + + if(!indexIsEmpty && !resourceIsEmpty){ + return dependenciesIndex + "\n" + loadMainResources; + } else if(!indexIsEmpty){ + return dependenciesIndex; + } else if(!resourceIsEmpty){ + return loadMainResources; + } else { + return ""; + } + } + + protected String getDependenciesIndex() throws Exception { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(RESOURCES_DEFINE_DEPENDENCIES).append("\n"); Set libIndex = getDependenciesIndexSet(); for (String index : libIndex) { stringBuilder.append(index).append("\n"); } - String content = stringBuilder.toString(); - FileUtils.write(resourcesDefineFile, content, CHARSET_NAME, true); + return stringBuilder.toString(); + } + + protected String getLoadMainResources(){ + LoadMainResourcePattern loadMainResourcePattern = repackageMojo.getLoadMainResourcePattern(); + if(loadMainResourcePattern == null){ + return null; + } + String[] includes = loadMainResourcePattern.getIncludes(); + String[] excludes = loadMainResourcePattern.getExcludes(); + StringBuilder stringBuilder = new StringBuilder(); + addLoadMainResources(stringBuilder, RESOURCES_DEFINE_LOAD_MAIN_INCLUDES, includes); + addLoadMainResources(stringBuilder, RESOURCES_DEFINE_LOAD_MAIN_EXCLUDES, excludes); + return stringBuilder.toString(); } protected Set getDependenciesIndexSet() throws Exception { @@ -303,27 +328,6 @@ public class BasicRepackager implements Repackager{ return artifact.getFile().getPath() + repackageMojo.resolveLoadToMain(artifact); } - protected void writeLoadMainResources() throws Exception { - String loadMainResources = getLoadMainResources(); - if(ObjectUtils.isEmpty(loadMainResources)){ - return; - } - FileUtils.write(resourcesDefineFile, loadMainResources, CHARSET_NAME, true); - } - - protected String getLoadMainResources(){ - LoadMainResourcePattern loadMainResourcePattern = repackageMojo.getLoadMainResourcePattern(); - if(loadMainResourcePattern == null){ - return null; - } - String[] includes = loadMainResourcePattern.getIncludes(); - String[] excludes = loadMainResourcePattern.getExcludes(); - StringBuilder stringBuilder = new StringBuilder(); - addLoadMainResources(stringBuilder, RESOURCES_DEFINE_LOAD_MAIN_INCLUDES, includes); - addLoadMainResources(stringBuilder, RESOURCES_DEFINE_LOAD_MAIN_EXCLUDES, excludes); - return stringBuilder.toString(); - } - private void addLoadMainResources(StringBuilder stringBuilder, String header, String[] patterns){ if(ObjectUtils.isEmpty(patterns)){ return; @@ -348,7 +352,8 @@ public class BasicRepackager implements Repackager{ * @return 返回true表示被过滤掉 */ protected boolean filterArtifact(Artifact artifact){ - return Constant.scopeFilter(artifact.getScope()); + return Constant.filterMainTypeArtifact(artifact) || + Constant.filterArtifact(artifact, repackageMojo.getIncludeSystemScope()); } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/Constant.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/Constant.java index 3358372d3de7bf558eac1c7f0e701f525b0d5593..a08d41bc78b9ac8b07b2c6c74e22b7ec1a76cb5c 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/Constant.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/Constant.java @@ -16,6 +16,8 @@ package com.gitee.starblues.plugin.pack; +import org.apache.maven.artifact.Artifact; + /** * 静态类 * @author starBlues @@ -26,8 +28,11 @@ public class Constant { public static final String PACKAGING_POM = "pom"; public static final String SCOPE_PROVIDED = "provided"; public static final String SCOPE_COMPILE = "compile"; + public static final String SCOPE_SYSTEM = "system"; public static final String SCOPE_TEST = "test"; + public static final String MAVEN_MAIN_TYPE = "main"; + public static final String MODE_MAIN = "main"; public static final String MODE_DEV = "dev"; public static final String MODE_PROD = "prod"; @@ -38,9 +43,29 @@ public class Constant { return PACKAGING_POM.equalsIgnoreCase(packageType); } + public static boolean filterArtifact(Artifact artifact, Boolean includeSystemScope){ + boolean scopeFilter = Constant.scopeFilter(artifact.getScope()); + if(scopeFilter){ + return true; + } + if(Constant.isSystemScope(artifact.getScope())){ + return includeSystemScope == null || !includeSystemScope; + } + return false; + } + + public static boolean filterMainTypeArtifact(Artifact artifact){ + // 配置了为main的依赖, 则对其过滤 + return MAVEN_MAIN_TYPE.equalsIgnoreCase(artifact.getType()); + } + public static boolean scopeFilter(String scope){ return SCOPE_PROVIDED.equalsIgnoreCase(scope) || SCOPE_TEST.equalsIgnoreCase(scope); } + public static boolean isSystemScope(String scope){ + return SCOPE_SYSTEM.equalsIgnoreCase(scope); + } + } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarNestPackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarNestPackager.java index 429c4b51ea9421f6154b58341839f304e8f89388..e866c3d373098fadf8431acb04ee3ee4b47f1139 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarNestPackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarNestPackager.java @@ -16,6 +16,7 @@ package com.gitee.starblues.plugin.pack.main; +import com.gitee.starblues.common.PackageType; import com.gitee.starblues.plugin.pack.Constant; import com.gitee.starblues.plugin.pack.RepackageMojo; import com.gitee.starblues.plugin.pack.Repackager; @@ -38,7 +39,7 @@ import static com.gitee.starblues.common.ManifestKey.*; /** * 嵌套jar打包 * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ public class JarNestPackager implements Repackager { @@ -81,6 +82,7 @@ public class JarNestPackager implements Repackager { attributes.putValue(MANIFEST_VERSION, MANIFEST_VERSION_1_0); attributes.putValue(START_CLASS, mainConfig.getMainClass()); attributes.putValue(MAIN_CLASS, MAIN_CLASS_VALUE); + attributes.putValue(MAIN_PACKAGE_TYPE, PackageType.MAIN_PACKAGE_TYPE_JAR); return manifest; } @@ -106,7 +108,7 @@ public class JarNestPackager implements Repackager { } protected boolean filterArtifact(Artifact artifact) { - return Constant.scopeFilter(artifact.getScope()); + return Constant.filterArtifact(artifact, repackageMojo.getIncludeSystemScope()); } protected String createLibEntry() throws Exception { @@ -115,6 +117,4 @@ public class JarNestPackager implements Repackager { return libDirEntryName; } - - } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarOuterPackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarOuterPackager.java index 3eef4d5b3390adb607b7508b826698bc38f144d1..3e24c97d77bbffed6a2202e95c3f6bed2ae07c1a 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarOuterPackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarOuterPackager.java @@ -16,32 +16,34 @@ package com.gitee.starblues.plugin.pack.main; -import com.gitee.starblues.common.ManifestKey; import com.gitee.starblues.common.PackageStructure; +import com.gitee.starblues.common.PackageType; +import com.gitee.starblues.plugin.pack.utils.CommonUtils; import com.gitee.starblues.utils.FilesUtils; +import com.gitee.starblues.utils.ObjectUtils; import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import java.io.File; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; import java.util.Set; import java.util.jar.Attributes; import java.util.jar.Manifest; import static com.gitee.starblues.common.ManifestKey.*; -import static com.gitee.starblues.common.ManifestKey.MANIFEST_VERSION_1_0; /** * jar 外置包 * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ public class JarOuterPackager extends JarNestPackager { - private final List dependenciesName = new ArrayList<>(); + private static final String LIB_INDEXES_SPLIT = " "; + + private final Set dependencyIndexNames = new HashSet<>(); public JarOuterPackager(MainRepackager mainRepackager) { super(mainRepackager); @@ -58,7 +60,7 @@ public class JarOuterPackager extends JarNestPackager { @Override protected void writeClasses() throws Exception { String buildDir = repackageMojo.getProject().getBuild().getOutputDirectory(); - packageJar.copyDirToPackage(new File(buildDir), ""); + packageJar.copyDirToPackage(new File(buildDir), null); } private String createRootDir() throws MojoFailureException{ @@ -81,16 +83,30 @@ public class JarOuterPackager extends JarNestPackager { Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.putValue(MANIFEST_VERSION, MANIFEST_VERSION_1_0); - attributes.remove(START_CLASS); - attributes.putValue(MAIN_CLASS, mainConfig.getMainClass()); - if(dependenciesName.isEmpty()){ - return manifest; + attributes.putValue(START_CLASS, mainConfig.getMainClass()); + attributes.putValue(MAIN_CLASS, MAIN_CLASS_VALUE); + attributes.putValue(MAIN_PACKAGE_TYPE, PackageType.MAIN_PACKAGE_TYPE_JAR_OUTER); + String libDir = PackageStructure.LIB_NAME; + if(!ObjectUtils.isEmpty(mainConfig.getLibDir())){ + libDir = mainConfig.getLibDir(); } - String classPathStr = String.join(" ", dependenciesName); - attributes.putValue(ManifestKey.CLASS_PATH, classPathStr); + attributes.putValue(MAIN_LIB_DIR, libDir); + attributes.putValue(MAIN_LIB_INDEXES, getLibIndexes()); return manifest; } + + private String getLibIndexes() throws Exception { + if(dependencyIndexNames.isEmpty()){ + return ""; + } + StringBuilder libName = new StringBuilder(); + for (String dependencyIndexName : dependencyIndexNames) { + libName.append(dependencyIndexName).append(LIB_INDEXES_SPLIT); + } + return libName.toString(); + } + @Override protected void writeDependencies() throws Exception { Set dependencies = repackageMojo.getSourceDependencies(); @@ -98,12 +114,16 @@ public class JarOuterPackager extends JarNestPackager { if(filterArtifact(artifact)){ continue; } - File artifactFile = artifact.getFile(); - String targetFilePath = FilesUtils.joiningFilePath( - mainConfig.getOutputDirectory(), PackageStructure.LIB_NAME, artifactFile.getName()); - - FileUtils.copyFile(artifactFile, new File(targetFilePath)); - dependenciesName.add(PackageStructure.LIB_NAME + "/" + artifactFile.getName()); + if(CommonUtils.isPluginFrameworkLoader(artifact)){ + // 本框架loader依赖 + packageJar.copyZipToPackage(artifact.getFile()); + } else { + File artifactFile = artifact.getFile(); + String targetFilePath = FilesUtils.joiningFilePath( + mainConfig.getOutputDirectory(), PackageStructure.LIB_NAME, artifactFile.getName()); + FileUtils.copyFile(artifactFile, new File(targetFilePath)); + dependencyIndexNames.add(artifactFile.getName()); + } } } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/MainConfig.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/MainConfig.java index 8c5e58ba724b54965d8b9785ff2b29f0e55eec8b..f8ee0ee39b6335e6ab73e28761ff38d66e8dab15 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/MainConfig.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/MainConfig.java @@ -17,6 +17,7 @@ package com.gitee.starblues.plugin.pack.main; +import com.gitee.starblues.common.PackageStructure; import lombok.Data; import org.apache.maven.plugins.annotations.Parameter; import com.gitee.starblues.plugin.pack.Constant; @@ -48,10 +49,16 @@ public class MainConfig { */ private String fileName; + /** + * 依赖包所在目录 + */ + private String libDir; + /** * 输出文件目录。默认target */ private String outputDirectory; + } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/DirProdRepackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/DirProdRepackager.java index bc06e6281e2690a84b0808c0490f3f31f086cc1f..5a3bd8ec2d0588c12a07bd1938b18869d4121062 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/DirProdRepackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/DirProdRepackager.java @@ -16,14 +16,12 @@ package com.gitee.starblues.plugin.pack.prod; -import com.gitee.starblues.common.ManifestKey; -import com.gitee.starblues.common.PackageStructure; -import com.gitee.starblues.common.PackageType; -import com.gitee.starblues.common.PluginDescriptorKey; +import com.gitee.starblues.common.*; import com.gitee.starblues.plugin.pack.RepackageMojo; import com.gitee.starblues.plugin.pack.dev.DevRepackager; import com.gitee.starblues.plugin.pack.utils.CommonUtils; import com.gitee.starblues.utils.FilesUtils; +import com.gitee.starblues.utils.ObjectUtils; import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; @@ -108,7 +106,7 @@ public class DirProdRepackager extends DevRepackager { Manifest manifest = super.getManifest(); Attributes attributes = manifest.getMainAttributes(); attributes.putValue(ManifestKey.PLUGIN_META_PATH, PROD_PLUGIN_META_PATH); - attributes.putValue(ManifestKey.PLUGIN_PACKAGE_TYPE, PackageType.PLUGIN_PACKAGE_TYPE_ZIP_OUTER); + attributes.putValue(ManifestKey.PLUGIN_PACKAGE_TYPE, PackageType.PLUGIN_PACKAGE_TYPE_DIR); return manifest; } @@ -117,6 +115,11 @@ public class DirProdRepackager extends DevRepackager { Properties properties = super.createPluginMetaInfo(); properties.put(PluginDescriptorKey.PLUGIN_PATH, CLASSES_NAME); properties.put(PluginDescriptorKey.PLUGIN_RESOURCES_CONFIG, PROD_RESOURCES_DEFINE_PATH); + String libDir = prodConfig.getLibDir(); + if(ObjectUtils.isEmpty(libDir)){ + libDir = Constants.RELATIVE_SIGN + PackageStructure.PROD_LIB_PATH; + } + properties.put(PluginDescriptorKey.PLUGIN_LIB_DIR, libDir); return properties; } @@ -139,8 +142,7 @@ public class DirProdRepackager extends DevRepackager { } File artifactFile = artifact.getFile(); FileUtils.copyFile(artifactFile, new File(FilesUtils.joiningFilePath(libDir, artifactFile.getName()))); - dependencyIndexNames.add(PackageStructure.PROD_LIB_PATH + artifactFile.getName() - + repackageMojo.resolveLoadToMain(artifact)); + dependencyIndexNames.add(artifactFile.getName() + repackageMojo.resolveLoadToMain(artifact)); } return dependencyIndexNames; } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/JarOuterProdRepackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/JarOuterProdRepackager.java index 0132c3bbbe930585729f2512ab1ee8aad744082b..c6951b850355713919b323cedb7a011c39e7addf 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/JarOuterProdRepackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/JarOuterProdRepackager.java @@ -50,4 +50,5 @@ public class JarOuterProdRepackager extends ZipOuterProdRepackager { return manifest; } + } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ProdConfig.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ProdConfig.java index 3943b38a23dcaac1410b19cc347db98fbfd7fdb6..529f03fbb834c9258010dfb042623e154d1ebb1a 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ProdConfig.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ProdConfig.java @@ -23,7 +23,7 @@ import org.apache.maven.plugins.annotations.Parameter; /** * 生产环境打包配置 * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ @Data public class ProdConfig { @@ -50,4 +50,9 @@ public class ProdConfig { */ private String outputDirectory; + /** + * jar-outer、zip-outer、dir 类型可指定依赖包目录 + */ + private String libDir; + } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ZipOuterProdRepackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ZipOuterProdRepackager.java index d3938924ebbfb6d5b1b9c5f1c22124a6d391a37d..7b84adae340d5b775a10aef4f002b8b609151c84 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ZipOuterProdRepackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ZipOuterProdRepackager.java @@ -19,6 +19,7 @@ package com.gitee.starblues.plugin.pack.prod; import com.gitee.starblues.common.ManifestKey; import com.gitee.starblues.common.PackageType; import com.gitee.starblues.common.PluginDescriptorKey; +import com.gitee.starblues.plugin.pack.Constant; import com.gitee.starblues.plugin.pack.RepackageMojo; import com.gitee.starblues.plugin.pack.utils.PackageJar; import com.gitee.starblues.plugin.pack.utils.PackageZip; @@ -30,6 +31,8 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import java.io.File; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Properties; import java.util.Set; @@ -100,4 +103,24 @@ public class ZipOuterProdRepackager extends DirProdRepackager { properties.put(PluginDescriptorKey.PLUGIN_PATH, packageZip.getFileName()); return properties; } + @Override + protected void writeManifest(Manifest manifest) throws Exception { + packageZip.writeManifest(manifest); + } + + @Override + protected String writePluginMetaInfo(Properties properties) throws Exception { + packageZip.write(PROD_PLUGIN_META_PATH, outputStream->{ + properties.store(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), + Constant.PLUGIN_METE_COMMENTS); + }); + return PROD_PLUGIN_META_PATH; + } + + + @Override + protected String writeResourcesDefineFile(String resourcesDefineContent) throws Exception { + packageZip.write(PROD_RESOURCES_DEFINE_PATH, resourcesDefineContent); + return PROD_RESOURCES_DEFINE_PATH; + } } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ZipProdRepackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ZipProdRepackager.java index 75f005303110b9fe0e4e577fdc597a8cbf4d125a..dacd996be1cd34ce27de958b9d667cba793f112e 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ZipProdRepackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/prod/ZipProdRepackager.java @@ -17,6 +17,7 @@ package com.gitee.starblues.plugin.pack.prod; import com.gitee.starblues.common.ManifestKey; +import com.gitee.starblues.common.PackageStructure; import com.gitee.starblues.common.PackageType; import com.gitee.starblues.common.PluginDescriptorKey; import com.gitee.starblues.plugin.pack.Constant; @@ -106,11 +107,6 @@ public class ZipProdRepackager extends DevRepackager { return CLASSES_NAME + SEPARATOR; } - @Override - protected String getLibIndex(Artifact artifact){ - return PROD_LIB_PATH + artifact.getFile().getName() + repackageMojo.resolveLoadToMain(artifact); - } - @Override protected boolean filterArtifact(Artifact artifact) { return Constant.scopeFilter(artifact.getScope()); @@ -134,6 +130,7 @@ public class ZipProdRepackager extends DevRepackager { protected Properties createPluginMetaInfo() throws Exception { Properties properties = super.createPluginMetaInfo(); properties.put(PluginDescriptorKey.PLUGIN_RESOURCES_CONFIG, PROD_RESOURCES_DEFINE_PATH); + properties.put(PluginDescriptorKey.PLUGIN_LIB_DIR, PROD_LIB_PATH); return properties; } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/utils/PackageZip.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/utils/PackageZip.java index 20d3931ab04d140df7811bd2aef6a89ff157d465..7867941ff37cd64c8adc15c83d26e2879b13ac7a 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/utils/PackageZip.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/utils/PackageZip.java @@ -178,6 +178,12 @@ public class PackageZip implements Closeable{ outputStream.closeArchiveEntry(); } + public void write(String name, String content) throws Exception { + outputStream.putArchiveEntry(getArchiveEntry(name)); + IOUtils.write(content, outputStream, CHARSET_NAME); + outputStream.closeArchiveEntry(); + } + public void write(String name, Writer writer) throws Exception { outputStream.putArchiveEntry(getArchiveEntry(name)); writer.write(outputStream); diff --git a/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml b/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml index a0cff0ffe4330182164e4eca3c160e0e74be813e..ebe06e796278073f4d6a73fc560a66a0b5b8e87d 100644 --- a/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml +++ b/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml @@ -6,7 +6,7 @@ Spring Boot Plugin Maven Packager com.gitee.starblues spring-brick-maven-packager - 3.0.1 + 3.0.2 spring-brick-packager false true @@ -141,6 +141,14 @@ true 加密配置 + + includeSystemScope + boolean + 3.0.2 + false + true + 是否包含scope类型为system的依赖 + @@ -155,6 +163,7 @@ + diff --git a/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml b/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml index a0cff0ffe4330182164e4eca3c160e0e74be813e..ebe06e796278073f4d6a73fc560a66a0b5b8e87d 100644 --- a/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml +++ b/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml @@ -6,7 +6,7 @@ Spring Boot Plugin Maven Packager com.gitee.starblues spring-brick-maven-packager - 3.0.1 + 3.0.2 spring-brick-packager false true @@ -141,6 +141,14 @@ true 加密配置 + + includeSystemScope + boolean + 3.0.2 + false + true + 是否包含scope类型为system的依赖 + @@ -155,6 +163,7 @@ + diff --git a/spring-brick/pom.xml b/spring-brick/pom.xml index 385671a2241abbe5716da622f92d184f8713ecb2..e0c702dbc4442e60d3613bd95a2087a2253f29a9 100644 --- a/spring-brick/pom.xml +++ b/spring-brick/pom.xml @@ -7,7 +7,7 @@ spring-brick-parent com.gitee.starblues - 3.0.1 + 3.0.2 spring-brick diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java b/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java index a94344aae2aca6fa9b7191e24e5d360c629d0d91..3d6fe0b3133e86344cb62065a667998581f77908 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginManager.java @@ -49,7 +49,7 @@ import java.util.stream.Collectors; /** * 抽象的插件管理者 * @author starBlues - * @version 3.0.1 + * @version 3.0.2 */ public class DefaultPluginManager implements PluginManager{ @@ -121,6 +121,7 @@ public class DefaultPluginManager implements PluginManager{ } pluginListenerFactory = createPluginListenerFactory(); try { + pluginListenerFactory = createPluginListenerFactory(); if(ObjectUtils.isEmpty(pluginRootDirs)){ log.warn("插件根目录为空, 无法加载插件."); return Collections.emptyList(); @@ -131,6 +132,7 @@ public class DefaultPluginManager implements PluginManager{ return Collections.emptyList(); } Map pluginInfoMap = new LinkedHashMap<>(scanPluginPaths.size()); + boolean findException = false; for (Path path : scanPluginPaths) { try { PluginInsideInfo pluginInfo = loadPlugin(path, false); @@ -140,12 +142,13 @@ public class DefaultPluginManager implements PluginManager{ pluginListenerFactory.loadSuccess(pluginInfoFace); pluginInfoMap.put(pluginInfo.getPluginId(), pluginInfoFace); } - } catch (PluginException e) { + } catch (Throwable e) { pluginListenerFactory.loadFailure(path, e); log.error("加载插件包失败: {}. {}", path, e.getMessage(), e); + findException = true; } } - if(pluginInfoMap.isEmpty()){ + if(!findException && pluginInfoMap.isEmpty()){ printOfNotFoundPlugins(); } return getSortPlugin(pluginInfoMap); @@ -165,8 +168,8 @@ public class DefaultPluginManager implements PluginManager{ basicChecker.checkPath(pluginPath); PluginDescriptor pluginDescriptor = pluginDescriptorLoader.load(pluginPath); return pluginDescriptor != null; - } catch (Exception e) { - log.error("插件jar包校验失败: {}" , pluginPath, e); + } catch (Throwable e) { + log.error("插件包校验失败: {}" , pluginPath, e); return false; } } @@ -208,7 +211,7 @@ public class DefaultPluginManager implements PluginManager{ pluginListenerFactory.loadFailure(pluginPath, new PluginException("Not found PluginInsideInfo")); return null; } - } catch (Exception e) { + } catch (Throwable e) { PluginException pluginException = PluginException.getPluginException(e, () -> { throw new PluginException("插件包加载失败: " + sourcePluginPath, e); }); @@ -243,13 +246,13 @@ public class DefaultPluginManager implements PluginManager{ pluginListenerFactory.startSuccess(pluginInfo); log.info("插件[{}]安装成功", MsgUtils.getPluginUnique(pluginInsideInfo.getPluginDescriptor())); return pluginInsideInfo.toPluginInfo(); - } catch (Exception e){ + } catch (Throwable e){ if(e instanceof PluginDisabledException){ throw (PluginDisabledException)e; } PluginException pluginException = PluginException.getPluginException(e, ()-> { unLoad(loadPluginInfo.getPluginId()); - throw new PluginException("插件包[ " + pluginPath + " ]安装失败: " + e.getMessage(), e); + throw new PluginException("插件包[ " + pluginPath + " ]安装: " + e.getMessage(), e); }); pluginListenerFactory.startFailure(pluginInfo, pluginException); throw pluginException; @@ -268,7 +271,7 @@ public class DefaultPluginManager implements PluginManager{ try { stop(wrapperInside); pluginListenerFactory.stopSuccess(pluginInfo); - } catch (Exception e) { + } catch (Throwable e) { PluginException pluginException = PluginException.getPluginException(e, ()-> new PluginException("停止", pluginId, e)); pluginListenerFactory.stopFailure(pluginInfo, pluginException); @@ -312,9 +315,9 @@ public class DefaultPluginManager implements PluginManager{ install(pluginPath, unpackPlugin); log.info("更新插件[{}]成功", MsgUtils.getPluginUnique(upgradePluginDescriptor)); return upgradePlugin; - } catch (Exception e){ + } catch (Throwable e){ throw PluginException.getPluginException(e, ()-> - new PluginException(upgradePluginDescriptor, "更新失败", e)); + new PluginException(upgradePluginDescriptor, "更新", e)); } } @@ -333,9 +336,9 @@ public class DefaultPluginManager implements PluginManager{ log.info("插件[{}]启动成功", MsgUtils.getPluginUnique(pluginInsideInfo.getPluginDescriptor())); pluginListenerFactory.startSuccess(pluginInfo); return pluginInfo; - } catch (Exception e){ + } catch (Throwable e){ PluginException pluginException = PluginException.getPluginException(e, - ()-> new PluginException(pluginInsideInfo.getPluginDescriptor(), "启动失败", e)); + ()-> new PluginException(pluginInsideInfo.getPluginDescriptor(), "启动", e)); pluginListenerFactory.startFailure(pluginInfo, pluginException); throw pluginException; } @@ -356,9 +359,9 @@ public class DefaultPluginManager implements PluginManager{ log.info("停止插件[{}]成功", MsgUtils.getPluginUnique(pluginInsideInfo.getPluginDescriptor())); pluginListenerFactory.stopSuccess(pluginInfo); return pluginInfo; - } catch (Exception e) { + } catch (Throwable e) { PluginException pluginException = PluginException.getPluginException(e, - () -> new PluginException(pluginInsideInfo.getPluginDescriptor(), "停止失败", e)); + () -> new PluginException(pluginInsideInfo.getPluginDescriptor(), "停止", e)); pluginListenerFactory.stopFailure(pluginInfo, pluginException); throw pluginException; } @@ -419,7 +422,7 @@ public class DefaultPluginManager implements PluginManager{ protected PluginInsideInfo loadFromPath(Path pluginPath) { try { basicChecker.checkPath(pluginPath); - } catch (Exception e) { + } catch (Throwable e) { throw PluginException.getPluginException(e, ()-> { return new PluginException("非法插件包. " + e.getMessage(), e); }); @@ -438,7 +441,7 @@ public class DefaultPluginManager implements PluginManager{ pluginInsideInfo.setPluginState(PluginState.LOADED); } return pluginInsideInfo; - } catch (Exception e){ + } catch (Throwable e){ throw PluginException.getPluginException(e, ()-> new PluginException("加载插件失败")); } } @@ -622,8 +625,8 @@ public class DefaultPluginManager implements PluginManager{ File file = new File(""); String absolutePath = file.getAbsolutePath(); return path.stream() - .filter(p->!ObjectUtils.isEmpty(p)) - .map(p->FilesUtils.resolveRelativePath(absolutePath, p)) + .filter(p -> !ObjectUtils.isEmpty(p)) + .map(p -> FilesUtils.resolveRelativePath(absolutePath, p)) .collect(Collectors.toList()); } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/NestedPluginJarResourceLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/NestedPluginJarResourceLoader.java index 8bc4af1257ee2a395b9b11f3ede57efabf7cc2b3..79b393e676b851654788cc9ffb5e66babe9e6203 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/NestedPluginJarResourceLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/NestedPluginJarResourceLoader.java @@ -24,6 +24,8 @@ import com.gitee.starblues.loader.classloader.*; import com.gitee.starblues.loader.classloader.resource.loader.*; import com.gitee.starblues.loader.classloader.resource.storage.ResourceStorage; import com.gitee.starblues.loader.launcher.ResourceLoaderFactoryGetter; +import com.gitee.starblues.utils.MsgUtils; +import lombok.extern.slf4j.Slf4j; import java.io.InputStream; import java.net.URL; @@ -39,6 +41,7 @@ import java.util.zip.ZipEntry; * @author starBlues * @version 3.0.0 */ +@Slf4j public class NestedPluginJarResourceLoader extends AbstractResourceLoader { private final InsidePluginDescriptor pluginDescriptor; @@ -82,8 +85,13 @@ public class NestedPluginJarResourceLoader extends AbstractResourceLoader { private void addLib(JarFile jarFile) throws Exception { JarEntry jarEntry = null; Set pluginLibInfos = pluginDescriptor.getPluginLibInfo(); + String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); for (PluginLibInfo pluginLibInfo : pluginLibInfos) { jarEntry = jarFile.getJarEntry(pluginLibInfo.getPath()); + if(jarEntry == null){ + log.debug("Not found: " + pluginLibInfo.getPath()); + continue; + } if (jarEntry.getMethod() != ZipEntry.STORED) { throw new PluginException("插件依赖压缩方式错误, 必须是: 存储(stored)压缩方式"); } @@ -91,9 +99,11 @@ public class NestedPluginJarResourceLoader extends AbstractResourceLoader { URL url = new URL(baseUrl.toString() + pluginLibInfo.getPath() + "!/"); if(pluginLibInfo.isLoadToMain()){ parentClassLoader.addResource(new JarResourceLoader(url, new JarInputStream(jarFileInputStream))); + log.debug("插件[{}]依赖被加载到主程序中: {}", pluginUnique, pluginLibInfo.getPath()); } else { JarResourceLoader jarResourceLoader = new JarResourceLoader(url, new JarInputStream(jarFileInputStream)); resourceLoaderFactory.addResource(jarResourceLoader); + log.debug("插件[{}]依赖被加载: {}", pluginUnique, pluginLibInfo.getPath()); } } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginClassLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginClassLoader.java index a76be2cb28b39ebdf4a0520d4bfb8b0bf381ecf8..57ada00004dc1eceb8e028981702516d79fb75c8 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginClassLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginClassLoader.java @@ -19,11 +19,14 @@ package com.gitee.starblues.core.classloader; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.descriptor.PluginLibInfo; import com.gitee.starblues.core.descriptor.PluginType; +import com.gitee.starblues.core.exception.PluginException; import com.gitee.starblues.loader.classloader.*; import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; import com.gitee.starblues.utils.Assert; import com.gitee.starblues.utils.FilesUtils; +import com.gitee.starblues.utils.MsgUtils; import com.gitee.starblues.utils.ObjectUtils; +import lombok.extern.slf4j.Slf4j; import java.io.File; import java.io.IOException; @@ -37,6 +40,7 @@ import java.util.Set; * @author starBlues * @version 3.0.0 */ +@Slf4j public class PluginClassLoader extends GenericClassLoader { private final GenericClassLoader parentClassLoader; @@ -59,21 +63,36 @@ public class PluginClassLoader extends GenericClassLoader { public void addResource(InsidePluginDescriptor descriptor) throws Exception { PluginType pluginType = descriptor.getType(); - if(pluginType == PluginType.JAR || pluginType == PluginType.ZIP){ + if(PluginType.isNestedPackage(pluginType)){ NestedPluginJarResourceLoader resourceLoader = new NestedPluginJarResourceLoader(descriptor, parentClassLoader, resourceLoaderFactory); resourceLoaderFactory.addResource(resourceLoader); + } else if(PluginType.isOuterPackage(pluginType)){ + addOuterPluginClasspath(descriptor); + addLibFile(descriptor); } else { - addClasspath(descriptor); + addDirPluginClasspath(descriptor); addLibFile(descriptor); } } - private void addClasspath(InsidePluginDescriptor pluginDescriptor) throws Exception { - String pluginClassPath = pluginDescriptor.getPluginClassPath(); + private void addOuterPluginClasspath(InsidePluginDescriptor descriptor) throws Exception{ + String pluginPath = descriptor.getPluginPath(); + File existFile = FilesUtils.getExistFile(pluginPath); + if(existFile != null){ + addResource(existFile); + log.debug("插件[{}]Classpath已被加载: {}", MsgUtils.getPluginUnique(descriptor), existFile.getPath()); + } else { + throw new PluginException("没有发现插件路径: " + pluginPath); + } + } + + private void addDirPluginClasspath(InsidePluginDescriptor descriptor) throws Exception { + String pluginClassPath = descriptor.getPluginClassPath(); File existFile = FilesUtils.getExistFile(pluginClassPath); if(existFile != null){ addResource(existFile); + log.debug("插件[{}]Classpath已被加载: {}", MsgUtils.getPluginUnique(descriptor), existFile.getPath()); } } @@ -82,14 +101,22 @@ public class PluginClassLoader extends GenericClassLoader { if(ObjectUtils.isEmpty(pluginLibInfos)){ return; } + String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); + log.info("插件[{}]依赖加载目录: {}", pluginUnique, pluginDescriptor.getPluginLibDir()); + if(pluginLibInfos.isEmpty()){ + log.warn("插件[{}]依赖为空!", pluginUnique); + return; + } for (PluginLibInfo pluginLibInfo : pluginLibInfos) { File existFile = FilesUtils.getExistFile(pluginLibInfo.getPath()); if(existFile != null){ if(pluginLibInfo.isLoadToMain()){ // 加载到主程序中 parentClassLoader.addResource(existFile); + log.debug("插件[{}]依赖被加载到主程序中: {}", pluginUnique, existFile.getPath()); } else { addResource(existFile); + log.debug("插件[{}]依赖被加载: {}", pluginUnique, existFile.getPath()); } } } 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 40f62dc8c2ee39a946434a00689f46d73f2c9767..3f7bd7362d53942ecdf36f1643071fa3d88d2cec 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 @@ -20,10 +20,12 @@ package com.gitee.starblues.core.descriptor; import com.gitee.starblues.common.AbstractDependencyPlugin; import com.gitee.starblues.common.Constants; import com.gitee.starblues.common.DependencyPlugin; +import com.gitee.starblues.common.PackageStructure; import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; import com.gitee.starblues.core.exception.PluginDecryptException; import com.gitee.starblues.core.exception.PluginException; import com.gitee.starblues.utils.FilesUtils; +import com.gitee.starblues.utils.MsgUtils; import com.gitee.starblues.utils.ObjectUtils; import lombok.AllArgsConstructor; import lombok.Getter; @@ -46,7 +48,7 @@ import static com.gitee.starblues.utils.PropertiesUtils.getValue; /** * 抽象的 PluginDescriptorLoader * @author starBlues - * @version 3.0.1 + * @version 3.0.2 */ @Slf4j public abstract class AbstractPluginDescriptorLoader implements PluginDescriptorLoader{ @@ -69,13 +71,12 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor return null; } return create(pluginMeta, location); - } catch (Exception e) { - if(e instanceof PluginDecryptException){ - logger.error(e.getMessage(), e); + } catch (Throwable e) { + if(e instanceof PluginException){ + throw (PluginException) e; } else { - logger.debug(e.getMessage(), e); + throw new PluginException(e.getMessage(), e); } - return null; } } @@ -102,11 +103,13 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor ); descriptor.setType(PluginType.byName(pluginMeta.getPackageType())); - PluginResourcesConfig pluginResourcesConfig = getPluginResourcesConfig(path, properties); + PluginResourcesConfig resourcesConfig = getPluginResourcesConfig(path, properties); - descriptor.setPluginLibInfo(getPluginLibInfo(pluginResourcesConfig.getDependenciesIndex())); - descriptor.setIncludeMainResourcePatterns(pluginResourcesConfig.getLoadMainResourceIncludes()); - descriptor.setExcludeMainResourcePatterns(pluginResourcesConfig.getLoadMainResourceExcludes()); + String pluginLibDir = getValue(properties, PLUGIN_LIB_DIR, false); + descriptor.setPluginLibDir(pluginLibDir); + descriptor.setPluginLibInfo(getPluginLibInfo(descriptor, resourcesConfig.getDependenciesIndex())); + descriptor.setIncludeMainResourcePatterns(resourcesConfig.getLoadMainResourceIncludes()); + descriptor.setExcludeMainResourcePatterns(resourcesConfig.getLoadMainResourceExcludes()); descriptor.setProperties(properties); descriptor.setPluginClassPath(getValue(properties, PLUGIN_PATH, false)); @@ -151,28 +154,62 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor } } - protected Set getPluginLibInfo(Set dependenciesIndex){ + protected Set getPluginLibInfo(DefaultInsidePluginDescriptor descriptor, Set dependenciesIndex){ + String pluginLibDir = descriptor.getPluginLibDir(); + boolean configPluginLibDir = false; + if(!ObjectUtils.isEmpty(pluginLibDir)){ + descriptor.setPluginLibDir(getLibDir(descriptor, pluginLibDir)); + configPluginLibDir = true; + } if(ObjectUtils.isEmpty(dependenciesIndex)){ return Collections.emptySet(); } Set pluginLibInfos = new HashSet<>(dependenciesIndex.size()); - File file = new File(""); - String absolutePath = file.getAbsolutePath(); for (String index : dependenciesIndex) { - String libPath; boolean loadToMain; if(index.endsWith(Constants.LOAD_TO_MAIN_SIGN)){ - libPath = index.substring(0, index.lastIndexOf(Constants.LOAD_TO_MAIN_SIGN)); + index = index.substring(0, index.lastIndexOf(Constants.LOAD_TO_MAIN_SIGN)); loadToMain = true; } else { - libPath = index; loadToMain = false; } - pluginLibInfos.add(new PluginLibInfo(FilesUtils.resolveRelativePath(absolutePath, libPath), loadToMain)); + String libPath = index; + if(configPluginLibDir){ + libPath = getLibPath(descriptor, index); + } + pluginLibInfos.add(new PluginLibInfo(libPath, loadToMain)); } return pluginLibInfos; } + protected String getLibDir(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir){ + if(!FilesUtils.isRelativePath(configPluginLibDir)){ + return configPluginLibDir; + } + // 是相对路径 + // 先相对当前插件目录 + String resolveRelativePath = FilesUtils.resolveRelativePath(descriptor.getPluginPath(), configPluginLibDir); + if(new File(resolveRelativePath).exists()){ + return resolveRelativePath; + } + // 再相对插件存放目录 + resolveRelativePath = FilesUtils.resolveRelativePath(new File(descriptor.getPluginPath()).getParent(), configPluginLibDir); + if(new File(resolveRelativePath).exists()){ + return resolveRelativePath; + } + // 最后相对主程序目录 + resolveRelativePath = FilesUtils.resolveRelativePath(new File("").getAbsolutePath(), configPluginLibDir); + if(new File(resolveRelativePath).exists()){ + return resolveRelativePath; + } + throw new PluginException("插件["+ MsgUtils.getPluginUnique(descriptor) +"]" + + "依赖目录[" + descriptor.getPluginLibDir() + "]不存在!"); + } + + protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String index){ + return FilesUtils.joiningFilePath(descriptor.getPluginLibDir(), index); + } + protected Properties getDecryptProperties(InputStream inputStream) throws Exception{ Properties properties = new Properties(); try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ComposeDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ComposeDescriptorLoader.java index 669b4f3d1c67b18761728707b34008e78a22572b..5445d05ce6ec01dd28bd5918821b3d19d10278f3 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ComposeDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ComposeDescriptorLoader.java @@ -72,14 +72,10 @@ public class ComposeDescriptorLoader implements PluginDescriptorLoader{ @Override public InsidePluginDescriptor load(Path location) throws PluginException { for (PluginDescriptorLoader pluginDescriptorLoader : pluginDescriptorLoaders) { - try { - InsidePluginDescriptor pluginDescriptor = pluginDescriptorLoader.load(location); - if(pluginDescriptor != null){ - pluginChecker.checkDescriptor(pluginDescriptor); - return pluginDescriptor; - } - } catch (Exception e){ - // 忽略异常 + InsidePluginDescriptor pluginDescriptor = pluginDescriptorLoader.load(location); + if(pluginDescriptor != null){ + pluginChecker.checkDescriptor(pluginDescriptor); + return pluginDescriptor; } } return null; diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DefaultInsidePluginDescriptor.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DefaultInsidePluginDescriptor.java index ced611f24890c5333391ec308aa878af06c0bd55..f5cdd9219de0664360d48843a709e1169841e0a7 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DefaultInsidePluginDescriptor.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DefaultInsidePluginDescriptor.java @@ -26,7 +26,7 @@ import java.util.jar.Manifest; /** * 内部的默认插件描述者 * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ public class DefaultInsidePluginDescriptor extends DefaultPluginDescriptor implements InsidePluginDescriptor { @@ -44,6 +44,8 @@ public class DefaultInsidePluginDescriptor extends DefaultPluginDescriptor imple @Setter private String args; @Setter + private String pluginLibDir; + @Setter private Set pluginLibInfo; @Setter private Set includeMainResourcePatterns; @@ -62,6 +64,11 @@ public class DefaultInsidePluginDescriptor extends DefaultPluginDescriptor imple return pluginClassPath; } + @Override + public String getPluginLibDir() { + return pluginLibDir; + } + @Override public Set getPluginLibInfo() { return pluginLibInfo; diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DevPluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DevPluginDescriptorLoader.java index 2d16ba9351fe9ff44c68c7d75fd4140312d7036c..432e9fc1146831097d8fbbb15e9efcdfb0478ece 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DevPluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DevPluginDescriptorLoader.java @@ -31,7 +31,7 @@ import java.util.Properties; /** * 开发环境 PluginDescriptorLoader 加载者 * @author starBlues - * @version 3.0.1 + * @version 3.0.2 */ @Slf4j public class DevPluginDescriptorLoader extends AbstractPluginDescriptorLoader{ @@ -57,6 +57,11 @@ public class DevPluginDescriptorLoader extends AbstractPluginDescriptorLoader{ return new PluginMeta(PackageType.PLUGIN_PACKAGE_TYPE_DEV, properties); } + @Override + protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String index) { + return index; + } + @Override protected DefaultInsidePluginDescriptor create(PluginMeta pluginMeta, Path path) throws Exception { final DefaultInsidePluginDescriptor descriptor = super.create(pluginMeta, path); diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/InsidePluginDescriptor.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/InsidePluginDescriptor.java index 5064060fd7a6cfbcd65de7311bffd667d2c1349b..369af7825329b865f629c21960c2e79ac638f448 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/InsidePluginDescriptor.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/InsidePluginDescriptor.java @@ -73,6 +73,12 @@ public interface InsidePluginDescriptor extends PluginDescriptor{ */ String getPluginClassPath(); + /** + * 获取插件依赖配置的目录 + * @return String + */ + String getPluginLibDir(); + /** * 获取插件依赖的路径 * @return String diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/PluginType.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/PluginType.java index 5614e6c8d8008d06e8d5e1893a1370e197250936..eb42c15142531231fca6aedab7ff9679ba6d14a1 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/PluginType.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/PluginType.java @@ -24,7 +24,7 @@ import java.util.Objects; /** * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ public enum PluginType { @@ -80,8 +80,23 @@ public enum PluginType { return PluginType.ZIP; } else if(Objects.equals(packageType, PluginType.ZIP_OUTER.getName())){ return PluginType.ZIP_OUTER; + } else if(Objects.equals(packageType, PluginType.DIR.getName())){ + return PluginType.DIR; } else { throw new PluginException("不能解析'" + packageType + "'打包类型的插件"); } } + + public static boolean isOuterPackage(PluginType pluginType){ + return pluginType == PluginType.JAR_OUTER || pluginType == PluginType.ZIP_OUTER; + } + + public static boolean isDirPackage(PluginType pluginType){ + return pluginType == PluginType.DIR; + } + + public static boolean isNestedPackage(PluginType pluginType){ + return pluginType == PluginType.JAR || pluginType == PluginType.ZIP; + } + } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdDirPluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdDirPluginDescriptorLoader.java index 8edea7c3893360d8e02ed3f37aaf0d334aae9f97..541fcbbff5f2c589885a12107da28286da77a269 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdDirPluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdDirPluginDescriptorLoader.java @@ -44,7 +44,7 @@ import static com.gitee.starblues.common.PackageStructure.*; * 生产环境目录式插件 PluginDescriptorLoader 加载者 * 解析生产的dir * @author starBlues - * @version 3.0.1 + * @version 3.0.2 */ public class ProdDirPluginDescriptorLoader extends AbstractPluginDescriptorLoader{ @@ -104,25 +104,7 @@ public class ProdDirPluginDescriptorLoader extends AbstractPluginDescriptorLoade } File libFile = new File(libIndexFile); List lines = FileUtils.readLines(libFile, CHARSET_NAME); - PluginResourcesConfig pluginResourcesConfig = PluginResourcesConfig.parse(lines); - - Set dependenciesIndex = pluginResourcesConfig.getDependenciesIndex(); - Set pluginLibPaths = new HashSet<>(); - for (String index : dependenciesIndex) { - index = resolvePath(index); - File file = new File(index); - if(!file.exists()){ - // 如果直接读取的路径不存在, 则从相对路径读取 - file = new File(FilesUtils.joiningFilePath( - pathStr, index - )); - } - if(file.exists()){ - pluginLibPaths.add(file.getPath()); - } - } - pluginResourcesConfig.setDependenciesIndex(pluginLibPaths); - return pluginResourcesConfig; + return PluginResourcesConfig.parse(lines); } protected String getExistResourcesConfFile(String rootPath, String libIndexPath){ 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 2226d50d054400bd9d339b26914ecb9566c4de41..4721da36c8fc7cf426878116e87ac8460c2e0204 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 @@ -20,6 +20,7 @@ package com.gitee.starblues.core.descriptor; import com.gitee.starblues.common.ManifestKey; import com.gitee.starblues.common.PackageStructure; import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; +import com.gitee.starblues.utils.FilesUtils; import com.gitee.starblues.utils.PropertiesUtils; import com.gitee.starblues.utils.ObjectUtils; import org.apache.commons.io.IOUtils; @@ -41,7 +42,7 @@ import static com.gitee.starblues.common.PluginDescriptorKey.PLUGIN_RESOURCES_CO * 生产环境打包好的插件 PluginDescriptorLoader 加载者 * 解析 jar、zip * @author starBlues - * @version 3.0.1 + * @version 3.0.2 */ public class ProdPackagePluginDescriptorLoader extends AbstractPluginDescriptorLoader{ @@ -79,6 +80,24 @@ public class ProdPackagePluginDescriptorLoader extends AbstractPluginDescriptorL return pluginResourcesConfig; } + @Override + protected String getLibDir(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir) { + if(PluginType.isNestedPackage(descriptor.getType())){ + return descriptor.getPluginLibDir(); + } else { + return super.getLibDir(descriptor, configPluginLibDir); + } + } + + @Override + protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String index) { + if(PluginType.isNestedPackage(descriptor.getType())){ + return index; + } else { + return super.getLibPath(descriptor, index); + } + } + protected PluginResourcesConfig getPluginResourcesConfig(JarFile jarFile, Properties properties) throws Exception { String pluginResourcesConf = PropertiesUtils.getValue(properties, PLUGIN_RESOURCES_CONFIG); if(ObjectUtils.isEmpty(pluginResourcesConf)){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginLauncher.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginLauncher.java index 65fad6583845a13975c97669e6eb768ee45bb6c9..9878c3d685cb7a238ebd73d066b1c5a6e7a611b9 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginLauncher.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginLauncher.java @@ -23,6 +23,7 @@ import com.gitee.starblues.loader.classloader.GenericClassLoader; import com.gitee.starblues.loader.classloader.resource.loader.DefaultResourceLoaderFactory; import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; import com.gitee.starblues.loader.launcher.AbstractLauncher; +import com.gitee.starblues.loader.launcher.LauncherContext; import com.gitee.starblues.spring.SpringPluginHook; import com.gitee.starblues.utils.MsgUtils; @@ -79,7 +80,7 @@ public class PluginLauncher extends AbstractLauncher { } protected GenericClassLoader getParentClassLoader() throws Exception { - ClassLoader contextClassLoader = pluginInteractive.getMainApplicationContext().getClassLoader(); + ClassLoader contextClassLoader = LauncherContext.getMainClassLoader(); if(contextClassLoader instanceof GenericClassLoader){ return (GenericClassLoader) contextClassLoader; } else { diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMethodRunner.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMethodRunner.java index c5d12d4add3ba48709865563d5d1712f5b81e9eb..e91403f2eb45bf06c3ec1d34835b5f1b032ed46f 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMethodRunner.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginMethodRunner.java @@ -17,7 +17,9 @@ package com.gitee.starblues.core.launcher.plugin; import com.gitee.starblues.core.RuntimeMode; +import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.loader.launcher.runner.MethodRunner; +import com.gitee.starblues.utils.MsgUtils; import com.gitee.starblues.utils.ObjectUtils; import com.gitee.starblues.utils.ReflectionUtils; import org.slf4j.Logger; @@ -51,14 +53,19 @@ public class PluginMethodRunner extends MethodRunner { protected Class loadRunClass(ClassLoader classLoader) throws Exception { try { return super.loadRunClass(classLoader); - } catch (Exception e){ + } catch (Throwable e){ + InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); + String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); if(e instanceof ClassNotFoundException){ - String pluginId = pluginInteractive.getPluginDescriptor().getPluginId(); - String error = "插件[" + pluginId + "]没有发现" + "[" + className + "]引导类"; + String error = "插件[" + pluginUnique + "]没有发现" + "[" + className + "]引导类"; if(pluginInteractive.getConfiguration().environment() == RuntimeMode.DEV){ - error = error + ", 请确保已经编译!"; + error = error + ", 请确保已经编译!"; } throw new ClassNotFoundException(error); + } else if(e instanceof NoClassDefFoundError){ + String error = "插件[" + pluginUnique + "]没有发现依赖类: [" + e.getMessage() + "], " + + "请确保插件依赖被完整加载!"; + throw new NoClassDefFoundError(error); } throw e; } @@ -74,21 +81,7 @@ public class PluginMethodRunner extends MethodRunner { Object instance = getInstance(runClass); setPluginInteractive(instance); runMethod.setAccessible(true); - try { - return runMethod.invoke(instance, runClass, this.args); - } catch (Exception e){ - String error = "Invoke failure: " - + ReflectionUtils.methodToString(runClass, runMethodName, runMethod.getParameterTypes()) - + ". "; - String message = e.getMessage(); - if(message != null){ - error = error + message; - logger.error(error, e); - } else { - logger.error(error); - } - throw new Exception(error); - } + return runMethod.invoke(instance, runClass, this.args); } private void setPluginInteractive(Object launchObject) throws Exception { diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java b/spring-brick/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java index 451103bc0d036508f437ce1ea26642a0369a504f..42a855792e7b7d0737256789ef52cdabfc264e83 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java @@ -103,7 +103,7 @@ public abstract class DefaultIntegrationConfiguration implements IntegrationConf */ @Override public void checkConfig(){ - Assert.isNotEmpty(mainPackage(), "插件配置: mainPackage 不能为空"); + Assert.isNotEmpty(mainPackage(), "插件配置: [plugin.mainPackage] 不能为空"); } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/MainApplicationContext.java b/spring-brick/src/main/java/com/gitee/starblues/spring/MainApplicationContext.java index e1cdb4d8d33c8aa6a379d75427130114c6497b6b..d7017a0c1d8cca17583fd3dd862e6c18fae45dd3 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/MainApplicationContext.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/MainApplicationContext.java @@ -32,10 +32,4 @@ public interface MainApplicationContext extends ApplicationContext { */ Map> getConfigurableEnvironment(); - /** - * 得到主程序的 ClassLoader - * @return ClassLoader - */ - ClassLoader getClassLoader(); - } diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/MainApplicationContextProxy.java b/spring-brick/src/main/java/com/gitee/starblues/spring/MainApplicationContextProxy.java index b2779e407785ab78ce193dd2ed4b276d30e88f8e..b77d4a3b610a8cc4a2f2c9aa528699b0aa2f2db3 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/MainApplicationContextProxy.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/MainApplicationContextProxy.java @@ -34,18 +34,15 @@ import java.util.Map; public class MainApplicationContextProxy extends ApplicationContextProxy implements MainApplicationContext{ private final GenericApplicationContext applicationContext; - private final ClassLoader classLoader; public MainApplicationContextProxy(GenericApplicationContext applicationContext) { super(applicationContext.getBeanFactory()); this.applicationContext = applicationContext; - this.classLoader = Thread.currentThread().getContextClassLoader(); } public MainApplicationContextProxy(GenericApplicationContext applicationContext, AutoCloseable autoCloseable) { super(applicationContext.getBeanFactory(), autoCloseable); this.applicationContext = applicationContext; - this.classLoader = Thread.currentThread().getContextClassLoader(); } @Override @@ -70,8 +67,4 @@ public class MainApplicationContextProxy extends ApplicationContextProxy impleme return environmentMap; } - @Override - public ClassLoader getClassLoader() { - return classLoader; - } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceResolver.java b/spring-brick/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceResolver.java index ea2a84cb7a1394ca7ac7fe743fdaa1df8c11ee3d..b923cde29c9bc2ba05aeda8d1210235ebb671045 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceResolver.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/web/PluginStaticResourceResolver.java @@ -42,7 +42,7 @@ import java.util.stream.Collectors; /** * 插件web静态资源Resolver * @author starBlues - * @version 3.0.0 + * @version 3.0.2 */ public class PluginStaticResourceResolver extends AbstractResourceResolver { @@ -66,7 +66,8 @@ public class PluginStaticResourceResolver extends AbstractResourceResolver { if(request != null){ String requestUri = request.getRequestURI(); String formatUri = UrlUtils.format(requestUri); - requestPath = UrlUtils.format(formatUri.replace(config.getPathPrefix(), "")); + // fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I53T9W + requestPath = UrlUtils.format(formatUri.replaceFirst(config.getPathPrefix(), "")); } int startOffset = requestPath.indexOf("/"); String pluginId = null; diff --git a/update.md b/update.md index 717eff72034983b6eb6daf00cf6507490f21d5d8..f2ee7f572cab55d8b8c99fdee742d857ab76c4b7 100644 --- a/update.md +++ b/update.md @@ -1,6 +1,7 @@ -1. 新增插件包启动时可进行密码校验功能。 -2. 修复插件动态安装的问题。 -3. 修复主程序打包为生产环境jar包后,启动问题。 -4. 修复插件更新时, 版本校验问题。 -5. 修复插件排序、启用、禁用配置无效的问题。 -6. 修复插件更新时, 新代码不无法变更的问题。 \ No newline at end of file +1. 新增 `xx-outer、dir` 打包类型的插件可自定义依赖目录 +2. 新增`includeSystemScope`、`type=main` 打包属性 +3. 修复插件拦截器无法拦截不存在的url +4. 修复主程序在 `jar-outer` 打包模式后无法启动问题 +5. 修复插件首次安装时异常 +6. fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I53K4G +7. fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I53T9W \ No newline at end of file