From 599d4d75f781e5666abdbf08f339bec96e2fc651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=BC=9F=E5=8D=8E?= Date: Wed, 8 Jun 2022 13:26:54 +0800 Subject: [PATCH 01/12] =?UTF-8?q?1.=20=E5=A2=9E=E5=8A=A0=E4=B8=BB=E5=8C=85?= =?UTF-8?q?MAINIFEST=E4=B8=ADtitle=E5=92=8Cversion=E5=AE=9A=E4=B9=89=202.?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8DLiveBeansView=E5=9C=A8=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E6=B3=A8=E5=86=8C=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootstrap/ConfigurePluginEnvironment.java | 17 ++++++++++++++++- .../com/gitee/starblues/common/ManifestKey.java | 10 ++++++++++ .../plugin/pack/main/JarNestPackager.java | 6 +++++- .../plugin/pack/main/JarOuterPackager.java | 7 ++++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java index 67ba594..99ebfb3 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java @@ -23,11 +23,15 @@ import com.gitee.starblues.utils.Assert; import com.gitee.starblues.utils.FilesUtils; import com.gitee.starblues.utils.ObjectUtils; import com.gitee.starblues.utils.PluginFileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.LiveBeansView; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; import java.io.File; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @@ -37,7 +41,7 @@ import java.util.Map; * @version 3.0.0 */ class ConfigurePluginEnvironment { - + private final Logger logger = LoggerFactory.getLogger(ConfigurePluginEnvironment.class); private final static String PLUGIN_PROPERTY_NAME = "pluginPropertySources"; private final static String SPRING_CONFIG_NAME = "spring.config.name"; @@ -77,6 +81,17 @@ class ConfigurePluginEnvironment { env.put(MBEAN_DOMAIN_PROPERTY_NAME, pluginId); environment.getPropertySources().addFirst(new MapPropertySource(PLUGIN_PROPERTY_NAME, env)); + try{ + // fix: https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I57965 + // 优先注册LiveBeansView对象,防止注册异常 + Method method = LiveBeansView.class.getDeclaredMethod("registerApplicationContext", ConfigurableApplicationContext.class); + method.setAccessible(true); + method.invoke(null,processorContext.getApplicationContext()); + } + catch (Exception ex){ + logger.error("LiveBeansView.registerApplicationContext失败. {}", + ex.getMessage(), ex); + } if(processorContext.runMode() == ProcessorContext.RunMode.ONESELF){ ConfigureMainPluginEnvironment configureMainPluginEnvironment = new ConfigureMainPluginEnvironment(processorContext); 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 b4ffd09..fdfc471 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 @@ -83,6 +83,16 @@ public class ManifestKey { */ public static final String MAIN_PACKAGE_TYPE = "Main-Package-Type"; + /** + * jar package name + */ + public static final String IMPLEMENTATION_TITLE = "Implementation-Title"; + + /** + * jar package version + */ + public static final String IMPLEMENTATION_VERSION = "Implementation-Version"; + /** * 获取值 * 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 e866c3d..fb8f52c 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 @@ -22,11 +22,11 @@ import com.gitee.starblues.plugin.pack.RepackageMojo; import com.gitee.starblues.plugin.pack.Repackager; import com.gitee.starblues.plugin.pack.utils.CommonUtils; import com.gitee.starblues.plugin.pack.utils.PackageJar; -import com.gitee.starblues.utils.ObjectUtils; import org.apache.commons.io.IOUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; import java.io.File; import java.util.Set; @@ -83,6 +83,10 @@ public class JarNestPackager implements Repackager { attributes.putValue(START_CLASS, mainConfig.getMainClass()); attributes.putValue(MAIN_CLASS, MAIN_CLASS_VALUE); attributes.putValue(MAIN_PACKAGE_TYPE, PackageType.MAIN_PACKAGE_TYPE_JAR); + // 增加jar包title和version属性 + MavenProject mavenProject = this.repackageMojo.getProject(); + attributes.putValue(IMPLEMENTATION_TITLE,mavenProject.getArtifactId()); + attributes.putValue(IMPLEMENTATION_VERSION,mavenProject.getVersion()); return manifest; } 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 de5a493..0b79204 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 @@ -25,13 +25,13 @@ 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 org.apache.maven.project.MavenProject; import java.io.File; import java.util.HashSet; import java.util.Set; import java.util.jar.Attributes; import java.util.jar.Manifest; -import java.util.stream.Stream; import static com.gitee.starblues.common.ManifestKey.*; @@ -87,6 +87,11 @@ public class JarOuterPackager extends JarNestPackager { attributes.putValue(MAIN_PACKAGE_TYPE, PackageType.MAIN_PACKAGE_TYPE_JAR_OUTER); attributes.putValue(MAIN_LIB_DIR, getLibPath()); attributes.putValue(MAIN_LIB_INDEXES, getLibIndexes()); + + // 增加jar包title和version属性 + MavenProject mavenProject = this.repackageMojo.getProject(); + attributes.putValue(IMPLEMENTATION_TITLE,mavenProject.getArtifactId()); + attributes.putValue(IMPLEMENTATION_VERSION,mavenProject.getVersion()); return manifest; } -- Gitee From f45c3f3abacb00b31bbb09fcc65796460752d1d2 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Wed, 8 Jun 2022 17:35:20 +0800 Subject: [PATCH 02/12] init 3.0.4 --- pom.xml | 2 +- spring-brick-bootstrap/pom.xml | 2 +- spring-brick-common/pom.xml | 2 +- spring-brick-loader/pom.xml | 2 +- .../starblues/loader/DevelopmentMode.java | 44 +++++++++++++++++++ .../classloader/GeneralUrlClassLoader.java | 42 ++++++++++++++++++ .../launcher/SimpleModeMainLauncher.java | 29 ++++++++++++ .../loader/launcher/SpringBootstrap.java | 12 +++++ .../loader/launcher/SpringMainBootstrap.java | 9 +++- spring-brick-maven-packager/pom.xml | 2 +- .../plugin-help.xml | 2 +- .../main/resources/META-INF/maven/plugin.xml | 2 +- spring-brick/pom.xml | 2 +- .../SimpleDevelopmentModeInitializer.java | 24 ++++++++++ .../main/resources/META-INF/spring.factories | 6 ++- 15 files changed, 171 insertions(+), 11 deletions(-) create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SimpleModeMainLauncher.java create mode 100644 spring-brick/src/main/java/com/gitee/starblues/integration/simple/SimpleDevelopmentModeInitializer.java diff --git a/pom.xml b/pom.xml index 0970aa1..3c86c78 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.gitee.starblues spring-brick-parent pom - 3.0.3 + 3.0.4 spring-brick-common diff --git a/spring-brick-bootstrap/pom.xml b/spring-brick-bootstrap/pom.xml index cf32d07..6c779cb 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.3 + 3.0.4 spring-brick-bootstrap diff --git a/spring-brick-common/pom.xml b/spring-brick-common/pom.xml index 98f6c36..c284b46 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.3 + 3.0.4 spring-brick-common diff --git a/spring-brick-loader/pom.xml b/spring-brick-loader/pom.xml index 2b61cd0..90eaa40 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.3 + 3.0.4 4.0.0 diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java new file mode 100644 index 0000000..8f88e5d --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java @@ -0,0 +1,44 @@ +package com.gitee.starblues.loader; + +/** + * 插件开发模式 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public enum DevelopmentMode { + + /** + * 简单模式 + */ + SIMPLE("simple"), + + /** + * 动态模式 + */ + DYNAMIC("dynamic"); + + private final String developmentMode; + + DevelopmentMode(String developmentMode) { + this.developmentMode = developmentMode; + } + + public String getDevelopmentMode() { + return developmentMode; + } + + @Override + public String toString() { + return developmentMode; + } + + public static DevelopmentMode byName(String model){ + if(SIMPLE.name().equalsIgnoreCase(model)){ + return DevelopmentMode.SIMPLE; + } else { + return DevelopmentMode.DYNAMIC; + } + } +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java new file mode 100644 index 0000000..f76b416 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java @@ -0,0 +1,42 @@ +package com.gitee.starblues.loader.classloader; + +import java.io.File; +import java.io.FileNotFoundException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * 通用的Url ClassLoader + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class GeneralUrlClassLoader extends URLClassLoader { + + public GeneralUrlClassLoader(ClassLoader parent) { + super(new URL[]{}, parent); + } + + public void addUrl(String url) throws Exception{ + addPath(Paths.get(url)); + } + + public void addPath(Path path) throws Exception{ + addFile(path.toFile()); + } + + public void addFile(File file) throws Exception { + if(!file.exists()){ + throw new FileNotFoundException("Not found file:" + file.getPath()); + } + addURL(file.toPath().toUri().toURL()); + } + + @Override + public void addURL(URL url) { + super.addURL(url); + } +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SimpleModeMainLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SimpleModeMainLauncher.java new file mode 100644 index 0000000..067a74c --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SimpleModeMainLauncher.java @@ -0,0 +1,29 @@ +package com.gitee.starblues.loader.launcher; + +import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; +import com.gitee.starblues.loader.launcher.runner.MethodRunner; + + +/** + * @author starBlues + * @since 3.0.4 + */ +public class SimpleModeMainLauncher extends AbstractMainLauncher{ + + private final MethodRunner methodRunner; + + public SimpleModeMainLauncher(MethodRunner methodRunner) { + this.methodRunner = methodRunner; + } + + @Override + protected ClassLoader createClassLoader(String... args) throws Exception { + return new GeneralUrlClassLoader(this.getClass().getClassLoader()); + } + + @Override + protected ClassLoader launch(ClassLoader classLoader, String... args) throws Exception { + methodRunner.run(classLoader); + return classLoader; + } +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java index 2a34660..a6e50d6 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java @@ -16,6 +16,8 @@ package com.gitee.starblues.loader.launcher; +import com.gitee.starblues.loader.DevelopmentMode; + /** * 主程序实现该接口引导启动SpringBoot * @author starBlues @@ -30,4 +32,14 @@ public interface SpringBootstrap { */ void run(String[] args) throws Exception; + + /** + * 设置开发模式 + * + * @return DevelopmentMode + */ + default DevelopmentMode developmentMode(){ + return DevelopmentMode.DYNAMIC; + } + } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java index 19db6ba..1d31e04 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java @@ -16,6 +16,7 @@ package com.gitee.starblues.loader.launcher; +import com.gitee.starblues.loader.DevelopmentMode; import com.gitee.starblues.loader.jar.JarFile; import com.gitee.starblues.loader.launcher.runner.MainMethodRunner; import com.gitee.starblues.loader.launcher.runner.MethodRunner; @@ -38,7 +39,6 @@ public class SpringMainBootstrap { private static SpringBootstrap springBootstrap; - public static void launch(Class bootstrapClass, String[] args) { try { SpringBootstrap springBootstrap = bootstrapClass.getConstructor().newInstance(); @@ -86,7 +86,12 @@ public class SpringMainBootstrap { private static void main(String[] args) throws Exception { Objects.requireNonNull(springBootstrap, "springBootBootstrap 不能为空"); MethodRunner run = new MethodRunner(springBootstrap.getClass().getName(), SPRING_BOOTSTRAP_RUN_METHOD, args); - Launcher launcher = new MainProgramLauncher(run); + Launcher launcher; + if(springBootstrap.developmentMode() == DevelopmentMode.SIMPLE){ + launcher = new SimpleModeMainLauncher(run); + } else { + launcher = new MainProgramLauncher(run); + } launcher.run(args); } diff --git a/spring-brick-maven-packager/pom.xml b/spring-brick-maven-packager/pom.xml index cb70eb6..dd87002 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.3 + 3.0.4 spring-brick-maven-packager 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 ddfb1ce..c2b84e8 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 @@ -4,7 +4,7 @@ Spring Boot Plugin Maven Packager com.gitee.starblues spring-brick-maven-packager - 3.0.3 + 3.0.4 spring-brick-packager false true 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 ddfb1ce..c2b84e8 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 @@ -4,7 +4,7 @@ Spring Boot Plugin Maven Packager com.gitee.starblues spring-brick-maven-packager - 3.0.3 + 3.0.4 spring-brick-packager false true diff --git a/spring-brick/pom.xml b/spring-brick/pom.xml index c92fc86..0f86efc 100644 --- a/spring-brick/pom.xml +++ b/spring-brick/pom.xml @@ -7,7 +7,7 @@ spring-brick-parent com.gitee.starblues - 3.0.3 + 3.0.4 spring-brick diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/simple/SimpleDevelopmentModeInitializer.java b/spring-brick/src/main/java/com/gitee/starblues/integration/simple/SimpleDevelopmentModeInitializer.java new file mode 100644 index 0000000..da477cb --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/simple/SimpleDevelopmentModeInitializer.java @@ -0,0 +1,24 @@ +package com.gitee.starblues.integration.simple; + +import com.gitee.starblues.core.DefaultPluginManager; +import com.gitee.starblues.core.PluginManager; +import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * @author starBlues + * @version 1.0 + */ +public class SimpleDevelopmentModeInitializer implements ApplicationContextInitializer { + @Override + public void initialize(ConfigurableApplicationContext applicationContext) { + GeneralUrlClassLoader classLoader = (GeneralUrlClassLoader)Thread.currentThread().getContextClassLoader(); + try { + classLoader.addUrl("D:\\etc\\kitte\\ksm\\springboot-plugin-framework-parent\\springboot-plugin-framework-example\\example-plugins-basic\\example-basic-1\\target\\classes"); + classLoader.addUrl("D:\\etc\\kitte\\ksm\\springboot-plugin-framework-parent\\spring-brick-bootstrap\\target\\classes"); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/spring-brick/src/main/resources/META-INF/spring.factories b/spring-brick/src/main/resources/META-INF/spring.factories index cc6e350..36a4620 100644 --- a/spring-brick/src/main/resources/META-INF/spring.factories +++ b/spring-brick/src/main/resources/META-INF/spring.factories @@ -1,2 +1,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.gitee.starblues.integration.SpringBootPluginStarter \ No newline at end of file + com.gitee.starblues.integration.SpringBootPluginStarter + +# Application Context Initializers +org.springframework.context.ApplicationContextInitializer=\ +com.gitee.starblues.integration.simple.SimpleDevelopmentModeInitializer \ No newline at end of file -- Gitee From 0812bcd4baef3b7d55c19a2852222faab7838f69 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Thu, 30 Jun 2022 18:27:32 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=8C=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootstrap/AutowiredTypeResolver.java | 46 ++------ .../bootstrap/ConfigurePluginEnvironment.java | 15 ++- .../DefaultAutowiredTypeResolver.java | 76 ++++++++++++ .../bootstrap/DefaultSpringPluginHook.java | 7 +- .../EmptyMainApplicationContext.java | 10 ++ .../bootstrap/PluginApplicationContext.java | 2 - .../PluginDisableAutoConfiguration.java | 98 ++++++++++++---- .../bootstrap/PluginListableBeanFactory.java | 11 +- .../bootstrap/PluginSpringApplication.java | 24 ++-- .../bootstrap/SpringPluginBootstrap.java | 24 ++-- .../bootstrap/launcher/BootstrapLauncher.java | 38 ++++++ .../launcher/BootstrapLauncherFactory.java | 37 ++++++ .../launcher/CoexistBootstrapLauncher.java | 108 ++++++++++++++++++ .../DefaultBootstrapLauncherFactory.java | 55 +++++++++ .../launcher/IsolationBootstrapLauncher.java | 58 ++++++++++ .../bootstrap/processor/ProcessorContext.java | 1 + .../web/PluginControllerProcessor.java | 4 +- .../starblues/loader/DevelopmentMode.java | 19 ++- .../loader/launcher/AbstractMainLauncher.java | 54 ++++++++- .../launcher/DevelopmentModeSetting.java | 38 ++++++ .../launcher/MainJarOuterProgramLauncher.java | 5 + .../launcher/MainJarProgramLauncher.java | 5 + .../loader/launcher/MainProgramLauncher.java | 5 + .../loader/launcher/SpringBootstrap.java | 2 +- .../loader/launcher/SpringMainBootstrap.java | 6 +- .../starblues/core/PluginLauncherManager.java | 14 ++- .../plugin/PluginCoexistLauncher.java | 60 ++++++++++ ...cher.java => PluginIsolationLauncher.java} | 16 ++- .../integration/SpringBootPluginStarter.java | 1 - .../application/AutoPluginApplication.java | 7 +- .../application/DefaultPluginApplication.java | 4 +- .../operator/DefaultPluginOperator.java | 6 +- .../starblues/spring/ApplicationContext.java | 9 +- .../spring/ApplicationContextProxy.java | 1 + .../spring/GenericApplicationContext.java | 11 ++ .../spring/MainApplicationContext.java | 6 + .../spring/MainApplicationContextProxy.java | 10 ++ .../web/PluginStaticResourceResolver.java | 2 +- .../main/resources/META-INF/spring.factories | 6 +- ...ltPluginIsolationLauncherCheckerTest.java} | 2 +- 40 files changed, 773 insertions(+), 130 deletions(-) create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultAutowiredTypeResolver.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncher.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncherFactory.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java create mode 100644 spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java rename spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/{PluginLauncher.java => PluginIsolationLauncher.java} (91%) rename spring-brick/src/test/java/com/gitee/starblues/core/checker/{DefaultPluginLauncherCheckerTest.java => DefaultPluginIsolationLauncherCheckerTest.java} (97%) diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/AutowiredTypeResolver.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/AutowiredTypeResolver.java index cdbf345..3dd0603 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/AutowiredTypeResolver.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/AutowiredTypeResolver.java @@ -31,43 +31,15 @@ import java.util.Set; /** * @author starBlues * @since 3.0.3 - * @version 3.0.3 + * @version 3.0.4 */ -public class AutowiredTypeResolver { - - private final Set classDefiners; - private final PathMatcher pathMatcher = new AntPathMatcher(); - - - public AutowiredTypeResolver(ProcessorContext processorContext) { - AutowiredTypeDefiner autowiredTypeDefiner = processorContext.getSpringPluginBootstrap().autowiredTypeDefiner(); - if(autowiredTypeDefiner != null){ - AutowiredTypeDefinerConfig definerConfig = new AutowiredTypeDefinerConfig(); - autowiredTypeDefiner.config(definerConfig); - classDefiners = definerConfig.getClassDefiners(); - } else { - classDefiners = Collections.emptySet(); - } - } - - public AutowiredType.Type resolve(DependencyDescriptor descriptor){ - String name = descriptor.getDependencyType().getName(); - String classNamePath = UrlUtils.formatMatchUrl(name); - for (AutowiredTypeDefiner.ClassDefiner classDefiner : classDefiners) { - Set classNamePatterns = classDefiner.getClassNamePatterns(); - for (String classNamePattern : classNamePatterns) { - if(pathMatcher.match(classNamePattern, classNamePath)){ - return classDefiner.getAutowiredType(); - } - } - } - AutowiredType autowiredType = descriptor.getAnnotation(AutowiredType.class); - if(autowiredType != null){ - return autowiredType.value(); - } else { - return AutowiredType.Type.PLUGIN; - } - } - +public interface AutowiredTypeResolver { + + /** + * 通过 descriptor 获取注入类型 + * @param descriptor descriptor + * @return AutowiredType.Type + */ + AutowiredType.Type resolve(DependencyDescriptor descriptor); } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java index 67ba594..c481f0e 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java @@ -19,6 +19,7 @@ package com.gitee.starblues.bootstrap; import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.integration.AutoIntegrationConfiguration; +import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.utils.Assert; import com.gitee.starblues.utils.FilesUtils; import com.gitee.starblues.utils.ObjectUtils; @@ -36,7 +37,7 @@ import java.util.Map; * @author starBlues * @version 3.0.0 */ -class ConfigurePluginEnvironment { +public class ConfigurePluginEnvironment { private final static String PLUGIN_PROPERTY_NAME = "pluginPropertySources"; @@ -53,13 +54,13 @@ class ConfigurePluginEnvironment { private final ProcessorContext processorContext; private final InsidePluginDescriptor pluginDescriptor; - ConfigurePluginEnvironment(ProcessorContext processorContext) { + public ConfigurePluginEnvironment(ProcessorContext processorContext) { this.processorContext = Assert.isNotNull(processorContext, "processorContext 不能为空"); this.pluginDescriptor = Assert.isNotNull(processorContext.getPluginDescriptor(), "pluginDescriptor 不能为空"); } - void configureEnvironment(ConfigurableEnvironment environment, String[] args) { + public void configureEnvironment(ConfigurableEnvironment environment, String[] args) { Map env = new HashMap<>(); String pluginId = pluginDescriptor.getPluginId(); String configFileName = pluginDescriptor.getConfigFileName(); @@ -75,13 +76,19 @@ class ConfigurePluginEnvironment { env.put(SPRING_ADMIN_JMX_NAME, SPRING_ADMIN_JMX_VALUE + pluginId); env.put(REGISTER_SHUTDOWN_HOOK_PROPERTY, false); env.put(MBEAN_DOMAIN_PROPERTY_NAME, pluginId); - environment.getPropertySources().addFirst(new MapPropertySource(PLUGIN_PROPERTY_NAME, env)); if(processorContext.runMode() == ProcessorContext.RunMode.ONESELF){ ConfigureMainPluginEnvironment configureMainPluginEnvironment = new ConfigureMainPluginEnvironment(processorContext); configureMainPluginEnvironment.configureEnvironment(environment, args); + env.put(AutoIntegrationConfiguration.ENABLE_STARTER_KEY, false); + } + + if(DevelopmentModeSetting.coexist()){ + env.put(AutoIntegrationConfiguration.ENABLE_STARTER_KEY, false); } + + environment.getPropertySources().addFirst(new MapPropertySource(PLUGIN_PROPERTY_NAME, env)); } private String getConfigFileLocation(String configFileLocation){ diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultAutowiredTypeResolver.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultAutowiredTypeResolver.java new file mode 100644 index 0000000..0c62ebf --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultAutowiredTypeResolver.java @@ -0,0 +1,76 @@ +/** + * 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.bootstrap; + +import com.gitee.starblues.bootstrap.annotation.AutowiredType; +import com.gitee.starblues.bootstrap.processor.ProcessorContext; +import com.gitee.starblues.bootstrap.realize.AutowiredTypeDefiner; +import com.gitee.starblues.utils.UrlUtils; +import org.springframework.beans.factory.config.DependencyDescriptor; +import org.springframework.util.AntPathMatcher; +import org.springframework.util.PathMatcher; + +import java.util.Collections; +import java.util.Set; + +/** + * 默认的 AutowiredTypeResolver 实现 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class DefaultAutowiredTypeResolver implements AutowiredTypeResolver{ + + + private final Set classDefiners; + private final PathMatcher pathMatcher = new AntPathMatcher(); + + + public DefaultAutowiredTypeResolver(ProcessorContext processorContext) { + AutowiredTypeDefiner autowiredTypeDefiner = processorContext.getSpringPluginBootstrap().autowiredTypeDefiner(); + if(autowiredTypeDefiner != null){ + AutowiredTypeDefinerConfig definerConfig = new AutowiredTypeDefinerConfig(); + autowiredTypeDefiner.config(definerConfig); + classDefiners = definerConfig.getClassDefiners(); + } else { + classDefiners = Collections.emptySet(); + } + } + + @Override + public AutowiredType.Type resolve(DependencyDescriptor descriptor){ + String name = descriptor.getDependencyType().getName(); + String classNamePath = UrlUtils.formatMatchUrl(name); + for (AutowiredTypeDefiner.ClassDefiner classDefiner : classDefiners) { + Set classNamePatterns = classDefiner.getClassNamePatterns(); + for (String classNamePattern : classNamePatterns) { + if(pathMatcher.match(classNamePattern, classNamePath)){ + return classDefiner.getAutowiredType(); + } + } + } + AutowiredType autowiredType = descriptor.getAnnotation(AutowiredType.class); + if(autowiredType != null){ + return autowiredType.value(); + } else { + return AutowiredType.Type.PLUGIN; + } + } + + +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java index 7e47ff4..6908c12 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java @@ -38,12 +38,13 @@ import java.util.Map; /** * 默认的插件钩子器 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.0.4 */ public class DefaultSpringPluginHook implements SpringPluginHook { - private final SpringPluginProcessor pluginProcessor; - private final ProcessorContext processorContext; + protected final SpringPluginProcessor pluginProcessor; + protected final ProcessorContext processorContext; private final StopValidator stopValidator; public DefaultSpringPluginHook(SpringPluginProcessor pluginProcessor, 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 7608046..b83c3f8 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 @@ -39,6 +39,11 @@ public class EmptyMainApplicationContext implements MainApplicationContext { return springBeanFactory; } + @Override + public Object getSourceBeanFactory() { + return null; + } + @Override public void close() throws Exception { @@ -64,4 +69,9 @@ public class EmptyMainApplicationContext implements MainApplicationContext { return false; } + @Override + public Object getSourceApplicationContext() { + return null; + } + } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginApplicationContext.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginApplicationContext.java index b6c9cb1..be1181a 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginApplicationContext.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginApplicationContext.java @@ -20,8 +20,6 @@ import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.core.descriptor.PluginDescriptor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.boot.web.context.WebServerApplicationContext; -import org.springframework.boot.web.server.WebServer; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java index 7c69c39..40e0d16 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java @@ -16,6 +16,7 @@ package com.gitee.starblues.bootstrap; +import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.utils.ObjectUtils; import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter; import org.springframework.boot.autoconfigure.AutoConfigurationMetadata; @@ -31,42 +32,91 @@ import java.util.List; */ public class PluginDisableAutoConfiguration implements AutoConfigurationImportFilter { - private static final List DISABLE_FUZZY_CLASSES = new ArrayList<>(); - public PluginDisableAutoConfiguration(){ - addDisableFuzzyClasses(); + private static final ThreadLocal LAUNCH_PLUGIN = new ThreadLocal(); + + public static void setLaunchPlugin() { + LAUNCH_PLUGIN.set(true); } - private void addDisableFuzzyClasses() { - DISABLE_FUZZY_CLASSES.add("org.springframework.boot.autoconfigure.http"); - DISABLE_FUZZY_CLASSES.add("org.springframework.boot.autoconfigure.web"); - DISABLE_FUZZY_CLASSES.add("org.springframework.boot.autoconfigure.websocket"); - DISABLE_FUZZY_CLASSES.add("org.springframework.boot.autoconfigure.jackson"); - DISABLE_FUZZY_CLASSES.add("org.springframework.boot.autoconfigure.webservices"); + @Override + public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) { + if(DevelopmentModeSetting.isolation()){ + return new IsolationDisableAutoConfiguration().match(autoConfigurationClasses, autoConfigurationMetadata); + } else if(DevelopmentModeSetting.coexist()){ + return new CoexistDisableAutoConfiguration().match(autoConfigurationClasses, autoConfigurationMetadata); + } else { + boolean[] permitAll = new boolean[autoConfigurationClasses.length]; + for (int i = 0; i < autoConfigurationClasses.length; i++) { + permitAll[i] = true; + } + return permitAll; + } } - public static boolean isDisabled(String className){ - if(ObjectUtils.isEmpty(className)){ + + private static class IsolationDisableAutoConfiguration implements AutoConfigurationImportFilter{ + + private final List disableFuzzyClass = new ArrayList<>(); + + IsolationDisableAutoConfiguration(){ + addDisableFuzzyClasses(); + } + + private void addDisableFuzzyClasses() { + disableFuzzyClass.add("org.springframework.boot.autoconfigure.http"); + disableFuzzyClass.add("org.springframework.boot.autoconfigure.web"); + disableFuzzyClass.add("org.springframework.boot.autoconfigure.websocket"); + disableFuzzyClass.add("org.springframework.boot.autoconfigure.jackson"); + disableFuzzyClass.add("org.springframework.boot.autoconfigure.webservices"); + } + + private boolean isDisabled(String className){ + if(ObjectUtils.isEmpty(className)){ + return false; + } + for (String disableFuzzyClass : disableFuzzyClass) { + if (className.contains(disableFuzzyClass)) { + return true; + } + } return false; } - for (String disableFuzzyClass : DISABLE_FUZZY_CLASSES) { - if (className.contains(disableFuzzyClass)) { - return true; + + @Override + public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) { + boolean[] match = new boolean[autoConfigurationClasses.length]; + for (int i = 0; i < autoConfigurationClasses.length; i++) { + String autoConfigurationClass = autoConfigurationClasses[i]; + if(autoConfigurationClass == null || "".equals(autoConfigurationClass)){ + continue; + } + match[i] = !isDisabled(autoConfigurationClass); } + return match; } - return false; } - @Override - public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) { - boolean[] match = new boolean[autoConfigurationClasses.length]; - for (int i = 0; i < autoConfigurationClasses.length; i++) { - String autoConfigurationClass = autoConfigurationClasses[i]; - if(autoConfigurationClass == null || "".equals(autoConfigurationClass)){ - continue; + + private static class CoexistDisableAutoConfiguration implements AutoConfigurationImportFilter{ + + @Override + public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) { + Boolean launchPlugin = LAUNCH_PLUGIN.get(); + boolean[] match = new boolean[autoConfigurationClasses.length]; + try { + if(launchPlugin != null && launchPlugin){ + return match; + } else { + for (int i = 0; i < autoConfigurationClasses.length; i++) { + match[i] = true; + } + } + return match; + } finally { + LAUNCH_PLUGIN.remove(); } - match[i] = !isDisabled(autoConfigurationClass); } - return match; } + } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginListableBeanFactory.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginListableBeanFactory.java index 76e8f7f..b9ff5c4 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginListableBeanFactory.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginListableBeanFactory.java @@ -22,6 +22,7 @@ import com.gitee.starblues.bootstrap.utils.DestroyUtils; import com.gitee.starblues.spring.MainApplicationContext; import com.gitee.starblues.spring.SpringBeanFactory; import com.gitee.starblues.utils.ReflectionUtils; +import lombok.Setter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; @@ -49,11 +50,17 @@ public class PluginListableBeanFactory extends DefaultListableBeanFactory { private static final Logger LOG = LoggerFactory.getLogger(PluginListableBeanFactory.class); private final MainApplicationContext applicationContext; - private final AutowiredTypeResolver autowiredTypeResolver; + + @Setter + private AutowiredTypeResolver autowiredTypeResolver; public PluginListableBeanFactory(ProcessorContext processorContext) { this.applicationContext = processorContext.getMainApplicationContext(); - this.autowiredTypeResolver = new AutowiredTypeResolver(processorContext); + this.autowiredTypeResolver = getAutowiredTypeResolver(processorContext); + } + + protected AutowiredTypeResolver getAutowiredTypeResolver(ProcessorContext processorContext){ + return new DefaultAutowiredTypeResolver(processorContext); } @SuppressWarnings("unchecked") 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 d923a1b..3eabf8f 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 @@ -21,11 +21,14 @@ import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; import com.gitee.starblues.spring.ApplicationContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; +import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.StandardEnvironment; @@ -34,22 +37,21 @@ import org.springframework.core.io.ResourceLoader; /** * 插件SpringApplication实现 * @author starBlues - * @version 3.0.3 + * @since 3.0.0 + * @version 3.0.4 */ public class PluginSpringApplication extends SpringApplication { private final Logger logger = LoggerFactory.getLogger(PluginSpringApplication.class); - private final ProcessorContext.RunMode runMode; - - private final SpringPluginProcessor pluginProcessor; - private final ProcessorContext processorContext; + protected final SpringPluginProcessor pluginProcessor; + protected final ProcessorContext processorContext; + private final ProcessorContext.RunMode runMode; + private final ConfigurePluginEnvironment configurePluginEnvironment; private final GenericApplicationContext applicationContext; - - private final DefaultListableBeanFactory beanFactory; private final ResourceLoader resourceLoader; - private final ConfigurePluginEnvironment configurePluginEnvironment; + public PluginSpringApplication(SpringPluginProcessor pluginProcessor, ProcessorContext processorContext, @@ -59,7 +61,6 @@ public class PluginSpringApplication extends SpringApplication { this.pluginProcessor = pluginProcessor; this.processorContext = processorContext; this.resourceLoader = processorContext.getResourceLoader(); - this.beanFactory = new PluginListableBeanFactory(processorContext); this.configurePluginEnvironment = new ConfigurePluginEnvironment(processorContext); this.applicationContext = getApplicationContext(); setDefaultPluginConfig(); @@ -69,6 +70,7 @@ public class PluginSpringApplication extends SpringApplication { if(runMode == ProcessorContext.RunMode.ONESELF){ return (GenericApplicationContext) super.createApplicationContext(); } + DefaultListableBeanFactory beanFactory = getBeanFactory(processorContext); if(processorContext.getMainApplicationContext().isWebEnvironment()){ return new PluginWebApplicationContext(beanFactory, processorContext); } else { @@ -76,6 +78,10 @@ public class PluginSpringApplication extends SpringApplication { } } + protected DefaultListableBeanFactory getBeanFactory(ProcessorContext processorContext){ + return new PluginListableBeanFactory(processorContext); + } + public void setDefaultPluginConfig(){ if(runMode == ProcessorContext.RunMode.PLUGIN){ setResourceLoader(resourceLoader); diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrap.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrap.java index 14d882a..d8a05b9 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrap.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrap.java @@ -16,13 +16,17 @@ package com.gitee.starblues.bootstrap; +import com.gitee.starblues.bootstrap.launcher.*; import com.gitee.starblues.bootstrap.processor.ComposeSpringPluginProcessor; import com.gitee.starblues.bootstrap.processor.DefaultProcessorContext; import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; import com.gitee.starblues.bootstrap.realize.AutowiredTypeDefiner; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; +import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.spring.SpringPluginHook; +import lombok.Getter; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import java.util.ArrayList; import java.util.List; @@ -35,12 +39,16 @@ import java.util.List; */ public abstract class SpringPluginBootstrap { + @Getter private ProcessorContext.RunMode runMode = ProcessorContext.RunMode.ONESELF; - + @Getter private volatile PluginInteractive pluginInteractive; - + @Getter private final List customPluginProcessors = new ArrayList<>(); + private final BootstrapLauncherFactory launcherFactory = new DefaultBootstrapLauncherFactory(); + + public final SpringPluginHook run(String[] args){ return run(this.getClass(), args); } @@ -56,16 +64,8 @@ public abstract class SpringPluginBootstrap { private SpringPluginHook start(Class[] primarySources, String[] args){ createPluginInteractive(); addCustomSpringPluginProcessor(); - SpringPluginProcessor pluginProcessor = new ComposeSpringPluginProcessor(runMode, customPluginProcessors); - ProcessorContext processorContext = new DefaultProcessorContext( - runMode, this, pluginInteractive, this.getClass() - ); - PluginSpringApplication springApplication = new PluginSpringApplication( - pluginProcessor, - processorContext, - primarySources); - springApplication.run(args); - return new DefaultSpringPluginHook(pluginProcessor, processorContext); + BootstrapLauncher bootstrapLauncher = launcherFactory.create(this); + return bootstrapLauncher.launch(primarySources, args); } public final SpringPluginBootstrap setPluginInteractive(PluginInteractive pluginInteractive) { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncher.java new file mode 100644 index 0000000..6ed280a --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncher.java @@ -0,0 +1,38 @@ +/** + * 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.bootstrap.launcher; + +import com.gitee.starblues.spring.SpringPluginHook; + +/** + * 插件启动器 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public interface BootstrapLauncher { + + /** + * 启动插件 + * @param primarySources 主启动类 + * @param args 启动参数 + * @return SpringPluginHook + */ + SpringPluginHook launch(Class[] primarySources, String[] args); + +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncherFactory.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncherFactory.java new file mode 100644 index 0000000..42292ad --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncherFactory.java @@ -0,0 +1,37 @@ +/** + * 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.bootstrap.launcher; + +import com.gitee.starblues.bootstrap.SpringPluginBootstrap; + +/** + * BootstrapLauncher 创造工厂 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public interface BootstrapLauncherFactory { + + /** + * 创造 BootstrapLauncher + * @param bootstrap SpringPluginBootstrap + * @return BootstrapLauncher + */ + BootstrapLauncher create(SpringPluginBootstrap bootstrap); + +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java new file mode 100644 index 0000000..e930959 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java @@ -0,0 +1,108 @@ +/** + * 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.bootstrap.launcher; + +import com.gitee.starblues.bootstrap.*; +import com.gitee.starblues.bootstrap.annotation.AutowiredType; +import com.gitee.starblues.bootstrap.processor.DefaultProcessorContext; +import com.gitee.starblues.bootstrap.processor.ProcessorContext; +import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; +import com.gitee.starblues.core.launcher.plugin.PluginInteractive; +import com.gitee.starblues.spring.SpringPluginHook; +import lombok.AllArgsConstructor; +import org.springframework.beans.factory.config.DependencyDescriptor; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; +import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * Coexist 类型启动器 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + * @see com.gitee.starblues.loader.DevelopmentMode#COEXIST + */ +@AllArgsConstructor +public class CoexistBootstrapLauncher implements BootstrapLauncher{ + + private final SpringPluginBootstrap bootstrap; + private final SpringPluginProcessor pluginProcessor; + private final PluginInteractive pluginInteractive; + + @Override + public SpringPluginHook launch(Class[] primarySources, String[] args) { + ProcessorContext processorContext = new DefaultProcessorContext( + bootstrap.getRunMode(), bootstrap, pluginInteractive, bootstrap.getClass() + ); + CoexistSpringApplication springApplication = new CoexistSpringApplication( + pluginProcessor, + processorContext, + primarySources); + springApplication.run(args); + return new DefaultSpringPluginHook(pluginProcessor, processorContext); + } + + private static class CoexistSpringApplication extends PluginSpringApplication{ + + public CoexistSpringApplication(SpringPluginProcessor pluginProcessor, ProcessorContext processorContext, Class... primarySources) { + super(pluginProcessor, processorContext, primarySources); + } + + @Override + protected DefaultListableBeanFactory getBeanFactory(ProcessorContext processorContext) { + return new CoexistPluginListableBeanFactory(processorContext); + } + + @Override + protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) { + super.configureEnvironment(environment, args); + } + + } + + private static class CoexistPluginListableBeanFactory extends PluginListableBeanFactory{ + + public CoexistPluginListableBeanFactory(ProcessorContext processorContext) { + super(processorContext); + } + + @Override + protected AutowiredTypeResolver getAutowiredTypeResolver(ProcessorContext processorContext) { + return new CoexistAutowiredTypeResolver(); + } + } + + private static class CoexistAutowiredTypeResolver implements AutowiredTypeResolver{ + + @Override + public AutowiredType.Type resolve(DependencyDescriptor descriptor) { + AutowiredType autowiredType = descriptor.getAnnotation(AutowiredType.class); + if(autowiredType != null){ + return autowiredType.value(); + } else { + return AutowiredType.Type.PLUGIN; + } + } + } + +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java new file mode 100644 index 0000000..b4de874 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java @@ -0,0 +1,55 @@ +/** + * 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.bootstrap.launcher; + +import com.gitee.starblues.bootstrap.PluginDisableAutoConfiguration; +import com.gitee.starblues.bootstrap.SpringPluginBootstrap; +import com.gitee.starblues.bootstrap.processor.ComposeSpringPluginProcessor; +import com.gitee.starblues.bootstrap.processor.ProcessorContext; +import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; +import com.gitee.starblues.core.launcher.plugin.PluginInteractive; +import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; +import org.springframework.boot.context.config.ConfigDataLoader; +import org.springframework.core.io.support.SpringFactoriesLoader; + +import java.util.List; + +/** + * 默认的 BootstrapLauncher 创造工厂 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class DefaultBootstrapLauncherFactory implements BootstrapLauncherFactory{ + @Override + public BootstrapLauncher create(SpringPluginBootstrap bootstrap) { + PluginDisableAutoConfiguration.setLaunchPlugin(); + ProcessorContext.RunMode runMode = bootstrap.getRunMode(); + List customPluginProcessors = bootstrap.getCustomPluginProcessors(); + PluginInteractive pluginInteractive = bootstrap.getPluginInteractive(); + + SpringPluginProcessor pluginProcessor = new ComposeSpringPluginProcessor(runMode, customPluginProcessors); + BootstrapLauncher bootstrapLauncher = null; + if(DevelopmentModeSetting.isolation()){ + bootstrapLauncher = new IsolationBootstrapLauncher(bootstrap, pluginProcessor, pluginInteractive); + } else { + bootstrapLauncher = new CoexistBootstrapLauncher(bootstrap, pluginProcessor, pluginInteractive); + } + return bootstrapLauncher; + } +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java new file mode 100644 index 0000000..6d4fde7 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java @@ -0,0 +1,58 @@ +/** + * 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.bootstrap.launcher; + +import com.gitee.starblues.bootstrap.DefaultSpringPluginHook; +import com.gitee.starblues.bootstrap.PluginSpringApplication; +import com.gitee.starblues.bootstrap.SpringPluginBootstrap; +import com.gitee.starblues.bootstrap.processor.DefaultProcessorContext; +import com.gitee.starblues.bootstrap.processor.ProcessorContext; +import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; +import com.gitee.starblues.core.launcher.plugin.PluginInteractive; +import com.gitee.starblues.spring.SpringPluginHook; +import lombok.AllArgsConstructor; + +/** + * isolation 模式插件启动器 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + * @see com.gitee.starblues.loader.DevelopmentMode#ISOLATION + */ +@AllArgsConstructor +public class IsolationBootstrapLauncher implements BootstrapLauncher{ + + private final SpringPluginBootstrap bootstrap; + private final SpringPluginProcessor pluginProcessor; + private final PluginInteractive pluginInteractive; + + @Override + public SpringPluginHook launch(Class[] primarySources, String[] args) { + ProcessorContext.RunMode runMode = bootstrap.getRunMode(); + + ProcessorContext processorContext = new DefaultProcessorContext( + runMode, bootstrap, pluginInteractive, bootstrap.getClass() + ); + PluginSpringApplication springApplication = new PluginSpringApplication( + pluginProcessor, + processorContext, + primarySources); + springApplication.run(args); + return new DefaultSpringPluginHook(pluginProcessor, processorContext); + } +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java index 42cec5b..e9c23b4 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java @@ -132,6 +132,7 @@ public interface ProcessorContext extends RegistryInfo { /** * 插件独立运行 */ + @Deprecated ONESELF } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginControllerProcessor.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginControllerProcessor.java index 3840ab5..bfba726 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginControllerProcessor.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/web/PluginControllerProcessor.java @@ -81,8 +81,8 @@ public class PluginControllerProcessor implements SpringPluginProcessor { return; } GenericApplicationContext applicationContext = processorContext.getApplicationContext(); - applicationContext.registerBean("pluginControllerPostProcessor", - ControllerPostProcessor.class, ()-> new ControllerPostProcessor(processorContext)); + applicationContext.getDefaultListableBeanFactory() + .addBeanPostProcessor(new ControllerPostProcessor(processorContext)); } @Override diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java index 8f88e5d..cd50338 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java @@ -10,14 +10,19 @@ package com.gitee.starblues.loader; public enum DevelopmentMode { /** - * 简单模式 + * 隔离模式 */ - SIMPLE("simple"), + ISOLATION("isolation"), /** - * 动态模式 + * 共存模式 + */ + COEXIST("coexist"), + + /** + * 简单模式 */ - DYNAMIC("dynamic"); + SIMPLE("simple"); private final String developmentMode; @@ -35,10 +40,12 @@ public enum DevelopmentMode { } public static DevelopmentMode byName(String model){ - if(SIMPLE.name().equalsIgnoreCase(model)){ + if(COEXIST.getDevelopmentMode().equalsIgnoreCase(model)){ + return DevelopmentMode.ISOLATION; + } else if(SIMPLE.getDevelopmentMode().equalsIgnoreCase(model)){ return DevelopmentMode.SIMPLE; } else { - return DevelopmentMode.DYNAMIC; + return DevelopmentMode.ISOLATION; } } } 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 index f68fbd3..b49225b 100644 --- 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 @@ -16,6 +16,8 @@ package com.gitee.starblues.loader.launcher; +import com.gitee.starblues.loader.DevelopmentMode; + /** * 抽象的启动引导者 * @author starBlues @@ -23,18 +25,66 @@ package com.gitee.starblues.loader.launcher; */ public abstract class AbstractMainLauncher extends AbstractLauncher { + + /** + * SpringPluginBootstrap 包名称 + * @since 3.0.4 + */ + private final static String SPRING_PLUGIN_BOOTSTRAP_PACKAGE_NAME = "com.gitee.starblues.bootstrap.SpringPluginBootstrap"; + + /** + * SpringPluginBootstrap 依赖坐标 + * @since 3.0.4 + */ + private final static String SPRING_PLUGIN_BOOTSTRAP_COORDINATE = "com.gitee.starblues:spring-brick-bootstrap"; + + @Override public R run(String... args) throws Exception { ClassLoader classLoader = createClassLoader(args); + if(!resolveThreadClassLoader()){ + return toLaunch(classLoader, args); + } Thread thread = Thread.currentThread(); ClassLoader oldClassLoader = thread.getContextClassLoader(); try { thread.setContextClassLoader(classLoader); - LauncherContext.setMainClassLoader(classLoader); - return launch(classLoader, args); + return toLaunch(classLoader, args); } finally { thread.setContextClassLoader(oldClassLoader); } } + protected R toLaunch(ClassLoader classLoader, String... args) throws Exception{ + LauncherContext.setMainClassLoader(classLoader); + checkSpringPluginBootstrap(classLoader); + return launch(classLoader, args); + } + + protected boolean resolveThreadClassLoader(){ + return true; + } + + /** + * 检查 {@link this#SPRING_PLUGIN_BOOTSTRAP_COORDINATE} 依赖是否配置合适 + * @param classLoader 当前主程序classloader + * @throws RuntimeException 检查一次 + */ + private void checkSpringPluginBootstrap(ClassLoader classLoader) throws RuntimeException{ + try { + classLoader.loadClass(SPRING_PLUGIN_BOOTSTRAP_PACKAGE_NAME); + if(DevelopmentModeSetting.isolation()){ + // 主程序加载到了 + throw new RuntimeException("[" + DevelopmentMode.ISOLATION.getDevelopmentMode() + "]模式下" + + "不能将[" + SPRING_PLUGIN_BOOTSTRAP_COORDINATE + "]依赖定义到主程序中, 只能依赖到插件中!"); + } + } catch (ClassNotFoundException e) { + if(!DevelopmentModeSetting.isolation()){ + String mode = DevelopmentMode.COEXIST.getDevelopmentMode() + "/" + DevelopmentMode.SIMPLE.getDevelopmentMode(); + throw new RuntimeException("[" + mode + "]模式" + + "需要将[" + SPRING_PLUGIN_BOOTSTRAP_COORDINATE + "]依赖定义到主程序中!"); + } + } + } + } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java new file mode 100644 index 0000000..6c5cd34 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java @@ -0,0 +1,38 @@ +package com.gitee.starblues.loader.launcher; + +import com.gitee.starblues.loader.DevelopmentMode; + +import java.util.Objects; + +/** + * DevelopmentMode 设置者 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class DevelopmentModeSetting { + + private static DevelopmentMode developmentMode; + + static void setDevelopmentMode(DevelopmentMode developmentMode) { + DevelopmentModeSetting.developmentMode = developmentMode; + } + + public static boolean isolation(){ + return Objects.equals(developmentMode, DevelopmentMode.ISOLATION); + } + + public static boolean coexist(){ + return Objects.equals(developmentMode, DevelopmentMode.COEXIST); + } + + public static boolean simple(){ + return Objects.equals(developmentMode, DevelopmentMode.SIMPLE); + } + + public static DevelopmentMode getDevelopmentMode(){ + return developmentMode; + } + +} 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 index 22249d6..41b1005 100644 --- 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 @@ -63,6 +63,11 @@ public class MainJarOuterProgramLauncher extends MainProgramLauncher{ this.rootJarFile = Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); } + @Override + protected boolean resolveThreadClassLoader() { + return true; + } + @Override protected void addResource(GenericClassLoader classLoader) throws Exception { super.addResource(classLoader); 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 c423b92..c260d17 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 @@ -58,6 +58,11 @@ public class MainJarProgramLauncher extends MainProgramLauncher{ this.rootJarFile = Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); } + @Override + protected boolean resolveThreadClassLoader() { + return true; + } + @Override protected void addResource(GenericClassLoader classLoader) throws Exception { super.addResource(classLoader); 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 00ce88a..e8df24e 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 @@ -41,6 +41,11 @@ public class MainProgramLauncher extends AbstractMainLauncher{ this.methodRunner = methodRunner; } + @Override + protected boolean resolveThreadClassLoader() { + return false; + } + @Override protected ClassLoader createClassLoader(String... args) throws Exception { GenericClassLoader classLoader = new GenericClassLoader(MAIN_CLASS_LOADER_NAME, getParentClassLoader(), diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java index a6e50d6..ee02018 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java @@ -39,7 +39,7 @@ public interface SpringBootstrap { * @return DevelopmentMode */ default DevelopmentMode developmentMode(){ - return DevelopmentMode.DYNAMIC; + return DevelopmentMode.ISOLATION; } } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java index 1d31e04..a13d086 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java @@ -21,7 +21,6 @@ import com.gitee.starblues.loader.jar.JarFile; import com.gitee.starblues.loader.launcher.runner.MainMethodRunner; import com.gitee.starblues.loader.launcher.runner.MethodRunner; -import java.io.File; import java.util.Objects; import java.util.concurrent.CountDownLatch; @@ -50,6 +49,7 @@ public class SpringMainBootstrap { public static void launch(SpringBootstrap springBootstrap, String[] args) { SpringMainBootstrap.springBootstrap = Objects.requireNonNull(springBootstrap, "springBootBootstrap 不能为空"); + DevelopmentModeSetting.setDevelopmentMode(springBootstrap.developmentMode()); MainMethodRunner mainMethodRunner = new MainMethodRunner(SpringMainBootstrap.class.getName(), MAIN_RUN_METHOD, args); JarFile.registerUrlProtocolHandler(); @@ -87,7 +87,9 @@ public class SpringMainBootstrap { Objects.requireNonNull(springBootstrap, "springBootBootstrap 不能为空"); MethodRunner run = new MethodRunner(springBootstrap.getClass().getName(), SPRING_BOOTSTRAP_RUN_METHOD, args); Launcher launcher; - if(springBootstrap.developmentMode() == DevelopmentMode.SIMPLE){ + + DevelopmentMode developmentMode = springBootstrap.developmentMode(); + if(developmentMode == DevelopmentMode.SIMPLE || developmentMode == DevelopmentMode.COEXIST){ launcher = new SimpleModeMainLauncher(run); } else { launcher = new MainProgramLauncher(run); diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java index a7b4f05..ae87521 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java @@ -23,12 +23,15 @@ import com.gitee.starblues.core.exception.PluginException; import com.gitee.starblues.core.exception.PluginProhibitStopException; import com.gitee.starblues.core.launcher.plugin.DefaultPluginInteractive; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; -import com.gitee.starblues.core.launcher.plugin.PluginLauncher; +import com.gitee.starblues.core.launcher.plugin.PluginCoexistLauncher; +import com.gitee.starblues.core.launcher.plugin.PluginIsolationLauncher; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolvedFactory; import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.integration.listener.DefaultPluginListenerFactory; import com.gitee.starblues.integration.listener.PluginListenerFactory; +import com.gitee.starblues.loader.launcher.AbstractLauncher; +import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.spring.MainApplicationContext; import com.gitee.starblues.spring.MainApplicationContextProxy; import com.gitee.starblues.spring.SpringPluginHook; @@ -96,7 +99,12 @@ public class PluginLauncherManager extends DefaultPluginManager{ InsidePluginDescriptor pluginDescriptor = pluginInsideInfo.getPluginDescriptor(); PluginInteractive pluginInteractive = new DefaultPluginInteractive(pluginDescriptor, mainApplicationContext, configuration, invokeSupperCache); - PluginLauncher pluginLauncher = new PluginLauncher(pluginInteractive, pluginLaunchInvolved); + AbstractLauncher pluginLauncher; + if(DevelopmentModeSetting.isolation()){ + pluginLauncher = new PluginIsolationLauncher(pluginInteractive, pluginLaunchInvolved); + } else { + pluginLauncher = new PluginCoexistLauncher(pluginInteractive, pluginLaunchInvolved); + } SpringPluginHook springPluginHook = pluginLauncher.run(); RegistryPluginInfo registryPluginInfo = new RegistryPluginInfo(pluginDescriptor, springPluginHook); registryInfo.put(pluginDescriptor.getPluginId(), registryPluginInfo); @@ -110,6 +118,8 @@ public class PluginLauncherManager extends DefaultPluginManager{ } + + @Override protected void stop(PluginInsideInfo pluginInsideInfo) throws Exception { String pluginId = pluginInsideInfo.getPluginId(); diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java new file mode 100644 index 0000000..b146453 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java @@ -0,0 +1,60 @@ +package com.gitee.starblues.core.launcher.plugin; + +import com.gitee.starblues.common.Constants; +import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; +import com.gitee.starblues.core.descriptor.PluginLibInfo; +import com.gitee.starblues.core.exception.PluginException; +import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; +import com.gitee.starblues.loader.DevelopmentMode; +import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; +import com.gitee.starblues.loader.launcher.AbstractLauncher; +import com.gitee.starblues.spring.SpringPluginHook; + +import java.util.Set; + +/** + * 插件共享式启动引导 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class PluginCoexistLauncher extends AbstractLauncher { + + protected final PluginInteractive pluginInteractive; + protected final PluginLaunchInvolved pluginLaunchInvolved; + + public PluginCoexistLauncher(PluginInteractive pluginInteractive, + PluginLaunchInvolved pluginLaunchInvolved) { + this.pluginInteractive = pluginInteractive; + this.pluginLaunchInvolved = pluginLaunchInvolved; + } + + @Override + protected ClassLoader createClassLoader(String... args) throws Exception { + GeneralUrlClassLoader classLoader = new GeneralUrlClassLoader(this.getClass().getClassLoader()); + InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); + String pluginClassPath = pluginDescriptor.getPluginClassPath(); + classLoader.addUrl(pluginClassPath); + return classLoader; + } + + @Override + protected SpringPluginHook launch(ClassLoader classLoader, String... args) throws Exception { + InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); + pluginLaunchInvolved.before(pluginDescriptor, classLoader); + try { + SpringPluginHook springPluginHook = (SpringPluginHook) new PluginMethodRunner(pluginInteractive) + .run(classLoader); + if(springPluginHook == null){ + throw new PluginException("插件返回的 SpringPluginHook 不能为空"); + } + pluginLaunchInvolved.after(pluginDescriptor, classLoader, springPluginHook); + return new SpringPluginHookWrapper(springPluginHook, pluginDescriptor, pluginLaunchInvolved, classLoader); + } catch (Throwable throwable){ + pluginLaunchInvolved.failure(pluginDescriptor,classLoader, throwable); + throw throwable; + } + } + +} 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/PluginIsolationLauncher.java similarity index 91% rename from spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginLauncher.java rename to spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java index 8ff0e50..0edcdbe 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/PluginIsolationLauncher.java @@ -16,9 +16,12 @@ package com.gitee.starblues.core.launcher.plugin; +import com.gitee.starblues.common.Constants; import com.gitee.starblues.core.classloader.*; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; +import com.gitee.starblues.core.exception.PluginException; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; +import com.gitee.starblues.loader.DevelopmentMode; 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; @@ -35,11 +38,13 @@ import java.util.Map; import java.util.WeakHashMap; /** - * 插件启动引导类 + * 插件隔离式启动引导 + * * @author starBlues - * @version 3.0.3 + * @since 3.0.0 + * @version 3.0.4 */ -public class PluginLauncher extends AbstractLauncher { +public class PluginIsolationLauncher extends AbstractLauncher { private static final Map CLASS_LOADER_CACHE = new WeakHashMap<>(); @@ -49,8 +54,8 @@ public class PluginLauncher extends AbstractLauncher { protected final PluginLaunchInvolved pluginLaunchInvolved; - public PluginLauncher(PluginInteractive pluginInteractive, - PluginLaunchInvolved pluginLaunchInvolved) { + public PluginIsolationLauncher(PluginInteractive pluginInteractive, + PluginLaunchInvolved pluginLaunchInvolved) { this.pluginInteractive = pluginInteractive; this.pluginDescriptor = pluginInteractive.getPluginDescriptor(); this.mainResourceMatcher = getMainResourceMatcher(pluginInteractive); @@ -119,5 +124,4 @@ public class PluginLauncher extends AbstractLauncher { } } - } diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/SpringBootPluginStarter.java b/spring-brick/src/main/java/com/gitee/starblues/integration/SpringBootPluginStarter.java index bb5289d..bf12f77 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/SpringBootPluginStarter.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/SpringBootPluginStarter.java @@ -28,7 +28,6 @@ import org.springframework.context.annotation.Import; * @author starBlues * @version 3.0.0 */ -@Configuration(proxyBeanMethods = true) @EnableConfigurationProperties(AutoIntegrationConfiguration.class) @ConditionalOnExpression("${" + AutoIntegrationConfiguration.ENABLE_STARTER_KEY + ":true}") @Import(AutoPluginApplication.class) diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java b/spring-brick/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java index 7d19440..3d54fef 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java @@ -26,6 +26,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Import; +import org.springframework.context.support.GenericApplicationContext; /** * 自动初始化的 PluginApplication。该PluginApplication 基于 Spring InitializingBean 自动初始化插件。 @@ -57,12 +58,16 @@ public class AutoPluginApplication extends DefaultPluginApplication throw new RuntimeException("Cannot be initialized manually"); } - @Override public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } + @Override + protected void setBeanFactory(GenericApplicationContext applicationContext) { + // 忽略 + } + /** * Spring boot bean属性被Set完后调用。会自动初始化插件 */ diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java b/spring-brick/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java index c193dce..f0c46ce 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java @@ -23,7 +23,6 @@ import com.gitee.starblues.integration.operator.PluginOperator; import com.gitee.starblues.integration.operator.PluginOperatorWrapper; import com.gitee.starblues.integration.user.PluginUser; import com.gitee.starblues.spring.extract.DefaultExtractFactory; -import com.gitee.starblues.spring.extract.DefaultOpExtractFactory; import com.gitee.starblues.spring.extract.ExtractFactory; import com.gitee.starblues.spring.extract.OpExtractFactory; import com.gitee.starblues.utils.ObjectUtils; @@ -150,8 +149,7 @@ public class DefaultPluginApplication extends AbstractPluginApplication { * 直接将 PluginOperator 和 PluginUser 注入到ApplicationContext容器中 * @param applicationContext ApplicationContext */ - @Deprecated - private void setBeanFactory(GenericApplicationContext applicationContext){ + protected void setBeanFactory(GenericApplicationContext applicationContext){ DefaultListableBeanFactory defaultListableBeanFactory = applicationContext.getDefaultListableBeanFactory(); defaultListableBeanFactory.registerSingleton(pluginOperator.getClass().getName(), pluginOperator); defaultListableBeanFactory.registerSingleton(pluginUser.getClass().getName(), pluginUser); diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java index ea44db3..39e2548 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java @@ -60,7 +60,7 @@ public class DefaultPluginOperator implements PluginOperator { private final static DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); - private final AtomicBoolean isInit = new AtomicBoolean(false); + private static final AtomicBoolean IS_INIT = new AtomicBoolean(false); private final GenericApplicationContext applicationContext; private final IntegrationConfiguration configuration; @@ -79,7 +79,7 @@ public class DefaultPluginOperator implements PluginOperator { @Override public synchronized boolean initPlugins(PluginInitializerListener pluginInitializerListener) throws PluginException { - if(isInit.get()){ + if(IS_INIT.get()){ throw new RuntimeException("插件已经被初始化了, 不能再初始化."); } try { @@ -114,7 +114,7 @@ public class DefaultPluginOperator implements PluginOperator { isFoundException = true; } } - isInit.set(true); + IS_INIT.set(true); if(isFoundException){ log.error("插件初始化失败"); pluginInitializerListenerFactory.failure(new PluginException("插件初始化存在异常")); diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContext.java b/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContext.java index 9056235..77c9079 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContext.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContext.java @@ -19,7 +19,8 @@ package com.gitee.starblues.spring; /** * 自定义ApplicationContext * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.0.4 */ public interface ApplicationContext extends AutoCloseable { @@ -29,4 +30,10 @@ public interface ApplicationContext extends AutoCloseable { */ SpringBeanFactory getSpringBeanFactory(); + /** + * 得到原始的BeanFactory + * @return BeanFactory + */ + Object getSourceBeanFactory(); + } diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContextProxy.java b/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContextProxy.java index 706c7a9..88e6ea5 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContextProxy.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContextProxy.java @@ -32,6 +32,7 @@ public class ApplicationContextProxy extends GenericApplicationContext{ public ApplicationContextProxy(Object targetBeanFactory) { super(); setSpringBeanFactory(createSpringBeanFactory(targetBeanFactory)); + setSourcesBeanFactory(targetBeanFactory); } protected SpringBeanFactory createSpringBeanFactory(Object targetBeanFactory){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/GenericApplicationContext.java b/spring-brick/src/main/java/com/gitee/starblues/spring/GenericApplicationContext.java index 922899c..71590fa 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/GenericApplicationContext.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/GenericApplicationContext.java @@ -28,6 +28,7 @@ public class GenericApplicationContext implements ApplicationContext{ public final AutoCloseable autoCloseable; protected SpringBeanFactory springBeanFactory; + protected Object sourcesBeanFactory; public GenericApplicationContext() { this(null); @@ -41,11 +42,21 @@ public class GenericApplicationContext implements ApplicationContext{ this.springBeanFactory = Assert.isNotNull(springBeanFactory, "参数 springBeanFactory 不能为空"); } + public void setSourcesBeanFactory(Object sourcesBeanFactory) { + this.sourcesBeanFactory = sourcesBeanFactory; + } + @Override public SpringBeanFactory getSpringBeanFactory() { return springBeanFactory; } + @Override + public Object getSourceBeanFactory() { + return sourcesBeanFactory; + } + + @Override public void close() throws Exception { if(autoCloseable != null){ 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 b143ef5..b6d6dec 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 @@ -19,6 +19,7 @@ package com.gitee.starblues.spring; import com.gitee.starblues.spring.environment.EnvironmentProvider; import java.util.Map; +import java.util.Objects; /** * 主程序 ApplicationContext 接口 @@ -57,5 +58,10 @@ public interface MainApplicationContext extends ApplicationContext { */ boolean isWebEnvironment(); + /** + * 得到原始的 ApplicationContext + * @return Object + */ + Object getSourceApplicationContext(); } 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 4c5b61b..f876ffe 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 @@ -95,6 +95,16 @@ public class MainApplicationContextProxy extends ApplicationContextProxy impleme return isWebEnvironment; } + @Override + public Object getSourceApplicationContext() { + return applicationContext; + } + + @Override + public Object getSourceBeanFactory() { + return applicationContext.getBeanFactory(); + } + private boolean getIsWebEnvironment(GenericApplicationContext applicationContext){ return applicationContext instanceof AnnotationConfigServletWebServerApplicationContext || applicationContext instanceof AnnotationConfigReactiveWebServerApplicationContext; 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 3a2d192..09232f3 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 @@ -252,7 +252,7 @@ public class PluginStaticResourceResolver extends AbstractResourceResolver { public static synchronized void parse(PluginDescriptor pluginDescriptor, ClassLoader pluginClassLoader, WebConfig webConfig){ - if(!webConfig.isEnable()){ + if(webConfig == null || !webConfig.isEnable()){ return; } final Set locations = webConfig.getResourceLocations(); diff --git a/spring-brick/src/main/resources/META-INF/spring.factories b/spring-brick/src/main/resources/META-INF/spring.factories index 36a4620..cc6e350 100644 --- a/spring-brick/src/main/resources/META-INF/spring.factories +++ b/spring-brick/src/main/resources/META-INF/spring.factories @@ -1,6 +1,2 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.gitee.starblues.integration.SpringBootPluginStarter - -# Application Context Initializers -org.springframework.context.ApplicationContextInitializer=\ -com.gitee.starblues.integration.simple.SimpleDevelopmentModeInitializer \ No newline at end of file + com.gitee.starblues.integration.SpringBootPluginStarter \ No newline at end of file diff --git a/spring-brick/src/test/java/com/gitee/starblues/core/checker/DefaultPluginLauncherCheckerTest.java b/spring-brick/src/test/java/com/gitee/starblues/core/checker/DefaultPluginIsolationLauncherCheckerTest.java similarity index 97% rename from spring-brick/src/test/java/com/gitee/starblues/core/checker/DefaultPluginLauncherCheckerTest.java rename to spring-brick/src/test/java/com/gitee/starblues/core/checker/DefaultPluginIsolationLauncherCheckerTest.java index 09d895d..559b996 100644 --- a/spring-brick/src/test/java/com/gitee/starblues/core/checker/DefaultPluginLauncherCheckerTest.java +++ b/spring-brick/src/test/java/com/gitee/starblues/core/checker/DefaultPluginIsolationLauncherCheckerTest.java @@ -44,7 +44,7 @@ import static org.powermock.api.mockito.PowerMockito.*; * @version 3.0.0 */ @RunWith(PowerMockRunner.class) -public class DefaultPluginLauncherCheckerTest extends TestCase { +public class DefaultPluginIsolationLauncherCheckerTest extends TestCase { private DefaultPluginLauncherChecker launcherChecker; -- Gitee From ca31636a5cd58ff2965f95737e53bc0c426c78fe Mon Sep 17 00:00:00 2001 From: StarBlues Date: Mon, 18 Jul 2022 10:18:10 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E6=89=A9=E5=B1=95classloader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootstrap/DefaultSpringPluginHook.java | 2 + .../PluginDisableAutoConfiguration.java | 15 +++ .../bootstrap/SpringPluginBootstrap.java | 18 ++- .../SpringPluginBootstrapBinder.java | 28 +++++ .../annotation/ResolveClassLoader.java | 16 +++ .../CoexistAllowAutoConfiguration.java | 44 +++++++ .../CoexistResolveClassLoaderAspect.java | 37 ++++++ .../launcher/CoexistBootstrapLauncher.java | 12 +- .../classloader/ClassLoaderTranslator.java | 106 +++++++++++++++++ .../classloader/GeneralUrlClassLoader.java | 64 ++++++++-- .../classloader/GenericClassLoader.java | 41 +++++-- .../loader/classloader/resource/Resource.java | 1 + .../loader/DefaultResourceLoaderFactory.java | 12 +- .../resource/loader/ResourceLoader.java | 9 ++ .../loader/ResourceLoaderFactory.java | 6 +- .../loader/launcher/AbstractMainLauncher.java | 7 +- .../loader/launcher/DevLauncher.java | 37 ++++++ .../starblues/loader/launcher/Launcher.java | 2 + .../loader/launcher/ProdLauncher.java | 109 ++++++++++++++++++ .../loader/launcher/SpringMainBootstrap.java | 11 +- .../launcher/SpringMainProdBootstrap.java | 42 +------ .../launcher/classpath/ClasspathResource.java | 23 ++++ .../classpath/FastJarClasspathResource.java | 57 +++++++++ .../JarOutClasspathResource.java} | 78 +++++-------- .../launcher/coexist/CoexistBaseLauncher.java | 43 +++++++ .../coexist/CoexistFastJarLauncher.java | 65 +++++++++++ .../coexist/CoexistJarOuterLauncher.java | 63 ++++++++++ .../IsolationBaseLauncher.java} | 11 +- .../IsolationFastJarLauncher.java} | 47 ++------ .../isolation/IsolationJarOuterLauncher.java | 60 ++++++++++ .../ResourceLoaderFactoryGetter.java | 2 +- .../SimpleBaseLauncher.java} | 13 ++- .../NestedPluginJarResourceLoader.java | 17 +-- .../PluginGeneralUrlClassLoader.java | 79 +++++++++++++ .../plugin/PluginCoexistLauncher.java | 54 ++++++++- 35 files changed, 1030 insertions(+), 201 deletions(-) create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrapBinder.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java rename spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/{MainJarOuterProgramLauncher.java => classpath/JarOutClasspathResource.java} (57%) create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java rename spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/{MainProgramLauncher.java => isolation/IsolationBaseLauncher.java} (89%) rename spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/{MainJarProgramLauncher.java => isolation/IsolationFastJarLauncher.java} (51%) create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationJarOuterLauncher.java rename spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/{ => isolation}/ResourceLoaderFactoryGetter.java (98%) rename spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/{SimpleModeMainLauncher.java => simple/SimpleBaseLauncher.java} (53%) create mode 100644 spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java index 6908c12..5b24cff 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java @@ -89,6 +89,8 @@ public class DefaultSpringPluginHook implements SpringPluginHook { DestroyUtils.destroyAll(null, SpringFactoriesLoader.class, "cache", Map.class); } catch (Exception e){ e.printStackTrace(); + } finally { + SpringPluginBootstrapBinder.remove(); } } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java index 40e0d16..ae2026e 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java @@ -16,6 +16,7 @@ package com.gitee.starblues.bootstrap; +import com.gitee.starblues.bootstrap.coexist.CoexistAllowAutoConfiguration; import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.utils.ObjectUtils; import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter; @@ -100,12 +101,26 @@ public class PluginDisableAutoConfiguration implements AutoConfigurationImportFi private static class CoexistDisableAutoConfiguration implements AutoConfigurationImportFilter{ + public CoexistDisableAutoConfiguration(){ + + } + @Override public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) { Boolean launchPlugin = LAUNCH_PLUGIN.get(); boolean[] match = new boolean[autoConfigurationClasses.length]; try { if(launchPlugin != null && launchPlugin){ + CoexistAllowAutoConfiguration configuration = SpringPluginBootstrapBinder.get() + .getCoexistAllowAutoConfiguration(); + for (int i = 0; i < autoConfigurationClasses.length; i++) { + String autoConfigurationClass = autoConfigurationClasses[i]; + if(ObjectUtils.isEmpty(autoConfigurationClass)){ + continue; + } + + match[i] = configuration.match(autoConfigurationClass); + } return match; } else { for (int i = 0; i < autoConfigurationClasses.length; i++) { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrap.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrap.java index d8a05b9..096984e 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrap.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrap.java @@ -16,17 +16,14 @@ package com.gitee.starblues.bootstrap; +import com.gitee.starblues.bootstrap.coexist.CoexistAllowAutoConfiguration; import com.gitee.starblues.bootstrap.launcher.*; -import com.gitee.starblues.bootstrap.processor.ComposeSpringPluginProcessor; -import com.gitee.starblues.bootstrap.processor.DefaultProcessorContext; import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; import com.gitee.starblues.bootstrap.realize.AutowiredTypeDefiner; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; -import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.spring.SpringPluginHook; import lombok.Getter; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import java.util.ArrayList; import java.util.List; @@ -45,9 +42,14 @@ public abstract class SpringPluginBootstrap { private volatile PluginInteractive pluginInteractive; @Getter private final List customPluginProcessors = new ArrayList<>(); + @Getter + private final CoexistAllowAutoConfiguration coexistAllowAutoConfiguration = new CoexistAllowAutoConfiguration(); private final BootstrapLauncherFactory launcherFactory = new DefaultBootstrapLauncherFactory(); + public SpringPluginBootstrap() { + SpringPluginBootstrapBinder.set(this); + } public final SpringPluginHook run(String[] args){ return run(this.getClass(), args); @@ -62,6 +64,7 @@ public abstract class SpringPluginBootstrap { } private SpringPluginHook start(Class[] primarySources, String[] args){ + configCoexistAllowAutoConfiguration(this.coexistAllowAutoConfiguration); createPluginInteractive(); addCustomSpringPluginProcessor(); BootstrapLauncher bootstrapLauncher = launcherFactory.create(this); @@ -107,5 +110,12 @@ public abstract class SpringPluginBootstrap { return null; } + /** + * 在 Coexist 模式下手动配置 spring-boot-auto-configuration 类 + * @param configuration 配置的类 + */ + protected void configCoexistAllowAutoConfiguration(CoexistAllowAutoConfiguration configuration){ + + } } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrapBinder.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrapBinder.java new file mode 100644 index 0000000..19f4ad5 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrapBinder.java @@ -0,0 +1,28 @@ +package com.gitee.starblues.bootstrap; + +/** + * SpringPluginBootstrap 实例绑者 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class SpringPluginBootstrapBinder { + + private final static ThreadLocal BINDER = new ThreadLocal<>(); + + + public static SpringPluginBootstrap get(){ + return BINDER.get(); + } + + public static void set(SpringPluginBootstrap bootstrap){ + BINDER.set(bootstrap); + } + + public static void remove(){ + BINDER.remove(); + } + + +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java new file mode 100644 index 0000000..47ae224 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java @@ -0,0 +1,16 @@ +package com.gitee.starblues.bootstrap.annotation; + +import java.lang.annotation.*; + +/** + * 解决方法级别调用时, 当前线程非本插件的ClassLoader注解 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface ResolveClassLoader { +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java new file mode 100644 index 0000000..46c26f6 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java @@ -0,0 +1,44 @@ +package com.gitee.starblues.bootstrap.coexist; + +import com.gitee.starblues.utils.ObjectUtils; + +import java.util.HashSet; +import java.util.Set; + +/** + * Coexist模式下存储当前插件允许的 auto 配置 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class CoexistAllowAutoConfiguration { + + private final Set allowPrefix = new HashSet<>(); + + public CoexistAllowAutoConfiguration(){ + addDefault(); + } + + private void addDefault(){ + allowPrefix.add("org.springframework.boot.autoconfigure.aop.AopAutoConfiguration"); + } + + public CoexistAllowAutoConfiguration add(String autoConfigurationClass){ + if(ObjectUtils.isEmpty(autoConfigurationClass)){ + return this; + } + allowPrefix.add(autoConfigurationClass); + return this; + } + + public boolean match(String autoConfigurationClass){ + for (String prefix : allowPrefix) { + if(autoConfigurationClass.startsWith(prefix)){ + return true; + } + } + return false; + } + +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java new file mode 100644 index 0000000..9654061 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java @@ -0,0 +1,37 @@ +package com.gitee.starblues.bootstrap.coexist; + +import com.gitee.starblues.bootstrap.annotation.ResolveClassLoader; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; + +/** + * Coexist模式下解决当前调用现场非本插件的ClassLoader的切面 + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +@Aspect +public class CoexistResolveClassLoaderAspect { + + @Pointcut("@annotation(com.gitee.starblues.bootstrap.annotation.ResolveClassLoader)") + public void test() { + + } + + @Around("@annotation(resolveClassLoader)") + public Object around(ProceedingJoinPoint pjp, ResolveClassLoader resolveClassLoader) throws Throwable{ + Thread thread = Thread.currentThread(); + ClassLoader oldClassLoader = thread.getContextClassLoader(); + try { + Object target = pjp.getTarget(); + thread.setContextClassLoader(target.getClass().getClassLoader()); + return pjp.proceed(); + } finally { + thread.setContextClassLoader(oldClassLoader); + } + } + +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java index e930959..abd8a2e 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java @@ -18,6 +18,7 @@ package com.gitee.starblues.bootstrap.launcher; import com.gitee.starblues.bootstrap.*; import com.gitee.starblues.bootstrap.annotation.AutowiredType; +import com.gitee.starblues.bootstrap.coexist.CoexistResolveClassLoaderAspect; import com.gitee.starblues.bootstrap.processor.DefaultProcessorContext; import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; @@ -26,11 +27,6 @@ import com.gitee.starblues.spring.SpringPluginHook; import lombok.AllArgsConstructor; import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; -import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; @@ -78,6 +74,12 @@ public class CoexistBootstrapLauncher implements BootstrapLauncher{ super.configureEnvironment(environment, args); } + @Override + protected GenericApplicationContext getApplicationContext() { + PluginApplicationContext applicationContext = (PluginApplicationContext) super.getApplicationContext(); + applicationContext.register(CoexistResolveClassLoaderAspect.class); + return applicationContext; + } } private static class CoexistPluginListableBeanFactory extends PluginListableBeanFactory{ diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java new file mode 100644 index 0000000..28f9697 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java @@ -0,0 +1,106 @@ +package com.gitee.starblues.loader.classloader; + +import com.gitee.starblues.loader.classloader.resource.Resource; +import com.gitee.starblues.loader.classloader.resource.loader.DefaultResource; +import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoader; +import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; + +/** + * 注释 + * + * @author starBlues + * @version 1.0.0 + * @since 1.0.0 + */ +public class ClassLoaderTranslator implements ResourceLoaderFactory { + + private final URLClassLoader classLoader; + + public ClassLoaderTranslator(URLClassLoader classLoader) { + this.classLoader = classLoader; + } + + @Override + public void addResource(String path) throws Exception { + throw new RuntimeException("Does not support!"); + } + + @Override + public void addResource(File file) throws Exception { + throw new RuntimeException("Does not support!"); + } + + @Override + public void addResource(Path path) throws Exception { + throw new RuntimeException("Does not support!"); + } + + @Override + public void addResource(URL url) throws Exception { + throw new RuntimeException("Does not support!"); + } + + @Override + public void addResource(ResourceLoader resourceLoader) throws Exception { + throw new RuntimeException("Does not support!"); + } + + @Override + public Resource findFirstResource(String name) { + URL url = classLoader.getResource(name); + if(url == null){ + return null; + } + return new DefaultResource(name, url, url); + } + + @Override + public Enumeration findAllResource(String name) { + try { + Enumeration resources = classLoader.getResources(name); + return new Enumeration() { + @Override + public boolean hasMoreElements() { + return resources.hasMoreElements(); + } + + @Override + public Resource nextElement() { + URL url = resources.nextElement(); + if(url == null){ + return null; + } + return new DefaultResource(name, url, url); + } + }; + } catch (IOException e) { + return Collections.emptyEnumeration(); + } + } + + @Override + public InputStream getInputStream(String name) { + return classLoader.getResourceAsStream(name); + } + + @Override + public List getUrls() { + return Arrays.asList(classLoader.getURLs()); + } + + @Override + public void close() throws Exception { + + } +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java index f76b416..849a2be 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java @@ -1,11 +1,22 @@ package com.gitee.starblues.loader.classloader; +import com.gitee.starblues.loader.classloader.resource.Resource; +import com.gitee.starblues.loader.classloader.resource.loader.DefaultResource; +import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoader; +import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; + import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; /** * 通用的Url ClassLoader @@ -14,21 +25,28 @@ import java.nio.file.Paths; * @since 3.0.4 * @version 3.0.4 */ -public class GeneralUrlClassLoader extends URLClassLoader { +public class GeneralUrlClassLoader extends URLClassLoader implements ResourceLoaderFactory { + + private final String name; + private final ResourceLoaderFactory classLoaderTranslator; - public GeneralUrlClassLoader(ClassLoader parent) { + public GeneralUrlClassLoader(String name, ClassLoader parent) { super(new URL[]{}, parent); + this.name = name; + this.classLoaderTranslator = new ClassLoaderTranslator(this); } - public void addUrl(String url) throws Exception{ - addPath(Paths.get(url)); + public String getName() { + return name; } - public void addPath(Path path) throws Exception{ - addFile(path.toFile()); + @Override + public void addResource(String path) throws Exception { + addResource(Paths.get(path)); } - public void addFile(File file) throws Exception { + @Override + public void addResource(File file) throws Exception { if(!file.exists()){ throw new FileNotFoundException("Not found file:" + file.getPath()); } @@ -36,7 +54,37 @@ public class GeneralUrlClassLoader extends URLClassLoader { } @Override - public void addURL(URL url) { + public void addResource(Path path) throws Exception { + addResource(path.toFile()); + } + + @Override + public void addResource(URL url) throws Exception { super.addURL(url); } + + @Override + public void addResource(ResourceLoader resourceLoader) throws Exception { + addResource(resourceLoader.getBaseUrl()); + } + + @Override + public Resource findFirstResource(String name) { + return classLoaderTranslator.findFirstResource(name); + } + + @Override + public Enumeration findAllResource(String name) { + return classLoaderTranslator.findAllResource(name); + } + + @Override + public InputStream getInputStream(String name) { + return classLoaderTranslator.getInputStream(name); + } + + @Override + public List getUrls() { + return classLoaderTranslator.getUrls(); + } } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GenericClassLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GenericClassLoader.java index fe1a01f..1542645 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GenericClassLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GenericClassLoader.java @@ -27,9 +27,7 @@ import java.io.*; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; -import java.util.Enumeration; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** @@ -37,13 +35,14 @@ import java.util.concurrent.ConcurrentHashMap; * @author starBlues * @version 3.0.0 */ -public class GenericClassLoader extends URLClassLoader { +public class GenericClassLoader extends URLClassLoader implements ResourceLoaderFactory{ private final String name; private final ClassLoader parent; protected final ResourceLoaderFactory resourceLoaderFactory; + private final ResourceLoaderFactory classLoaderTranslator; private final Map> pluginClassCache = new ConcurrentHashMap<>(); public GenericClassLoader(String name, ResourceLoaderFactory resourceLoaderFactory) { @@ -55,36 +54,56 @@ public class GenericClassLoader extends URLClassLoader { this.name = Assert.isNotEmpty(name, "name 不能为空"); this.resourceLoaderFactory = Assert.isNotNull(resourceLoaderFactory, "resourceLoaderFactory 不能为空"); this.parent = parent; - + this.classLoaderTranslator = new ClassLoaderTranslator(this); } public String getName() { return name; } - + @Override public void addResource(String path) throws Exception { resourceLoaderFactory.addResource(path); } + @Override public void addResource(File file) throws Exception { resourceLoaderFactory.addResource(file); } + @Override public void addResource(Path path) throws Exception { resourceLoaderFactory.addResource(path); } + @Override public void addResource(URL url) throws Exception { resourceLoaderFactory.addResource(url); } + @Override public void addResource(ResourceLoader resourceLoader) throws Exception{ resourceLoaderFactory.addResource(resourceLoader); } - public ClassLoader getParentClassLoader(){ - return parent; + @Override + public Resource findFirstResource(String name) { + return classLoaderTranslator.findFirstResource(name); + } + + @Override + public Enumeration findAllResource(String name) { + return classLoaderTranslator.findAllResource(name); + } + + @Override + public InputStream getInputStream(String name) { + return classLoaderTranslator.getInputStream(name); + } + + @Override + public List getUrls() { + return classLoaderTranslator.getUrls(); } @Override @@ -129,7 +148,7 @@ public class GenericClassLoader extends URLClassLoader { if (aClass != null) { return aClass; } - Resource resource = resourceLoaderFactory.findResource(formatClassName); + Resource resource = resourceLoaderFactory.findFirstResource(formatClassName); byte[] bytes = null; if(resource != null){ bytes = resource.getBytes(); @@ -218,7 +237,7 @@ public class GenericClassLoader extends URLClassLoader { } protected URL findResourceFromLocal(String name) { - Resource resource = resourceLoaderFactory.findResource(name); + Resource resource = resourceLoaderFactory.findFirstResource(name); if (resource == null) { return null; } @@ -262,7 +281,7 @@ public class GenericClassLoader extends URLClassLoader { } protected Enumeration findResourcesFromLocal(String name) throws IOException{ - Enumeration enumeration = resourceLoaderFactory.findResources(name); + Enumeration enumeration = resourceLoaderFactory.findAllResource(name); return new Enumeration() { @Override public boolean hasMoreElements() { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/Resource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/Resource.java index 59ea855..a2c4d01 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/Resource.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/Resource.java @@ -54,6 +54,7 @@ public interface Resource extends AutoCloseable{ /** * 设置字节数 * @param byteGetter byteGetter + * @throws Exception set byte 异常 */ void setBytes(ResourceByteGetter byteGetter) throws Exception; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/DefaultResourceLoaderFactory.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/DefaultResourceLoaderFactory.java index e853705..10790fa 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/DefaultResourceLoaderFactory.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/DefaultResourceLoaderFactory.java @@ -19,24 +19,18 @@ package com.gitee.starblues.loader.classloader.resource.loader; import com.gitee.starblues.loader.classloader.resource.Resource; import com.gitee.starblues.loader.classloader.resource.storage.ResourceStorage; import com.gitee.starblues.loader.classloader.resource.storage.SameRootResourceStorage; -import com.gitee.starblues.loader.launcher.ResourceLoaderFactoryGetter; +import com.gitee.starblues.loader.launcher.isolation.ResourceLoaderFactoryGetter; import com.gitee.starblues.loader.utils.IOUtils; import com.gitee.starblues.loader.utils.ResourceUtils; import java.io.File; -import java.io.IOException; import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; -import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Consumer; -import java.util.function.Function; /** * 默认的资源加载工厂 @@ -119,7 +113,7 @@ public class DefaultResourceLoaderFactory implements ResourceLoaderFactory{ } @Override - public Resource findResource(String name) { + public Resource findFirstResource(String name) { for (Map.Entry entry : resourceLoaderMap.entrySet()) { ResourceStorage resourceStorage = entry.getValue(); Resource resource = resourceStorage.get(name); @@ -131,7 +125,7 @@ public class DefaultResourceLoaderFactory implements ResourceLoaderFactory{ } @Override - public Enumeration findResources(String name) { + public Enumeration findAllResource(String name) { return new Enumeration() { private final List list = new ArrayList<>(resourceLoaderMap.values()); private int index = 0; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoader.java index 819aff8..17fb706 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoader.java @@ -28,8 +28,17 @@ import java.net.URL; */ public interface ResourceLoader extends AutoCloseable{ + /** + * 获取资源基本 URL + * @return URL + */ URL getBaseUrl(); + /** + * 装载资源到ResourceStorage + * @param resourceStorage 资源存储 + * @throws Exception 装载异常 + */ void load(ResourceStorage resourceStorage) throws Exception; } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoaderFactory.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoaderFactory.java index e626f42..195d9f9 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoaderFactory.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoaderFactory.java @@ -32,7 +32,7 @@ import java.util.List; * @author starBlues * @version 3.0.0 */ -public interface ResourceLoaderFactory extends AutoCloseable{ +public interface ResourceLoaderFactory extends AutoCloseable { /** * 根据路径字符串添加资源 @@ -74,14 +74,14 @@ public interface ResourceLoaderFactory extends AutoCloseable{ * @param name 资源名称 * @return Resource */ - Resource findResource(String name); + Resource findFirstResource(String name); /** * 根据资源名称获取资源集合 * @param name 资源名称 * @return Resource */ - Enumeration findResources(String name); + Enumeration findAllResource(String name); /** * 根据资源名称获取第一个资源的 InputStream 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 index b49225b..509097a 100644 --- 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 @@ -23,8 +23,9 @@ import com.gitee.starblues.loader.DevelopmentMode; * @author starBlues * @version 3.0.2 */ -public abstract class AbstractMainLauncher extends AbstractLauncher { +public abstract class AbstractMainLauncher extends AbstractLauncher { + public static final String MAIN_CLASS_LOADER_NAME = "MainProgramLauncherClassLoader"; /** * SpringPluginBootstrap 包名称 @@ -40,7 +41,7 @@ public abstract class AbstractMainLauncher extends AbstractLauncher { @Override - public R run(String... args) throws Exception { + public ClassLoader run(String... args) throws Exception { ClassLoader classLoader = createClassLoader(args); if(!resolveThreadClassLoader()){ return toLaunch(classLoader, args); @@ -55,7 +56,7 @@ public abstract class AbstractMainLauncher extends AbstractLauncher { } } - protected R toLaunch(ClassLoader classLoader, String... args) throws Exception{ + protected ClassLoader toLaunch(ClassLoader classLoader, String... args) throws Exception{ LauncherContext.setMainClassLoader(classLoader); checkSpringPluginBootstrap(classLoader); return launch(classLoader, args); diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java new file mode 100644 index 0000000..8ae95c9 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java @@ -0,0 +1,37 @@ +package com.gitee.starblues.loader.launcher; + +import com.gitee.starblues.loader.launcher.coexist.CoexistBaseLauncher; +import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; +import com.gitee.starblues.loader.launcher.runner.MethodRunner; +import com.gitee.starblues.loader.launcher.simple.SimpleBaseLauncher; +import lombok.AllArgsConstructor; + +/** + * 开发环境 Launcher + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +@AllArgsConstructor +public class DevLauncher implements Launcher{ + + + + private final SpringBootstrap springBootstrap; + + @Override + public ClassLoader run(String... args) throws Exception { + MethodRunner methodRunner = new MethodRunner(springBootstrap.getClass().getName(), + SPRING_BOOTSTRAP_RUN_METHOD, args); + AbstractMainLauncher launcher; + if(DevelopmentModeSetting.coexist()){ + launcher = new CoexistBaseLauncher(methodRunner); + } else if(DevelopmentModeSetting.simple()) { + launcher = new SimpleBaseLauncher(methodRunner); + } else { + launcher = new IsolationBaseLauncher(methodRunner); + } + return launcher.run(args); + } +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/Launcher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/Launcher.java index cee4f12..3a996ee 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/Launcher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/Launcher.java @@ -23,6 +23,8 @@ package com.gitee.starblues.loader.launcher; */ public interface Launcher { + String SPRING_BOOTSTRAP_RUN_METHOD = "run"; + /** * 启动运行 * @param args 启动传入的参数 diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java new file mode 100644 index 0000000..25e807f --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java @@ -0,0 +1,109 @@ +package com.gitee.starblues.loader.launcher; + +import com.gitee.starblues.loader.jar.JarFile; +import com.gitee.starblues.loader.launcher.coexist.CoexistBaseLauncher; +import com.gitee.starblues.loader.launcher.coexist.CoexistFastJarLauncher; +import com.gitee.starblues.loader.launcher.coexist.CoexistJarOuterLauncher; +import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; +import com.gitee.starblues.loader.launcher.isolation.IsolationFastJarLauncher; +import com.gitee.starblues.loader.launcher.isolation.IsolationJarOuterLauncher; +import com.gitee.starblues.loader.launcher.runner.MethodRunner; +import com.gitee.starblues.loader.launcher.simple.SimpleBaseLauncher; +import com.gitee.starblues.loader.utils.ObjectUtils; +import lombok.AllArgsConstructor; + +import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; +import java.security.CodeSource; +import java.security.ProtectionDomain; +import java.util.Objects; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +import static com.gitee.starblues.loader.LoaderConstant.*; + +/** + * 生产环境 Launcher + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class ProdLauncher implements Launcher{ + + static final String SPRING_BOOTSTRAP_RUN_METHOD = "run"; + + @Override + public ClassLoader 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; + } + 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, SPRING_BOOTSTRAP_RUN_METHOD, args); + AbstractMainLauncher launcher; + + if(Objects.equals(mainPackageType, MAIN_PACKAGE_TYPE_JAR_OUTER)){ + launcher = getJarOuterLauncher(methodRunner, rootJarFile); + } else { + launcher = getFastJarLauncher(methodRunner, rootJarFile); + } + + return launcher.run(args); + } + + + private File getRootJarFile() throws URISyntaxException { + ProtectionDomain protectionDomain = SpringMainBootstrap.class.getProtectionDomain(); + CodeSource codeSource = protectionDomain.getCodeSource(); + URI location = (codeSource != null) ? codeSource.getLocation().toURI() : null; + String path = (location != null) ? location.getSchemeSpecificPart() : null; + if (path == null) { + throw new IllegalStateException("Unable to determine code source archive"); + } + File root = new File(path); + if (!root.exists()) { + throw new IllegalStateException("Unable to determine code source archive from " + root); + } + return root; + } + + private AbstractMainLauncher getFastJarLauncher(MethodRunner methodRunner, File rootJarFile){ + AbstractMainLauncher launcher; + if(DevelopmentModeSetting.coexist()){ + launcher = new CoexistFastJarLauncher(methodRunner, rootJarFile); + } else if(DevelopmentModeSetting.simple()) { + launcher = new CoexistFastJarLauncher(methodRunner, rootJarFile); + } else { + launcher = new IsolationFastJarLauncher(methodRunner, rootJarFile); + } + return launcher; + } + + private AbstractMainLauncher getJarOuterLauncher(MethodRunner methodRunner, File rootJarFile){ + AbstractMainLauncher launcher; + if(DevelopmentModeSetting.coexist()){ + launcher = new CoexistJarOuterLauncher(methodRunner, rootJarFile); + } else if(DevelopmentModeSetting.simple()) { + launcher = new CoexistJarOuterLauncher(methodRunner, rootJarFile); + } else { + launcher = new IsolationJarOuterLauncher(methodRunner, rootJarFile); + } + return launcher; + } + + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java index a13d086..6c913dc 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java @@ -32,7 +32,6 @@ import java.util.concurrent.CountDownLatch; public class SpringMainBootstrap { static final String MAIN_RUN_METHOD = "main"; - static final String SPRING_BOOTSTRAP_RUN_METHOD = "run"; private static final CountDownLatch COUNT_DOWN_LATCH = new CountDownLatch(1); @@ -85,15 +84,7 @@ public class SpringMainBootstrap { private static void main(String[] args) throws Exception { Objects.requireNonNull(springBootstrap, "springBootBootstrap 不能为空"); - MethodRunner run = new MethodRunner(springBootstrap.getClass().getName(), SPRING_BOOTSTRAP_RUN_METHOD, args); - Launcher launcher; - - DevelopmentMode developmentMode = springBootstrap.developmentMode(); - if(developmentMode == DevelopmentMode.SIMPLE || developmentMode == DevelopmentMode.COEXIST){ - launcher = new SimpleModeMainLauncher(run); - } else { - launcher = new MainProgramLauncher(run); - } + Launcher launcher = new DevLauncher(springBootstrap); launcher.run(args); } 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 47fc054..8558224 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainProdBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainProdBootstrap.java @@ -17,6 +17,8 @@ package com.gitee.starblues.loader.launcher; import com.gitee.starblues.loader.jar.JarFile; +import com.gitee.starblues.loader.launcher.isolation.IsolationJarOuterLauncher; +import com.gitee.starblues.loader.launcher.isolation.IsolationFastJarLauncher; import com.gitee.starblues.loader.launcher.runner.MethodRunner; import com.gitee.starblues.loader.utils.ObjectUtils; @@ -37,52 +39,14 @@ import static com.gitee.starblues.loader.LoaderConstant.*; */ public class SpringMainProdBootstrap { - public static void main(String[] args) throws Exception { JarFile.registerUrlProtocolHandler(); new SpringMainProdBootstrap().run(args); } 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; - } - 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; - if(Objects.equals(mainPackageType, MAIN_PACKAGE_TYPE_JAR_OUTER)){ - launcher = new MainJarOuterProgramLauncher(methodRunner, rootJarFile); - } else { - launcher = new MainJarProgramLauncher(methodRunner, rootJarFile); - } + Launcher launcher = new ProdLauncher(); launcher.run(args); } - private File getRootJarFile() throws URISyntaxException { - ProtectionDomain protectionDomain = SpringMainBootstrap.class.getProtectionDomain(); - CodeSource codeSource = protectionDomain.getCodeSource(); - URI location = (codeSource != null) ? codeSource.getLocation().toURI() : null; - String path = (location != null) ? location.getSchemeSpecificPart() : null; - if (path == null) { - throw new IllegalStateException("Unable to determine code source archive"); - } - File root = new File(path); - if (!root.exists()) { - throw new IllegalStateException("Unable to determine code source archive from " + root); - } - return root; - } - } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java new file mode 100644 index 0000000..0d98135 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java @@ -0,0 +1,23 @@ +package com.gitee.starblues.loader.launcher.classpath; + + +import java.net.URL; +import java.util.List; + +/** + * 获取classpath资源路径 + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +public interface ClasspathResource { + + /** + * 获取 classpath url 集合 + * @return List + * @throws Exception 获取异常 + */ + List getClasspath() throws Exception; + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java new file mode 100644 index 0000000..2ed6500 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java @@ -0,0 +1,57 @@ +package com.gitee.starblues.loader.launcher.classpath; + +import com.gitee.starblues.loader.archive.Archive; +import com.gitee.starblues.loader.archive.ExplodedArchive; +import com.gitee.starblues.loader.archive.JarFileArchive; +import lombok.AllArgsConstructor; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import static com.gitee.starblues.loader.LoaderConstant.*; + +/** + * fast jar 类型的 classpath 获取者 + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +@AllArgsConstructor +public class FastJarClasspathResource implements ClasspathResource{ + + private final static Archive.EntryFilter ENTRY_FILTER = (entry)->{ + String name = entry.getName(); + return name.startsWith(PROD_CLASSES_PATH) || name.startsWith(PROD_LIB_PATH); + }; + + private final static Archive.EntryFilter INCLUDE_FILTER = (entry) -> { + if (entry.isDirectory()) { + return entry.getName().equals(PROD_CLASSES_PATH); + } + return entry.getName().startsWith(PROD_LIB_PATH); + }; + + private final File rootJarFile; + + @Override + public List getClasspath() throws Exception{ + Archive archive = getArchive(); + Iterator archiveIterator = archive.getNestedArchives(ENTRY_FILTER, INCLUDE_FILTER); + List urls = new ArrayList<>(); + while (archiveIterator.hasNext()){ + URL url = archiveIterator.next().getUrl(); + urls.add(url); + } + return urls; + } + + private Archive getArchive() throws IOException { + return (rootJarFile.isDirectory() ? new ExplodedArchive(rootJarFile) : new JarFileArchive(rootJarFile)); + } + +} 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/classpath/JarOutClasspathResource.java similarity index 57% rename from spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarOuterProgramLauncher.java rename to spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java index 41b1005..3f695f6 100644 --- 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/classpath/JarOutClasspathResource.java @@ -1,29 +1,12 @@ -/** - * 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; +package com.gitee.starblues.loader.launcher.classpath; 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 lombok.AllArgsConstructor; import java.io.File; import java.io.FileFilter; @@ -34,14 +17,15 @@ import java.util.jar.Manifest; import static com.gitee.starblues.loader.LoaderConstant.*; - /** - * 主程序jar-outer 模式启动者 + * jar out 类型的 classpath 获取者 * * @author starBlues - * @version 3.0.2 + * @version 3.0.4 + * @since 3.0.4 */ -public class MainJarOuterProgramLauncher extends MainProgramLauncher{ +@AllArgsConstructor +public class JarOutClasspathResource implements ClasspathResource{ private final static Archive.EntryFilter ENTRY_FILTER = (entry)->{ @@ -58,52 +42,49 @@ public class MainJarOuterProgramLauncher extends MainProgramLauncher{ private final File rootJarFile; - public MainJarOuterProgramLauncher(MethodRunner methodRunner, File rootJarFile) { - super(methodRunner); - this.rootJarFile = Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); - } - - @Override - protected boolean resolveThreadClassLoader() { - return true; - } @Override - protected void addResource(GenericClassLoader classLoader) throws Exception { - super.addResource(classLoader); + public List getClasspath() throws Exception { Archive archive = getArchive(); - Iterator archiveIterator = archive.getNestedArchives(ENTRY_FILTER, INCLUDE_FILTER); - addEntryResource(archiveIterator, classLoader); - addLibResource(archive, classLoader); + Iterator archives = archive.getNestedArchives(ENTRY_FILTER, INCLUDE_FILTER); + List urls = getEntryResource(archives); + urls.addAll(getLibResource(archive)); + return urls; } private Archive getArchive() throws IOException { return (rootJarFile.isDirectory() ? new ExplodedArchive(rootJarFile) : new JarFileArchive(rootJarFile)); } - private void addEntryResource(Iterator archives, GenericClassLoader classLoader) throws Exception { + private List getEntryResource(Iterator archives) throws Exception{ + List urls = new ArrayList<>(); 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)); + urls.add(url); } } + return urls; } - private void addLibResource(Archive archive, GenericClassLoader classLoader) throws Exception { + private List getLibResource(Archive archive) 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 { + if(!libJarDir.exists()){ throw new IllegalStateException("主程序依赖目录不存在: " + libDir); } + List libIndexes = getLibIndexes(manifest); + File[] libJarFile = getLibJarFile(libJarDir, libIndexes); + List urls = new ArrayList<>(libJarFile.length); + for (File file : libJarFile) { + urls.add(file.toPath().toUri().toURL()); + } + return urls; } private List getLibIndexes(Manifest manifest){ @@ -125,7 +106,7 @@ public class MainJarOuterProgramLauncher extends MainProgramLauncher{ return indexes; } - private void addLibJarFile(File rootFile, List libIndexes, GenericClassLoader classLoader) throws Exception { + private File[] getLibJarFile(File rootFile, List libIndexes) { Set linIndexes = new HashSet<>(libIndexes); File[] listFiles = rootFile.listFiles(new FileFilter() { @Override @@ -134,13 +115,10 @@ public class MainJarOuterProgramLauncher extends MainProgramLauncher{ } }); if(listFiles == null || listFiles.length == 0){ - return; - } - for (File listFile : listFiles) { - classLoader.addResource(listFile); + return new File[0]; } + return listFiles; } - } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java new file mode 100644 index 0000000..d76026d --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java @@ -0,0 +1,43 @@ +package com.gitee.starblues.loader.launcher.coexist; + +import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; +import com.gitee.starblues.loader.classloader.GenericClassLoader; +import com.gitee.starblues.loader.launcher.AbstractMainLauncher; +import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; +import com.gitee.starblues.loader.launcher.runner.MethodRunner; + + +/** + * coexist 模式 launcher + * + * @author starBlues + * @since 3.0.4 + * @version 3.0.4 + */ +public class CoexistBaseLauncher extends AbstractMainLauncher { + + private final MethodRunner methodRunner; + + public CoexistBaseLauncher(MethodRunner methodRunner) { + this.methodRunner = methodRunner; + } + + @Override + protected ClassLoader createClassLoader(String... args) throws Exception { + GeneralUrlClassLoader urlClassLoader = new GeneralUrlClassLoader(MAIN_CLASS_LOADER_NAME, + this.getClass().getClassLoader()); + addResource(urlClassLoader); + return urlClassLoader; + } + + @Override + protected ClassLoader launch(ClassLoader classLoader, String... args) throws Exception { + methodRunner.run(classLoader); + return classLoader; + } + + protected void addResource(GeneralUrlClassLoader classLoader) throws Exception{ + + } + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java new file mode 100644 index 0000000..2d83c90 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java @@ -0,0 +1,65 @@ +/** + * 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.coexist; + +import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; +import com.gitee.starblues.loader.classloader.GenericClassLoader; +import com.gitee.starblues.loader.classloader.resource.loader.JarResourceLoader; +import com.gitee.starblues.loader.classloader.resource.loader.MainJarResourceLoader; +import com.gitee.starblues.loader.launcher.classpath.ClasspathResource; +import com.gitee.starblues.loader.launcher.classpath.FastJarClasspathResource; +import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; +import com.gitee.starblues.loader.launcher.runner.MethodRunner; + +import java.io.File; +import java.net.URL; +import java.util.List; +import java.util.Objects; + +import static com.gitee.starblues.loader.LoaderConstant.PROD_CLASSES_URL_SIGN; + +/** + * 主程序jar in jar 模式启动者 + * @author starBlues + * @version 3.0.2 + */ +public class CoexistFastJarLauncher extends CoexistBaseLauncher { + + private final ClasspathResource classpathResource; + + public CoexistFastJarLauncher(MethodRunner methodRunner, File rootJarFile) { + super(methodRunner); + Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); + this.classpathResource = new FastJarClasspathResource(rootJarFile); + } + + @Override + protected boolean resolveThreadClassLoader() { + return true; + } + + @Override + protected void addResource(GeneralUrlClassLoader classLoader) throws Exception { + super.addResource(classLoader); + List classpath = classpathResource.getClasspath(); + for (URL url : classpath) { + classLoader.addURL(url); + } + } + + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java new file mode 100644 index 0000000..12797ff --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java @@ -0,0 +1,63 @@ +/** + * 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.coexist; + +import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; +import com.gitee.starblues.loader.classloader.GenericClassLoader; +import com.gitee.starblues.loader.launcher.classpath.ClasspathResource; +import com.gitee.starblues.loader.launcher.classpath.JarOutClasspathResource; +import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; +import com.gitee.starblues.loader.launcher.runner.MethodRunner; + +import java.io.File; +import java.net.URL; +import java.util.List; +import java.util.Objects; + + +/** + * 主程序jar-outer 模式启动者 + * + * @author starBlues + * @version 3.0.2 + */ +public class CoexistJarOuterLauncher extends CoexistBaseLauncher { + + private final ClasspathResource classpathResource; + + public CoexistJarOuterLauncher(MethodRunner methodRunner, File rootJarFile) { + super(methodRunner); + Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); + this.classpathResource = new JarOutClasspathResource(rootJarFile); + } + + @Override + protected boolean resolveThreadClassLoader() { + return true; + } + + @Override + protected void addResource(GeneralUrlClassLoader classLoader) throws Exception { + super.addResource(classLoader); + List classpath = classpathResource.getClasspath(); + for (URL url : classpath) { + classLoader.addURL(url); + } + } + + +} 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/isolation/IsolationBaseLauncher.java similarity index 89% rename from spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainProgramLauncher.java rename to spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationBaseLauncher.java index e8df24e..bea556f 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/isolation/IsolationBaseLauncher.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package com.gitee.starblues.loader.launcher; +package com.gitee.starblues.loader.launcher.isolation; import com.gitee.starblues.loader.classloader.GenericClassLoader; import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; +import com.gitee.starblues.loader.launcher.AbstractMainLauncher; import com.gitee.starblues.loader.launcher.runner.MethodRunner; import com.gitee.starblues.loader.utils.ObjectUtils; @@ -31,13 +32,11 @@ import java.net.URLClassLoader; * @author starBlues * @version 3.0.0 */ -public class MainProgramLauncher extends AbstractMainLauncher{ - - public static final String MAIN_CLASS_LOADER_NAME = "MainProgramLauncherClassLoader"; +public class IsolationBaseLauncher extends AbstractMainLauncher { private final MethodRunner methodRunner; - public MainProgramLauncher(MethodRunner methodRunner) { + public IsolationBaseLauncher(MethodRunner methodRunner) { this.methodRunner = methodRunner; } @@ -65,7 +64,7 @@ public class MainProgramLauncher extends AbstractMainLauncher{ } protected ClassLoader getParentClassLoader(){ - return MainProgramLauncher.class.getClassLoader(); + return IsolationBaseLauncher.class.getClassLoader(); } protected void addResource(GenericClassLoader classLoader) throws Exception{ 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/isolation/IsolationFastJarLauncher.java similarity index 51% rename from spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarProgramLauncher.java rename to spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationFastJarLauncher.java index c260d17..8b4928f 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/isolation/IsolationFastJarLauncher.java @@ -14,20 +14,18 @@ * limitations under the License. */ -package com.gitee.starblues.loader.launcher; +package com.gitee.starblues.loader.launcher.isolation; -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.JarResourceLoader; import com.gitee.starblues.loader.classloader.resource.loader.MainJarResourceLoader; +import com.gitee.starblues.loader.launcher.classpath.ClasspathResource; +import com.gitee.starblues.loader.launcher.classpath.FastJarClasspathResource; import com.gitee.starblues.loader.launcher.runner.MethodRunner; import java.io.File; -import java.io.IOException; import java.net.URL; -import java.util.Iterator; +import java.util.List; import java.util.Objects; import static com.gitee.starblues.loader.LoaderConstant.*; @@ -37,25 +35,14 @@ import static com.gitee.starblues.loader.LoaderConstant.*; * @author starBlues * @version 3.0.2 */ -public class MainJarProgramLauncher extends MainProgramLauncher{ +public class IsolationFastJarLauncher extends IsolationBaseLauncher { - private final static Archive.EntryFilter ENTRY_FILTER = (entry)->{ - String name = entry.getName(); - return name.startsWith(PROD_CLASSES_PATH) || name.startsWith(PROD_LIB_PATH); - }; + private final ClasspathResource classpathResource; - private final static Archive.EntryFilter INCLUDE_FILTER = (entry) -> { - if (entry.isDirectory()) { - return entry.getName().equals(PROD_CLASSES_PATH); - } - return entry.getName().startsWith(PROD_LIB_PATH); - }; - - private final File rootJarFile; - - public MainJarProgramLauncher(MethodRunner methodRunner, File rootJarFile) { + public IsolationFastJarLauncher(MethodRunner methodRunner, File rootJarFile) { super(methodRunner); - this.rootJarFile = Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); + Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); + this.classpathResource = new FastJarClasspathResource(rootJarFile); } @Override @@ -66,19 +53,8 @@ public class MainJarProgramLauncher extends MainProgramLauncher{ @Override protected void addResource(GenericClassLoader classLoader) throws Exception { super.addResource(classLoader); - Archive archive = getArchive(); - Iterator archiveIterator = archive.getNestedArchives(ENTRY_FILTER, INCLUDE_FILTER); - addLibResource(archiveIterator, classLoader); - } - - private Archive getArchive() throws IOException { - return (rootJarFile.isDirectory() ? new ExplodedArchive(rootJarFile) : new JarFileArchive(rootJarFile)); - } - - private void addLibResource(Iterator archives, GenericClassLoader classLoader) throws Exception { - while (archives.hasNext()){ - Archive archive = archives.next(); - URL url = archive.getUrl(); + List classpath = classpathResource.getClasspath(); + for (URL url : classpath) { String path = url.getPath(); if(path.contains(PROD_CLASSES_URL_SIGN)){ classLoader.addResource(new MainJarResourceLoader(url)); @@ -88,4 +64,5 @@ public class MainJarProgramLauncher extends MainProgramLauncher{ } } + } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationJarOuterLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationJarOuterLauncher.java new file mode 100644 index 0000000..f06013f --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationJarOuterLauncher.java @@ -0,0 +1,60 @@ +/** + * 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.isolation; + +import com.gitee.starblues.loader.classloader.GenericClassLoader; +import com.gitee.starblues.loader.launcher.classpath.ClasspathResource; +import com.gitee.starblues.loader.launcher.classpath.JarOutClasspathResource; +import com.gitee.starblues.loader.launcher.runner.MethodRunner; + +import java.io.File; +import java.net.URL; +import java.util.List; +import java.util.Objects; + + +/** + * 主程序jar-outer 模式启动者 + * + * @author starBlues + * @version 3.0.2 + */ +public class IsolationJarOuterLauncher extends IsolationBaseLauncher { + + private final ClasspathResource classpathResource; + + public IsolationJarOuterLauncher(MethodRunner methodRunner, File rootJarFile) { + super(methodRunner); + Objects.requireNonNull(rootJarFile, "参数 rootJarFile 不能为空"); + this.classpathResource = new JarOutClasspathResource(rootJarFile); + } + + @Override + protected boolean resolveThreadClassLoader() { + return true; + } + + @Override + protected void addResource(GenericClassLoader classLoader) throws Exception { + super.addResource(classLoader); + List classpath = classpathResource.getClasspath(); + for (URL url : classpath) { + classLoader.addResource(url); + } + } + +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ResourceLoaderFactoryGetter.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java similarity index 98% rename from spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ResourceLoaderFactoryGetter.java rename to spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java index 61dbbd9..b04ba0a 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ResourceLoaderFactoryGetter.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.gitee.starblues.loader.launcher; +package com.gitee.starblues.loader.launcher.isolation; import com.gitee.starblues.loader.classloader.resource.loader.DefaultResourceLoaderFactory; import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SimpleModeMainLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java similarity index 53% rename from spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SimpleModeMainLauncher.java rename to spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java index 067a74c..d2198f0 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SimpleModeMainLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java @@ -1,24 +1,29 @@ -package com.gitee.starblues.loader.launcher; +package com.gitee.starblues.loader.launcher.simple; import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; +import com.gitee.starblues.loader.launcher.AbstractMainLauncher; +import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; import com.gitee.starblues.loader.launcher.runner.MethodRunner; /** + * simple 模式 launcher + * * @author starBlues * @since 3.0.4 + * @version 3.0.4 */ -public class SimpleModeMainLauncher extends AbstractMainLauncher{ +public class SimpleBaseLauncher extends AbstractMainLauncher { private final MethodRunner methodRunner; - public SimpleModeMainLauncher(MethodRunner methodRunner) { + public SimpleBaseLauncher(MethodRunner methodRunner) { this.methodRunner = methodRunner; } @Override protected ClassLoader createClassLoader(String... args) throws Exception { - return new GeneralUrlClassLoader(this.getClass().getClassLoader()); + return new GeneralUrlClassLoader(IsolationBaseLauncher.MAIN_CLASS_LOADER_NAME, this.getClass().getClassLoader()); } @Override 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 79b393e..7b49e61 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 @@ -23,7 +23,6 @@ import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; 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; @@ -45,17 +44,21 @@ import java.util.zip.ZipEntry; public class NestedPluginJarResourceLoader extends AbstractResourceLoader { private final InsidePluginDescriptor pluginDescriptor; - private final GenericClassLoader parentClassLoader; private final ResourceLoaderFactory resourceLoaderFactory; - + private final ResourceLoaderFactory parentResourceLoaderFactory; public NestedPluginJarResourceLoader(InsidePluginDescriptor pluginDescriptor, - GenericClassLoader parentClassLoader, ResourceLoaderFactory resourceLoaderFactory) throws Exception { + this(pluginDescriptor, resourceLoaderFactory, null); + } + + public NestedPluginJarResourceLoader(InsidePluginDescriptor pluginDescriptor, + ResourceLoaderFactory resourceLoaderFactory, + ResourceLoaderFactory parentResourceLoaderFactory) throws Exception { super(new URL("jar:" + pluginDescriptor.getInsidePluginPath().toUri().toURL() + "!/")); this.pluginDescriptor = pluginDescriptor; - this.parentClassLoader = parentClassLoader; this.resourceLoaderFactory = resourceLoaderFactory; + this.parentResourceLoaderFactory = parentResourceLoaderFactory; } @Override @@ -97,8 +100,8 @@ public class NestedPluginJarResourceLoader extends AbstractResourceLoader { } InputStream jarFileInputStream = jarFile.getInputStream(jarEntry); URL url = new URL(baseUrl.toString() + pluginLibInfo.getPath() + "!/"); - if(pluginLibInfo.isLoadToMain()){ - parentClassLoader.addResource(new JarResourceLoader(url, new JarInputStream(jarFileInputStream))); + if(parentResourceLoaderFactory != null && pluginLibInfo.isLoadToMain()){ + parentResourceLoaderFactory.addResource(new JarResourceLoader(url, new JarInputStream(jarFileInputStream))); log.debug("插件[{}]依赖被加载到主程序中: {}", pluginUnique, pluginLibInfo.getPath()); } else { JarResourceLoader jarResourceLoader = new JarResourceLoader(url, new JarInputStream(jarFileInputStream)); diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java new file mode 100644 index 0000000..503ddee --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java @@ -0,0 +1,79 @@ +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.loader.classloader.GeneralUrlClassLoader; +import com.gitee.starblues.loader.launcher.LauncherContext; +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.util.Set; + +/** + * 插件基本 url classLoader + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +@Slf4j +public class PluginGeneralUrlClassLoader extends GeneralUrlClassLoader { + + public PluginGeneralUrlClassLoader(String name, ClassLoader parent) { + super(name, parent); + } + + public void addResource(InsidePluginDescriptor descriptor) throws Exception { + PluginType pluginType = descriptor.getType(); + if(PluginType.isNestedPackage(pluginType)){ + NestedPluginJarResourceLoader resourceLoader = + new NestedPluginJarResourceLoader(descriptor, parentClassLoader, resourceLoaderFactory); + resourceLoaderFactory.addResource(resourceLoader); + } else if(PluginType.isOuterPackage(pluginType)){ + addOuterPluginClasspath(descriptor); + addLibFile(descriptor); + } else { + addDirPluginClasspath(descriptor); + addLibFile(descriptor); + } + } + + + private void addLibFile(InsidePluginDescriptor pluginDescriptor) throws Exception { + Set pluginLibInfos = pluginDescriptor.getPluginLibInfo(); + if(ObjectUtils.isEmpty(pluginLibInfos)){ + return; + } + String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); + String pluginLibDir = pluginDescriptor.getPluginLibDir(); + if(!ObjectUtils.isEmpty(pluginLibDir)){ + log.info("插件[{}]依赖加载目录: {}", pluginUnique, pluginLibDir); + } + if(pluginLibInfos.isEmpty()){ + log.warn("插件[{}]依赖为空!", pluginUnique); + return; + } + + GeneralUrlClassLoader parentClassLoader = (GeneralUrlClassLoader) LauncherContext.getMainClassLoader(); + + + for (PluginLibInfo pluginLibInfo : pluginLibInfos) { + File existFile = FilesUtils.getExistFile(pluginLibInfo.getPath()); + if(existFile != null){ + if(pluginLibInfo.isLoadToMain()){ + // 加载到主程序中 + parentClassLoader.addFile(existFile); + log.debug("插件[{}]依赖被加载到主程序中: {}", pluginUnique, existFile.getPath()); + } else { + super.addFile(existFile); + log.debug("插件[{}]依赖被加载: {}", pluginUnique, existFile.getPath()); + } + } + } + } + +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java index b146453..9433e73 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java @@ -1,15 +1,20 @@ package com.gitee.starblues.core.launcher.plugin; -import com.gitee.starblues.common.Constants; +import com.gitee.starblues.core.classloader.NestedPluginJarResourceLoader; 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.core.launcher.plugin.involved.PluginLaunchInvolved; -import com.gitee.starblues.loader.DevelopmentMode; import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; import com.gitee.starblues.loader.launcher.AbstractLauncher; import com.gitee.starblues.spring.SpringPluginHook; +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.util.Set; /** @@ -19,6 +24,7 @@ import java.util.Set; * @since 3.0.4 * @version 3.0.4 */ +@Slf4j public class PluginCoexistLauncher extends AbstractLauncher { protected final PluginInteractive pluginInteractive; @@ -32,10 +38,10 @@ public class PluginCoexistLauncher extends AbstractLauncher { @Override protected ClassLoader createClassLoader(String... args) throws Exception { - GeneralUrlClassLoader classLoader = new GeneralUrlClassLoader(this.getClass().getClassLoader()); - InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); - String pluginClassPath = pluginDescriptor.getPluginClassPath(); - classLoader.addUrl(pluginClassPath); + GeneralUrlClassLoader classLoader = new GeneralUrlClassLoader( + pluginInteractive.getPluginDescriptor().getPluginId(), + this.getClass().getClassLoader()); + addClasspath(classLoader); return classLoader; } @@ -57,4 +63,40 @@ public class PluginCoexistLauncher extends AbstractLauncher { } } + private void addClasspath(GeneralUrlClassLoader classLoader) throws Exception { + InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); + PluginType pluginType = pluginDescriptor.getType(); + if(PluginType.isNestedPackage(pluginType)){ + classLoader.addResource(new NestedPluginJarResourceLoader( + pluginDescriptor, null, classLoader + )); + + } else if(PluginType.isOuterPackage(pluginType)){ + // TODO addOuterPluginClasspath(descriptor); + addLibFile(classLoader); + } else { + // TODO addDirPluginClasspath(descriptor); + addLibFile(classLoader); + } + String pluginClassPath = pluginDescriptor.getPluginClassPath(); + classLoader.addResource(pluginClassPath); + } + + private void addLibFile(GeneralUrlClassLoader classLoader) throws Exception { + InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); + Set pluginLibInfos = pluginDescriptor.getPluginLibInfo(); + String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); + if(ObjectUtils.isEmpty(pluginLibInfos)){ + log.warn("插件[{}]依赖为空!", pluginUnique); + return; + } + for (PluginLibInfo pluginLibInfo : pluginLibInfos) { + File existFile = FilesUtils.getExistFile(pluginLibInfo.getPath()); + if(existFile != null){ + classLoader.addResource(existFile); + log.debug("插件[{}]依赖被加载: {}", pluginUnique, existFile.getPath()); + } + } + } + } -- Gitee From 2b5ddda68a8f81650f00eb7e9eac78c53ff1add1 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Wed, 27 Jul 2022 22:03:21 +0800 Subject: [PATCH 05/12] =?UTF-8?q?3.0.4=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../classloader/ClassLoaderTranslator.java | 6 +- .../classloader/GeneralUrlClassLoader.java | 10 +- .../loader/AbstractResourceLoader.java | 2 + .../storage/EmptyResourceStorage.java | 57 +++++++ .../coexist/CoexistFastJarLauncher.java | 2 +- .../coexist/CoexistJarOuterLauncher.java | 2 +- .../NestedPluginJarResourceLoader.java | 1 + .../core/classloader/PluginClassLoader.java | 71 +------- .../PluginGeneralUrlClassLoader.java | 56 +----- .../PluginResourceLoaderFactory.java | 24 +++ .../PluginResourceLoaderFactoryProxy.java | 161 ++++++++++++++++++ .../plugin/PluginCoexistLauncher.java | 45 ++--- .../operator/DefaultPluginOperator.java | 3 + .../SimpleDevelopmentModeInitializer.java | 24 --- 14 files changed, 282 insertions(+), 182 deletions(-) create mode 100644 spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java create mode 100644 spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java create mode 100644 spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java delete mode 100644 spring-brick/src/main/java/com/gitee/starblues/integration/simple/SimpleDevelopmentModeInitializer.java diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java index 28f9697..193e312 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java @@ -17,11 +17,11 @@ import java.util.Enumeration; import java.util.List; /** - * 注释 + * classloader 转换器 * * @author starBlues - * @version 1.0.0 - * @since 1.0.0 + * @version 3.0.4 + * @since 3.0.4 */ public class ClassLoaderTranslator implements ResourceLoaderFactory { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java index 849a2be..69d09f8 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java @@ -1,20 +1,18 @@ package com.gitee.starblues.loader.classloader; import com.gitee.starblues.loader.classloader.resource.Resource; -import com.gitee.starblues.loader.classloader.resource.loader.DefaultResource; import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoader; import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; +import com.gitee.starblues.loader.classloader.resource.storage.EmptyResourceStorage; +import com.gitee.starblues.loader.classloader.resource.storage.ResourceStorage; import java.io.File; import java.io.FileNotFoundException; -import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Collections; import java.util.Enumeration; import java.util.List; @@ -30,6 +28,8 @@ public class GeneralUrlClassLoader extends URLClassLoader implements ResourceLoa private final String name; private final ResourceLoaderFactory classLoaderTranslator; + private final ResourceStorage resourceStorage = new EmptyResourceStorage(); + public GeneralUrlClassLoader(String name, ClassLoader parent) { super(new URL[]{}, parent); this.name = name; @@ -65,7 +65,7 @@ public class GeneralUrlClassLoader extends URLClassLoader implements ResourceLoa @Override public void addResource(ResourceLoader resourceLoader) throws Exception { - addResource(resourceLoader.getBaseUrl()); + resourceLoader.load(resourceStorage); } @Override diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/AbstractResourceLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/AbstractResourceLoader.java index 67d1a91..b8e7adc 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/AbstractResourceLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/AbstractResourceLoader.java @@ -17,6 +17,7 @@ package com.gitee.starblues.loader.classloader.resource.loader; +import com.gitee.starblues.loader.classloader.resource.storage.EmptyResourceStorage; import com.gitee.starblues.loader.classloader.resource.storage.ResourceStorage; import com.gitee.starblues.loader.utils.IOUtils; @@ -66,6 +67,7 @@ public abstract class AbstractResourceLoader implements ResourceLoader{ /** * 子类初始化实现 + * @param resourceStorage 资源存储 * @throws Exception 初始异常 */ protected abstract void loadOfChild(ResourceStorage resourceStorage) throws Exception; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java new file mode 100644 index 0000000..43e0bf4 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java @@ -0,0 +1,57 @@ +package com.gitee.starblues.loader.classloader.resource.storage; + +import com.gitee.starblues.loader.classloader.resource.Resource; +import com.gitee.starblues.loader.classloader.resource.ResourceByteGetter; + +import java.io.InputStream; +import java.net.URL; +import java.util.List; + +/** + * 空的资源存储 + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +public class EmptyResourceStorage implements ResourceStorage{ + @Override + public void add(String name, URL url, ResourceByteGetter byteGetter) throws Exception { + + } + + @Override + public void add(String name, URL url) throws Exception { + + } + + @Override + public boolean exist(String name) { + return false; + } + + @Override + public Resource get(String name) { + return null; + } + + @Override + public InputStream getInputStream(String name) { + return null; + } + + @Override + public List getAll() { + return null; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public void close() throws Exception { + + } +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java index 2d83c90..48431a8 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java @@ -57,7 +57,7 @@ public class CoexistFastJarLauncher extends CoexistBaseLauncher { super.addResource(classLoader); List classpath = classpathResource.getClasspath(); for (URL url : classpath) { - classLoader.addURL(url); + classLoader.addResource(url); } } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java index 12797ff..6f85129 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java @@ -55,7 +55,7 @@ public class CoexistJarOuterLauncher extends CoexistBaseLauncher { super.addResource(classLoader); List classpath = classpathResource.getClasspath(); for (URL url : classpath) { - classLoader.addURL(url); + classLoader.addResource(url); } } 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 7b49e61..4430b53 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 @@ -79,6 +79,7 @@ public class NestedPluginJarResourceLoader extends AbstractResourceLoader { } String realName = jarEntry.getName().replace(classesPath, ""); URL url = new URL(baseUrl.toString() + jarEntry.getName()); + resourceLoaderFactory.addResource(url); resourceStorage.add(realName, url, ()->{ return getClassBytes(realName, jarFile.getInputStream(jarEntry), true); }); 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 c94681e..3d19150 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 @@ -41,88 +41,29 @@ import java.util.Set; * @version 3.0.3 */ @Slf4j -public class PluginClassLoader extends GenericClassLoader { +public class PluginClassLoader extends GenericClassLoader implements PluginResourceLoaderFactory{ - private final GenericClassLoader parentClassLoader; private final MainResourceMatcher mainResourceMatcher; + private final PluginResourceLoaderFactory proxy; + public PluginClassLoader(String name, GenericClassLoader parentClassLoader, ResourceLoaderFactory resourceLoaderFactory, MainResourceMatcher mainResourceMatcher) { super(name, parentClassLoader, resourceLoaderFactory); - this.parentClassLoader = parentClassLoader; this.mainResourceMatcher = mainResourceMatcher; + this.proxy = new PluginResourceLoaderFactoryProxy(resourceLoaderFactory, parentClassLoader); } public MainResourceMatcher getMainResourceMatcher() { return mainResourceMatcher; } + @Override public void addResource(InsidePluginDescriptor descriptor) throws Exception { - PluginType pluginType = descriptor.getType(); - if(PluginType.isNestedPackage(pluginType)){ - NestedPluginJarResourceLoader resourceLoader = - new NestedPluginJarResourceLoader(descriptor, parentClassLoader, resourceLoaderFactory); - resourceLoaderFactory.addResource(resourceLoader); - } else if(PluginType.isOuterPackage(pluginType)){ - addOuterPluginClasspath(descriptor); - addLibFile(descriptor); - } else { - addDirPluginClasspath(descriptor); - addLibFile(descriptor); - } - } - - 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()); - } + proxy.addResource(descriptor); } - private void addLibFile(InsidePluginDescriptor pluginDescriptor) throws Exception { - Set pluginLibInfos = pluginDescriptor.getPluginLibInfo(); - if(ObjectUtils.isEmpty(pluginLibInfos)){ - return; - } - String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); - String pluginLibDir = pluginDescriptor.getPluginLibDir(); - if(!ObjectUtils.isEmpty(pluginLibDir)){ - log.info("插件[{}]依赖加载目录: {}", pluginUnique, pluginLibDir); - } - 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()); - } - } - } - } - - @Override protected Class findClassFromParent(String className) throws ClassNotFoundException { if(mainResourceMatcher.match(className.replace(".", "/"))){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java index 503ddee..843ac22 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java @@ -3,6 +3,7 @@ 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.GeneralUrlClassLoader; import com.gitee.starblues.loader.launcher.LauncherContext; import com.gitee.starblues.utils.FilesUtils; @@ -21,59 +22,18 @@ import java.util.Set; * @since 3.0.4 */ @Slf4j -public class PluginGeneralUrlClassLoader extends GeneralUrlClassLoader { +public class PluginGeneralUrlClassLoader extends GeneralUrlClassLoader implements PluginResourceLoaderFactory{ - public PluginGeneralUrlClassLoader(String name, ClassLoader parent) { + private final PluginResourceLoaderFactory proxy; + + public PluginGeneralUrlClassLoader(String name, GeneralUrlClassLoader parent) { super(name, parent); + this.proxy = new PluginResourceLoaderFactoryProxy(this, parent); } + @Override public void addResource(InsidePluginDescriptor descriptor) throws Exception { - PluginType pluginType = descriptor.getType(); - if(PluginType.isNestedPackage(pluginType)){ - NestedPluginJarResourceLoader resourceLoader = - new NestedPluginJarResourceLoader(descriptor, parentClassLoader, resourceLoaderFactory); - resourceLoaderFactory.addResource(resourceLoader); - } else if(PluginType.isOuterPackage(pluginType)){ - addOuterPluginClasspath(descriptor); - addLibFile(descriptor); - } else { - addDirPluginClasspath(descriptor); - addLibFile(descriptor); - } - } - - - private void addLibFile(InsidePluginDescriptor pluginDescriptor) throws Exception { - Set pluginLibInfos = pluginDescriptor.getPluginLibInfo(); - if(ObjectUtils.isEmpty(pluginLibInfos)){ - return; - } - String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); - String pluginLibDir = pluginDescriptor.getPluginLibDir(); - if(!ObjectUtils.isEmpty(pluginLibDir)){ - log.info("插件[{}]依赖加载目录: {}", pluginUnique, pluginLibDir); - } - if(pluginLibInfos.isEmpty()){ - log.warn("插件[{}]依赖为空!", pluginUnique); - return; - } - - GeneralUrlClassLoader parentClassLoader = (GeneralUrlClassLoader) LauncherContext.getMainClassLoader(); - - - for (PluginLibInfo pluginLibInfo : pluginLibInfos) { - File existFile = FilesUtils.getExistFile(pluginLibInfo.getPath()); - if(existFile != null){ - if(pluginLibInfo.isLoadToMain()){ - // 加载到主程序中 - parentClassLoader.addFile(existFile); - log.debug("插件[{}]依赖被加载到主程序中: {}", pluginUnique, existFile.getPath()); - } else { - super.addFile(existFile); - log.debug("插件[{}]依赖被加载: {}", pluginUnique, existFile.getPath()); - } - } - } + proxy.addResource(descriptor); } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java new file mode 100644 index 0000000..8e3ed05 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java @@ -0,0 +1,24 @@ +package com.gitee.starblues.core.classloader; + +import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; +import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; + +/** + * 插件资源工程 + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +public interface PluginResourceLoaderFactory extends ResourceLoaderFactory { + + /** + * 加载插件资源 + * @param descriptor 插件资源描述 + * @throws Exception 添加插件资源异常 + * @since 3.0.4 + */ + void addResource(InsidePluginDescriptor descriptor) throws Exception; + + +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java new file mode 100644 index 0000000..ad863c7 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java @@ -0,0 +1,161 @@ +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.resource.Resource; +import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoader; +import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; +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.InputStream; +import java.net.URL; +import java.nio.file.Path; +import java.util.Enumeration; +import java.util.List; +import java.util.Set; + +/** + * 插件资源加载工厂代理 + * + * @author starBlues + * @version 3.0.4 + * @since 13.0.4 + */ +@Slf4j +public class PluginResourceLoaderFactoryProxy implements PluginResourceLoaderFactory { + + private final ResourceLoaderFactory target; + private final ResourceLoaderFactory parent; + + public PluginResourceLoaderFactoryProxy(ResourceLoaderFactory target) { + this(target, null); + } + + public PluginResourceLoaderFactoryProxy(ResourceLoaderFactory target, ResourceLoaderFactory parent) { + this.target = target; + this.parent = parent; + } + + @Override + public void addResource(InsidePluginDescriptor descriptor) throws Exception { + PluginType pluginType = descriptor.getType(); + if(PluginType.isNestedPackage(pluginType)){ + NestedPluginJarResourceLoader resourceLoader = + new NestedPluginJarResourceLoader(descriptor, target, parent); + target.addResource(resourceLoader); + } else if(PluginType.isOuterPackage(pluginType)){ + addOuterPluginClasspath(descriptor); + addLibFile(descriptor); + } else { + addDirPluginClasspath(descriptor); + addLibFile(descriptor); + } + } + + 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()); + } + } + + + private void addLibFile(InsidePluginDescriptor pluginDescriptor) throws Exception { + Set pluginLibInfos = pluginDescriptor.getPluginLibInfo(); + if(ObjectUtils.isEmpty(pluginLibInfos)){ + return; + } + String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); + String pluginLibDir = pluginDescriptor.getPluginLibDir(); + if(!ObjectUtils.isEmpty(pluginLibDir)){ + log.info("插件[{}]依赖加载目录: {}", pluginUnique, pluginLibDir); + } + if(pluginLibInfos.isEmpty()){ + log.warn("插件[{}]依赖为空!", pluginUnique); + return; + } + for (PluginLibInfo pluginLibInfo : pluginLibInfos) { + File existFile = FilesUtils.getExistFile(pluginLibInfo.getPath()); + if(existFile != null){ + if(pluginLibInfo.isLoadToMain()){ + // 加载到主程序中 + parent.addResource(existFile); + log.debug("插件[{}]依赖被加载到主程序中: {}", pluginUnique, existFile.getPath()); + } else { + target.addResource(existFile); + log.debug("插件[{}]依赖被加载: {}", pluginUnique, existFile.getPath()); + } + } + } + } + + + @Override + public void addResource(String path) throws Exception { + target.addResource(path); + } + + @Override + public void addResource(File file) throws Exception { + target.addResource(file); + } + + @Override + public void addResource(Path path) throws Exception { + target.addResource(path); + } + + @Override + public void addResource(URL url) throws Exception { + target.addResource(url); + } + + @Override + public void addResource(ResourceLoader resourceLoader) throws Exception { + target.addResource(resourceLoader); + } + + @Override + public Resource findFirstResource(String name) { + return target.findFirstResource(name); + } + + @Override + public Enumeration findAllResource(String name) { + return target.findAllResource(name); + } + + @Override + public InputStream getInputStream(String name) { + return target.getInputStream(name); + } + + @Override + public List getUrls() { + return target.getUrls(); + } + + @Override + public void close() throws Exception { + target.close(); + } +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java index 9433e73..5b8f502 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java @@ -1,6 +1,7 @@ package com.gitee.starblues.core.launcher.plugin; import com.gitee.starblues.core.classloader.NestedPluginJarResourceLoader; +import com.gitee.starblues.core.classloader.PluginGeneralUrlClassLoader; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.descriptor.PluginLibInfo; import com.gitee.starblues.core.descriptor.PluginType; @@ -8,6 +9,7 @@ import com.gitee.starblues.core.exception.PluginException; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; 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.FilesUtils; import com.gitee.starblues.utils.MsgUtils; @@ -38,10 +40,10 @@ public class PluginCoexistLauncher extends AbstractLauncher { @Override protected ClassLoader createClassLoader(String... args) throws Exception { - GeneralUrlClassLoader classLoader = new GeneralUrlClassLoader( + PluginGeneralUrlClassLoader classLoader = new PluginGeneralUrlClassLoader( pluginInteractive.getPluginDescriptor().getPluginId(), - this.getClass().getClassLoader()); - addClasspath(classLoader); + getParentClassLoader()); + classLoader.addResource(pluginInteractive.getPluginDescriptor()); return classLoader; } @@ -63,39 +65,12 @@ public class PluginCoexistLauncher extends AbstractLauncher { } } - private void addClasspath(GeneralUrlClassLoader classLoader) throws Exception { - InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); - PluginType pluginType = pluginDescriptor.getType(); - if(PluginType.isNestedPackage(pluginType)){ - classLoader.addResource(new NestedPluginJarResourceLoader( - pluginDescriptor, null, classLoader - )); - - } else if(PluginType.isOuterPackage(pluginType)){ - // TODO addOuterPluginClasspath(descriptor); - addLibFile(classLoader); + protected GeneralUrlClassLoader getParentClassLoader() throws Exception { + ClassLoader contextClassLoader = LauncherContext.getMainClassLoader(); + if(contextClassLoader instanceof GeneralUrlClassLoader){ + return (GeneralUrlClassLoader) contextClassLoader; } else { - // TODO addDirPluginClasspath(descriptor); - addLibFile(classLoader); - } - String pluginClassPath = pluginDescriptor.getPluginClassPath(); - classLoader.addResource(pluginClassPath); - } - - private void addLibFile(GeneralUrlClassLoader classLoader) throws Exception { - InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); - Set pluginLibInfos = pluginDescriptor.getPluginLibInfo(); - String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); - if(ObjectUtils.isEmpty(pluginLibInfos)){ - log.warn("插件[{}]依赖为空!", pluginUnique); - return; - } - for (PluginLibInfo pluginLibInfo : pluginLibInfos) { - File existFile = FilesUtils.getExistFile(pluginLibInfo.getPath()); - if(existFile != null){ - classLoader.addResource(existFile); - log.debug("插件[{}]依赖被加载: {}", pluginUnique, existFile.getPath()); - } + throw new Exception("非法父类加载器: " + contextClassLoader.getClass().getName()); } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java index 39e2548..c91557e 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java @@ -28,6 +28,8 @@ import com.gitee.starblues.integration.listener.PluginInitializerListenerFactory import com.gitee.starblues.integration.operator.upload.UploadByInputStreamParam; import com.gitee.starblues.integration.operator.upload.UploadByMultipartFileParam; import com.gitee.starblues.integration.operator.upload.UploadParam; +import com.gitee.starblues.loader.DevelopmentMode; +import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.spring.web.PluginStaticResourceConfig; import com.gitee.starblues.utils.*; import org.apache.commons.io.FileUtils; @@ -84,6 +86,7 @@ public class DefaultPluginOperator implements PluginOperator { } try { log.info("插件加载环境: {}", configuration.environment().toString()); + log.info("插件加载模式: {}", DevelopmentModeSetting.getDevelopmentMode().getDevelopmentMode()); pluginInitializerListenerFactory.addListener(pluginInitializerListener); List pluginsRoots = pluginManager.getPluginsRoots(); if(pluginsRoots.isEmpty()){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/simple/SimpleDevelopmentModeInitializer.java b/spring-brick/src/main/java/com/gitee/starblues/integration/simple/SimpleDevelopmentModeInitializer.java deleted file mode 100644 index da477cb..0000000 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/simple/SimpleDevelopmentModeInitializer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.gitee.starblues.integration.simple; - -import com.gitee.starblues.core.DefaultPluginManager; -import com.gitee.starblues.core.PluginManager; -import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; - -/** - * @author starBlues - * @version 1.0 - */ -public class SimpleDevelopmentModeInitializer implements ApplicationContextInitializer { - @Override - public void initialize(ConfigurableApplicationContext applicationContext) { - GeneralUrlClassLoader classLoader = (GeneralUrlClassLoader)Thread.currentThread().getContextClassLoader(); - try { - classLoader.addUrl("D:\\etc\\kitte\\ksm\\springboot-plugin-framework-parent\\springboot-plugin-framework-example\\example-plugins-basic\\example-basic-1\\target\\classes"); - classLoader.addUrl("D:\\etc\\kitte\\ksm\\springboot-plugin-framework-parent\\spring-brick-bootstrap\\target\\classes"); - } catch (Exception e) { - e.printStackTrace(); - } - } -} -- Gitee From e6de73438fb87a9d27eb8cfc95add3db53908f1a Mon Sep 17 00:00:00 2001 From: StarBlues Date: Wed, 27 Jul 2022 22:30:21 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootstrap/ConfigurePluginEnvironment.java | 6 +-- .../PluginDisableAutoConfiguration.java | 49 +++++++++++-------- .../annotation/ResolveClassLoader.java | 16 ++++++ .../CoexistAllowAutoConfiguration.java | 16 ++++++ .../CoexistResolveClassLoaderAspect.java | 18 ++++++- .../DefaultBootstrapLauncherFactory.java | 2 - .../common/PluginDisableAutoConfig.java | 25 ++++++++++ .../starblues/loader/DevelopmentMode.java | 16 ++++++ .../classloader/ClassLoaderTranslator.java | 16 ++++++ .../classloader/GeneralUrlClassLoader.java | 16 ++++++ .../loader/AbstractResourceLoader.java | 2 - .../storage/EmptyResourceStorage.java | 16 ++++++ .../loader/launcher/DevLauncher.java | 18 ++++++- .../launcher/DevelopmentModeSetting.java | 16 ++++++ .../loader/launcher/ProdLauncher.java | 16 ++++++ .../loader/launcher/SpringMainBootstrap.java | 1 - .../launcher/classpath/ClasspathResource.java | 16 ++++++ .../classpath/FastJarClasspathResource.java | 19 ++++++- .../classpath/JarOutClasspathResource.java | 17 ++++++- .../launcher/coexist/CoexistBaseLauncher.java | 26 ++++++---- .../coexist/CoexistFastJarLauncher.java | 6 --- .../coexist/CoexistJarOuterLauncher.java | 2 - .../launcher/simple/SimpleBaseLauncher.java | 16 ++++++ .../plugin/pack/main/JarNestPackager.java | 4 +- .../plugin/pack/main/JarOuterPackager.java | 4 +- .../starblues/core/DefaultPluginManager.java | 6 ++- .../NestedPluginJarResourceLoader.java | 3 +- .../core/classloader/PluginClassLoader.java | 1 + .../PluginGeneralUrlClassLoader.java | 26 ++++++---- .../PluginResourceLoaderFactory.java | 16 ++++++ .../PluginResourceLoaderFactoryProxy.java | 16 ++++++ .../extract/DefaultOpExtractFactory.java | 8 +-- update.md | 26 +++------- 33 files changed, 370 insertions(+), 91 deletions(-) create mode 100644 spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDisableAutoConfig.java diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java index 85aeda3..4e09169 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java @@ -88,10 +88,8 @@ public class ConfigurePluginEnvironment { Method method = LiveBeansView.class.getDeclaredMethod("registerApplicationContext", ConfigurableApplicationContext.class); method.setAccessible(true); method.invoke(null,processorContext.getApplicationContext()); - } - catch (Exception ex){ - logger.error("LiveBeansView.registerApplicationContext失败. {}", - ex.getMessage(), ex); + } catch (Exception ex){ + logger.error("LiveBeansView.registerApplicationContext失败. {}", ex.getMessage(), ex); } if(processorContext.runMode() == ProcessorContext.RunMode.ONESELF){ ConfigureMainPluginEnvironment configureMainPluginEnvironment = diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java index ae2026e..7586a5c 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java @@ -17,25 +17,29 @@ package com.gitee.starblues.bootstrap; import com.gitee.starblues.bootstrap.coexist.CoexistAllowAutoConfiguration; +import com.gitee.starblues.common.PluginDisableAutoConfig; import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.utils.ObjectUtils; import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter; import org.springframework.boot.autoconfigure.AutoConfigurationMetadata; -import java.util.ArrayList; -import java.util.List; +import java.util.*; /** * 插件禁用的 AutoConfiguration * * @author starBlues - * @version 3.0.3 + * @version 3.0.4 + * @since 3.0.3 */ public class PluginDisableAutoConfiguration implements AutoConfigurationImportFilter { - private static final ThreadLocal LAUNCH_PLUGIN = new ThreadLocal(); + public PluginDisableAutoConfiguration(){ + + } + public static void setLaunchPlugin() { LAUNCH_PLUGIN.set(true); } @@ -49,12 +53,25 @@ public class PluginDisableAutoConfiguration implements AutoConfigurationImportFi } else { boolean[] permitAll = new boolean[autoConfigurationClasses.length]; for (int i = 0; i < autoConfigurationClasses.length; i++) { - permitAll[i] = true; + permitAll[i] = permit( + PluginDisableAutoConfig.getCommonPluginDisableAutoConfig(), + autoConfigurationClasses[i]); } return permitAll; } } + private static boolean permit(Collection disableCollection, String className){ + if(ObjectUtils.isEmpty(className)){ + return true; + } + for (String disableFuzzyClass : disableCollection) { + if (className.contains(disableFuzzyClass)) { + return false; + } + } + return true; + } private static class IsolationDisableAutoConfiguration implements AutoConfigurationImportFilter{ @@ -70,18 +87,7 @@ public class PluginDisableAutoConfiguration implements AutoConfigurationImportFi disableFuzzyClass.add("org.springframework.boot.autoconfigure.websocket"); disableFuzzyClass.add("org.springframework.boot.autoconfigure.jackson"); disableFuzzyClass.add("org.springframework.boot.autoconfigure.webservices"); - } - - private boolean isDisabled(String className){ - if(ObjectUtils.isEmpty(className)){ - return false; - } - for (String disableFuzzyClass : disableFuzzyClass) { - if (className.contains(disableFuzzyClass)) { - return true; - } - } - return false; + disableFuzzyClass.addAll(PluginDisableAutoConfig.getCommonPluginDisableAutoConfig()); } @Override @@ -92,7 +98,7 @@ public class PluginDisableAutoConfiguration implements AutoConfigurationImportFi if(autoConfigurationClass == null || "".equals(autoConfigurationClass)){ continue; } - match[i] = !isDisabled(autoConfigurationClass); + match[i] = permit(disableFuzzyClass, autoConfigurationClass); } return match; } @@ -118,8 +124,11 @@ public class PluginDisableAutoConfiguration implements AutoConfigurationImportFi if(ObjectUtils.isEmpty(autoConfigurationClass)){ continue; } - - match[i] = configuration.match(autoConfigurationClass); + if(permit(PluginDisableAutoConfig.getCommonPluginDisableAutoConfig(), autoConfigurationClass)){ + match[i] = configuration.match(autoConfigurationClass); + } else { + match[i] = false; + } } return match; } else { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java index 47ae224..b9f22e9 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java @@ -1,3 +1,19 @@ +/** + * 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.bootstrap.annotation; import java.lang.annotation.*; diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java index 46c26f6..fc646f0 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java @@ -1,3 +1,19 @@ +/** + * 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.bootstrap.coexist; import com.gitee.starblues.utils.ObjectUtils; diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java index 9654061..9b6c309 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java @@ -1,3 +1,19 @@ +/** + * 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.bootstrap.coexist; import com.gitee.starblues.bootstrap.annotation.ResolveClassLoader; @@ -7,7 +23,7 @@ import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; /** - * Coexist模式下解决当前调用现场非本插件的ClassLoader的切面 + * Coexist模式下解决调用方法时, 非本插件的ClassLoader * * @author starBlues * @since 3.0.4 diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java index b4de874..3632f20 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java @@ -23,8 +23,6 @@ import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; -import org.springframework.boot.context.config.ConfigDataLoader; -import org.springframework.core.io.support.SpringFactoriesLoader; import java.util.List; diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDisableAutoConfig.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDisableAutoConfig.java new file mode 100644 index 0000000..bf7ecfd --- /dev/null +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDisableAutoConfig.java @@ -0,0 +1,25 @@ +package com.gitee.starblues.common; + +import java.util.HashSet; +import java.util.Set; + +/** + * 插件禁用的 AutoConfiguration 配置 + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +public class PluginDisableAutoConfig { + + private final static Set COMMON_PLUGIN_DISABLE_AUTO_CONFIG = new HashSet<>(); + + + static { + COMMON_PLUGIN_DISABLE_AUTO_CONFIG.add("com.gitee.starblues.integration.SpringBootPluginStarter"); + } + + public static Set getCommonPluginDisableAutoConfig() { + return COMMON_PLUGIN_DISABLE_AUTO_CONFIG; + } +} diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java index cd50338..08d7a9a 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java @@ -1,3 +1,19 @@ +/** + * 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; /** diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java index 193e312..1465f26 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java @@ -1,3 +1,19 @@ +/** + * 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; import com.gitee.starblues.loader.classloader.resource.Resource; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java index 69d09f8..eb93c52 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java @@ -1,3 +1,19 @@ +/** + * 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; import com.gitee.starblues.loader.classloader.resource.Resource; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/AbstractResourceLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/AbstractResourceLoader.java index b8e7adc..b3ec9ec 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/AbstractResourceLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/AbstractResourceLoader.java @@ -16,8 +16,6 @@ package com.gitee.starblues.loader.classloader.resource.loader; - -import com.gitee.starblues.loader.classloader.resource.storage.EmptyResourceStorage; import com.gitee.starblues.loader.classloader.resource.storage.ResourceStorage; import com.gitee.starblues.loader.utils.IOUtils; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java index 43e0bf4..5d248db 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java @@ -1,3 +1,19 @@ +/** + * 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.storage; import com.gitee.starblues.loader.classloader.resource.Resource; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java index 8ae95c9..ae80c9b 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java @@ -1,3 +1,19 @@ +/** + * 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.launcher.coexist.CoexistBaseLauncher; @@ -16,8 +32,6 @@ import lombok.AllArgsConstructor; @AllArgsConstructor public class DevLauncher implements Launcher{ - - private final SpringBootstrap springBootstrap; @Override diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java index 6c5cd34..21635c1 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java @@ -1,3 +1,19 @@ +/** + * 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.DevelopmentMode; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java index 25e807f..bf6d92c 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java @@ -1,3 +1,19 @@ +/** + * 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.jar.JarFile; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java index 6c913dc..bfcf712 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringMainBootstrap.java @@ -16,7 +16,6 @@ package com.gitee.starblues.loader.launcher; -import com.gitee.starblues.loader.DevelopmentMode; import com.gitee.starblues.loader.jar.JarFile; import com.gitee.starblues.loader.launcher.runner.MainMethodRunner; import com.gitee.starblues.loader.launcher.runner.MethodRunner; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java index 0d98135..e390b5d 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java @@ -1,3 +1,19 @@ +/** + * 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.classpath; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java index 2ed6500..ab18378 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java @@ -1,3 +1,19 @@ +/** + * 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.classpath; import com.gitee.starblues.loader.archive.Archive; @@ -12,7 +28,8 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import static com.gitee.starblues.loader.LoaderConstant.*; +import static com.gitee.starblues.loader.LoaderConstant.PROD_CLASSES_PATH; +import static com.gitee.starblues.loader.LoaderConstant.PROD_LIB_PATH; /** * fast jar 类型的 classpath 获取者 diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java index 3f695f6..0b94363 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java @@ -1,9 +1,24 @@ +/** + * 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.classpath; 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.resource.loader.MainJarResourceLoader; import com.gitee.starblues.loader.utils.FilesUtils; import com.gitee.starblues.loader.utils.ObjectUtils; import lombok.AllArgsConstructor; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java index d76026d..790bb6b 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java @@ -1,9 +1,23 @@ +/** + * 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.coexist; import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; -import com.gitee.starblues.loader.classloader.GenericClassLoader; import com.gitee.starblues.loader.launcher.AbstractMainLauncher; -import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; import com.gitee.starblues.loader.launcher.runner.MethodRunner; @@ -24,10 +38,8 @@ public class CoexistBaseLauncher extends AbstractMainLauncher { @Override protected ClassLoader createClassLoader(String... args) throws Exception { - GeneralUrlClassLoader urlClassLoader = new GeneralUrlClassLoader(MAIN_CLASS_LOADER_NAME, + return new GeneralUrlClassLoader(MAIN_CLASS_LOADER_NAME, this.getClass().getClassLoader()); - addResource(urlClassLoader); - return urlClassLoader; } @Override @@ -36,8 +48,4 @@ public class CoexistBaseLauncher extends AbstractMainLauncher { return classLoader; } - protected void addResource(GeneralUrlClassLoader classLoader) throws Exception{ - - } - } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java index 48431a8..f882a17 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistFastJarLauncher.java @@ -17,12 +17,8 @@ package com.gitee.starblues.loader.launcher.coexist; import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; -import com.gitee.starblues.loader.classloader.GenericClassLoader; -import com.gitee.starblues.loader.classloader.resource.loader.JarResourceLoader; -import com.gitee.starblues.loader.classloader.resource.loader.MainJarResourceLoader; import com.gitee.starblues.loader.launcher.classpath.ClasspathResource; import com.gitee.starblues.loader.launcher.classpath.FastJarClasspathResource; -import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; import com.gitee.starblues.loader.launcher.runner.MethodRunner; import java.io.File; @@ -30,8 +26,6 @@ import java.net.URL; import java.util.List; import java.util.Objects; -import static com.gitee.starblues.loader.LoaderConstant.PROD_CLASSES_URL_SIGN; - /** * 主程序jar in jar 模式启动者 * @author starBlues diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java index 6f85129..f3ee5ac 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistJarOuterLauncher.java @@ -17,10 +17,8 @@ package com.gitee.starblues.loader.launcher.coexist; import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; -import com.gitee.starblues.loader.classloader.GenericClassLoader; import com.gitee.starblues.loader.launcher.classpath.ClasspathResource; import com.gitee.starblues.loader.launcher.classpath.JarOutClasspathResource; -import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; import com.gitee.starblues.loader.launcher.runner.MethodRunner; import java.io.File; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java index d2198f0..fce2b97 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java @@ -1,3 +1,19 @@ +/** + * 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.simple; import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; 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 fb8f52c..9e1eeaa 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 @@ -85,8 +85,8 @@ public class JarNestPackager implements Repackager { attributes.putValue(MAIN_PACKAGE_TYPE, PackageType.MAIN_PACKAGE_TYPE_JAR); // 增加jar包title和version属性 MavenProject mavenProject = this.repackageMojo.getProject(); - attributes.putValue(IMPLEMENTATION_TITLE,mavenProject.getArtifactId()); - attributes.putValue(IMPLEMENTATION_VERSION,mavenProject.getVersion()); + attributes.putValue(IMPLEMENTATION_TITLE, mavenProject.getArtifactId()); + attributes.putValue(IMPLEMENTATION_VERSION, mavenProject.getVersion()); return manifest; } 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 0b79204..ec75a2b 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 @@ -90,8 +90,8 @@ public class JarOuterPackager extends JarNestPackager { // 增加jar包title和version属性 MavenProject mavenProject = this.repackageMojo.getProject(); - attributes.putValue(IMPLEMENTATION_TITLE,mavenProject.getArtifactId()); - attributes.putValue(IMPLEMENTATION_VERSION,mavenProject.getVersion()); + attributes.putValue(IMPLEMENTATION_TITLE, mavenProject.getArtifactId()); + attributes.putValue(IMPLEMENTATION_VERSION, mavenProject.getVersion()); return manifest; } 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 ac678a2..27237f4 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,8 @@ import java.util.stream.Collectors; /** * 抽象的插件管理者 * @author starBlues - * @version 3.0.3 + * @version 3.0.4 + * @since 3.0.0 */ public class DefaultPluginManager implements PluginManager{ @@ -251,7 +252,8 @@ public class DefaultPluginManager implements PluginManager{ } PluginException pluginException = PluginException.getPluginException(e, ()-> { unLoad(loadPluginInfo.getPluginId()); - throw new PluginException("插件包[ " + pluginPath + " ]安装: " + e.getMessage(), e); + // fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5GJO9 + return new PluginException("插件包[ " + pluginPath + " ]安装: " + e.getMessage(), e); }); pluginListenerFactory.startFailure(pluginInfo, pluginException); throw pluginException; 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 4430b53..628c96b 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 @@ -38,7 +38,8 @@ import java.util.zip.ZipEntry; /** * 嵌套插件jar加载者 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.0.4 */ @Slf4j public class NestedPluginJarResourceLoader extends AbstractResourceLoader { 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 3d19150..f26ece9 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 @@ -39,6 +39,7 @@ import java.util.Set; * 插件 classLoader * @author starBlues * @version 3.0.3 + * @since 3.0.0 */ @Slf4j public class PluginClassLoader extends GenericClassLoader implements PluginResourceLoaderFactory{ diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java index 843ac22..e93e7fa 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java @@ -1,19 +1,25 @@ +/** + * 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.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.GeneralUrlClassLoader; -import com.gitee.starblues.loader.launcher.LauncherContext; -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.util.Set; - /** * 插件基本 url classLoader * diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java index 8e3ed05..64e7089 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java @@ -1,3 +1,19 @@ +/** + * 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.core.classloader; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java index ad863c7..a3839ef 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java @@ -1,3 +1,19 @@ +/** + * 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.core.classloader; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/extract/DefaultOpExtractFactory.java b/spring-brick/src/main/java/com/gitee/starblues/spring/extract/DefaultOpExtractFactory.java index 48b05fc..7f56688 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/extract/DefaultOpExtractFactory.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/extract/DefaultOpExtractFactory.java @@ -28,7 +28,8 @@ import java.util.stream.Collectors; /** * 默认的可扩展的工厂 * @author starBlues - * @version 3.0.0 + * @version 3.0.4 + * @since 3.0.0 */ public class DefaultOpExtractFactory implements OpExtractFactory { @@ -93,11 +94,12 @@ public class DefaultOpExtractFactory implements OpExtractFactory { if(extractCoordinates == null){ throw new RuntimeException("Not found " + coordinate + " from plugin '" + pluginId + "'"); } - Object extracts = extractCoordinates.get(coordinate); + ExtractWrapper extracts = extractCoordinates.get(coordinate); if(extracts == null){ throw new RuntimeException("Not found " + coordinate + " from plugin '" + pluginId + "'"); } - return (T) extracts; + // fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5IFR4 + return (T) extracts.getObject(); } @SuppressWarnings("unchecked") diff --git a/update.md b/update.md index 6bbe377..5abb417 100644 --- a/update.md +++ b/update.md @@ -1,20 +1,6 @@ -1. 【新增[#I58CDB]([#I58CDB](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I58CDB))】 插件可触发`WebServerInitializedEvent`类型的事件 -2. 【新增】插件`dev`模式打包, 新增`localJars`配置(配置本地`jar`依赖文件) -3. 【新增】插件新增`@AutowiredType`注解, 可指定依赖注入类型 -4. 【新增】支持插件`Controller`可不配置地址前缀(配置后会影响插件拦截器和静态资源访问) -5. 【新增】新增`PluginContextHolder`, 使用方式见文档: [PluginContextHolder使用说明](https://www.yuque.com/starblues/spring-brick-3.0.0/un3cic) -6. 【优化】优化静态资源文件加载问题 -7. 【优化】优化插件在某些版本的`idea`中缺失`debug`包, 导致无法`debug` -8. 【优化】优化从主程序依赖加载`Class`资源模块 -9. 【优化】优化插件中注入异常提示 -10. 【优化】优化`Swagger`相关功能 -11. 【修复】`enablePluginIdRestPathPrefix`不生效问题 -12. 【修复】插件无法注入`ObjectProvider`、`ObjectFactory`类型为主程序的`Bean` -13. 【修复[#I58CDB](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I58CDB)】 插件`Controller`使用`Aop`后, 获取不到参数 -14. 【修复[#I58GCI](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I58GCI)】 主程序打包参数`libDir`不生效问题 -15. 【修复】修复主程序配置`version`, 插件未配置`requires`导致出现版本校验失败的问题 -16. 【修复】修复`StopValidator`禁止插件停止时, 插件状态变为`STOPPED_FAILURE`问题 -17. 【修复】解决`jdk17`反射问题, 导致无法注册插件`Controller`问题 - - -- 注意: 本次升级后, 从主程序注入的`Bean`, 需设置注入类型, 详见文档: [插件中注入主程序Bean说明](https://www.yuque.com/starblues/spring-brick-3.0.0/vot8gg) \ No newline at end of file +1. 【新增】增加主包MAINIFEST中title和version定义, 标准jar包中包含`Implementation-Version`和`Implementation-Title`属性 +2. 【新增】新增根据个人需求选择开发模式,支持隔离式开发模式(目前已有的)、共享式开发模式 +3. 【修复】修复插件中`LiveBeansView`注册异常问题 +4. 【修复[#I5IFR4](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5IFR3)】 `ExtractFactory#getExtractByCoordinate` 类型转换`Bug` +5. 【修复[#I5GJO9](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5GJO9)】`DefaultPluginManager#install` 异常无法抛出 +6. 【修复】修复插件无法加载其他包依赖中的`mybatis-xml`问题 \ No newline at end of file -- Gitee From b003eb9bc9d2d56b60a698a8ec473be460724a7c Mon Sep 17 00:00:00 2001 From: StarBlues Date: Sat, 30 Jul 2022 20:30:36 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PluginOneselfSpringApplication.java | 90 +++++++++++++++++++ .../bootstrap/PluginSpringApplication.java | 23 ++--- .../launcher/CoexistBootstrapLauncher.java | 3 +- .../DefaultBootstrapLauncherFactory.java | 7 +- .../launcher/IsolationBootstrapLauncher.java | 3 +- .../launcher/OneselfBootstrapLauncher.java | 47 ++++++++++ .../bootstrap/processor/ProcessorContext.java | 1 - .../gitee/starblues/common/ManifestKey.java | 6 ++ .../starblues/loader/DevelopmentMode.java | 50 +++-------- .../starblues/loader/LoaderConstant.java | 1 + .../storage/DefaultResourceStorage.java | 13 ++- .../loader/launcher/AbstractMainLauncher.java | 4 +- .../launcher/DevelopmentModeSetting.java | 31 +++++-- .../loader/launcher/ProdLauncher.java | 7 ++ .../loader/launcher/SpringBootstrap.java | 2 +- .../launcher/coexist/CoexistBaseLauncher.java | 8 +- .../isolation/IsolationBaseLauncher.java | 2 +- .../ResourceLoaderFactoryGetter.java | 9 +- .../gitee/starblues/plugin/pack/Constant.java | 6 ++ .../plugin/pack/main/JarNestPackager.java | 2 + .../plugin/pack/main/JarOuterPackager.java | 2 +- .../plugin/pack/main/MainConfig.java | 11 ++- .../plugin/pack/main/MainRepackager.java | 51 +++++++++++ .../AbstractPluginDescriptorLoader.java | 10 ++- .../plugin/PluginIsolationLauncher.java | 3 - .../operator/DefaultPluginOperator.java | 6 +- .../operator/EmptyPluginOperator.java | 88 ++++++++++++++++++ update.md | 4 +- 28 files changed, 395 insertions(+), 95 deletions(-) create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java create mode 100644 spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/OneselfBootstrapLauncher.java create mode 100644 spring-brick/src/main/java/com/gitee/starblues/integration/operator/EmptyPluginOperator.java diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java new file mode 100644 index 0000000..677b081 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java @@ -0,0 +1,90 @@ +package com.gitee.starblues.bootstrap; + +import com.gitee.starblues.bootstrap.processor.ProcessorContext; +import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; +import com.gitee.starblues.integration.operator.EmptyPluginOperator; +import com.gitee.starblues.integration.user.DefaultPluginUser; +import com.gitee.starblues.spring.extract.DefaultOpExtractFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * 插件自主启动的 SpringApplication + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +public class PluginOneselfSpringApplication extends SpringApplication { + + private final Logger logger = LoggerFactory.getLogger(PluginSpringApplication.class); + + protected final SpringPluginProcessor pluginProcessor; + protected final ProcessorContext processorContext; + + private final ConfigurePluginEnvironment configurePluginEnvironment; + private final GenericApplicationContext applicationContext; + + + public PluginOneselfSpringApplication(SpringPluginProcessor pluginProcessor, + ProcessorContext processorContext, + Class... primarySources) { + super(primarySources); + this.pluginProcessor = pluginProcessor; + this.processorContext = processorContext; + this.configurePluginEnvironment = new ConfigurePluginEnvironment(processorContext); + this.applicationContext = getApplicationContext(); + } + + protected GenericApplicationContext getApplicationContext() { + return (GenericApplicationContext) super.createApplicationContext(); + } + + @Override + protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) { + super.configureEnvironment(environment, args); + configurePluginEnvironment.configureEnvironment(environment, args); + } + + @Override + protected ConfigurableApplicationContext createApplicationContext() { + return this.applicationContext; + } + + @Override + public ConfigurableApplicationContext run(String... args) { + try { + processorContext.setApplicationContext(this.applicationContext); + PluginContextHolder.initialize(processorContext); + pluginProcessor.initialize(processorContext); + registerMainBean(); + return super.run(args); + } catch (Exception e) { + pluginProcessor.failure(processorContext); + logger.debug("启动插件[{}]失败. {}", + processorContext.getPluginDescriptor().getPluginId(), + e.getMessage(), e); + throw new RuntimeException(e); + } + } + + @Override + protected void refresh(ConfigurableApplicationContext applicationContext) { + pluginProcessor.refreshBefore(processorContext); + super.refresh(applicationContext); + pluginProcessor.refreshAfter(processorContext); + } + + private void registerMainBean(){ + DefaultListableBeanFactory beanFactory = applicationContext.getDefaultListableBeanFactory(); + beanFactory.registerSingleton("extractFactory", new DefaultOpExtractFactory()); + beanFactory.registerSingleton("pluginUser", new DefaultPluginUser(applicationContext)); + beanFactory.registerSingleton("pluginOperator", new EmptyPluginOperator()); + } + +} 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 3eabf8f..fdbff10 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 @@ -18,17 +18,13 @@ package com.gitee.starblues.bootstrap; import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; -import com.gitee.starblues.spring.ApplicationContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; -import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.StandardEnvironment; @@ -47,7 +43,6 @@ public class PluginSpringApplication extends SpringApplication { protected final SpringPluginProcessor pluginProcessor; protected final ProcessorContext processorContext; - private final ProcessorContext.RunMode runMode; private final ConfigurePluginEnvironment configurePluginEnvironment; private final GenericApplicationContext applicationContext; private final ResourceLoader resourceLoader; @@ -57,7 +52,6 @@ public class PluginSpringApplication extends SpringApplication { ProcessorContext processorContext, Class... primarySources) { super(primarySources); - this.runMode = processorContext.runMode(); this.pluginProcessor = pluginProcessor; this.processorContext = processorContext; this.resourceLoader = processorContext.getResourceLoader(); @@ -67,9 +61,6 @@ public class PluginSpringApplication extends SpringApplication { } protected GenericApplicationContext getApplicationContext(){ - if(runMode == ProcessorContext.RunMode.ONESELF){ - return (GenericApplicationContext) super.createApplicationContext(); - } DefaultListableBeanFactory beanFactory = getBeanFactory(processorContext); if(processorContext.getMainApplicationContext().isWebEnvironment()){ return new PluginWebApplicationContext(beanFactory, processorContext); @@ -83,14 +74,12 @@ public class PluginSpringApplication extends SpringApplication { } public void setDefaultPluginConfig(){ - if(runMode == ProcessorContext.RunMode.PLUGIN){ - setResourceLoader(resourceLoader); - setBannerMode(Banner.Mode.OFF); - setEnvironment(new StandardEnvironment()); - setWebApplicationType(WebApplicationType.NONE); - setRegisterShutdownHook(false); - setLogStartupInfo(false); - } + setResourceLoader(resourceLoader); + setBannerMode(Banner.Mode.OFF); + setEnvironment(new StandardEnvironment()); + setWebApplicationType(WebApplicationType.NONE); + setRegisterShutdownHook(false); + setLogStartupInfo(false); } @Override diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java index abd8a2e..be89db5 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java @@ -27,6 +27,7 @@ import com.gitee.starblues.spring.SpringPluginHook; import lombok.AllArgsConstructor; import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.boot.SpringApplication; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; @@ -50,7 +51,7 @@ public class CoexistBootstrapLauncher implements BootstrapLauncher{ ProcessorContext processorContext = new DefaultProcessorContext( bootstrap.getRunMode(), bootstrap, pluginInteractive, bootstrap.getClass() ); - CoexistSpringApplication springApplication = new CoexistSpringApplication( + SpringApplication springApplication = new CoexistSpringApplication( pluginProcessor, processorContext, primarySources); diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java index 3632f20..f76074f 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java @@ -23,6 +23,7 @@ import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; +import com.gitee.starblues.loader.launcher.simple.SimpleBaseLauncher; import java.util.List; @@ -45,8 +46,12 @@ public class DefaultBootstrapLauncherFactory implements BootstrapLauncherFactory BootstrapLauncher bootstrapLauncher = null; if(DevelopmentModeSetting.isolation()){ bootstrapLauncher = new IsolationBootstrapLauncher(bootstrap, pluginProcessor, pluginInteractive); - } else { + } else if(DevelopmentModeSetting.coexist()){ bootstrapLauncher = new CoexistBootstrapLauncher(bootstrap, pluginProcessor, pluginInteractive); + } else if(DevelopmentModeSetting.simple()){ + throw new RuntimeException("暂未实现[" + DevelopmentModeSetting.getDevelopmentMode().toString() + "]模式"); + } else { + bootstrapLauncher = new OneselfBootstrapLauncher(bootstrap, pluginProcessor, pluginInteractive); } return bootstrapLauncher; } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java index 6d4fde7..f4b2f97 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java @@ -25,6 +25,7 @@ import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; import com.gitee.starblues.spring.SpringPluginHook; import lombok.AllArgsConstructor; +import org.springframework.boot.SpringApplication; /** * isolation 模式插件启动器 @@ -48,7 +49,7 @@ public class IsolationBootstrapLauncher implements BootstrapLauncher{ ProcessorContext processorContext = new DefaultProcessorContext( runMode, bootstrap, pluginInteractive, bootstrap.getClass() ); - PluginSpringApplication springApplication = new PluginSpringApplication( + SpringApplication springApplication = new PluginSpringApplication( pluginProcessor, processorContext, primarySources); diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/OneselfBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/OneselfBootstrapLauncher.java new file mode 100644 index 0000000..4949b30 --- /dev/null +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/OneselfBootstrapLauncher.java @@ -0,0 +1,47 @@ +package com.gitee.starblues.bootstrap.launcher; + +import com.gitee.starblues.bootstrap.DefaultSpringPluginHook; +import com.gitee.starblues.bootstrap.PluginOneselfSpringApplication; +import com.gitee.starblues.bootstrap.PluginSpringApplication; +import com.gitee.starblues.bootstrap.SpringPluginBootstrap; +import com.gitee.starblues.bootstrap.processor.DefaultProcessorContext; +import com.gitee.starblues.bootstrap.processor.ProcessorContext; +import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; +import com.gitee.starblues.core.launcher.plugin.PluginInteractive; +import com.gitee.starblues.spring.SpringPluginHook; +import lombok.AllArgsConstructor; +import org.springframework.boot.SpringApplication; + +/** + * 插件自主启动配置 + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +@AllArgsConstructor +public class OneselfBootstrapLauncher implements BootstrapLauncher{ + + private final SpringPluginBootstrap bootstrap; + private final SpringPluginProcessor pluginProcessor; + private final PluginInteractive pluginInteractive; + + + @Override + public SpringPluginHook launch(Class[] primarySources, String[] args) { + ProcessorContext.RunMode runMode = bootstrap.getRunMode(); + + ProcessorContext processorContext = new DefaultProcessorContext( + runMode, bootstrap, pluginInteractive, bootstrap.getClass() + ); + SpringApplication springApplication = new PluginOneselfSpringApplication( + pluginProcessor, + processorContext, + primarySources); + springApplication.run(args); + return new DefaultSpringPluginHook(pluginProcessor, processorContext); + } + + + +} diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java index e9c23b4..42cec5b 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java @@ -132,7 +132,6 @@ public interface ProcessorContext extends RegistryInfo { /** * 插件独立运行 */ - @Deprecated ONESELF } 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 fdfc471..c4b25f9 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 @@ -83,6 +83,11 @@ public class ManifestKey { */ public static final String MAIN_PACKAGE_TYPE = "Main-Package-Type"; + /** + * jar main development mode + */ + public static final String DEVELOPMENT_MODE = "Development-Mode"; + /** * jar package name */ @@ -93,6 +98,7 @@ public class ManifestKey { */ public static final String IMPLEMENTATION_VERSION = "Implementation-Version"; + /** * 获取值 * diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java index 08d7a9a..ac3e555 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java @@ -1,67 +1,43 @@ /** * 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 + * 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. + * 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; /** - * 插件开发模式 + * 开发模式key定义 * * @author starBlues * @since 3.0.4 * @version 3.0.4 */ -public enum DevelopmentMode { +public abstract class DevelopmentMode { /** * 隔离模式 */ - ISOLATION("isolation"), + public static final String ISOLATION = "isolation"; /** * 共存模式 */ - COEXIST("coexist"), + public static final String COEXIST = "coexist"; /** * 简单模式 */ - SIMPLE("simple"); + public static final String SIMPLE = "simple"; - private final String developmentMode; - - DevelopmentMode(String developmentMode) { - this.developmentMode = developmentMode; - } - - public String getDevelopmentMode() { - return developmentMode; - } - - @Override - public String toString() { - return developmentMode; - } - - public static DevelopmentMode byName(String model){ - if(COEXIST.getDevelopmentMode().equalsIgnoreCase(model)){ - return DevelopmentMode.ISOLATION; - } else if(SIMPLE.getDevelopmentMode().equalsIgnoreCase(model)){ - return DevelopmentMode.SIMPLE; - } else { - return DevelopmentMode.ISOLATION; - } - } } 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 index 4631f55..94f65c4 100644 --- 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 @@ -42,6 +42,7 @@ public class LoaderConstant { public static final String MAIN_LIB_INDEXES_SPLIT = " "; public static final String START_CLASS = "Start-Class"; + public static final String MAIN_DEVELOPMENT_MODE = "Development-Mode"; public static final String MAIN_PACKAGE_TYPE = "Main-Package-Type"; public static final String MAIN_PACKAGE_TYPE_JAR = "jar"; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/DefaultResourceStorage.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/DefaultResourceStorage.java index 84a7733..a1f230d 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/DefaultResourceStorage.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/DefaultResourceStorage.java @@ -49,8 +49,9 @@ public class DefaultResourceStorage extends SameRootResourceStorage{ @Override public void add(String name, URL url, ResourceByteGetter byteGetter) throws Exception{ - Assert.isNotEmpty(name, "name 不能为空"); - Assert.isNotNull(url, "url 不能为空"); + if(ObjectUtils.isEmpty(name) || url == null){ + return; + } name = formatResourceName(name); if(resourceStorage.containsKey(name)){ return; @@ -61,6 +62,9 @@ public class DefaultResourceStorage extends SameRootResourceStorage{ @Override public void add(String name, URL url) throws Exception{ + if(ObjectUtils.isEmpty(name) || url == null){ + return; + } this.add(name, url, null); } @@ -74,8 +78,9 @@ public class DefaultResourceStorage extends SameRootResourceStorage{ } protected void addResource(String name, Resource resource){ - Assert.isNotEmpty(name, "name 不能为空"); - Assert.isNotNull(resource, "resource 不能为空"); + if(ObjectUtils.isEmpty(name) || resource == null){ + return; + } resourceStorage.put(name, resource); } 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 index 509097a..06be41d 100644 --- 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 @@ -76,12 +76,12 @@ public abstract class AbstractMainLauncher extends AbstractLauncher classLoader.loadClass(SPRING_PLUGIN_BOOTSTRAP_PACKAGE_NAME); if(DevelopmentModeSetting.isolation()){ // 主程序加载到了 - throw new RuntimeException("[" + DevelopmentMode.ISOLATION.getDevelopmentMode() + "]模式下" + + throw new RuntimeException("[" + DevelopmentMode.ISOLATION + "]模式下" + "不能将[" + SPRING_PLUGIN_BOOTSTRAP_COORDINATE + "]依赖定义到主程序中, 只能依赖到插件中!"); } } catch (ClassNotFoundException e) { if(!DevelopmentModeSetting.isolation()){ - String mode = DevelopmentMode.COEXIST.getDevelopmentMode() + "/" + DevelopmentMode.SIMPLE.getDevelopmentMode(); + String mode = DevelopmentMode.COEXIST + "/" + DevelopmentMode.SIMPLE; throw new RuntimeException("[" + mode + "]模式" + "需要将[" + SPRING_PLUGIN_BOOTSTRAP_COORDINATE + "]依赖定义到主程序中!"); } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java index 21635c1..4ee30c5 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java @@ -18,8 +18,6 @@ package com.gitee.starblues.loader.launcher; import com.gitee.starblues.loader.DevelopmentMode; -import java.util.Objects; - /** * DevelopmentMode 设置者 * @@ -29,26 +27,41 @@ import java.util.Objects; */ public class DevelopmentModeSetting { - private static DevelopmentMode developmentMode; + private static String developmentMode = DevelopmentMode.ISOLATION; - static void setDevelopmentMode(DevelopmentMode developmentMode) { - DevelopmentModeSetting.developmentMode = developmentMode; + static void setDevelopmentMode(String developmentMode) { + DevelopmentModeSetting.developmentMode = checkModeKey(developmentMode); } public static boolean isolation(){ - return Objects.equals(developmentMode, DevelopmentMode.ISOLATION); + return DevelopmentMode.ISOLATION.equalsIgnoreCase(developmentMode); } public static boolean coexist(){ - return Objects.equals(developmentMode, DevelopmentMode.COEXIST); + return DevelopmentMode.COEXIST.equalsIgnoreCase(developmentMode); } public static boolean simple(){ - return Objects.equals(developmentMode, DevelopmentMode.SIMPLE); + return DevelopmentMode.SIMPLE.equalsIgnoreCase(developmentMode); } - public static DevelopmentMode getDevelopmentMode(){ + public static String getDevelopmentMode(){ return developmentMode; } + private static String checkModeKey(String developmentMode){ + if(developmentMode == null || "".equals(developmentMode)){ + throw new RuntimeException("developmentMode设置不能为空"); + } + if(DevelopmentMode.ISOLATION.equalsIgnoreCase(developmentMode)){ + return developmentMode; + } else if(DevelopmentMode.COEXIST.equalsIgnoreCase(developmentMode)){ + return developmentMode; + } else if(DevelopmentMode.SIMPLE.equalsIgnoreCase(developmentMode)){ + return developmentMode; + } else { + throw new RuntimeException("不支持开发模式: " + developmentMode); + } + } + } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java index bf6d92c..eb2893b 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java @@ -55,6 +55,7 @@ public class ProdLauncher implements Launcher{ File rootJarFile = getRootJarFile(); String startClass = null; String mainPackageType; + String developmentMode; try (JarFile jarFile = new JarFile(rootJarFile)){ Manifest manifest = jarFile.getManifest(); IllegalStateException exception = new IllegalStateException("当前启动包非法包!"); @@ -67,8 +68,14 @@ public class ProdLauncher implements Launcher{ throw exception; } mainPackageType = mainAttributes.getValue(MAIN_PACKAGE_TYPE); + developmentMode = mainAttributes.getValue(MAIN_DEVELOPMENT_MODE); } + if(ObjectUtils.isEmpty(developmentMode)){ + throw new RuntimeException("未发现 developmentMode 配置"); + } + DevelopmentModeSetting.setDevelopmentMode(developmentMode); + MethodRunner methodRunner = new MethodRunner(startClass, SPRING_BOOTSTRAP_RUN_METHOD, args); AbstractMainLauncher launcher; diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java index ee02018..8a86ddf 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/SpringBootstrap.java @@ -38,7 +38,7 @@ public interface SpringBootstrap { * * @return DevelopmentMode */ - default DevelopmentMode developmentMode(){ + default String developmentMode(){ return DevelopmentMode.ISOLATION; } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java index 790bb6b..2ce521e 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java @@ -38,8 +38,10 @@ public class CoexistBaseLauncher extends AbstractMainLauncher { @Override protected ClassLoader createClassLoader(String... args) throws Exception { - return new GeneralUrlClassLoader(MAIN_CLASS_LOADER_NAME, + GeneralUrlClassLoader urlClassLoader = new GeneralUrlClassLoader(MAIN_CLASS_LOADER_NAME, this.getClass().getClassLoader()); + addResource(urlClassLoader); + return urlClassLoader; } @Override @@ -48,4 +50,8 @@ public class CoexistBaseLauncher extends AbstractMainLauncher { return classLoader; } + protected void addResource(GeneralUrlClassLoader classLoader) throws Exception{ + + } + } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationBaseLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationBaseLauncher.java index bea556f..9cb11d5 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationBaseLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/IsolationBaseLauncher.java @@ -48,7 +48,7 @@ public class IsolationBaseLauncher extends AbstractMainLauncher { @Override protected ClassLoader createClassLoader(String... args) throws Exception { GenericClassLoader classLoader = new GenericClassLoader(MAIN_CLASS_LOADER_NAME, getParentClassLoader(), - getResourceLoaderFactory()); + getResourceLoaderFactory(args)); addResource(classLoader); return classLoader; } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java index b04ba0a..6cbd1f1 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java @@ -80,12 +80,15 @@ public class ResourceLoaderFactoryGetter { public static SameRootResourceStorage getResourceStorage(String key, URL baseUrl){ SameRootResourceStorage resourceStorage = null; - if(Objects.equals(resourceMode, RESOURCE_MODE_NO_CACHE)){ - resourceStorage = new DefaultResourceStorage(baseUrl); + if(Objects.equals(resourceMode, RESOURCE_MODE_CACHE_ISOLATION)){ + // 资源可缓存, 且隔离 + resourceStorage = new CacheResourceStorage(baseUrl); } else if(Objects.equals(resourceMode, RESOURCE_MODE_CACHE_SHARE)){ + // 资源可缓存, 共享式 resourceStorage = new ShareResourceStorage(key, baseUrl); } else { - resourceStorage = new CacheResourceStorage(baseUrl); + // 资源不缓存 + resourceStorage = new DefaultResourceStorage(baseUrl); } return resourceStorage; } 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 b66fe5d..ce11f06 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 @@ -41,6 +41,12 @@ public class Constant { public static final String PLUGIN_METE_COMMENTS = "plugin meta configuration"; + /** + * 开发模式方法名称 + */ + public static final String DEVELOPMENT_MODE_METHOD_NAME = "developmentMode"; + + public static boolean isPom(String packageType){ return PACKAGING_POM.equalsIgnoreCase(packageType); } 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 9e1eeaa..3b9eeb9 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 @@ -83,6 +83,8 @@ public class JarNestPackager implements Repackager { attributes.putValue(START_CLASS, mainConfig.getMainClass()); attributes.putValue(MAIN_CLASS, MAIN_CLASS_VALUE); attributes.putValue(MAIN_PACKAGE_TYPE, PackageType.MAIN_PACKAGE_TYPE_JAR); + attributes.putValue(DEVELOPMENT_MODE, mainConfig.getDevelopmentMode()); + // 增加jar包title和version属性 MavenProject mavenProject = this.repackageMojo.getProject(); attributes.putValue(IMPLEMENTATION_TITLE, mavenProject.getArtifactId()); 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 ec75a2b..f4db7ca 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 @@ -86,7 +86,7 @@ public class JarOuterPackager extends JarNestPackager { attributes.putValue(MAIN_CLASS, MAIN_CLASS_VALUE); attributes.putValue(MAIN_PACKAGE_TYPE, PackageType.MAIN_PACKAGE_TYPE_JAR_OUTER); attributes.putValue(MAIN_LIB_DIR, getLibPath()); - attributes.putValue(MAIN_LIB_INDEXES, getLibIndexes()); + attributes.putValue(DEVELOPMENT_MODE, mainConfig.getDevelopmentMode()); // 增加jar包title和version属性 MavenProject mavenProject = this.repackageMojo.getProject(); 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 f8ee0ee..1640483 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 @@ -16,11 +16,8 @@ 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; /** * 主程序打包配置 @@ -59,6 +56,12 @@ public class MainConfig { */ private String outputDirectory; - + /** + * 开发模式: + * isolation: 隔离模式[默认] + * coexist: 共享模式 + * simple: 简单模式 + */ + private String developmentMode; } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/MainRepackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/MainRepackager.java index a32d278..e0d73f2 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/MainRepackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/MainRepackager.java @@ -17,14 +17,24 @@ 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; import com.gitee.starblues.utils.ObjectUtils; +import com.gitee.starblues.utils.ReflectionUtils; import lombok.Getter; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Set; + /** * 主程序打包 * @author starBlues @@ -44,6 +54,7 @@ public class MainRepackager implements Repackager { @Override public void repackage() throws MojoExecutionException, MojoFailureException { checkConfig(); + setDevelopmentMode(); String packageType = mainConfig.getPackageType(); if(PackageType.MAIN_PACKAGE_TYPE_JAR.equalsIgnoreCase(packageType)){ new JarNestPackager(this).repackage(); @@ -76,5 +87,45 @@ public class MainRepackager implements Repackager { } } + private void setDevelopmentMode() throws MojoFailureException{ + String developmentMode = mainConfig.getDevelopmentMode(); + if(!ObjectUtils.isEmpty(developmentMode)){ + return; + } + try { + File file = new File(repackageMojo.getProject().getBuild().getOutputDirectory()); + Set artifacts = repackageMojo.getProject().getArtifacts(); + + URL[] urls = new URL[artifacts.size() + 1]; + int i = 0; + for (Artifact artifact : artifacts) { + urls[i] = artifact.getFile().toURI().toURL(); + i++; + } + urls[i] = file.toURI().toURL(); + URLClassLoader urlClassLoader = new URLClassLoader(urls, null); + + String mainClass = repackageMojo.getMainConfig().getMainClass(); + if(ObjectUtils.isEmpty(mainClass)){ + throw new Exception("mainConfig.mainClass config can't be empty"); + } + Class aClass = urlClassLoader.loadClass(mainClass); + Method method = ReflectionUtils.findMethod(aClass, Constant.DEVELOPMENT_MODE_METHOD_NAME); + String methodKey = aClass.getName() + "#" + Constant.DEVELOPMENT_MODE_METHOD_NAME + "()"; + if(method == null){ + throw new Exception("Not found method : " + methodKey); + } + method.setAccessible(true); + Object o = aClass.getConstructor().newInstance(); + Object result = method.invoke(o); + if(ObjectUtils.isEmpty(result)){ + throw new Exception(methodKey + " return value can't be empty"); + } + getMainConfig().setDevelopmentMode(String.valueOf(result)); + } catch (Exception e) { + throw new MojoFailureException("Set developmentMode failure:" + e.getMessage()); + } + } + } 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 3f7bd73..d9d48f1 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 @@ -158,8 +158,11 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor String pluginLibDir = descriptor.getPluginLibDir(); boolean configPluginLibDir = false; if(!ObjectUtils.isEmpty(pluginLibDir)){ - descriptor.setPluginLibDir(getLibDir(descriptor, pluginLibDir)); - configPluginLibDir = true; + String libDir = getLibDir(descriptor, pluginLibDir); + if(!ObjectUtils.isEmpty(libDir)){ + descriptor.setPluginLibDir(libDir); + configPluginLibDir = true; + } } if(ObjectUtils.isEmpty(dependenciesIndex)){ return Collections.emptySet(); @@ -202,8 +205,7 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor if(new File(resolveRelativePath).exists()){ return resolveRelativePath; } - throw new PluginException("插件["+ MsgUtils.getPluginUnique(descriptor) +"]" + - "依赖目录[" + descriptor.getPluginLibDir() + "]不存在!"); + return null; } protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String index){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java index 0edcdbe..af1baad 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java @@ -16,12 +16,9 @@ package com.gitee.starblues.core.launcher.plugin; -import com.gitee.starblues.common.Constants; import com.gitee.starblues.core.classloader.*; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; -import com.gitee.starblues.core.exception.PluginException; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; -import com.gitee.starblues.loader.DevelopmentMode; 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; diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java index c91557e..11e1c82 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java @@ -28,7 +28,6 @@ import com.gitee.starblues.integration.listener.PluginInitializerListenerFactory import com.gitee.starblues.integration.operator.upload.UploadByInputStreamParam; import com.gitee.starblues.integration.operator.upload.UploadByMultipartFileParam; import com.gitee.starblues.integration.operator.upload.UploadParam; -import com.gitee.starblues.loader.DevelopmentMode; import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; import com.gitee.starblues.spring.web.PluginStaticResourceConfig; import com.gitee.starblues.utils.*; @@ -55,7 +54,8 @@ import java.util.concurrent.atomic.AtomicBoolean; /** * 默认的插件操作者 * @author starBlues - * @version 3.0.1 + * @version 3.0.0 + * @since 3.0.4 */ public class DefaultPluginOperator implements PluginOperator { protected final Logger log = LoggerFactory.getLogger(this.getClass()); @@ -86,7 +86,7 @@ public class DefaultPluginOperator implements PluginOperator { } try { log.info("插件加载环境: {}", configuration.environment().toString()); - log.info("插件加载模式: {}", DevelopmentModeSetting.getDevelopmentMode().getDevelopmentMode()); + log.info("插件加载模式: {}", DevelopmentModeSetting.getDevelopmentMode()); pluginInitializerListenerFactory.addListener(pluginInitializerListener); List pluginsRoots = pluginManager.getPluginsRoots(); if(pluginsRoots.isEmpty()){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/EmptyPluginOperator.java b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/EmptyPluginOperator.java new file mode 100644 index 0000000..c56b306 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/EmptyPluginOperator.java @@ -0,0 +1,88 @@ +package com.gitee.starblues.integration.operator; + +import com.gitee.starblues.core.PluginInfo; +import com.gitee.starblues.core.exception.PluginException; +import com.gitee.starblues.integration.listener.PluginInitializerListener; +import com.gitee.starblues.integration.operator.upload.UploadParam; + +import java.nio.file.Path; +import java.util.List; + +/** + * 空操作的 PluginOperator + * + * @author starBlues + * @version 3.0.4 + * @since 3.0.4 + */ +public class EmptyPluginOperator implements PluginOperator{ + @Override + public boolean initPlugins(PluginInitializerListener pluginInitializerListener) throws PluginException { + return false; + } + + @Override + public boolean verify(Path pluginPath) throws PluginException { + return false; + } + + @Override + public PluginInfo parse(Path pluginPath) throws PluginException { + return null; + } + + @Override + public PluginInfo install(Path pluginPath, boolean unpackPlugin) throws PluginException { + return null; + } + + @Override + public void uninstall(String pluginId, boolean isDelete, boolean isBackup) throws PluginException { + + } + + @Override + public PluginInfo load(Path pluginPath, boolean unpackPlugin) throws PluginException { + return null; + } + + @Override + public boolean unload(String pluginId) throws PluginException { + return false; + } + + @Override + public boolean start(String pluginId) throws PluginException { + return false; + } + + @Override + public boolean stop(String pluginId) throws PluginException { + return false; + } + + @Override + public PluginInfo uploadPlugin(UploadParam uploadParam) throws PluginException { + return null; + } + + @Override + public Path backupPlugin(Path backDirPath, String sign) throws PluginException { + return null; + } + + @Override + public Path backupPlugin(String pluginId, String sign) throws PluginException { + return null; + } + + @Override + public List getPluginInfo() { + return null; + } + + @Override + public PluginInfo getPluginInfo(String pluginId) { + return null; + } +} diff --git a/update.md b/update.md index 5abb417..467a849 100644 --- a/update.md +++ b/update.md @@ -3,4 +3,6 @@ 3. 【修复】修复插件中`LiveBeansView`注册异常问题 4. 【修复[#I5IFR4](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5IFR3)】 `ExtractFactory#getExtractByCoordinate` 类型转换`Bug` 5. 【修复[#I5GJO9](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5GJO9)】`DefaultPluginManager#install` 异常无法抛出 -6. 【修复】修复插件无法加载其他包依赖中的`mybatis-xml`问题 \ No newline at end of file +6. 【修复】修复插件无法加载其他包依赖中的`mybatis-xml`问题 +7. 【修复】修复插件子启动问题 +8. 【优化】优化依赖资源默认不缓存, 以减少内存 -- Gitee From 26ff0ed55a199c52aa2df8c4813dd1df694c8f35 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Sun, 14 Aug 2022 11:26:31 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=90=AFbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootstrap/ConfigurePluginEnvironment.java | 6 --- .../PluginOneselfSpringApplication.java | 14 +------ .../DefaultBootstrapLauncherFactory.java | 3 -- .../ComposeSpringPluginProcessor.java | 11 +++++ .../processor/SpringPluginProcessor.java | 1 + .../ConfigureMainPluginEnvironment.java | 10 ++--- .../processor/oneself/OneselfProcessor.java | 42 ++++++++++--------- .../starblues/loader/DevelopmentMode.java | 5 --- .../loader/launcher/AbstractMainLauncher.java | 3 +- .../loader/launcher/DevLauncher.java | 9 ++-- .../launcher/DevelopmentModeSetting.java | 23 ++++++---- .../loader/launcher/ProdLauncher.java | 20 ++++----- 12 files changed, 69 insertions(+), 78 deletions(-) rename spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/{ => processor/oneself}/ConfigureMainPluginEnvironment.java (92%) rename spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java => spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/OneselfProcessor.java (34%) diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java index 4e09169..536b4e3 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigurePluginEnvironment.java @@ -91,12 +91,6 @@ public class ConfigurePluginEnvironment { } catch (Exception ex){ logger.error("LiveBeansView.registerApplicationContext失败. {}", ex.getMessage(), ex); } - if(processorContext.runMode() == ProcessorContext.RunMode.ONESELF){ - ConfigureMainPluginEnvironment configureMainPluginEnvironment = - new ConfigureMainPluginEnvironment(processorContext); - configureMainPluginEnvironment.configureEnvironment(environment, args); - env.put(AutoIntegrationConfiguration.ENABLE_STARTER_KEY, false); - } if(DevelopmentModeSetting.coexist()){ env.put(AutoIntegrationConfiguration.ENABLE_STARTER_KEY, false); diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java index 677b081..d7db19c 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java @@ -2,12 +2,9 @@ package com.gitee.starblues.bootstrap; import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; -import com.gitee.starblues.integration.operator.EmptyPluginOperator; -import com.gitee.starblues.integration.user.DefaultPluginUser; -import com.gitee.starblues.spring.extract.DefaultOpExtractFactory; +import com.gitee.starblues.bootstrap.processor.oneself.ConfigureMainPluginEnvironment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.GenericApplicationContext; @@ -49,6 +46,7 @@ public class PluginOneselfSpringApplication extends SpringApplication { protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) { super.configureEnvironment(environment, args); configurePluginEnvironment.configureEnvironment(environment, args); + new ConfigureMainPluginEnvironment(processorContext).configureEnvironment(environment, args); } @Override @@ -62,7 +60,6 @@ public class PluginOneselfSpringApplication extends SpringApplication { processorContext.setApplicationContext(this.applicationContext); PluginContextHolder.initialize(processorContext); pluginProcessor.initialize(processorContext); - registerMainBean(); return super.run(args); } catch (Exception e) { pluginProcessor.failure(processorContext); @@ -80,11 +77,4 @@ public class PluginOneselfSpringApplication extends SpringApplication { pluginProcessor.refreshAfter(processorContext); } - private void registerMainBean(){ - DefaultListableBeanFactory beanFactory = applicationContext.getDefaultListableBeanFactory(); - beanFactory.registerSingleton("extractFactory", new DefaultOpExtractFactory()); - beanFactory.registerSingleton("pluginUser", new DefaultPluginUser(applicationContext)); - beanFactory.registerSingleton("pluginOperator", new EmptyPluginOperator()); - } - } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java index f76074f..fcf5d72 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java @@ -23,7 +23,6 @@ import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; import com.gitee.starblues.loader.launcher.DevelopmentModeSetting; -import com.gitee.starblues.loader.launcher.simple.SimpleBaseLauncher; import java.util.List; @@ -48,8 +47,6 @@ public class DefaultBootstrapLauncherFactory implements BootstrapLauncherFactory bootstrapLauncher = new IsolationBootstrapLauncher(bootstrap, pluginProcessor, pluginInteractive); } else if(DevelopmentModeSetting.coexist()){ bootstrapLauncher = new CoexistBootstrapLauncher(bootstrap, pluginProcessor, pluginInteractive); - } else if(DevelopmentModeSetting.simple()){ - throw new RuntimeException("暂未实现[" + DevelopmentModeSetting.getDevelopmentMode().toString() + "]模式"); } else { bootstrapLauncher = new OneselfBootstrapLauncher(bootstrap, pluginProcessor, pluginInteractive); } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ComposeSpringPluginProcessor.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ComposeSpringPluginProcessor.java index 63e7911..c21dc11 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ComposeSpringPluginProcessor.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ComposeSpringPluginProcessor.java @@ -18,6 +18,7 @@ package com.gitee.starblues.bootstrap.processor; import com.gitee.starblues.bootstrap.SpringPluginBootstrap; import com.gitee.starblues.bootstrap.annotation.DisablePluginWeb; +import com.gitee.starblues.bootstrap.processor.oneself.OneselfProcessor; import com.gitee.starblues.bootstrap.processor.web.PluginControllerProcessor; import com.gitee.starblues.bootstrap.processor.web.PluginInterceptorsProcessor; import com.gitee.starblues.bootstrap.processor.web.PluginSpringDocControllerProcessor; @@ -72,6 +73,7 @@ public class ComposeSpringPluginProcessor implements SpringPluginProcessor { List processors = new ArrayList<>(); addDefaultProcessors(context, processors); addDefaultWebEnvProcessors(context, processors); + addOneselfProcessors(context, processors); processors.addAll(this.processors); this.processors = processors.stream() .filter(p->{ @@ -178,6 +180,15 @@ public class ComposeSpringPluginProcessor implements SpringPluginProcessor { ProcessorUtils.add(processors, PluginSpringDocControllerProcessor::new); } + /** + * 添加 Oneself 模式处理者 + * @param context ProcessorContext + * @param processors 处理者容器集合 + */ + private void addOneselfProcessors(ProcessorContext context, List processors) { + processors.add(new OneselfProcessor()); + } + private void processException(SpringPluginProcessor processor, String executeType, Throwable e, boolean isThrow) throws ProcessorException{ String error = "Processor[" + processor.getClass().getName() + "] execute[" + executeType + "] failure : " diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/SpringPluginProcessor.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/SpringPluginProcessor.java index 5b95946..ce59321 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/SpringPluginProcessor.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/SpringPluginProcessor.java @@ -26,6 +26,7 @@ import com.gitee.starblues.utils.OrderPriority; */ public interface SpringPluginProcessor extends Order { + /** * 初始化时 * @param context ProcessorContext diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigureMainPluginEnvironment.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/ConfigureMainPluginEnvironment.java similarity index 92% rename from spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigureMainPluginEnvironment.java rename to spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/ConfigureMainPluginEnvironment.java index 912f254..0471be3 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/ConfigureMainPluginEnvironment.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/ConfigureMainPluginEnvironment.java @@ -14,8 +14,9 @@ * limitations under the License. */ -package com.gitee.starblues.bootstrap; +package com.gitee.starblues.bootstrap.processor.oneself; +import com.gitee.starblues.bootstrap.SpringPluginBootstrap; import com.gitee.starblues.bootstrap.annotation.OneselfConfig; import com.gitee.starblues.bootstrap.processor.ProcessorContext; import com.gitee.starblues.bootstrap.utils.AnnotationUtils; @@ -41,12 +42,12 @@ import java.util.*; * @version 3.0.0 * @since 3.0.0 */ -class ConfigureMainPluginEnvironment { +public class ConfigureMainPluginEnvironment { private final ProcessorContext processorContext; private final List propertySourceLoaders; - ConfigureMainPluginEnvironment(ProcessorContext processorContext) { + public ConfigureMainPluginEnvironment(ProcessorContext processorContext) { this.processorContext = processorContext; this.propertySourceLoaders = new ArrayList<>(2); @@ -54,7 +55,7 @@ class ConfigureMainPluginEnvironment { this.propertySourceLoaders.add(new PropertiesPropertySourceLoader()); } - void configureEnvironment(ConfigurableEnvironment environment, String[] args) { + public void configureEnvironment(ConfigurableEnvironment environment, String[] args) { SpringPluginBootstrap springPluginBootstrap = processorContext.getSpringPluginBootstrap(); OneselfConfig oneselfConfig = AnnotationUtils.findAnnotation(springPluginBootstrap.getClass(), OneselfConfig.class); @@ -104,5 +105,4 @@ class ConfigureMainPluginEnvironment { throw new RuntimeException(e); } } - } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/OneselfProcessor.java similarity index 34% rename from spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java rename to spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/OneselfProcessor.java index fce2b97..8808ba2 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/simple/SimpleBaseLauncher.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/OneselfProcessor.java @@ -14,37 +14,41 @@ * limitations under the License. */ -package com.gitee.starblues.loader.launcher.simple; - -import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; -import com.gitee.starblues.loader.launcher.AbstractMainLauncher; -import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; -import com.gitee.starblues.loader.launcher.runner.MethodRunner; +package com.gitee.starblues.bootstrap.processor.oneself; +import com.gitee.starblues.bootstrap.processor.ProcessorContext; +import com.gitee.starblues.bootstrap.processor.ProcessorException; +import com.gitee.starblues.bootstrap.processor.SpringPluginProcessor; +import com.gitee.starblues.integration.operator.EmptyPluginOperator; +import com.gitee.starblues.integration.user.DefaultPluginUser; +import com.gitee.starblues.spring.extract.DefaultOpExtractFactory; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.support.GenericApplicationContext; /** - * simple 模式 launcher + * 子启动处理器 * * @author starBlues - * @since 3.0.4 * @version 3.0.4 + * @since 3.0.4 */ -public class SimpleBaseLauncher extends AbstractMainLauncher { - - private final MethodRunner methodRunner; +public class OneselfProcessor implements SpringPluginProcessor { - public SimpleBaseLauncher(MethodRunner methodRunner) { - this.methodRunner = methodRunner; + @Override + public void initialize(ProcessorContext context) throws ProcessorException { + registerMainBean(context.getApplicationContext()); } @Override - protected ClassLoader createClassLoader(String... args) throws Exception { - return new GeneralUrlClassLoader(IsolationBaseLauncher.MAIN_CLASS_LOADER_NAME, this.getClass().getClassLoader()); + public ProcessorContext.RunMode runMode() { + return ProcessorContext.RunMode.ONESELF; } - @Override - protected ClassLoader launch(ClassLoader classLoader, String... args) throws Exception { - methodRunner.run(classLoader); - return classLoader; + private void registerMainBean(GenericApplicationContext applicationContext){ + DefaultListableBeanFactory beanFactory = applicationContext.getDefaultListableBeanFactory(); + beanFactory.registerSingleton("extractFactory", new DefaultOpExtractFactory()); + beanFactory.registerSingleton("pluginUser", new DefaultPluginUser(applicationContext)); + beanFactory.registerSingleton("pluginOperator", new EmptyPluginOperator()); } + } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java index ac3e555..eddd279 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java @@ -35,9 +35,4 @@ public abstract class DevelopmentMode { */ public static final String COEXIST = "coexist"; - /** - * 简单模式 - */ - public static final String SIMPLE = "simple"; - } 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 index 06be41d..3860e83 100644 --- 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 @@ -81,8 +81,7 @@ public abstract class AbstractMainLauncher extends AbstractLauncher } } catch (ClassNotFoundException e) { if(!DevelopmentModeSetting.isolation()){ - String mode = DevelopmentMode.COEXIST + "/" + DevelopmentMode.SIMPLE; - throw new RuntimeException("[" + mode + "]模式" + + throw new RuntimeException("[" + DevelopmentMode.COEXIST + "]模式" + "需要将[" + SPRING_PLUGIN_BOOTSTRAP_COORDINATE + "]依赖定义到主程序中!"); } } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java index ae80c9b..cc112ab 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java @@ -19,7 +19,6 @@ package com.gitee.starblues.loader.launcher; import com.gitee.starblues.loader.launcher.coexist.CoexistBaseLauncher; import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; import com.gitee.starblues.loader.launcher.runner.MethodRunner; -import com.gitee.starblues.loader.launcher.simple.SimpleBaseLauncher; import lombok.AllArgsConstructor; /** @@ -39,12 +38,12 @@ public class DevLauncher implements Launcher{ MethodRunner methodRunner = new MethodRunner(springBootstrap.getClass().getName(), SPRING_BOOTSTRAP_RUN_METHOD, args); AbstractMainLauncher launcher; - if(DevelopmentModeSetting.coexist()){ + if(DevelopmentModeSetting.isolation()){ + launcher = new IsolationBaseLauncher(methodRunner); + } else if(DevelopmentModeSetting.coexist()) { launcher = new CoexistBaseLauncher(methodRunner); - } else if(DevelopmentModeSetting.simple()) { - launcher = new SimpleBaseLauncher(methodRunner); } else { - launcher = new IsolationBaseLauncher(methodRunner); + throw DevelopmentModeSetting.getUnknownModeException(); } return launcher.run(args); } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java index 4ee30c5..11f08c6 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java @@ -27,7 +27,7 @@ import com.gitee.starblues.loader.DevelopmentMode; */ public class DevelopmentModeSetting { - private static String developmentMode = DevelopmentMode.ISOLATION; + private static String developmentMode; static void setDevelopmentMode(String developmentMode) { DevelopmentModeSetting.developmentMode = checkModeKey(developmentMode); @@ -41,26 +41,31 @@ public class DevelopmentModeSetting { return DevelopmentMode.COEXIST.equalsIgnoreCase(developmentMode); } - public static boolean simple(){ - return DevelopmentMode.SIMPLE.equalsIgnoreCase(developmentMode); - } - public static String getDevelopmentMode(){ return developmentMode; } + public static IllegalStateException getUnknownModeException(){ + return getUnknownModeException(null); + } + + public static IllegalStateException getUnknownModeException(String developmentMode){ + if(developmentMode == null || "".equals(developmentMode)){ + developmentMode = DevelopmentModeSetting.developmentMode; + } + return new IllegalStateException("不支持开发模式:" + developmentMode); + } + private static String checkModeKey(String developmentMode){ if(developmentMode == null || "".equals(developmentMode)){ - throw new RuntimeException("developmentMode设置不能为空"); + throw new RuntimeException("developmentMode 设置不能为空"); } if(DevelopmentMode.ISOLATION.equalsIgnoreCase(developmentMode)){ return developmentMode; } else if(DevelopmentMode.COEXIST.equalsIgnoreCase(developmentMode)){ return developmentMode; - } else if(DevelopmentMode.SIMPLE.equalsIgnoreCase(developmentMode)){ - return developmentMode; } else { - throw new RuntimeException("不支持开发模式: " + developmentMode); + throw getUnknownModeException(developmentMode); } } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java index eb2893b..34117cc 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java @@ -17,16 +17,12 @@ package com.gitee.starblues.loader.launcher; import com.gitee.starblues.loader.jar.JarFile; -import com.gitee.starblues.loader.launcher.coexist.CoexistBaseLauncher; import com.gitee.starblues.loader.launcher.coexist.CoexistFastJarLauncher; import com.gitee.starblues.loader.launcher.coexist.CoexistJarOuterLauncher; -import com.gitee.starblues.loader.launcher.isolation.IsolationBaseLauncher; import com.gitee.starblues.loader.launcher.isolation.IsolationFastJarLauncher; import com.gitee.starblues.loader.launcher.isolation.IsolationJarOuterLauncher; import com.gitee.starblues.loader.launcher.runner.MethodRunner; -import com.gitee.starblues.loader.launcher.simple.SimpleBaseLauncher; import com.gitee.starblues.loader.utils.ObjectUtils; -import lombok.AllArgsConstructor; import java.io.File; import java.net.URI; @@ -106,24 +102,24 @@ public class ProdLauncher implements Launcher{ private AbstractMainLauncher getFastJarLauncher(MethodRunner methodRunner, File rootJarFile){ AbstractMainLauncher launcher; - if(DevelopmentModeSetting.coexist()){ - launcher = new CoexistFastJarLauncher(methodRunner, rootJarFile); - } else if(DevelopmentModeSetting.simple()) { + if(DevelopmentModeSetting.isolation()){ + launcher = new IsolationFastJarLauncher(methodRunner, rootJarFile); + } else if(DevelopmentModeSetting.coexist()){ launcher = new CoexistFastJarLauncher(methodRunner, rootJarFile); } else { - launcher = new IsolationFastJarLauncher(methodRunner, rootJarFile); + throw DevelopmentModeSetting.getUnknownModeException(); } return launcher; } private AbstractMainLauncher getJarOuterLauncher(MethodRunner methodRunner, File rootJarFile){ AbstractMainLauncher launcher; - if(DevelopmentModeSetting.coexist()){ - launcher = new CoexistJarOuterLauncher(methodRunner, rootJarFile); - } else if(DevelopmentModeSetting.simple()) { + if(DevelopmentModeSetting.isolation()){ + launcher = new IsolationJarOuterLauncher(methodRunner, rootJarFile); + } else if(DevelopmentModeSetting.coexist()){ launcher = new CoexistJarOuterLauncher(methodRunner, rootJarFile); } else { - launcher = new IsolationJarOuterLauncher(methodRunner, rootJarFile); + throw DevelopmentModeSetting.getUnknownModeException(); } return launcher; } -- Gitee From b47a539996a3f90361422578cf9c90b6de494541 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Sat, 27 Aug 2022 17:40:36 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B03.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- spring-brick-bootstrap/pom.xml | 2 +- .../bootstrap/AutowiredTypeResolver.java | 2 +- .../DefaultAutowiredTypeResolver.java | 2 +- .../bootstrap/DefaultSpringPluginHook.java | 14 +++--- .../PluginDisableAutoConfiguration.java | 2 +- .../bootstrap/PluginOneselfInteractive.java | 20 +++++--- .../PluginOneselfSpringApplication.java | 2 +- .../bootstrap/PluginSpringApplication.java | 2 +- .../SpringPluginBootstrapBinder.java | 2 +- .../annotation/ResolveClassLoader.java | 2 +- .../CoexistAllowAutoConfiguration.java | 2 +- .../CoexistResolveClassLoaderAspect.java | 2 +- .../bootstrap/launcher/BootstrapLauncher.java | 2 +- .../launcher/BootstrapLauncherFactory.java | 2 +- .../launcher/CoexistBootstrapLauncher.java | 2 +- .../DefaultBootstrapLauncherFactory.java | 2 +- .../launcher/IsolationBootstrapLauncher.java | 2 +- .../launcher/OneselfBootstrapLauncher.java | 2 +- .../processor/DefaultProcessorContext.java | 6 +++ .../processor/FrameDefineBeanProcessor.java | 1 + .../bootstrap/processor/ProcessorContext.java | 7 +++ .../processor/oneself/OneselfProcessor.java | 2 +- .../realize/PluginCloseListener.java | 20 +++++++- spring-brick-common/pom.xml | 2 +- .../common/PluginDisableAutoConfig.java | 2 +- spring-brick-loader/pom.xml | 2 +- .../starblues/loader/DevelopmentMode.java | 2 +- .../loader/PluginResourceStorage.java | 6 +++ .../starblues/loader/archive/Archive.java | 14 ++++-- .../loader/archive/ExplodedArchive.java | 16 +++--- .../loader/archive/JarFileArchive.java | 15 +++--- .../classloader/ClassLoaderTranslator.java | 7 ++- .../classloader/GeneralUrlClassLoader.java | 7 ++- .../classloader/GenericClassLoader.java | 31 ++++++------ .../loader/DefaultResourceLoaderFactory.java | 28 +++++++---- .../resource/loader/ResourceLoader.java | 1 + .../loader/ResourceLoaderFactory.java | 7 +++ .../storage/CacheResourceStorage.java | 2 - .../storage/EmptyResourceStorage.java | 2 +- .../storage/ShareResourceStorage.java | 7 ++- .../starblues/loader/jar/AbstractJarFile.java | 13 ++--- .../starblues/loader/jar/AsciiBytes.java | 15 +++--- .../com/gitee/starblues/loader/jar/Bytes.java | 13 ++--- .../loader/jar/CentralDirectoryEndRecord.java | 16 +++--- .../jar/CentralDirectoryFileHeader.java | 16 +++--- .../loader/jar/CentralDirectoryParser.java | 15 +++--- .../loader/jar/CentralDirectoryVisitor.java | 13 ++--- .../starblues/loader/jar/FileHeader.java | 15 +++--- .../gitee/starblues/loader/jar/Handler.java | 16 +++--- .../gitee/starblues/loader/jar/JarEntry.java | 14 +++--- .../loader/jar/JarEntryCertification.java | 14 +++--- .../starblues/loader/jar/JarEntryFilter.java | 13 ++--- .../gitee/starblues/loader/jar/JarFile.java | 22 ++++++--- .../starblues/loader/jar/JarFileEntries.java | 22 ++++++--- .../starblues/loader/jar/JarFileWrapper.java | 18 ++++--- .../loader/jar/JarURLConnection.java | 15 +++--- .../loader/jar/RandomAccessData.java | 15 +++--- .../loader/jar/RandomAccessDataFile.java | 15 +++--- .../starblues/loader/jar/StringSequence.java | 15 +++--- .../loader/jar/ZipInflaterInputStream.java | 14 +++--- .../loader/launcher/DevLauncher.java | 2 +- .../launcher/DevelopmentModeSetting.java | 2 +- .../loader/launcher/ProdLauncher.java | 2 +- .../launcher/classpath/ClasspathResource.java | 2 +- .../classpath/FastJarClasspathResource.java | 2 +- .../classpath/JarOutClasspathResource.java | 2 +- .../launcher/coexist/CoexistBaseLauncher.java | 2 +- .../ResourceLoaderFactoryGetter.java | 10 +--- spring-brick-maven-packager/pom.xml | 2 +- .../plugin-help.xml | 2 +- .../main/resources/META-INF/maven/plugin.xml | 2 +- spring-brick/pom.xml | 2 +- .../core/DefaultPluginInsideInfo.java | 20 ++++++++ .../starblues/core/DefaultPluginManager.java | 9 ++-- .../starblues/core/PluginExtensionInfo.java | 21 ++++++++ .../com/gitee/starblues/core/PluginInfo.java | 8 ++- .../gitee/starblues/core/PluginInfoFace.java | 9 ++++ .../starblues/core/PluginInsideInfo.java | 19 ++++++- .../starblues/core/PluginLauncherManager.java | 15 +++--- .../ComposeMainResourceMatcher.java | 12 ++++- .../NestedPluginJarResourceLoader.java | 4 +- .../core/classloader/PluginClassLoader.java | 7 ++- .../PluginGeneralUrlClassLoader.java | 9 +++- .../PluginResourceLoaderFactory.java | 2 +- .../PluginResourceLoaderFactoryProxy.java | 7 ++- .../plugin/DefaultPluginInteractive.java | 15 ++++-- .../plugin/PluginCoexistLauncher.java | 12 +++-- .../launcher/plugin/PluginInteractive.java | 7 +++ .../plugin/PluginIsolationLauncher.java | 23 ++++----- .../plugin/SpringPluginHookWrapper.java | 24 ++++++--- .../involved/DefaultPluginLaunchInvolved.java | 49 +++++++++++++++++-- .../PluginApplicationContextGetter.java | 10 ++-- .../plugin/involved/PluginLaunchInvolved.java | 21 ++++---- .../involved/PluginLaunchInvolvedFactory.java | 20 ++++---- .../integration/ExtendPointConfiguration.java | 4 +- .../operator/EmptyPluginOperator.java | 2 +- .../starblues/spring/ApplicationContext.java | 2 +- .../starblues/spring/SpringPluginHook.java | 10 +++- .../extract/DefaultOpExtractFactory.java | 2 +- .../thymeleaf/PluginThymeleafInvolved.java | 11 +++-- update.md | 2 + 102 files changed, 640 insertions(+), 312 deletions(-) create mode 100644 spring-brick/src/main/java/com/gitee/starblues/core/PluginExtensionInfo.java diff --git a/pom.xml b/pom.xml index 3c86c78..c14f897 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ com.gitee.starblues spring-brick-parent pom - 3.0.4 + 3.1.0 spring-brick-common diff --git a/spring-brick-bootstrap/pom.xml b/spring-brick-bootstrap/pom.xml index 6c779cb..d6cf302 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.4 + 3.1.0 spring-brick-bootstrap diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/AutowiredTypeResolver.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/AutowiredTypeResolver.java index 3dd0603..3941f87 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/AutowiredTypeResolver.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/AutowiredTypeResolver.java @@ -31,7 +31,7 @@ import java.util.Set; /** * @author starBlues * @since 3.0.3 - * @version 3.0.4 + * @version 3.1.0 */ public interface AutowiredTypeResolver { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultAutowiredTypeResolver.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultAutowiredTypeResolver.java index 0c62ebf..ef58144 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultAutowiredTypeResolver.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultAutowiredTypeResolver.java @@ -32,7 +32,7 @@ import java.util.Set; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public class DefaultAutowiredTypeResolver implements AutowiredTypeResolver{ diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java index 5b24cff..f2680f8 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java @@ -39,7 +39,7 @@ import java.util.Map; * 默认的插件钩子器 * @author starBlues * @since 3.0.0 - * @version 3.0.4 + * @version 3.1.0 */ public class DefaultSpringPluginHook implements SpringPluginHook { @@ -77,14 +77,12 @@ public class DefaultSpringPluginHook implements SpringPluginHook { @Override - public void close() throws Exception{ + public void close(boolean isUninstall) throws Exception{ try { GenericApplicationContext applicationContext = processorContext.getApplicationContext(); - callPluginCloseListener(applicationContext); + callPluginCloseListener(applicationContext, isUninstall); pluginProcessor.close(processorContext); - if(applicationContext != null){ - applicationContext.close(); - } + applicationContext.close(); processorContext.clearRegistryInfo(); DestroyUtils.destroyAll(null, SpringFactoriesLoader.class, "cache", Map.class); } catch (Exception e){ @@ -109,7 +107,7 @@ public class DefaultSpringPluginHook implements SpringPluginHook { return processorContext.getRegistryInfo(PluginThymeleafProcessor.CONFIG_KEY); } - private void callPluginCloseListener(GenericApplicationContext applicationContext){ + private void callPluginCloseListener(GenericApplicationContext applicationContext, boolean isUninstall){ List pluginCloseListeners = SpringBeanUtils.getBeans( applicationContext, PluginCloseListener.class); if(pluginCloseListeners.isEmpty()){ @@ -117,7 +115,7 @@ public class DefaultSpringPluginHook implements SpringPluginHook { } for (PluginCloseListener pluginCloseListener : pluginCloseListeners) { try { - pluginCloseListener.close(processorContext.getPluginDescriptor()); + pluginCloseListener.close(applicationContext, processorContext.getPluginInfo(), isUninstall); } catch (Exception e){ e.printStackTrace(); } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java index 7586a5c..b126533 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginDisableAutoConfiguration.java @@ -29,7 +29,7 @@ import java.util.*; * 插件禁用的 AutoConfiguration * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.3 */ public class PluginDisableAutoConfiguration implements AutoConfigurationImportFilter { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfInteractive.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfInteractive.java index 2723562..6d13aca 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfInteractive.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfInteractive.java @@ -17,6 +17,8 @@ package com.gitee.starblues.bootstrap; import com.gitee.starblues.common.PackageStructure; +import com.gitee.starblues.core.DefaultPluginInsideInfo; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.descriptor.DevPluginDescriptorLoader; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.descriptor.PluginDescriptorLoader; @@ -37,27 +39,33 @@ import java.nio.file.Paths; /** * 插件自己的Interactive * @author starBlues - * @version 3.0.0 + * @version 3.1.0 */ public class PluginOneselfInteractive implements PluginInteractive { - private final InsidePluginDescriptor pluginDescriptor; + private final PluginInsideInfo pluginInsideInfo; private final MainApplicationContext mainApplicationContext; private final IntegrationConfiguration configuration; private final InvokeSupperCache invokeSupperCache; private final OpExtractFactory opExtractFactory; public PluginOneselfInteractive(){ - this.pluginDescriptor = createPluginDescriptor(); + this.pluginInsideInfo = createPluginInsideInfo(); this.mainApplicationContext = new EmptyMainApplicationContext(); this.configuration = new AutoIntegrationConfiguration(); this.invokeSupperCache = new DefaultInvokeSupperCache(); this.opExtractFactory = new DefaultOpExtractFactory(); } + @Override public InsidePluginDescriptor getPluginDescriptor() { - return pluginDescriptor; + return pluginInsideInfo.getPluginDescriptor(); + } + + @Override + public PluginInsideInfo getPluginInsideInfo() { + return pluginInsideInfo; } @Override @@ -80,7 +88,7 @@ public class PluginOneselfInteractive implements PluginInteractive { return opExtractFactory; } - private InsidePluginDescriptor createPluginDescriptor(){ + private PluginInsideInfo createPluginInsideInfo(){ EmptyPluginDescriptorDecrypt descriptorDecrypt = new EmptyPluginDescriptorDecrypt(); try (PluginDescriptorLoader pluginDescriptorLoader = new DevPluginDescriptorLoader(descriptorDecrypt)){ Path classesPath = Paths.get(this.getClass().getResource("/").toURI()).getParent(); @@ -89,7 +97,7 @@ public class PluginOneselfInteractive implements PluginInteractive { if(pluginDescriptor == null){ throw new RuntimeException("没有发现插件信息, 请使用框架提供的Maven插件器对插件进行编译!"); } - return pluginDescriptor; + return new DefaultPluginInsideInfo(pluginDescriptor); } catch (Exception e){ throw new RuntimeException(e); } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java index d7db19c..b49e658 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfSpringApplication.java @@ -14,7 +14,7 @@ import org.springframework.core.env.ConfigurableEnvironment; * 插件自主启动的 SpringApplication * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ public class PluginOneselfSpringApplication extends SpringApplication { 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 fdbff10..c21fc3f 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 @@ -34,7 +34,7 @@ import org.springframework.core.io.ResourceLoader; * 插件SpringApplication实现 * @author starBlues * @since 3.0.0 - * @version 3.0.4 + * @version 3.1.0 */ public class PluginSpringApplication extends SpringApplication { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrapBinder.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrapBinder.java index 19f4ad5..da40ce7 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrapBinder.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/SpringPluginBootstrapBinder.java @@ -5,7 +5,7 @@ package com.gitee.starblues.bootstrap; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public class SpringPluginBootstrapBinder { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java index b9f22e9..a93ffe0 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/annotation/ResolveClassLoader.java @@ -23,7 +23,7 @@ import java.lang.annotation.*; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java index fc646f0..73355f6 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistAllowAutoConfiguration.java @@ -26,7 +26,7 @@ import java.util.Set; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public class CoexistAllowAutoConfiguration { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java index 9b6c309..0d1b1df 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/coexist/CoexistResolveClassLoaderAspect.java @@ -27,7 +27,7 @@ import org.aspectj.lang.annotation.Pointcut; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ @Aspect public class CoexistResolveClassLoaderAspect { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncher.java index 6ed280a..994ecc0 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncher.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncher.java @@ -23,7 +23,7 @@ import com.gitee.starblues.spring.SpringPluginHook; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public interface BootstrapLauncher { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncherFactory.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncherFactory.java index 42292ad..bc14d22 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncherFactory.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/BootstrapLauncherFactory.java @@ -23,7 +23,7 @@ import com.gitee.starblues.bootstrap.SpringPluginBootstrap; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public interface BootstrapLauncherFactory { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java index be89db5..59868a6 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/CoexistBootstrapLauncher.java @@ -36,7 +36,7 @@ import org.springframework.core.env.ConfigurableEnvironment; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 * @see com.gitee.starblues.loader.DevelopmentMode#COEXIST */ @AllArgsConstructor diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java index fcf5d72..5fb5cef 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/DefaultBootstrapLauncherFactory.java @@ -31,7 +31,7 @@ import java.util.List; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public class DefaultBootstrapLauncherFactory implements BootstrapLauncherFactory{ @Override diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java index f4b2f97..6618443 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/IsolationBootstrapLauncher.java @@ -32,7 +32,7 @@ import org.springframework.boot.SpringApplication; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 * @see com.gitee.starblues.loader.DevelopmentMode#ISOLATION */ @AllArgsConstructor diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/OneselfBootstrapLauncher.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/OneselfBootstrapLauncher.java index 4949b30..d0f4dcd 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/OneselfBootstrapLauncher.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/launcher/OneselfBootstrapLauncher.java @@ -16,7 +16,7 @@ import org.springframework.boot.SpringApplication; * 插件自主启动配置 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ @AllArgsConstructor diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/DefaultProcessorContext.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/DefaultProcessorContext.java index d0801cc..8867f90 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/DefaultProcessorContext.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/DefaultProcessorContext.java @@ -17,6 +17,7 @@ package com.gitee.starblues.bootstrap.processor; import com.gitee.starblues.bootstrap.SpringPluginBootstrap; +import com.gitee.starblues.core.PluginInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.launcher.plugin.CacheRegistryInfo; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; @@ -82,6 +83,11 @@ public class DefaultProcessorContext extends CacheRegistryInfo implements Proces return pluginInteractive.getPluginDescriptor(); } + @Override + public PluginInfo getPluginInfo() { + return pluginInteractive.getPluginInsideInfo(); + } + @Override public Class getRunnerClass() { return runnerClass; diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/FrameDefineBeanProcessor.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/FrameDefineBeanProcessor.java index ddbb7b8..5098ffc 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/FrameDefineBeanProcessor.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/FrameDefineBeanProcessor.java @@ -36,6 +36,7 @@ public class FrameDefineBeanProcessor implements SpringPluginProcessor { InsidePluginDescriptor pluginDescriptor = context.getPluginDescriptor(); ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); beanFactory.registerSingleton("pluginDescriptor", pluginDescriptor.toPluginDescriptor()); + beanFactory.registerSingleton("pluginInfo", context.getPluginInfo()); beanFactory.registerSingleton("mainApplicationContext", context.getMainApplicationContext()); } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java index 42cec5b..d7aba02 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/ProcessorContext.java @@ -18,6 +18,7 @@ package com.gitee.starblues.bootstrap.processor; import com.gitee.starblues.bootstrap.SpringPluginBootstrap; +import com.gitee.starblues.core.PluginInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; import com.gitee.starblues.core.launcher.plugin.RegistryInfo; @@ -54,6 +55,12 @@ public interface ProcessorContext extends RegistryInfo { */ InsidePluginDescriptor getPluginDescriptor(); + /** + * 得到插件信息 PluginInfo + * @return PluginInfo + */ + PluginInfo getPluginInfo(); + /** * 得到启动的class类 * @return Class diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/OneselfProcessor.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/OneselfProcessor.java index 8808ba2..f320f7c 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/OneselfProcessor.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/processor/oneself/OneselfProcessor.java @@ -29,7 +29,7 @@ import org.springframework.context.support.GenericApplicationContext; * 子启动处理器 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ public class OneselfProcessor implements SpringPluginProcessor { diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/PluginCloseListener.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/PluginCloseListener.java index 7942eae..5dd8ebf 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/PluginCloseListener.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/PluginCloseListener.java @@ -16,20 +16,36 @@ package com.gitee.starblues.bootstrap.realize; +import com.gitee.starblues.core.PluginInfo; import com.gitee.starblues.core.descriptor.PluginDescriptor; +import org.springframework.context.support.GenericApplicationContext; /** * 插件被停止监听者。用于自定义关闭资源 * @author starBlues * @since 3.0.0 - * @version 3.0.0 + * @version 3.1.0 */ public interface PluginCloseListener { /** * 关闭时调用 * @param descriptor 当前插件描述者 + * @deprecated 在 3.1.1 版本会被删除 + * @since 3.0.0 */ - void close(PluginDescriptor descriptor); + default void close(PluginDescriptor descriptor){} + + /** + * 关闭时调用 + * @param applicationContext 当前插件的ApplicationContext + * @param pluginInfo 当前插件信息 + * @param isUninstall 是否为卸载关闭。true: 为卸载, false 不为卸载 + * @since 3.1.0 + */ + default void close(GenericApplicationContext applicationContext, + PluginInfo pluginInfo, boolean isUninstall){ + close(pluginInfo != null ? pluginInfo.getPluginDescriptor() : null); + } } diff --git a/spring-brick-common/pom.xml b/spring-brick-common/pom.xml index c284b46..3a57ab0 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.4 + 3.1.0 spring-brick-common diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDisableAutoConfig.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDisableAutoConfig.java index bf7ecfd..24bdefe 100644 --- a/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDisableAutoConfig.java +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/PluginDisableAutoConfig.java @@ -7,7 +7,7 @@ import java.util.Set; * 插件禁用的 AutoConfiguration 配置 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ public class PluginDisableAutoConfig { diff --git a/spring-brick-loader/pom.xml b/spring-brick-loader/pom.xml index 90eaa40..d453dab 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.4 + 3.1.0 4.0.0 diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java index eddd279..82d0029 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/DevelopmentMode.java @@ -21,7 +21,7 @@ package com.gitee.starblues.loader; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public abstract class DevelopmentMode { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/PluginResourceStorage.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/PluginResourceStorage.java index 056596a..c32edec 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/PluginResourceStorage.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/PluginResourceStorage.java @@ -143,6 +143,12 @@ public class PluginResourceStorage { IOUtils.closeQuietly(jarFile); } } + for (JarFile jarFile : rootJarFileMap.values()) { + if(jarFile == null){ + continue; + } + IOUtils.closeQuietly(jarFile); + } jarFileMap.clear(); rootJarFileMap.clear(); } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/Archive.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/Archive.java index 50bdc2c..8c351af 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/Archive.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/Archive.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -25,8 +26,11 @@ import java.util.jar.Manifest; /** * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * An archive that can be launched by the {@link Launcher}. + * + * @author Phillip Webb + * @since 1.0.0 + * @see JarFileArchive */ public interface Archive extends Iterable, AutoCloseable { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/ExplodedArchive.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/ExplodedArchive.java index 284a270..7ec7f03 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/ExplodedArchive.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/ExplodedArchive.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -26,9 +27,12 @@ import java.util.*; import java.util.jar.Manifest; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * {@link Archive} implementation backed by an exploded archive directory. + * + * @author Phillip Webb + * @author Andy Wilkinson + * @author Madhura Bhave + * @since 1.0.0 */ public class ExplodedArchive implements Archive { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/JarFileArchive.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/JarFileArchive.java index 8698a2e..1183476 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/JarFileArchive.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/archive/JarFileArchive.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -35,9 +36,11 @@ import java.util.jar.JarEntry; import java.util.jar.Manifest; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * {@link Archive} implementation backed by a {@link JarFile}. + * + * @author Phillip Webb + * @author Andy Wilkinson + * @since 1.0.0 */ public class JarFileArchive implements Archive { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java index 1465f26..3030b17 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/ClassLoaderTranslator.java @@ -36,7 +36,7 @@ import java.util.List; * classloader 转换器 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ public class ClassLoaderTranslator implements ResourceLoaderFactory { @@ -67,6 +67,11 @@ public class ClassLoaderTranslator implements ResourceLoaderFactory { throw new RuntimeException("Does not support!"); } + @Override + public void addResource(Resource resource) throws Exception { + throw new RuntimeException("Does not support!"); + } + @Override public void addResource(ResourceLoader resourceLoader) throws Exception { throw new RuntimeException("Does not support!"); diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java index eb93c52..c7a07e6 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GeneralUrlClassLoader.java @@ -37,7 +37,7 @@ import java.util.List; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public class GeneralUrlClassLoader extends URLClassLoader implements ResourceLoaderFactory { @@ -79,6 +79,11 @@ public class GeneralUrlClassLoader extends URLClassLoader implements ResourceLoa super.addURL(url); } + @Override + public void addResource(Resource resource) throws Exception { + addResource(resource.getUrl()); + } + @Override public void addResource(ResourceLoader resourceLoader) throws Exception { resourceLoader.load(resourceStorage); diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GenericClassLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GenericClassLoader.java index 1542645..a186c84 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GenericClassLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/GenericClassLoader.java @@ -28,7 +28,6 @@ import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; /** * 基本的 ClassLoader @@ -43,7 +42,6 @@ public class GenericClassLoader extends URLClassLoader implements ResourceLoader protected final ResourceLoaderFactory resourceLoaderFactory; private final ResourceLoaderFactory classLoaderTranslator; - private final Map> pluginClassCache = new ConcurrentHashMap<>(); public GenericClassLoader(String name, ResourceLoaderFactory resourceLoaderFactory) { this(name, null, resourceLoaderFactory); @@ -81,6 +79,11 @@ public class GenericClassLoader extends URLClassLoader implements ResourceLoader resourceLoaderFactory.addResource(url); } + @Override + public void addResource(Resource resource) throws Exception { + resourceLoaderFactory.addResource(resource); + } + @Override public void addResource(ResourceLoader resourceLoader) throws Exception{ resourceLoaderFactory.addResource(resourceLoader); @@ -144,10 +147,6 @@ public class GenericClassLoader extends URLClassLoader implements ResourceLoader protected Class findClassFromLocal(String name) { Class aClass; String formatClassName = formatClassName(name); - aClass = pluginClassCache.get(formatClassName); - if (aClass != null) { - return aClass; - } Resource resource = resourceLoaderFactory.findFirstResource(formatClassName); byte[] bytes = null; if(resource != null){ @@ -159,17 +158,16 @@ public class GenericClassLoader extends URLClassLoader implements ResourceLoader if(bytes == null || bytes.length == 0){ return null; } - aClass = defineClass(name, bytes, 0, bytes.length ); + aClass = super.defineClass(name, bytes, 0, bytes.length ); if(aClass == null) { return null; } if (aClass.getPackage() == null) { int lastDotIndex = name.lastIndexOf( '.' ); String packageName = (lastDotIndex >= 0) ? name.substring( 0, lastDotIndex) : ""; - definePackage(packageName, null, null, null, + super.definePackage(packageName, null, null, null, null, null, null, null ); } - pluginClassCache.put(name, aClass); return aClass; } @@ -178,13 +176,20 @@ public class GenericClassLoader extends URLClassLoader implements ResourceLoader if(inputStream == null){ return null; } + ByteArrayOutputStream output = new ByteArrayOutputStream(); try { - return IOUtils.read(inputStream); + byte[] buffer = new byte[4096]; + int n = 0; + while (-1 != (n = inputStream.read(buffer))) { + output.write(buffer, 0, n); + } + return output.toByteArray(); } catch (Exception e){ e.printStackTrace(); return null; } finally { IOUtils.closeQuietly(inputStream); + IOUtils.closeQuietly(output); } } @@ -297,10 +302,8 @@ public class GenericClassLoader extends URLClassLoader implements ResourceLoader @Override public void close() throws IOException { - synchronized (pluginClassCache){ - pluginClassCache.clear(); - IOUtils.closeQuietly(resourceLoaderFactory); - } + super.close(); + IOUtils.closeQuietly(resourceLoaderFactory); } private String formatResourceName(String name) { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/DefaultResourceLoaderFactory.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/DefaultResourceLoaderFactory.java index 10790fa..5666521 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/DefaultResourceLoaderFactory.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/DefaultResourceLoaderFactory.java @@ -96,20 +96,30 @@ public class DefaultResourceLoaderFactory implements ResourceLoaderFactory{ } @Override - public void addResource(ResourceLoader resourceLoader) throws Exception { - if(resourceLoader == null){ - return; + public void addResource(Resource resource) throws Exception { + SameRootResourceStorage resourceStorage = resourceLoaderMap.get(resource.getBaseUrl()); + if (resourceStorage == null) { + resourceStorage = ResourceLoaderFactoryGetter.getResourceStorage( + classLoaderName, + resource.getBaseUrl()); + resourceLoaderMap.put(resource.getBaseUrl(), resourceStorage); } - if (resourceLoaderMap.containsKey(resourceLoader.getBaseUrl())) { + resourceStorage.add(resource.getName(), resource.getUrl(), resource::getBytes); + } + + @Override + public synchronized void addResource(ResourceLoader resourceLoader) throws Exception { + if(resourceLoader == null){ return; } - SameRootResourceStorage resourceStorage = ResourceLoaderFactoryGetter.getResourceStorage( - classLoaderName, - resourceLoader.getBaseUrl()); - resourceLoader.load(resourceStorage); - if(!resourceStorage.isEmpty()){ + SameRootResourceStorage resourceStorage = resourceLoaderMap.get(resourceLoader.getBaseUrl()); + if (resourceStorage == null) { + resourceStorage = ResourceLoaderFactoryGetter.getResourceStorage( + classLoaderName, + resourceLoader.getBaseUrl()); resourceLoaderMap.put(resourceLoader.getBaseUrl(), resourceStorage); } + resourceLoader.load(resourceStorage); } @Override diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoader.java index 17fb706..7b722d7 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoader.java @@ -34,6 +34,7 @@ public interface ResourceLoader extends AutoCloseable{ */ URL getBaseUrl(); + /** * 装载资源到ResourceStorage * @param resourceStorage 资源存储 diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoaderFactory.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoaderFactory.java index 195d9f9..60cedc8 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoaderFactory.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/loader/ResourceLoaderFactory.java @@ -62,6 +62,13 @@ public interface ResourceLoaderFactory extends AutoCloseable { */ void addResource(URL url) throws Exception; + /** + * 根据 Resource 添加 + * @param resource 资源 + * @throws Exception 添加资源异常 + */ + void addResource(Resource resource) throws Exception; + /** * 根据资源加载器添加资源 * @param resourceLoader 资源加载者 diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/CacheResourceStorage.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/CacheResourceStorage.java index 9f3872f..a24a7c9 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/CacheResourceStorage.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/CacheResourceStorage.java @@ -32,8 +32,6 @@ import java.util.concurrent.ConcurrentHashMap; */ public class CacheResourceStorage extends DefaultResourceStorage{ - protected final Map resourceStorage = new ConcurrentHashMap<>(); - public CacheResourceStorage(URL baseUrl) { super(baseUrl); } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java index 5d248db..72b1984 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/EmptyResourceStorage.java @@ -27,7 +27,7 @@ import java.util.List; * 空的资源存储 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ public class EmptyResourceStorage implements ResourceStorage{ diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/ShareResourceStorage.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/ShareResourceStorage.java index 6cd1cc9..ec61cf9 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/ShareResourceStorage.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/classloader/resource/storage/ShareResourceStorage.java @@ -52,6 +52,11 @@ public class ShareResourceStorage extends DefaultResourceStorage{ super.addResource(name, shareResource); } + @Override + public void close() throws Exception { + super.close(); + } + private static class ShareResource extends DefaultResource { private final static Map BYTE_STORE_MAP = new ConcurrentHashMap<>(); @@ -73,7 +78,7 @@ public class ShareResourceStorage extends DefaultResourceStorage{ if(byteStore == null){ byteStore = new ByteStore(name); byteStore.addByte(key, bytes); - BYTE_STORE_MAP.put(getName(), byteStore); + BYTE_STORE_MAP.put(name, byteStore); } else { byteStore.addByte(key, bytes); } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/AbstractJarFile.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/AbstractJarFile.java index ae88cdd..1e6f6f5 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/AbstractJarFile.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/AbstractJarFile.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -24,9 +25,9 @@ import java.net.URL; import java.security.Permission; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Base class for extended variants of {@link java.util.jar.JarFile}. + * + * @author Phillip Webb */ public abstract class AbstractJarFile extends java.util.jar.JarFile{ diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/AsciiBytes.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/AsciiBytes.java index be75721..801719b 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/AsciiBytes.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/AsciiBytes.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -19,9 +20,11 @@ package com.gitee.starblues.loader.jar; import java.nio.charset.StandardCharsets; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Simple wrapper around a byte array that represents an ASCII. Used for performance + * reasons to save constructing Strings for ZIP data. + * + * @author Phillip Webb + * @author Andy Wilkinson */ public class AsciiBytes { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/Bytes.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/Bytes.java index 6b19352..5f3dc3c 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/Bytes.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/Bytes.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -17,9 +18,9 @@ package com.gitee.starblues.loader.jar; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Utilities for dealing with bytes from ZIP files. + * + * @author Phillip Webb */ public class Bytes { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryEndRecord.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryEndRecord.java index 3e941a3..49d86d3 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryEndRecord.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryEndRecord.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -19,9 +20,12 @@ package com.gitee.starblues.loader.jar; import java.io.IOException; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * A ZIP File "End of central directory record" (EOCD). + * + * @author Phillip Webb + * @author Andy Wilkinson + * @author Camille Vienot + * @see Zip File Format */ public class CentralDirectoryEndRecord { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryFileHeader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryFileHeader.java index be3a473..a2a4d12 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryFileHeader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryFileHeader.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -24,9 +25,12 @@ import java.time.temporal.ChronoUnit; import java.time.temporal.ValueRange; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * A ZIP File "Central directory file header record" (CDFH). + * + * @author Phillip Webb + * @author Andy Wilkinson + * @author Dmytro Nosan + * @see Zip File Format */ public class CentralDirectoryFileHeader implements FileHeader { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryParser.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryParser.java index e589c37..8548c4b 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryParser.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryParser.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -21,9 +22,11 @@ import java.util.ArrayList; import java.util.List; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Parses the central directory from a JAR file. + * + * @author Phillip Webb + * @author Andy Wilkinson + * @see CentralDirectoryVisitor */ public class CentralDirectoryParser { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryVisitor.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryVisitor.java index 046120c..2f45f46 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryVisitor.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/CentralDirectoryVisitor.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -17,9 +18,9 @@ package com.gitee.starblues.loader.jar; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Callback visitor triggered by {@link CentralDirectoryParser}. + * + * @author Phillip Webb */ public interface CentralDirectoryVisitor { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/FileHeader.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/FileHeader.java index 7c500e4..a360f64 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/FileHeader.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/FileHeader.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -19,9 +20,11 @@ package com.gitee.starblues.loader.jar; import java.util.zip.ZipEntry; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * A file header record that has been loaded from a Jar file. + * + * @author Phillip Webb + * @see JarEntry + * @see CentralDirectoryFileHeader */ public interface FileHeader { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/Handler.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/Handler.java index 558bb1b..e8f61ed 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/Handler.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/Handler.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -29,9 +30,12 @@ import java.util.logging.Logger; import java.util.regex.Pattern; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * {@link URLStreamHandler} for Spring Boot loader {@link JarFile}s. + * + * @author Phillip Webb + * @author Andy Wilkinson + * @since 1.0.0 + * @see JarFile#registerUrlProtocolHandler() */ public class Handler extends URLStreamHandler { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntry.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntry.java index b5c0a7c..165e79d 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntry.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntry.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -25,9 +26,10 @@ import java.util.jar.Attributes; import java.util.jar.Manifest; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Extended variant of {@link java.util.jar.JarEntry} returned by {@link JarFile}s. + * + * @author Phillip Webb + * @author Andy Wilkinson */ public class JarEntry extends java.util.jar.JarEntry implements FileHeader { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntryCertification.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntryCertification.java index 2313ef8..2ba0a16 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntryCertification.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntryCertification.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -20,9 +21,10 @@ import java.security.CodeSigner; import java.security.cert.Certificate; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * {@link Certificate} and {@link CodeSigner} details for a {@link JarEntry} from a signed + * {@link JarFile}. + * + * @author Phillip Webb */ public class JarEntryCertification { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntryFilter.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntryFilter.java index defa225..e6990e7 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntryFilter.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarEntryFilter.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -17,9 +18,9 @@ package com.gitee.starblues.loader.jar; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Interface that can be used to filter and optionally rename jar entries. + * + * @author Phillip Webb */ public interface JarEntryFilter { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFile.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFile.java index e22c64d..a02274b 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFile.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFile.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -37,9 +38,18 @@ import java.util.stream.StreamSupport; import java.util.zip.ZipEntry; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Extended variant of {@link java.util.jar.JarFile} that behaves in the same way but + * offers the following additional functionality. + *
    + *
  • A nested {@link JarFile} can be {@link #getNestedJarFile(ZipEntry) obtained} based + * on any directory entry.
  • + *
  • A nested {@link JarFile} can be {@link #getNestedJarFile(ZipEntry) obtained} for + * embedded JAR files (as long as their entry is not compressed).
  • + *
+ * + * @author Phillip Webb + * @author Andy Wilkinson + * @since 1.0.0 */ public class JarFile extends AbstractJarFile implements Iterable { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFileEntries.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFileEntries.java index 9a45c20..bb43d83 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFileEntries.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFileEntries.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -25,9 +26,18 @@ import java.util.jar.Manifest; import java.util.zip.ZipEntry; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Provides access to entries from a {@link JarFile}. In order to reduce memory + * consumption entry details are stored using arrays. The {@code hashCodes} array stores + * the hash code of the entry name, the {@code centralDirectoryOffsets} provides the + * offset to the central directory record and {@code positions} provides the original + * order position of the entry. The arrays are stored in hashCode order so that a binary + * search can be used to find a name. + *

+ * A typical Spring Boot application will have somewhere in the region of 10,500 entries + * which should consume about 122K. + * + * @author Phillip Webb + * @author Andy Wilkinson */ public class JarFileEntries implements CentralDirectoryVisitor, Iterable { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFileWrapper.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFileWrapper.java index 212f4c4..46d6d21 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFileWrapper.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarFileWrapper.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -34,9 +35,10 @@ import java.util.zip.ZipEntry; import com.gitee.starblues.loader.utils.ObjectUtils; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * A wrapper used to create a copy of a {@link JarFile} so that it can be safely closed + * without closing the original. + * + * @author Phillip Webb */ public class JarFileWrapper extends AbstractJarFile { @@ -132,6 +134,7 @@ public class JarFileWrapper extends AbstractJarFile { @Override public void close() throws IOException { super.close(); + // Modified Added close logic if(canClosed.get()){ for (List inputStreams : inputStreamCache.values()) { if(ObjectUtils.isEmpty(inputStreams)){ @@ -144,7 +147,8 @@ public class JarFileWrapper extends AbstractJarFile { IOUtils.closeQuietly(inputStream); } } - parent.close(); + IOUtils.closeQuietly(parent); + inputStreamCache.clear(); } } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarURLConnection.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarURLConnection.java index b2aec95..5dce1fd 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarURLConnection.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/JarURLConnection.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -24,9 +25,11 @@ import java.net.*; import java.security.Permission; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * {@link java.net.JarURLConnection} used to support {@link JarFile#getUrl()}. + * + * @author Phillip Webb + * @author Andy Wilkinson + * @author Rostyslav Dudka */ public class JarURLConnection extends java.net.JarURLConnection { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/RandomAccessData.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/RandomAccessData.java index 52bc260..1c25ec0 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/RandomAccessData.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/RandomAccessData.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -21,9 +22,11 @@ import java.io.IOException; import java.io.InputStream; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * Interface that provides read-only random access to some underlying data. + * Implementations must allow concurrent reads in a thread-safe manner. + * + * @author Phillip Webb + * @since 1.0.0 */ public interface RandomAccessData { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/RandomAccessDataFile.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/RandomAccessDataFile.java index 8c97592..7f671af 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/RandomAccessDataFile.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/RandomAccessDataFile.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -19,9 +20,11 @@ package com.gitee.starblues.loader.jar; import java.io.*; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * {@link RandomAccessData} implementation backed by a {@link RandomAccessFile}. + * + * @author Phillip Webb + * @author Andy Wilkinson + * @since 1.0.0 */ public class RandomAccessDataFile implements RandomAccessData{ diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/StringSequence.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/StringSequence.java index 0cc1e81..7b2c1f7 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/StringSequence.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/StringSequence.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -19,9 +20,11 @@ package com.gitee.starblues.loader.jar; import java.util.Objects; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * A {@link CharSequence} backed by a single shared {@link String}. Unlike a regular + * {@link String}, {@link #subSequence(int, int)} operations will not copy the underlying + * character array. + * + * @author Phillip Webb */ public class StringSequence implements CharSequence { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/ZipInflaterInputStream.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/ZipInflaterInputStream.java index 05c8bf4..e4192f1 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/ZipInflaterInputStream.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/jar/ZipInflaterInputStream.java @@ -1,11 +1,12 @@ -/** - * Copyright [2019-2022] [starBlues] +/* + * Copyright 2012-2021 the original author or authors. + * Copy from spring-boot-loader * * 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 + * https://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, @@ -23,9 +24,10 @@ import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; /** - * copy from spring-boot-loader - * @author starBlues - * @version 3.0.0 + * {@link InflaterInputStream} that supports the writing of an extra "dummy" byte (which + * is required with JDK 6) and returns accurate available() results. + * + * @author Phillip Webb */ public class ZipInflaterInputStream extends InflaterInputStream { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java index cc112ab..2cac0c7 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevLauncher.java @@ -26,7 +26,7 @@ import lombok.AllArgsConstructor; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ @AllArgsConstructor public class DevLauncher implements Launcher{ diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java index 11f08c6..2d2ee5d 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/DevelopmentModeSetting.java @@ -23,7 +23,7 @@ import com.gitee.starblues.loader.DevelopmentMode; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public class DevelopmentModeSetting { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java index 34117cc..33d34ea 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/ProdLauncher.java @@ -40,7 +40,7 @@ import static com.gitee.starblues.loader.LoaderConstant.*; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public class ProdLauncher implements Launcher{ diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java index e390b5d..cd296e5 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/ClasspathResource.java @@ -24,7 +24,7 @@ import java.util.List; * 获取classpath资源路径 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ public interface ClasspathResource { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java index ab18378..a15ad9d 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/FastJarClasspathResource.java @@ -35,7 +35,7 @@ import static com.gitee.starblues.loader.LoaderConstant.PROD_LIB_PATH; * fast jar 类型的 classpath 获取者 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ @AllArgsConstructor diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java index 0b94363..69dab01 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/classpath/JarOutClasspathResource.java @@ -36,7 +36,7 @@ import static com.gitee.starblues.loader.LoaderConstant.*; * jar out 类型的 classpath 获取者 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ @AllArgsConstructor diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java index 2ce521e..c7fffbf 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/coexist/CoexistBaseLauncher.java @@ -26,7 +26,7 @@ import com.gitee.starblues.loader.launcher.runner.MethodRunner; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ public class CoexistBaseLauncher extends AbstractMainLauncher { diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java index 6cbd1f1..719f2a5 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/isolation/ResourceLoaderFactoryGetter.java @@ -46,11 +46,6 @@ public class ResourceLoaderFactoryGetter { private static final String RESOURCE_MODE_CACHE_SHARE = "cache-share"; - /** - * 资源模式--不缓存模式 - */ - private static final String RESOURCE_MODE_NO_CACHE = "no-cache"; - private static volatile String resourceMode; @@ -83,12 +78,9 @@ public class ResourceLoaderFactoryGetter { if(Objects.equals(resourceMode, RESOURCE_MODE_CACHE_ISOLATION)){ // 资源可缓存, 且隔离 resourceStorage = new CacheResourceStorage(baseUrl); - } else if(Objects.equals(resourceMode, RESOURCE_MODE_CACHE_SHARE)){ + } else { // 资源可缓存, 共享式 resourceStorage = new ShareResourceStorage(key, baseUrl); - } else { - // 资源不缓存 - resourceStorage = new DefaultResourceStorage(baseUrl); } return resourceStorage; } diff --git a/spring-brick-maven-packager/pom.xml b/spring-brick-maven-packager/pom.xml index dd87002..cacdf5d 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.4 + 3.1.0 spring-brick-maven-packager 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 c2b84e8..0e203ae 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 @@ -4,7 +4,7 @@ Spring Boot Plugin Maven Packager com.gitee.starblues spring-brick-maven-packager - 3.0.4 + 3.1.0 spring-brick-packager false true 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 c2b84e8..0e203ae 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 @@ -4,7 +4,7 @@ Spring Boot Plugin Maven Packager com.gitee.starblues spring-brick-maven-packager - 3.0.4 + 3.1.0 spring-brick-packager false true diff --git a/spring-brick/pom.xml b/spring-brick/pom.xml index 0f86efc..9118a92 100644 --- a/spring-brick/pom.xml +++ b/spring-brick/pom.xml @@ -7,7 +7,7 @@ spring-brick-parent com.gitee.starblues - 3.0.4 + 3.1.0 spring-brick diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginInsideInfo.java b/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginInsideInfo.java index 1f56225..c1374eb 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginInsideInfo.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginInsideInfo.java @@ -18,8 +18,11 @@ package com.gitee.starblues.core; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.utils.Assert; +import lombok.Setter; import java.util.Date; +import java.util.Map; +import java.util.function.Supplier; /** * 默认的内部PluginWrapperInside实现 @@ -36,6 +39,8 @@ public class DefaultPluginInsideInfo implements PluginInsideInfo { private Date startTime; private Date stopTime; + private Supplier> extensionInfoSupplier; + public DefaultPluginInsideInfo(InsidePluginDescriptor pluginDescriptor) { this.pluginId = pluginDescriptor.getPluginId(); this.pluginDescriptor = pluginDescriptor; @@ -52,6 +57,16 @@ public class DefaultPluginInsideInfo implements PluginInsideInfo { isFollowInitial = true; } + @Override + public void setExtensionInfoSupplier(Supplier> supplier) { + this.extensionInfoSupplier = supplier; + } + + @Override + public Supplier> getExtensionInfoSupplier() { + return extensionInfoSupplier; + } + @Override public String getPluginId() { return pluginId; @@ -92,6 +107,11 @@ public class DefaultPluginInsideInfo implements PluginInsideInfo { return isFollowInitial; } + @Override + public Map getExtensionInfo() { + return extensionInfoSupplier.get(); + } + private void resolveTime(PluginState pluginState){ if(pluginState == PluginState.STARTED || pluginState == PluginState.STARTED_FAILURE){ startTime = new Date(); 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 27237f4..047dc45 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.4 + * @version 3.1.0 * @since 3.0.0 */ public class DefaultPluginManager implements PluginManager{ @@ -270,7 +270,7 @@ public class DefaultPluginManager implements PluginManager{ PluginInfo pluginInfo = wrapperInside.toPluginInfo(); if(wrapperInside.getPluginState() == PluginState.STARTED){ try { - stop(wrapperInside); + stop(wrapperInside, true); pluginListenerFactory.stopSuccess(pluginInfo); } catch (Throwable e) { PluginException pluginException = PluginException.getPluginException(e, @@ -356,7 +356,7 @@ public class DefaultPluginManager implements PluginManager{ } PluginInfo pluginInfo = pluginInsideInfo.toPluginInfo(); try { - stop(pluginInsideInfo); + stop(pluginInsideInfo, false); log.info("停止插件[{}]成功", MsgUtils.getPluginUnique(pluginInsideInfo.getPluginDescriptor())); pluginListenerFactory.stopSuccess(pluginInfo); return pluginInfo; @@ -538,9 +538,10 @@ public class DefaultPluginManager implements PluginManager{ /** * 统一停止插件操作 * @param pluginInsideInfo PluginInsideInfo + * @param isUninstall 是否为卸载停止 * @throws Exception 启动异常 */ - protected void stop(PluginInsideInfo pluginInsideInfo) throws Exception{ + protected void stop(PluginInsideInfo pluginInsideInfo, boolean isUninstall) throws Exception{ launcherChecker.checkCanStop(pluginInsideInfo); pluginInsideInfo.setPluginState(PluginState.STOPPED); stopFinish(pluginInsideInfo); diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginExtensionInfo.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginExtensionInfo.java new file mode 100644 index 0000000..a741d01 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginExtensionInfo.java @@ -0,0 +1,21 @@ +package com.gitee.starblues.core; + +import java.util.Map; + +/** + * 自主实现插件的扩展信息 + * + * @author starBlues + * @version 3.1.0 + * @since 3.1.0 + */ +public interface PluginExtensionInfo { + + /** + * 实现返回扩展信息 + * @return 扩展信息Map + */ + Map extensionInfo(); + + +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginInfo.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginInfo.java index 851d1f8..9cf4d18 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginInfo.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginInfo.java @@ -19,6 +19,7 @@ package com.gitee.starblues.core; import com.gitee.starblues.core.descriptor.PluginDescriptor; import java.util.Date; +import java.util.Map; /** * 插件信息 @@ -63,11 +64,16 @@ public interface PluginInfo { */ Date getStopTime(); - /** * 是否跟随系统启动而加载的插件 * @return true: 是, false: 否 */ boolean isFollowSystem(); + /** + * 获取插件自主扩展信息 + * @return 扩展信息Map + */ + Map getExtensionInfo(); + } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginInfoFace.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginInfoFace.java index 0822f84..d0d53d7 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginInfoFace.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginInfoFace.java @@ -20,6 +20,8 @@ import com.gitee.starblues.core.descriptor.PluginDescriptor; import com.gitee.starblues.utils.Assert; import java.util.Date; +import java.util.Map; +import java.util.function.Supplier; /** * 外部 PluginWrapperFace @@ -31,6 +33,7 @@ public class PluginInfoFace implements PluginInfo { private final PluginDescriptor pluginDescriptor; private final PluginState pluginState; private final boolean followSystem; + private final Supplier> extensionInfoSupplier; private final Date startTime; private final Date stopTime; @@ -40,6 +43,7 @@ public class PluginInfoFace implements PluginInfo { this.pluginDescriptor = pluginInsideInfo.getPluginDescriptor().toPluginDescriptor(); this.pluginState = pluginInsideInfo.getPluginState(); this.followSystem = pluginInsideInfo.isFollowSystem(); + this.extensionInfoSupplier = pluginInsideInfo.getExtensionInfoSupplier(); this.startTime = pluginInsideInfo.getStartTime(); this.stopTime = pluginInsideInfo.getStopTime(); } @@ -78,4 +82,9 @@ public class PluginInfoFace implements PluginInfo { public boolean isFollowSystem() { return followSystem; } + + @Override + public Map getExtensionInfo() { + return extensionInfoSupplier.get(); + } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginInsideInfo.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginInsideInfo.java index 1c2a39c..0d7d7d6 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginInsideInfo.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginInsideInfo.java @@ -18,9 +18,14 @@ package com.gitee.starblues.core; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; +import java.util.Map; +import java.util.function.Supplier; + /** * 内部的 PluginInfo - * @version 3.0.0 + * + * @since 3.0.0 + * @version 3.1.0 * @author starBlues */ public interface PluginInsideInfo extends PluginInfo { @@ -36,6 +41,18 @@ public interface PluginInsideInfo extends PluginInfo { */ void setFollowSystem(); + /** + * 设置插件扩展信息 + * @param supplier 插件扩展信息自主提供者 + */ + void setExtensionInfoSupplier(Supplier> supplier); + + /** + * 获取插件信息提供者 + * @return 插件扩展信息自主提供者 + */ + Supplier> getExtensionInfoSupplier(); + /** * 得到插件描述 * @return PluginDescriptor diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java index ae87521..c64a2b1 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java @@ -97,13 +97,15 @@ public class PluginLauncherManager extends DefaultPluginManager{ launcherChecker.checkCanStart(pluginInsideInfo); try { InsidePluginDescriptor pluginDescriptor = pluginInsideInfo.getPluginDescriptor(); - PluginInteractive pluginInteractive = new DefaultPluginInteractive(pluginDescriptor, + PluginInteractive pluginInteractive = new DefaultPluginInteractive(pluginInsideInfo, mainApplicationContext, configuration, invokeSupperCache); AbstractLauncher pluginLauncher; if(DevelopmentModeSetting.isolation()){ pluginLauncher = new PluginIsolationLauncher(pluginInteractive, pluginLaunchInvolved); - } else { + } else if(DevelopmentModeSetting.coexist()){ pluginLauncher = new PluginCoexistLauncher(pluginInteractive, pluginLaunchInvolved); + } else { + throw DevelopmentModeSetting.getUnknownModeException(); } SpringPluginHook springPluginHook = pluginLauncher.run(); RegistryPluginInfo registryPluginInfo = new RegistryPluginInfo(pluginDescriptor, springPluginHook); @@ -117,11 +119,8 @@ public class PluginLauncherManager extends DefaultPluginManager{ } } - - - @Override - protected void stop(PluginInsideInfo pluginInsideInfo) throws Exception { + protected void stop(PluginInsideInfo pluginInsideInfo, boolean isUninstall) throws Exception { String pluginId = pluginInsideInfo.getPluginId(); RegistryPluginInfo registryPluginInfo = registryInfo.get(pluginId); if(registryPluginInfo == null){ @@ -130,10 +129,10 @@ public class PluginLauncherManager extends DefaultPluginManager{ try { SpringPluginHook springPluginHook = registryPluginInfo.getSpringPluginHook(); springPluginHook.stopVerify(); - springPluginHook.close(); + springPluginHook.close(isUninstall); invokeSupperCache.remove(pluginId); registryInfo.remove(pluginId); - super.stop(pluginInsideInfo); + super.stop(pluginInsideInfo, isUninstall); } catch (Exception e){ if(e instanceof PluginProhibitStopException){ // 禁止停止时, 不设置插件状态 diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/ComposeMainResourceMatcher.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/ComposeMainResourceMatcher.java index c993535..e8206bf 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/ComposeMainResourceMatcher.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/ComposeMainResourceMatcher.java @@ -16,6 +16,7 @@ package com.gitee.starblues.core.classloader; +import com.gitee.starblues.loader.utils.IOUtils; import com.gitee.starblues.utils.ObjectUtils; import java.util.ArrayList; @@ -27,7 +28,7 @@ import java.util.List; * @author starBlues * @version 3.0.3 */ -public class ComposeMainResourceMatcher implements MainResourceMatcher{ +public class ComposeMainResourceMatcher implements MainResourceMatcher, AutoCloseable{ private final List resourceMatchers; @@ -59,4 +60,13 @@ public class ComposeMainResourceMatcher implements MainResourceMatcher{ } return Boolean.FALSE; } + + @Override + public void close() throws Exception { + for (MainResourceMatcher resourceMatcher : resourceMatchers) { + if(resourceMatcher instanceof AutoCloseable){ + IOUtils.closeQuietly((AutoCloseable)resourceMatcher); + } + } + } } 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 628c96b..df179b0 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 @@ -39,7 +39,7 @@ import java.util.zip.ZipEntry; * 嵌套插件jar加载者 * @author starBlues * @since 3.0.0 - * @version 3.0.4 + * @version 3.1.0 */ @Slf4j public class NestedPluginJarResourceLoader extends AbstractResourceLoader { @@ -80,7 +80,7 @@ public class NestedPluginJarResourceLoader extends AbstractResourceLoader { } String realName = jarEntry.getName().replace(classesPath, ""); URL url = new URL(baseUrl.toString() + jarEntry.getName()); - resourceLoaderFactory.addResource(url); + resourceLoaderFactory.addResource(new DefaultResource(realName, baseUrl, url)); resourceStorage.add(realName, url, ()->{ return getClassBytes(realName, jarFile.getInputStream(jarEntry), true); }); 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 f26ece9..11ab94c 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 @@ -21,7 +21,9 @@ 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.Resource; import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFactory; +import com.gitee.starblues.loader.utils.IOUtils; import com.gitee.starblues.utils.Assert; import com.gitee.starblues.utils.FilesUtils; import com.gitee.starblues.utils.MsgUtils; @@ -56,10 +58,6 @@ public class PluginClassLoader extends GenericClassLoader implements PluginResou this.proxy = new PluginResourceLoaderFactoryProxy(resourceLoaderFactory, parentClassLoader); } - public MainResourceMatcher getMainResourceMatcher() { - return mainResourceMatcher; - } - @Override public void addResource(InsidePluginDescriptor descriptor) throws Exception { proxy.addResource(descriptor); @@ -117,4 +115,5 @@ public class PluginClassLoader extends GenericClassLoader implements PluginResou } } + } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java index e93e7fa..1326662 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginGeneralUrlClassLoader.java @@ -18,13 +18,16 @@ package com.gitee.starblues.core.classloader; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.loader.classloader.GeneralUrlClassLoader; +import com.gitee.starblues.loader.utils.IOUtils; import lombok.extern.slf4j.Slf4j; +import java.io.IOException; + /** * 插件基本 url classLoader * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ @Slf4j @@ -42,4 +45,8 @@ public class PluginGeneralUrlClassLoader extends GeneralUrlClassLoader implement proxy.addResource(descriptor); } + @Override + public void close() throws IOException { + super.close(); + } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java index 64e7089..3be839a 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactory.java @@ -23,7 +23,7 @@ import com.gitee.starblues.loader.classloader.resource.loader.ResourceLoaderFact * 插件资源工程 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ public interface PluginResourceLoaderFactory extends ResourceLoaderFactory { diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java index a3839ef..10143b8 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/classloader/PluginResourceLoaderFactoryProxy.java @@ -40,7 +40,7 @@ import java.util.Set; * 插件资源加载工厂代理 * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 13.0.4 */ @Slf4j @@ -145,6 +145,11 @@ public class PluginResourceLoaderFactoryProxy implements PluginResourceLoaderFac target.addResource(url); } + @Override + public void addResource(Resource resource) throws Exception { + target.addResource(resource); + } + @Override public void addResource(ResourceLoader resourceLoader) throws Exception { target.addResource(resourceLoader); diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/DefaultPluginInteractive.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/DefaultPluginInteractive.java index c26810d..a9249b8 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/DefaultPluginInteractive.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/DefaultPluginInteractive.java @@ -16,6 +16,7 @@ package com.gitee.starblues.core.launcher.plugin; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.spring.MainApplicationContext; @@ -31,17 +32,17 @@ import com.gitee.starblues.spring.invoke.InvokeSupperCache; */ public class DefaultPluginInteractive implements PluginInteractive{ - private final InsidePluginDescriptor pluginDescriptor; + private final PluginInsideInfo pluginInsideInfo; private final MainApplicationContext mainApplicationContext; private final IntegrationConfiguration configuration; private final InvokeSupperCache invokeSupperCache; private final OpExtractFactory opExtractFactory; - public DefaultPluginInteractive(InsidePluginDescriptor pluginDescriptor, + public DefaultPluginInteractive(PluginInsideInfo pluginInsideInfo, MainApplicationContext mainApplicationContext, IntegrationConfiguration configuration, InvokeSupperCache invokeSupperCache) { - this.pluginDescriptor = pluginDescriptor; + this.pluginInsideInfo = pluginInsideInfo; this.mainApplicationContext = mainApplicationContext; this.configuration = configuration; this.invokeSupperCache = invokeSupperCache; @@ -53,10 +54,14 @@ public class DefaultPluginInteractive implements PluginInteractive{ return (OpExtractFactory) defaultExtractFactory.getTarget(); } - @Override public InsidePluginDescriptor getPluginDescriptor() { - return pluginDescriptor; + return pluginInsideInfo.getPluginDescriptor(); + } + + @Override + public PluginInsideInfo getPluginInsideInfo() { + return pluginInsideInfo; } @Override diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java index 5b8f502..3bb4d27 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginCoexistLauncher.java @@ -1,5 +1,6 @@ package com.gitee.starblues.core.launcher.plugin; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.classloader.NestedPluginJarResourceLoader; import com.gitee.starblues.core.classloader.PluginGeneralUrlClassLoader; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; @@ -24,7 +25,7 @@ import java.util.Set; * * @author starBlues * @since 3.0.4 - * @version 3.0.4 + * @version 3.1.0 */ @Slf4j public class PluginCoexistLauncher extends AbstractLauncher { @@ -50,17 +51,18 @@ public class PluginCoexistLauncher extends AbstractLauncher { @Override protected SpringPluginHook launch(ClassLoader classLoader, String... args) throws Exception { InsidePluginDescriptor pluginDescriptor = pluginInteractive.getPluginDescriptor(); - pluginLaunchInvolved.before(pluginDescriptor, classLoader); + PluginInsideInfo pluginInsideInfo = pluginInteractive.getPluginInsideInfo(); + pluginLaunchInvolved.before(pluginInsideInfo, classLoader); try { SpringPluginHook springPluginHook = (SpringPluginHook) new PluginMethodRunner(pluginInteractive) .run(classLoader); if(springPluginHook == null){ throw new PluginException("插件返回的 SpringPluginHook 不能为空"); } - pluginLaunchInvolved.after(pluginDescriptor, classLoader, springPluginHook); - return new SpringPluginHookWrapper(springPluginHook, pluginDescriptor, pluginLaunchInvolved, classLoader); + pluginLaunchInvolved.after(pluginInsideInfo, classLoader, springPluginHook); + return new SpringPluginHookWrapper(springPluginHook, pluginInsideInfo, pluginLaunchInvolved, classLoader); } catch (Throwable throwable){ - pluginLaunchInvolved.failure(pluginDescriptor,classLoader, throwable); + pluginLaunchInvolved.failure(pluginInsideInfo,classLoader, throwable); throw throwable; } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginInteractive.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginInteractive.java index e9e87e1..2278ca5 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginInteractive.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginInteractive.java @@ -16,6 +16,7 @@ package com.gitee.starblues.core.launcher.plugin; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.spring.MainApplicationContext; @@ -35,6 +36,12 @@ public interface PluginInteractive { */ InsidePluginDescriptor getPluginDescriptor(); + /** + * 获取插件内部信息 + * @return PluginInsideInfo + */ + PluginInsideInfo getPluginInsideInfo(); + /** * 获取主程序的 MainApplicationContext * @return MainApplicationContext diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java index af1baad..9041233 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/PluginIsolationLauncher.java @@ -16,6 +16,7 @@ package com.gitee.starblues.core.launcher.plugin; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.classloader.*; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; @@ -39,14 +40,14 @@ import java.util.WeakHashMap; * * @author starBlues * @since 3.0.0 - * @version 3.0.4 + * @version 3.1.0 */ public class PluginIsolationLauncher extends AbstractLauncher { private static final Map CLASS_LOADER_CACHE = new WeakHashMap<>(); protected final PluginInteractive pluginInteractive; - protected final InsidePluginDescriptor pluginDescriptor; + protected final PluginInsideInfo pluginInsideInfo; protected final MainResourceMatcher mainResourceMatcher; protected final PluginLaunchInvolved pluginLaunchInvolved; @@ -54,7 +55,7 @@ public class PluginIsolationLauncher extends AbstractLauncher public PluginIsolationLauncher(PluginInteractive pluginInteractive, PluginLaunchInvolved pluginLaunchInvolved) { this.pluginInteractive = pluginInteractive; - this.pluginDescriptor = pluginInteractive.getPluginDescriptor(); + this.pluginInsideInfo = pluginInteractive.getPluginInsideInfo(); this.mainResourceMatcher = getMainResourceMatcher(pluginInteractive); this.pluginLaunchInvolved = pluginLaunchInvolved; } @@ -76,13 +77,13 @@ public class PluginIsolationLauncher extends AbstractLauncher @Override protected ClassLoader createClassLoader(String... args) throws Exception { PluginClassLoader pluginClassLoader = getPluginClassLoader(); - pluginClassLoader.addResource(pluginDescriptor); + pluginClassLoader.addResource(pluginInsideInfo.getPluginDescriptor()); return pluginClassLoader; } protected synchronized PluginClassLoader getPluginClassLoader() throws Exception { - String pluginId = pluginDescriptor.getPluginId(); - String key = MsgUtils.getPluginUnique(pluginDescriptor); + String pluginId = pluginInsideInfo.getPluginId(); + String key = MsgUtils.getPluginUnique(pluginInsideInfo.getPluginDescriptor()); PluginClassLoader classLoader = CLASS_LOADER_CACHE.get(key); if(classLoader != null){ return classLoader; @@ -95,7 +96,7 @@ public class PluginIsolationLauncher extends AbstractLauncher } protected ResourceLoaderFactory getResourceLoaderFactory(){ - return new DefaultResourceLoaderFactory(pluginDescriptor.getPluginId()); + return new DefaultResourceLoaderFactory(pluginInsideInfo.getPluginId()); } protected GenericClassLoader getParentClassLoader() throws Exception { @@ -109,14 +110,14 @@ public class PluginIsolationLauncher extends AbstractLauncher @Override protected SpringPluginHook launch(ClassLoader classLoader, String... args) throws Exception { - pluginLaunchInvolved.before(pluginDescriptor, classLoader); + pluginLaunchInvolved.before(pluginInsideInfo, classLoader); try { SpringPluginHook springPluginHook = (SpringPluginHook) new PluginMethodRunner(pluginInteractive) .run(classLoader); - pluginLaunchInvolved.after(pluginDescriptor, classLoader, springPluginHook); - return new SpringPluginHookWrapper(springPluginHook, pluginDescriptor, pluginLaunchInvolved, classLoader); + pluginLaunchInvolved.after(pluginInsideInfo, classLoader, springPluginHook); + return new SpringPluginHookWrapper(springPluginHook, pluginInsideInfo, pluginLaunchInvolved, classLoader); } catch (Throwable throwable){ - pluginLaunchInvolved.failure(pluginDescriptor,classLoader, throwable); + pluginLaunchInvolved.failure(pluginInsideInfo,classLoader, throwable); throw throwable; } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/SpringPluginHookWrapper.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/SpringPluginHookWrapper.java index e25ac5f..3ccdf43 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/SpringPluginHookWrapper.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/SpringPluginHookWrapper.java @@ -16,6 +16,7 @@ package com.gitee.starblues.core.launcher.plugin; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.exception.PluginProhibitStopException; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; @@ -24,24 +25,26 @@ import com.gitee.starblues.spring.SpringPluginHook; import com.gitee.starblues.spring.WebConfig; import com.gitee.starblues.spring.web.thymeleaf.ThymeleafConfig; import com.gitee.starblues.utils.ResourceUtils; +import lombok.extern.slf4j.Slf4j; /** * SpringPluginHook-Wrapper * @author starBlues * @version 3.0.0 */ +@Slf4j public class SpringPluginHookWrapper implements SpringPluginHook { private final SpringPluginHook target; - private final InsidePluginDescriptor descriptor; + private final PluginInsideInfo pluginInsideInfo; private final PluginLaunchInvolved pluginLaunchInvolved; private final ClassLoader classLoader; - public SpringPluginHookWrapper(SpringPluginHook target, InsidePluginDescriptor descriptor, + public SpringPluginHookWrapper(SpringPluginHook target, PluginInsideInfo pluginInsideInfo, PluginLaunchInvolved pluginLaunchInvolved, ClassLoader classLoader) { this.target = target; - this.descriptor = descriptor; + this.pluginInsideInfo = pluginInsideInfo; this.pluginLaunchInvolved = pluginLaunchInvolved; this.classLoader = classLoader; } @@ -67,9 +70,16 @@ public class SpringPluginHookWrapper implements SpringPluginHook { } @Override - public void close() throws Exception { - pluginLaunchInvolved.close(descriptor, classLoader); - ResourceUtils.closeQuietly(target); - ResourceUtils.closeQuietly(classLoader); + public void close(boolean isUninstall) throws Exception { + try { + pluginLaunchInvolved.close(pluginInsideInfo, classLoader); + } catch (Exception e){ + log.error("关闭插件异常: {}", e.getMessage(), e); + } + try { + target.close(isUninstall); + } finally { + ResourceUtils.closeQuietly(classLoader); + } } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/DefaultPluginLaunchInvolved.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/DefaultPluginLaunchInvolved.java index 33fee5b..4e25965 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/DefaultPluginLaunchInvolved.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/DefaultPluginLaunchInvolved.java @@ -16,32 +16,73 @@ package com.gitee.starblues.core.launcher.plugin.involved; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; +import com.gitee.starblues.core.PluginExtensionInfo; import com.gitee.starblues.loader.PluginResourceStorage; +import com.gitee.starblues.spring.ApplicationContext; import com.gitee.starblues.spring.SpringPluginHook; import com.gitee.starblues.spring.web.PluginStaticResourceResolver; +import com.gitee.starblues.utils.ObjectUtils; +import com.gitee.starblues.utils.SpringBeanCustomUtils; +import lombok.extern.slf4j.Slf4j; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * 默认的插件启动介入者 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ +@Slf4j public class DefaultPluginLaunchInvolved implements PluginLaunchInvolved{ @Override - public void before(InsidePluginDescriptor descriptor, ClassLoader classLoader) throws Exception { + public void before(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader) throws Exception { + InsidePluginDescriptor descriptor = pluginInsideInfo.getPluginDescriptor(); PluginResourceStorage.addPlugin(descriptor.getPluginId(), descriptor.getPluginFileName()); } @Override - public void after(InsidePluginDescriptor descriptor, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception { + public void after(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception { + InsidePluginDescriptor descriptor = pluginInsideInfo.getPluginDescriptor(); PluginStaticResourceResolver.parse(descriptor, classLoader, pluginHook.getWebConfig()); + setExtensionInfoSupplier(pluginInsideInfo, pluginHook); } @Override - public void close(InsidePluginDescriptor descriptor, ClassLoader classLoader) throws Exception { + public void close(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader) throws Exception { + InsidePluginDescriptor descriptor = pluginInsideInfo.getPluginDescriptor(); String pluginId = descriptor.getPluginId(); PluginResourceStorage.removePlugin(pluginId); PluginStaticResourceResolver.remove(pluginId); } + + private void setExtensionInfoSupplier(PluginInsideInfo pluginInsideInfo, SpringPluginHook pluginHook){ + pluginInsideInfo.setExtensionInfoSupplier(()->{ + // 设置插件自主扩展信息 + ApplicationContext applicationContext = pluginHook.getApplicationContext(); + List beans = SpringBeanCustomUtils.getBeans(applicationContext, + PluginExtensionInfo.class); + if(ObjectUtils.isEmpty(beans)){ + return new HashMap<>(0); + } + Map extensionInfos = new HashMap<>(); + for (PluginExtensionInfo extensionInfoBean : beans) { + try { + Map extensionInfo = extensionInfoBean.extensionInfo(); + if(!ObjectUtils.isEmpty(extensionInfo)){ + extensionInfos.putAll(extensionInfo); + } + } catch (Exception e){ + log.error(e.getMessage(), e); + } + } + return extensionInfos; + }); + } + } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginApplicationContextGetter.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginApplicationContextGetter.java index 8a8754e..165db50 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginApplicationContextGetter.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginApplicationContextGetter.java @@ -16,6 +16,7 @@ package com.gitee.starblues.core.launcher.plugin.involved; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.spring.ApplicationContext; import com.gitee.starblues.spring.SpringPluginHook; @@ -25,19 +26,22 @@ import java.util.concurrent.ConcurrentHashMap; /** * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ public class PluginApplicationContextGetter implements PluginLaunchInvolved{ private static final Map PLUGIN_CONTEXTS = new ConcurrentHashMap<>(); @Override - public void after(InsidePluginDescriptor descriptor, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception { + public void after(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception { + InsidePluginDescriptor descriptor = pluginInsideInfo.getPluginDescriptor(); PLUGIN_CONTEXTS.put(descriptor.getPluginId(), pluginHook.getApplicationContext()); } @Override - public void close(InsidePluginDescriptor descriptor, ClassLoader classLoader) throws Exception { + public void close(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader) throws Exception { + InsidePluginDescriptor descriptor = pluginInsideInfo.getPluginDescriptor(); PLUGIN_CONTEXTS.remove(descriptor.getPluginId()); } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginLaunchInvolved.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginLaunchInvolved.java index 93fc5a1..5e3bbe6 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginLaunchInvolved.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginLaunchInvolved.java @@ -16,7 +16,7 @@ package com.gitee.starblues.core.launcher.plugin.involved; -import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.spring.SpringPluginHook; import com.gitee.starblues.utils.OrderPriority; @@ -25,7 +25,8 @@ import org.springframework.context.support.GenericApplicationContext; /** * 插件启动前后介入 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ public interface PluginLaunchInvolved { @@ -38,38 +39,38 @@ public interface PluginLaunchInvolved { /** * 启动之前 - * @param descriptor 插件信息 + * @param pluginInsideInfo 插件信息 * @param classLoader 插件classloader * @throws Exception 执行异常 */ - default void before(InsidePluginDescriptor descriptor, ClassLoader classLoader) throws Exception{} + default void before(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader) throws Exception{} /** * 启动之后 - * @param descriptor 插件信息 + * @param pluginInsideInfo 插件信息 * @param classLoader 插件classloader * @param pluginHook 启动成功后插件返回的钩子 * @throws Exception 执行异常 */ - default void after(InsidePluginDescriptor descriptor, ClassLoader classLoader, + default void after(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception{} /** * 启动失败 - * @param descriptor 插件信息 + * @param pluginInsideInfo 插件信息 * @param classLoader 插件classloader * @param throwable 异常信息 * @throws Exception 执行异常 */ - default void failure(InsidePluginDescriptor descriptor, ClassLoader classLoader, Throwable throwable) throws Exception{} + default void failure(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader, Throwable throwable) throws Exception{} /** * 关闭的时候 - * @param descriptor 插件信息 + * @param pluginInsideInfo 插件信息 * @param classLoader 插件classloader * @throws Exception 执行异常 */ - default void close(InsidePluginDescriptor descriptor, ClassLoader classLoader) throws Exception{} + default void close(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader) throws Exception{} /** * 执行顺序 diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginLaunchInvolvedFactory.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginLaunchInvolvedFactory.java index 8330ce2..67bc9cc 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginLaunchInvolvedFactory.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/involved/PluginLaunchInvolvedFactory.java @@ -16,6 +16,7 @@ package com.gitee.starblues.core.launcher.plugin.involved; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.spring.SpringPluginHook; @@ -32,7 +33,8 @@ import java.util.Map; /** * 插件启动介入工厂 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ public class PluginLaunchInvolvedFactory implements PluginLaunchInvolved{ @@ -75,24 +77,24 @@ public class PluginLaunchInvolvedFactory implements PluginLaunchInvolved{ } @Override - public void before(InsidePluginDescriptor descriptor, ClassLoader classLoader) throws Exception { + public void before(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader) throws Exception { for (PluginLaunchInvolved pluginLaunchInvolved : pluginLaunchInvolvedList) { - pluginLaunchInvolved.before(descriptor, classLoader); + pluginLaunchInvolved.before(pluginInsideInfo, classLoader); } } @Override - public void after(InsidePluginDescriptor descriptor, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception { + public void after(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception { for (PluginLaunchInvolved pluginLaunchInvolved : pluginLaunchInvolvedList) { - pluginLaunchInvolved.after(descriptor, classLoader, pluginHook); + pluginLaunchInvolved.after(pluginInsideInfo, classLoader, pluginHook); } } @Override - public void failure(InsidePluginDescriptor descriptor, ClassLoader classLoader, Throwable throwable) throws Exception { + public void failure(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader, Throwable throwable) throws Exception { for (PluginLaunchInvolved pluginLaunchInvolved : pluginLaunchInvolvedList) { try { - pluginLaunchInvolved.failure(descriptor, classLoader, throwable); + pluginLaunchInvolved.failure(pluginInsideInfo, classLoader, throwable); } catch (Exception e){ logger.error("[{}] execute failure exception : {}", pluginLaunchInvolved.getClass().getName(), e.getMessage(), e); @@ -101,10 +103,10 @@ public class PluginLaunchInvolvedFactory implements PluginLaunchInvolved{ } @Override - public void close(InsidePluginDescriptor descriptor, ClassLoader classLoader) throws Exception { + public void close(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader) throws Exception { for (PluginLaunchInvolved pluginLaunchInvolved : pluginLaunchInvolvedList) { try { - pluginLaunchInvolved.close(descriptor, classLoader); + pluginLaunchInvolved.close(pluginInsideInfo, classLoader); } catch (Exception e){ logger.error("[{}] execute close exception : {}", pluginLaunchInvolved.getClass().getName(), e.getMessage(), e); diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/ExtendPointConfiguration.java b/spring-brick/src/main/java/com/gitee/starblues/integration/ExtendPointConfiguration.java index 79f43b5..1d0f5d7 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/ExtendPointConfiguration.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/ExtendPointConfiguration.java @@ -19,6 +19,7 @@ package com.gitee.starblues.integration; import com.gitee.starblues.core.DefaultRealizeProvider; import com.gitee.starblues.core.RealizeProvider;; import com.gitee.starblues.core.classloader.CacheMainResourceMatcher; +import com.gitee.starblues.core.classloader.DefaultMainResourceMatcher; import com.gitee.starblues.core.classloader.MainResourceMatcher; import com.gitee.starblues.core.descriptor.decrypt.DefaultPluginDescriptorDecrypt; import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; @@ -49,6 +50,7 @@ public class ExtendPointConfiguration { IntegrationConfiguration configuration) { this.applicationContext = applicationContext; this.configuration = configuration; + this.configuration.checkConfig(); } @Bean @@ -83,7 +85,7 @@ public class ExtendPointConfiguration { @Bean public MainResourceMatcher mainResourceMatcher(){ - return new CacheMainResourceMatcher(new DefaultMainResourcePatternDefiner( + return new DefaultMainResourceMatcher(new DefaultMainResourcePatternDefiner( configuration.mainPackage(), applicationContext )); diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/EmptyPluginOperator.java b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/EmptyPluginOperator.java index c56b306..7990567 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/EmptyPluginOperator.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/EmptyPluginOperator.java @@ -12,7 +12,7 @@ import java.util.List; * 空操作的 PluginOperator * * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.4 */ public class EmptyPluginOperator implements PluginOperator{ diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContext.java b/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContext.java index 77c9079..af30214 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContext.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/ApplicationContext.java @@ -20,7 +20,7 @@ package com.gitee.starblues.spring; * 自定义ApplicationContext * @author starBlues * @since 3.0.0 - * @version 3.0.4 + * @version 3.1.0 */ public interface ApplicationContext extends AutoCloseable { diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/SpringPluginHook.java b/spring-brick/src/main/java/com/gitee/starblues/spring/SpringPluginHook.java index 55cdc24..6cf5c61 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/SpringPluginHook.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/SpringPluginHook.java @@ -25,7 +25,7 @@ import com.gitee.starblues.spring.web.thymeleaf.ThymeleafConfig; * @author starBlues * @version 3.0.0 */ -public interface SpringPluginHook extends AutoCloseable{ +public interface SpringPluginHook { /** * 停止前校验. 如果抛出 PluginProhibitStopException 异常, 表示当前插件不可停止 @@ -51,4 +51,12 @@ public interface SpringPluginHook extends AutoCloseable{ */ ThymeleafConfig getThymeleafConfig(); + /** + * 卸载调用 + * @param isUninstall 是否是卸载关闭 + * @since 3.1.0 + * @throws Exception 关闭异常 + */ + void close(boolean isUninstall) throws Exception; + } diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/extract/DefaultOpExtractFactory.java b/spring-brick/src/main/java/com/gitee/starblues/spring/extract/DefaultOpExtractFactory.java index 7f56688..73096b2 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/extract/DefaultOpExtractFactory.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/extract/DefaultOpExtractFactory.java @@ -28,7 +28,7 @@ import java.util.stream.Collectors; /** * 默认的可扩展的工厂 * @author starBlues - * @version 3.0.4 + * @version 3.1.0 * @since 3.0.0 */ public class DefaultOpExtractFactory implements OpExtractFactory { diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/web/thymeleaf/PluginThymeleafInvolved.java b/spring-brick/src/main/java/com/gitee/starblues/spring/web/thymeleaf/PluginThymeleafInvolved.java index 999816e..333f0af 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/web/thymeleaf/PluginThymeleafInvolved.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/web/thymeleaf/PluginThymeleafInvolved.java @@ -16,6 +16,7 @@ package com.gitee.starblues.spring.web.thymeleaf; +import com.gitee.starblues.core.PluginInsideInfo; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; import com.gitee.starblues.integration.IntegrationConfiguration; @@ -36,7 +37,8 @@ import java.util.concurrent.ConcurrentHashMap; /** * 插件 Thymeleaf 注册 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ public class PluginThymeleafInvolved implements PluginLaunchInvolved { @@ -52,7 +54,7 @@ public class PluginThymeleafInvolved implements PluginLaunchInvolved { } @Override - public void after(InsidePluginDescriptor descriptor, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception { + public void after(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader, SpringPluginHook pluginHook) throws Exception { if(templateResolvers == null){ return; } @@ -93,14 +95,15 @@ public class PluginThymeleafInvolved implements PluginLaunchInvolved { } resolver.setCheckExistence(true); templateResolvers.add(resolver); + InsidePluginDescriptor descriptor = pluginInsideInfo.getPluginDescriptor(); if(!pluginTemplateResolver.containsKey(descriptor.getPluginId())){ pluginTemplateResolver.put(descriptor.getPluginId(), resolver); } } @Override - public void close(InsidePluginDescriptor descriptor, ClassLoader classLoader) throws Exception { - pluginTemplateResolver.remove(descriptor.getPluginId()); + public void close(PluginInsideInfo pluginInsideInfo, ClassLoader classLoader) throws Exception { + pluginTemplateResolver.remove(pluginInsideInfo.getPluginId()); } private SpringTemplateEngine getSpringTemplateEngine(GenericApplicationContext context){ diff --git a/update.md b/update.md index 467a849..fc7b034 100644 --- a/update.md +++ b/update.md @@ -1,5 +1,7 @@ 1. 【新增】增加主包MAINIFEST中title和version定义, 标准jar包中包含`Implementation-Version`和`Implementation-Title`属性 2. 【新增】新增根据个人需求选择开发模式,支持隔离式开发模式(目前已有的)、共享式开发模式 +3. 【新增】新增可自主实现扩展插件信息 +4. 【新增】新增插件可判断卸载事件 3. 【修复】修复插件中`LiveBeansView`注册异常问题 4. 【修复[#I5IFR4](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5IFR3)】 `ExtractFactory#getExtractByCoordinate` 类型转换`Bug` 5. 【修复[#I5GJO9](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5GJO9)】`DefaultPluginManager#install` 异常无法抛出 -- Gitee From 66ea617c5d20e7a97c9638bf5f682c2c3994ce95 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Sun, 4 Sep 2022 19:39:38 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6load?= =?UTF-8?q?=E8=A7=A3=E5=8E=8B=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gitee/starblues/common/Constants.java | 2 + .../com/gitee/starblues/utils/FilesUtils.java | 73 ++++++++++++++++++- .../plugin/pack/prod/ZipProdRepackager.java | 6 +- .../core/DefaultPluginInsideInfo.java | 3 +- .../starblues/core/DefaultPluginManager.java | 4 +- .../starblues/core/PluginLauncherManager.java | 1 + .../com/gitee/starblues/core/PluginState.java | 7 +- .../NestedPluginJarResourceLoader.java | 5 +- .../AbstractPluginDescriptorLoader.java | 55 +++++++++----- .../descriptor/DevPluginDescriptorLoader.java | 2 +- .../ProdPackagePluginDescriptorLoader.java | 14 +++- .../operator/PluginOperatorWrapper.java | 2 +- .../starblues/utils/PluginFileUtils.java | 45 ++++++++---- update.md | 6 +- 14 files changed, 176 insertions(+), 49 deletions(-) diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/Constants.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/Constants.java index 2151f6c..481b524 100644 --- a/spring-brick-common/src/main/java/com/gitee/starblues/common/Constants.java +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/Constants.java @@ -16,6 +16,8 @@ package com.gitee.starblues.common; +import java.io.File; + /** * 静态常量 * @author starBlues 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 34b06d3..78455f5 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 @@ -17,6 +17,7 @@ package com.gitee.starblues.utils; import com.gitee.starblues.common.Constants; +import com.gitee.starblues.common.PackageStructure; import java.io.File; import java.io.IOException; @@ -43,6 +44,18 @@ public class FilesUtils { return null; } + /** + * 是否存在文件 + * @param path 文件路径 + * @return boolean + */ + public static boolean existFile(String path){ + if(ObjectUtils.isEmpty(path)){ + return false; + } + return new File(path).exists(); + } + /** * 拼接file路径 @@ -50,7 +63,48 @@ public class FilesUtils { * @param paths 拼接的路径 * @return 拼接的路径 */ - public static String joiningFilePath(String ...paths){ + 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 < length - 1) { + if (path.endsWith("/")) { + path = path.replace("/", ""); + } else if (path.endsWith("\\")) { + path = path.replace("\\", ""); + } else if (path.endsWith("//")) { + path = path.replace("//", ""); + } + } + 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(); + } + + /** + * 拼接 zip /jar 路径 + * + * @param paths 拼接的路径 + * @return 拼接的路径 + */ + public static String joiningZipPath(String ...paths){ if(paths == null || paths.length == 0){ return ""; } @@ -61,12 +115,20 @@ public class FilesUtils { if(ObjectUtils.isEmpty(path)) { continue; } + if(i < length - 1){ + if(path.endsWith("/")){ + path = path.replace("/", ""); + } else if(path.endsWith("\\")){ + path = path.replace("\\", ""); + } else if(path.endsWith("//")){ + path = path.replace("//", ""); + } + } if(i > 0){ - if(path.startsWith(File.separator) || path.startsWith("/") || - path.startsWith("\\") || path.startsWith("//")){ + if(path.startsWith(PackageStructure.SEPARATOR)){ stringBuilder.append(path); } else { - stringBuilder.append(File.separator).append(path); + stringBuilder.append(PackageStructure.SEPARATOR).append(path); } } else { stringBuilder.append(path); @@ -79,6 +141,9 @@ public class FilesUtils { public static File createFile(String path) throws IOException { try { File file = new File(path); + if(file.exists()){ + return file; + } File parentFile = file.getParentFile(); if(!parentFile.exists()){ if(!parentFile.mkdirs()){ 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 303fd54..55ca154 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 @@ -174,8 +174,10 @@ public class ZipProdRepackager extends DevRepackager { if(filterArtifact(artifact)){ continue; } - String dependencyIndexName = packageZip.writeDependency(artifact.getFile(), libDirEntryName); - dependencyIndexNames.add(dependencyIndexName); + File artifactFile = artifact.getFile(); + packageZip.writeDependency(artifactFile, libDirEntryName); + // fix 解决依赖前缀携带, lib 配置的前缀 + dependencyIndexNames.add(artifactFile.getName()); } return dependencyIndexNames; } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginInsideInfo.java b/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginInsideInfo.java index c1374eb..1fdaf5b 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginInsideInfo.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/DefaultPluginInsideInfo.java @@ -20,6 +20,7 @@ import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.utils.Assert; import lombok.Setter; +import java.util.Collections; import java.util.Date; import java.util.Map; import java.util.function.Supplier; @@ -39,7 +40,7 @@ public class DefaultPluginInsideInfo implements PluginInsideInfo { private Date startTime; private Date stopTime; - private Supplier> extensionInfoSupplier; + private Supplier> extensionInfoSupplier = Collections::emptyMap; public DefaultPluginInsideInfo(InsidePluginDescriptor pluginDescriptor) { this.pluginId = pluginDescriptor.getPluginId(); 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 047dc45..392d847 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 @@ -180,6 +180,7 @@ public class DefaultPluginManager implements PluginManager{ if(pluginInsideInfo == null){ throw new PluginException("非法插件包: " + pluginPath); } + pluginInsideInfo.setPluginState(PluginState.PARSED); return pluginInsideInfo.toPluginInfo(); } @@ -350,7 +351,7 @@ public class DefaultPluginManager implements PluginManager{ if(ObjectUtils.isEmpty(pluginId)){ return null; } - PluginInsideInfo pluginInsideInfo = startedPlugins.get(pluginId); + PluginInsideInfo pluginInsideInfo = getPlugin(pluginId); if(pluginInsideInfo == null){ throw new PluginException("没有发现插件: " + pluginId); } @@ -546,7 +547,6 @@ public class DefaultPluginManager implements PluginManager{ pluginInsideInfo.setPluginState(PluginState.STOPPED); stopFinish(pluginInsideInfo); } - /** * 停止完成操作 * @param pluginInsideInfo pluginInsideInfo diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java index c64a2b1..0ec8340 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java @@ -121,6 +121,7 @@ public class PluginLauncherManager extends DefaultPluginManager{ @Override protected void stop(PluginInsideInfo pluginInsideInfo, boolean isUninstall) throws Exception { + launcherChecker.checkCanStop(pluginInsideInfo); String pluginId = pluginInsideInfo.getPluginId(); RegistryPluginInfo registryPluginInfo = registryInfo.get(pluginId); if(registryPluginInfo == null){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginState.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginState.java index ff12458..1af14d0 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginState.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginState.java @@ -51,7 +51,12 @@ public enum PluginState { /** * 停止失败状态 */ - STOPPED_FAILURE("STOPPED_FAILURE"); + STOPPED_FAILURE("STOPPED_FAILURE"), + + /** + * 被解析状态. 仅仅用于解析插件后被展示的状态 + */ + PARSED("PARSED"); private final String status; 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 df179b0..c09c2cc 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 @@ -23,7 +23,9 @@ import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; 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.utils.FilesUtils; import com.gitee.starblues.utils.MsgUtils; +import com.gitee.starblues.utils.ObjectUtils; import lombok.extern.slf4j.Slf4j; import java.io.InputStream; @@ -92,7 +94,8 @@ public class NestedPluginJarResourceLoader extends AbstractResourceLoader { Set pluginLibInfos = pluginDescriptor.getPluginLibInfo(); String pluginUnique = MsgUtils.getPluginUnique(pluginDescriptor); for (PluginLibInfo pluginLibInfo : pluginLibInfos) { - jarEntry = jarFile.getJarEntry(pluginLibInfo.getPath()); + String entryName = pluginLibInfo.getPath(); + jarEntry = jarFile.getJarEntry(entryName); if(jarEntry == null){ log.debug("Not found: " + pluginLibInfo.getPath()); continue; 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 d9d48f1..9178842 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 @@ -155,13 +155,13 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor } protected Set getPluginLibInfo(DefaultInsidePluginDescriptor descriptor, Set dependenciesIndex){ - String pluginLibDir = descriptor.getPluginLibDir(); - boolean configPluginLibDir = false; - if(!ObjectUtils.isEmpty(pluginLibDir)){ - String libDir = getLibDir(descriptor, pluginLibDir); + String configPluginLibDir = descriptor.getPluginLibDir(); + boolean isConfigPluginLibDir = false; + if(!ObjectUtils.isEmpty(configPluginLibDir)){ + String libDir = getLibDir(descriptor, configPluginLibDir); if(!ObjectUtils.isEmpty(libDir)){ descriptor.setPluginLibDir(libDir); - configPluginLibDir = true; + isConfigPluginLibDir = true; } } if(ObjectUtils.isEmpty(dependenciesIndex)){ @@ -177,8 +177,8 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor loadToMain = false; } String libPath = index; - if(configPluginLibDir){ - libPath = getLibPath(descriptor, index); + if(isConfigPluginLibDir){ + libPath = getLibPath(descriptor, configPluginLibDir, index); } pluginLibInfos.add(new PluginLibInfo(libPath, loadToMain)); } @@ -186,30 +186,49 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor } protected String getLibDir(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir){ - if(!FilesUtils.isRelativePath(configPluginLibDir)){ + if(FilesUtils.existFile(configPluginLibDir)){ return configPluginLibDir; } - // 是相对路径 - // 先相对当前插件目录 - String resolveRelativePath = FilesUtils.resolveRelativePath(descriptor.getPluginPath(), configPluginLibDir); - if(new File(resolveRelativePath).exists()){ + // 先检查插件相对目录 + String resolveRelativePath = null; + if(FilesUtils.isRelativePath(configPluginLibDir)){ + // 先相对当前插件目录 + resolveRelativePath = FilesUtils.resolveRelativePath(descriptor.getPluginPath(), configPluginLibDir); + } else { + resolveRelativePath = FilesUtils.joiningFilePath(descriptor.getPluginPath(), configPluginLibDir); + } + if(FilesUtils.existFile(resolveRelativePath)){ return resolveRelativePath; } // 再相对插件存放目录 - resolveRelativePath = FilesUtils.resolveRelativePath(new File(descriptor.getPluginPath()).getParent(), configPluginLibDir); - if(new File(resolveRelativePath).exists()){ + resolveRelativePath = FilesUtils.joiningFilePath(new File(descriptor.getPluginPath()).getParent(), configPluginLibDir); + if(FilesUtils.existFile(resolveRelativePath)){ return resolveRelativePath; } // 最后相对主程序目录 - resolveRelativePath = FilesUtils.resolveRelativePath(new File("").getAbsolutePath(), configPluginLibDir); - if(new File(resolveRelativePath).exists()){ + resolveRelativePath = FilesUtils.joiningFilePath(new File("").getAbsolutePath(), configPluginLibDir); + if(FilesUtils.existFile(resolveRelativePath)){ return resolveRelativePath; } return null; } - protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String index){ - return FilesUtils.joiningFilePath(descriptor.getPluginLibDir(), index); + protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir, String index){ + String pluginLibDir = descriptor.getPluginLibDir(); + if(ObjectUtils.isEmpty(pluginLibDir)){ + return index; + } + String joiningFilePath = FilesUtils.joiningFilePath(descriptor.getPluginLibDir(), index); + if(index.startsWith(configPluginLibDir)){ + // 如果 index 中前缀配置了 PLUGIN.META 中的 plugin.libDir 则尝试判断完整拼接的依赖路径文件是否存在 + // 如果存在, 则返回, 如果不存在, 则去掉重复前缀, 返回。该处是为了兼容解压后的jar中index存在 libDir 前缀 + if(FilesUtils.existFile(joiningFilePath)){ + return joiningFilePath; + } + return FilesUtils.joiningFilePath(descriptor.getPluginLibDir(), + index.replace(configPluginLibDir, "")); + } + return joiningFilePath; } protected Properties getDecryptProperties(InputStream inputStream) throws Exception{ 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 432e9fc..f759177 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 @@ -58,7 +58,7 @@ public class DevPluginDescriptorLoader extends AbstractPluginDescriptorLoader{ } @Override - protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String index) { + protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir, String index) { return index; } 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 4721da3..8740c64 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 @@ -90,11 +90,19 @@ public class ProdPackagePluginDescriptorLoader extends AbstractPluginDescriptorL } @Override - protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String index) { + protected String getLibPath(DefaultInsidePluginDescriptor descriptor, String configPluginLibDir, String index) { if(PluginType.isNestedPackage(descriptor.getType())){ - return index; + String pluginLibDir = descriptor.getPluginLibDir(); + if(ObjectUtils.isEmpty(pluginLibDir)){ + return index; + } + if(index.startsWith(configPluginLibDir)){ + // 兼容解决旧版本中 jar/zip 包中, 依赖前缀携带 配置的 lib 路径 + return index; + } + return FilesUtils.joiningZipPath(pluginLibDir, index); } else { - return super.getLibPath(descriptor, index); + return super.getLibPath(descriptor, configPluginLibDir, index); } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/PluginOperatorWrapper.java b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/PluginOperatorWrapper.java index aaf9007..130e2ce 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/PluginOperatorWrapper.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/PluginOperatorWrapper.java @@ -99,7 +99,7 @@ public class PluginOperatorWrapper implements PluginOperator{ if(isDisable()){ return null; } - return pluginOperator.install(pluginPath, unpackPlugin); + return pluginOperator.load(pluginPath, unpackPlugin); } @Override diff --git a/spring-brick/src/main/java/com/gitee/starblues/utils/PluginFileUtils.java b/spring-brick/src/main/java/com/gitee/starblues/utils/PluginFileUtils.java index 3e1423e..fb01b0d 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/utils/PluginFileUtils.java +++ b/spring-brick/src/main/java/com/gitee/starblues/utils/PluginFileUtils.java @@ -17,7 +17,9 @@ package com.gitee.starblues.utils; +import com.gitee.starblues.common.ManifestKey; import com.gitee.starblues.common.PackageStructure; +import com.gitee.starblues.common.PackageType; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -32,6 +34,7 @@ import java.nio.file.Paths; import java.security.MessageDigest; import java.util.Enumeration; import java.util.List; +import java.util.Objects; import java.util.jar.Attributes; import java.util.jar.Manifest; import java.util.zip.ZipEntry; @@ -41,7 +44,8 @@ import java.util.zip.ZipFile; * 插件文件工具类 * * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ public final class PluginFileUtils { @@ -187,7 +191,9 @@ public final class PluginFileUtils { } File targetDirFile = new File(targetDir); if(!targetDirFile.exists()){ - targetDirFile.mkdirs(); + if(!targetDirFile.mkdirs()){ + throw new IOException("创建目录异常: " + targetDir); + } } try (ZipFile zip = new ZipFile(zipFile, Charset.forName(PackageStructure.CHARSET_NAME))) { Enumeration zipEnumeration = zip.entries(); @@ -201,23 +207,34 @@ public final class PluginFileUtils { if (zipEntry.isDirectory()) { FileUtils.forceMkdir(new File(currentTargetPath)); continue; + } else { + FilesUtils.createFile(currentTargetPath); } - InputStream in = null; - FileOutputStream out = null; - try { - in = zip.getInputStream(zipEntry); - out = new FileOutputStream(currentTargetPath); - IOUtils.copy(in, out); - } finally { - if (in != null) { - IOUtils.closeQuietly(in); - } - if (out != null) { - IOUtils.closeQuietly(out); + try (InputStream in = zip.getInputStream(zipEntry); + FileOutputStream out = new FileOutputStream(currentTargetPath)){ + if(PackageStructure.PROD_MANIFEST_PATH.equals(zipEntryName)){ + // 如果为 Manifest 文件, 则将打包类型切换为 xx-outer + resolveDecompressPluginType(in, out); + } else { + IOUtils.copy(in, out); } } } } } + + private static void resolveDecompressPluginType(InputStream inputStream, OutputStream outputStream) throws IOException{ + Manifest manifest = new Manifest(inputStream); + Attributes mainAttributes = manifest.getMainAttributes(); + String value = mainAttributes.getValue(ManifestKey.PLUGIN_PACKAGE_TYPE); + if(Objects.equals(value, PackageType.MAIN_PACKAGE_TYPE_JAR)){ + value = PackageType.PLUGIN_PACKAGE_TYPE_DIR; + } else if(Objects.equals(value, PackageType.PLUGIN_PACKAGE_TYPE_ZIP)){ + value = PackageType.PLUGIN_PACKAGE_TYPE_DIR; + } + mainAttributes.putValue(ManifestKey.PLUGIN_PACKAGE_TYPE, value); + manifest.write(outputStream); + } + } diff --git a/update.md b/update.md index fc7b034..87c29f1 100644 --- a/update.md +++ b/update.md @@ -7,4 +7,8 @@ 5. 【修复[#I5GJO9](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5GJO9)】`DefaultPluginManager#install` 异常无法抛出 6. 【修复】修复插件无法加载其他包依赖中的`mybatis-xml`问题 7. 【修复】修复插件子启动问题 -8. 【优化】优化依赖资源默认不缓存, 以减少内存 +8. 【修复】修复插件`load`解压异常 +9. 【修复】修复`jar`、`zip`加载依赖时未使用`libDir`配置前缀 +10.【优化】优化插件`parse`后为`parsed`状态 +11.【优化】优化依赖资源默认不缓存, 以减少内存 + -- Gitee From a749acfeec0c809f91416caff8cfde03a9421fc7 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Sun, 4 Sep 2022 19:42:18 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E4=BF=AE=E6=94=B93.1.0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/gitee/starblues/utils/FilesUtils.java | 5 ++++- .../java/com/gitee/starblues/core/PluginLauncherManager.java | 2 +- .../core/descriptor/AbstractPluginDescriptorLoader.java | 3 ++- .../core/descriptor/ProdPackagePluginDescriptorLoader.java | 3 ++- .../integration/operator/PluginOperatorWrapper.java | 3 ++- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java b/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java index 78455f5..a057e15 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 @@ -26,7 +26,8 @@ import java.io.IOException; * 文件工具类 * * @author starBlues - * @version 3.0.2 + * @since 3.0.0 + * @version 3.1.0 */ public class FilesUtils { @@ -62,6 +63,7 @@ public class FilesUtils { * * @param paths 拼接的路径 * @return 拼接的路径 + * @since 3.0.0 */ public static String joiningFilePath(String ...paths) { if (paths == null || paths.length == 0) { @@ -103,6 +105,7 @@ public class FilesUtils { * * @param paths 拼接的路径 * @return 拼接的路径 + * @since 3.1.0 */ public static String joiningZipPath(String ...paths){ if(paths == null || paths.length == 0){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java index 0ec8340..69bf900 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java @@ -48,7 +48,7 @@ import java.util.concurrent.ConcurrentHashMap; * 可引导启动的插件管理者 * @author starBlues * @since 3.0.0 - * @version 3.0.3 + * @version 3.1.0 */ public class PluginLauncherManager extends DefaultPluginManager{ 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 9178842..efeb3a0 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 @@ -48,7 +48,8 @@ import static com.gitee.starblues.utils.PropertiesUtils.getValue; /** * 抽象的 PluginDescriptorLoader * @author starBlues - * @version 3.0.2 + * @since 3.0.0 + * @version 3.1.0 */ @Slf4j public abstract class AbstractPluginDescriptorLoader implements PluginDescriptorLoader{ 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 8740c64..a52b8a6 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 @@ -42,7 +42,8 @@ import static com.gitee.starblues.common.PluginDescriptorKey.PLUGIN_RESOURCES_CO * 生产环境打包好的插件 PluginDescriptorLoader 加载者 * 解析 jar、zip * @author starBlues - * @version 3.0.2 + * @since 3.0.0 + * @version 3.1.0 */ public class ProdPackagePluginDescriptorLoader extends AbstractPluginDescriptorLoader{ diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/PluginOperatorWrapper.java b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/PluginOperatorWrapper.java index 130e2ce..c96554b 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/operator/PluginOperatorWrapper.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/operator/PluginOperatorWrapper.java @@ -31,7 +31,8 @@ import java.util.List; /** * 插件操作包装者 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ public class PluginOperatorWrapper implements PluginOperator{ -- Gitee From fd717e71bd17a811fa2fbe12b785db455ffff256 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Sat, 17 Sep 2022 10:15:06 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootstrap/DefaultSpringPluginHook.java | 9 +-- .../realize/PluginCloseListener.java | 5 +- .../starblues/core/DefaultPluginManager.java | 61 +++++++++++-------- .../gitee/starblues/core/PluginCloseType.java | 27 ++++++++ .../starblues/core/PluginLauncherManager.java | 6 +- .../plugin/SpringPluginHookWrapper.java | 9 +-- .../starblues/spring/SpringPluginHook.java | 10 +-- update.md | 2 +- 8 files changed, 86 insertions(+), 43 deletions(-) create mode 100644 spring-brick/src/main/java/com/gitee/starblues/core/PluginCloseType.java diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java index f2680f8..bd3fdb2 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/DefaultSpringPluginHook.java @@ -23,6 +23,7 @@ import com.gitee.starblues.bootstrap.realize.PluginCloseListener; import com.gitee.starblues.bootstrap.realize.StopValidator; import com.gitee.starblues.bootstrap.utils.DestroyUtils; import com.gitee.starblues.bootstrap.utils.SpringBeanUtils; +import com.gitee.starblues.core.PluginCloseType; import com.gitee.starblues.core.exception.PluginProhibitStopException; import com.gitee.starblues.spring.ApplicationContext; import com.gitee.starblues.spring.ApplicationContextProxy; @@ -77,10 +78,10 @@ public class DefaultSpringPluginHook implements SpringPluginHook { @Override - public void close(boolean isUninstall) throws Exception{ + public void close(PluginCloseType closeType) throws Exception{ try { GenericApplicationContext applicationContext = processorContext.getApplicationContext(); - callPluginCloseListener(applicationContext, isUninstall); + callPluginCloseListener(applicationContext, closeType); pluginProcessor.close(processorContext); applicationContext.close(); processorContext.clearRegistryInfo(); @@ -107,7 +108,7 @@ public class DefaultSpringPluginHook implements SpringPluginHook { return processorContext.getRegistryInfo(PluginThymeleafProcessor.CONFIG_KEY); } - private void callPluginCloseListener(GenericApplicationContext applicationContext, boolean isUninstall){ + private void callPluginCloseListener(GenericApplicationContext applicationContext, PluginCloseType closeType){ List pluginCloseListeners = SpringBeanUtils.getBeans( applicationContext, PluginCloseListener.class); if(pluginCloseListeners.isEmpty()){ @@ -115,7 +116,7 @@ public class DefaultSpringPluginHook implements SpringPluginHook { } for (PluginCloseListener pluginCloseListener : pluginCloseListeners) { try { - pluginCloseListener.close(applicationContext, processorContext.getPluginInfo(), isUninstall); + pluginCloseListener.close(applicationContext, processorContext.getPluginInfo(), closeType); } catch (Exception e){ e.printStackTrace(); } diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/PluginCloseListener.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/PluginCloseListener.java index 5dd8ebf..c151abf 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/PluginCloseListener.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/PluginCloseListener.java @@ -16,6 +16,7 @@ package com.gitee.starblues.bootstrap.realize; +import com.gitee.starblues.core.PluginCloseType; import com.gitee.starblues.core.PluginInfo; import com.gitee.starblues.core.descriptor.PluginDescriptor; import org.springframework.context.support.GenericApplicationContext; @@ -40,11 +41,11 @@ public interface PluginCloseListener { * 关闭时调用 * @param applicationContext 当前插件的ApplicationContext * @param pluginInfo 当前插件信息 - * @param isUninstall 是否为卸载关闭。true: 为卸载, false 不为卸载 + * @param closeType 停止类型 * @since 3.1.0 */ default void close(GenericApplicationContext applicationContext, - PluginInfo pluginInfo, boolean isUninstall){ + PluginInfo pluginInfo, PluginCloseType closeType){ close(pluginInfo != null ? pluginInfo.getPluginDescriptor() : null); } 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 392d847..d7ab7bb 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,8 +49,8 @@ import java.util.stream.Collectors; /** * 抽象的插件管理者 * @author starBlues - * @version 3.1.0 * @since 3.0.0 + * @version 3.1.0 */ public class DefaultPluginManager implements PluginManager{ @@ -263,28 +263,10 @@ public class DefaultPluginManager implements PluginManager{ @Override public synchronized void uninstall(String pluginId) throws PluginException { - Assert.isNotNull(pluginId, "参数pluginId不能为空"); - PluginInsideInfo wrapperInside = getPlugin(pluginId); - if(wrapperInside == null){ - throw new PluginException("没有发现插件: " + pluginId); - } - PluginInfo pluginInfo = wrapperInside.toPluginInfo(); - if(wrapperInside.getPluginState() == PluginState.STARTED){ - try { - stop(wrapperInside, true); - pluginListenerFactory.stopSuccess(pluginInfo); - } catch (Throwable e) { - PluginException pluginException = PluginException.getPluginException(e, - ()-> new PluginException("停止", pluginId, e)); - pluginListenerFactory.stopFailure(pluginInfo, pluginException); - throw pluginException; - } - } - startedPlugins.remove(pluginId); - unLoad(pluginId); - LogUtils.info(log, wrapperInside.getPluginDescriptor(), "卸载成功"); + uninstall(pluginId, PluginCloseType.UNINSTALL); } + @Override public synchronized PluginInfo upgrade(Path pluginPath, boolean unpackPlugin) throws PluginException { Assert.isNotNull(pluginPath, "参数pluginPath不能为空"); @@ -307,7 +289,7 @@ public class DefaultPluginManager implements PluginManager{ checkVersion(oldPlugin.getPluginDescriptor(), upgradePluginDescriptor); if(oldPlugin.getPluginState() == PluginState.STARTED){ // 如果插件被启动, 则卸载旧的插件 - uninstall(pluginId); + uninstall(pluginId, PluginCloseType.UPGRADE_UNINSTALL); } else if(oldPlugin.getPluginState() == PluginState.LOADED){ // 如果插件被load unLoad(pluginId); @@ -357,7 +339,7 @@ public class DefaultPluginManager implements PluginManager{ } PluginInfo pluginInfo = pluginInsideInfo.toPluginInfo(); try { - stop(pluginInsideInfo, false); + stop(pluginInsideInfo, PluginCloseType.STOP); log.info("停止插件[{}]成功", MsgUtils.getPluginUnique(pluginInsideInfo.getPluginDescriptor())); pluginListenerFactory.stopSuccess(pluginInfo); return pluginInfo; @@ -398,6 +380,35 @@ public class DefaultPluginManager implements PluginManager{ return pluginDescriptors; } + /** + * 卸载插件 + * @param pluginId 插件id + * @param closeType 关闭类型 + * @throws PluginException 卸载异常 + */ + protected void uninstall(String pluginId, PluginCloseType closeType) throws PluginException{ + Assert.isNotNull(pluginId, "参数pluginId不能为空"); + PluginInsideInfo wrapperInside = getPlugin(pluginId); + if(wrapperInside == null){ + throw new PluginException("没有发现插件: " + pluginId); + } + PluginInfo pluginInfo = wrapperInside.toPluginInfo(); + if(wrapperInside.getPluginState() == PluginState.STARTED){ + try { + stop(wrapperInside, closeType); + pluginListenerFactory.stopSuccess(pluginInfo); + } catch (Throwable e) { + PluginException pluginException = PluginException.getPluginException(e, + ()-> new PluginException("停止", pluginId, e)); + pluginListenerFactory.stopFailure(pluginInfo, pluginException); + throw pluginException; + } + } + startedPlugins.remove(pluginId); + unLoad(pluginId); + LogUtils.info(log, wrapperInside.getPluginDescriptor(), "卸载成功"); + } + protected PluginInsideInfo loadPlugin(Path pluginPath, boolean resolvePath) { if(resolvePath){ Path sourcePluginPath = pluginPath; @@ -539,10 +550,10 @@ public class DefaultPluginManager implements PluginManager{ /** * 统一停止插件操作 * @param pluginInsideInfo PluginInsideInfo - * @param isUninstall 是否为卸载停止 + * @param closeType 停止类型 * @throws Exception 启动异常 */ - protected void stop(PluginInsideInfo pluginInsideInfo, boolean isUninstall) throws Exception{ + protected void stop(PluginInsideInfo pluginInsideInfo, PluginCloseType closeType) throws Exception{ launcherChecker.checkCanStop(pluginInsideInfo); pluginInsideInfo.setPluginState(PluginState.STOPPED); stopFinish(pluginInsideInfo); diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginCloseType.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginCloseType.java new file mode 100644 index 0000000..0aef5e4 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginCloseType.java @@ -0,0 +1,27 @@ +package com.gitee.starblues.core; + +/** + * 插件关闭类型 + * + * @author starBlues + * @since 3.1.0 + * @version 3.1.0 + */ +public enum PluginCloseType { + + /** + * 直接操作停止 + */ + STOP, + + /** + * 卸载时停止 + */ + UNINSTALL, + + /** + * 升级时停止 + */ + UPGRADE_UNINSTALL + +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java index 69bf900..bb15301 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/PluginLauncherManager.java @@ -120,7 +120,7 @@ public class PluginLauncherManager extends DefaultPluginManager{ } @Override - protected void stop(PluginInsideInfo pluginInsideInfo, boolean isUninstall) throws Exception { + protected void stop(PluginInsideInfo pluginInsideInfo, PluginCloseType closeType) throws Exception { launcherChecker.checkCanStop(pluginInsideInfo); String pluginId = pluginInsideInfo.getPluginId(); RegistryPluginInfo registryPluginInfo = registryInfo.get(pluginId); @@ -130,10 +130,10 @@ public class PluginLauncherManager extends DefaultPluginManager{ try { SpringPluginHook springPluginHook = registryPluginInfo.getSpringPluginHook(); springPluginHook.stopVerify(); - springPluginHook.close(isUninstall); + springPluginHook.close(closeType); invokeSupperCache.remove(pluginId); registryInfo.remove(pluginId); - super.stop(pluginInsideInfo, isUninstall); + super.stop(pluginInsideInfo, closeType); } catch (Exception e){ if(e instanceof PluginProhibitStopException){ // 禁止停止时, 不设置插件状态 diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/SpringPluginHookWrapper.java b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/SpringPluginHookWrapper.java index 3ccdf43..a2c6b95 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/SpringPluginHookWrapper.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/launcher/plugin/SpringPluginHookWrapper.java @@ -16,8 +16,8 @@ package com.gitee.starblues.core.launcher.plugin; +import com.gitee.starblues.core.PluginCloseType; import com.gitee.starblues.core.PluginInsideInfo; -import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.exception.PluginProhibitStopException; import com.gitee.starblues.core.launcher.plugin.involved.PluginLaunchInvolved; import com.gitee.starblues.spring.ApplicationContext; @@ -30,7 +30,8 @@ import lombok.extern.slf4j.Slf4j; /** * SpringPluginHook-Wrapper * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ @Slf4j public class SpringPluginHookWrapper implements SpringPluginHook { @@ -70,14 +71,14 @@ public class SpringPluginHookWrapper implements SpringPluginHook { } @Override - public void close(boolean isUninstall) throws Exception { + public void close(PluginCloseType closeType) throws Exception { try { pluginLaunchInvolved.close(pluginInsideInfo, classLoader); } catch (Exception e){ log.error("关闭插件异常: {}", e.getMessage(), e); } try { - target.close(isUninstall); + target.close(closeType); } finally { ResourceUtils.closeQuietly(classLoader); } diff --git a/spring-brick/src/main/java/com/gitee/starblues/spring/SpringPluginHook.java b/spring-brick/src/main/java/com/gitee/starblues/spring/SpringPluginHook.java index 6cf5c61..54a121d 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/spring/SpringPluginHook.java +++ b/spring-brick/src/main/java/com/gitee/starblues/spring/SpringPluginHook.java @@ -17,13 +17,15 @@ package com.gitee.starblues.spring; +import com.gitee.starblues.core.PluginCloseType; import com.gitee.starblues.core.exception.PluginProhibitStopException; import com.gitee.starblues.spring.web.thymeleaf.ThymeleafConfig; /** * 插件把柄接口 * @author starBlues - * @version 3.0.0 + * @since 3.0.0 + * @version 3.1.0 */ public interface SpringPluginHook { @@ -52,11 +54,11 @@ public interface SpringPluginHook { ThymeleafConfig getThymeleafConfig(); /** - * 卸载调用 - * @param isUninstall 是否是卸载关闭 + * 关闭调用 + * @param closeType 关闭类型 * @since 3.1.0 * @throws Exception 关闭异常 */ - void close(boolean isUninstall) throws Exception; + void close(PluginCloseType closeType) throws Exception; } diff --git a/update.md b/update.md index 87c29f1..6d62995 100644 --- a/update.md +++ b/update.md @@ -1,7 +1,7 @@ 1. 【新增】增加主包MAINIFEST中title和version定义, 标准jar包中包含`Implementation-Version`和`Implementation-Title`属性 2. 【新增】新增根据个人需求选择开发模式,支持隔离式开发模式(目前已有的)、共享式开发模式 3. 【新增】新增可自主实现扩展插件信息 -4. 【新增】新增插件可判断卸载事件 +4. 【新增】新增插件停止类型 3. 【修复】修复插件中`LiveBeansView`注册异常问题 4. 【修复[#I5IFR4](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5IFR3)】 `ExtractFactory#getExtractByCoordinate` 类型转换`Bug` 5. 【修复[#I5GJO9](https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I5GJO9)】`DefaultPluginManager#install` 异常无法抛出 -- Gitee