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 0000000000000000000000000000000000000000..677b0814873f90c07725245fb56419633fae5539 --- /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 3eabf8f00c0993b9376130c4573fec14c7dc13d9..fdbff1037ad6b7cb9ac993c73051f167f67c42af 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 abd8a2e6b74fae988553975271c113e72c63f17b..be89db5e98dba823e62fcb259aa68eb571f7be19 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 3632f2002497f95348c1f742d2a59f116c5baeb1..f76074f291317f050ac5a7fe6b3fee1e5557694c 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 6d4fde7039668eb359549ce154616314b0282a6a..f4b2f970e83dd5c0ba9fd3b08f0c6ad40aeba2d1 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 0000000000000000000000000000000000000000..4949b309d8b8cbcd7064ac43e61e72ae400d1132 --- /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 e9c23b4ba4cfe1fbbd688eff49b746c38cc6c3af..42cec5b55f5ed4a9e55a0ae6f1f09e1cf8140d77 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 fdfc471dd5bb31fb5dd3311bab0de9da5b53edd5..c4b25f9812565cc755be85f1f00c183ea9c3e14d 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 08d7a9a4d51c8b26cdcafa904ffcba3e6e03bf0b..ac3e5551708ae3a7af5e1d49b59e22059d63d2ed 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 4631f5566c84a77a096a8897f266b64dc9652614..94f65c44261b96653694d1cebec20b9fcb495d72 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 84a7733f89ff4f512e7bd856ae00b3d20bad3d89..a1f230d8aa19a8ff40645ec74ddf0a77cd6ee42e 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 509097a89110601d677659e60081fd01f13951fb..06be41d48929aec52ebd22e6c256b1343d5efd4e 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 21635c157931c57f34f6d490e10e584456ef2494..4ee30c562fb09e6dab9af1351efb4d326f80b147 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 bf6d92c71370bbd3e9f46ad11a8b27acebe8f0a8..eb2893b0c48a8a44e724413221c8c32991ab27f5 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 ee020188eebd6c93d2fae4830723ab3e3f7bc70b..8a86ddf0d67e766f6ec4777701f045c41f966dbc 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 790bb6b2558294db7530142357dc703f283276a2..2ce521ea05d9c725342244be5c19d8af92981300 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 bea556fc2a97b494830869d626137db7c898fe4a..9cb11d53539eb428673bbe7e082a3b5c9447e72f 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 b04ba0ab334d2d495b250fe9d3d5163d693034f8..6cbd1f11e4e950c475648d725e33314634496a48 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 b66fe5d1dedabcf8db9547b459b9ef5279c06055..ce11f06dd41fca3c294383f42b69fffe34a5c1b3 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 9e1eeaafaeb3be120a91fa7ec2915eec624c1851..3b9eeb972f16bb9f066b0261c6643a4187fc0f21 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 ec75a2be2f4c36da3da7242aff91c4d688de847a..f4db7cad9e194472b89e3f63145acb79ed931879 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 f8ee0ee39b6335e6ab73e28761ff38d66e8dab15..1640483757c6b36b8abcaac5e15ddc0c1cc4b6d7 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 a32d278c951951c0198ccb293ed2ecffdf78f745..e0d73f293a4f076108426a37b50ae729ea5e7e98 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 3f7bd7362d53942ecdf36f1643071fa3d88d2cec..d9d48f12e21d14abff1504542eff58f391a95ab1 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 0edcdbe8af2d26a7d5c48727dd82808ac70d12ee..af1baad2a344da5296d4ab281f2cac8cd98ada97 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 c91557eb86e53b669842fcb896bd3126bb8fbf9c..11e1c827df0312327657f1d1d0b80cb9a4189059 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 0000000000000000000000000000000000000000..c56b306d54e862173c5b953b844ea896349d87d8 --- /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 5abb417baf0090038b518e851e3a2da97fa1e5f7..467a8498beccb3cf0641608af72f31f3cfa3e17d 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. 【优化】优化依赖资源默认不缓存, 以减少内存