diff --git a/example/basic-example/basic-example-main/pom.xml b/example/basic-example/basic-example-main/pom.xml index 917f4e8730c8ef7750b912d29c6ef93cd55684d0..8ae868c38239e3b2d04c92059b7ece5c03553082 100644 --- a/example/basic-example/basic-example-main/pom.xml +++ b/example/basic-example/basic-example-main/pom.xml @@ -13,13 +13,13 @@ com.gitee.starblues basic-example-main - 2.2.1-RELEASE + 2.2.2-RELEASE jar 2.7.0 1.6 - 2.2.1-RELEASE + 2.2.2-RELEASE diff --git a/example/basic-example/basic-example-runner/pom.xml b/example/basic-example/basic-example-runner/pom.xml index 7935a9739af2bbe2b28ac15605606cc78f76a0f9..f15d476ab480918678ad5df34da2d6ba7a9c5d4d 100644 --- a/example/basic-example/basic-example-runner/pom.xml +++ b/example/basic-example/basic-example-runner/pom.xml @@ -14,7 +14,7 @@ com.gitee.starblues basic-example-runner - 2.2.1-RELEASE + 2.2.2-RELEASE pom diff --git a/example/basic-example/plugins/basic-example-plugin1/plugin.properties b/example/basic-example/plugins/basic-example-plugin1/plugin.properties index 48af35480f6f75a5371ed4352a74ab968ee9abf6..a62fec6bce54866ef729418a5d44723e622e7348 100644 --- a/example/basic-example/plugins/basic-example-plugin1/plugin.properties +++ b/example/basic-example/plugins/basic-example-plugin1/plugin.properties @@ -1,4 +1,4 @@ plugin.id=basic-example-plugin1 plugin.class=com.basic.example.plugin1.DefinePlugin -plugin.version=2.2.1-RELEASE +plugin.version=2.2.2-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/basic-example/plugins/basic-example-plugin1/pom.xml b/example/basic-example/plugins/basic-example-plugin1/pom.xml index 01a085bcb45f05d040b52acb9e7a52705bb98571..d5024f7c155da49415650b63efce368bff6c87de 100644 --- a/example/basic-example/plugins/basic-example-plugin1/pom.xml +++ b/example/basic-example/plugins/basic-example-plugin1/pom.xml @@ -8,12 +8,12 @@ com.gitee.starblues basic-example-plugin-parent - 2.2.1-RELEASE + 2.2.2-RELEASE ../pom.xml basic-example-plugin1 - 2.2.1-RELEASE + 2.2.2-RELEASE jar diff --git a/example/basic-example/plugins/basic-example-plugin2/plugin.properties b/example/basic-example/plugins/basic-example-plugin2/plugin.properties index 7f10c4ba9ab997abbc0759f3d7bb2c397d0f14ca..b1bef3ebfe73bfea4f73fd5fc6636825a2bae08d 100644 --- a/example/basic-example/plugins/basic-example-plugin2/plugin.properties +++ b/example/basic-example/plugins/basic-example-plugin2/plugin.properties @@ -1,4 +1,4 @@ plugin.id=basic-example-plugin2 plugin.class=com.basic.example.plugin2.DefinePlugin -plugin.version=2.2.1-RELEASE +plugin.version=2.2.2-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/basic-example/plugins/basic-example-plugin2/pom.xml b/example/basic-example/plugins/basic-example-plugin2/pom.xml index 4bc0a1a078a4d6920cb49f1a015bc17323b95cac..530c2eb2116bfa6b11a8bf45f33890a99ecd285b 100644 --- a/example/basic-example/plugins/basic-example-plugin2/pom.xml +++ b/example/basic-example/plugins/basic-example-plugin2/pom.xml @@ -8,12 +8,12 @@ com.gitee.starblues basic-example-plugin-parent - 2.2.1-RELEASE + 2.2.2-RELEASE ../pom.xml basic-example-plugin2 - 2.2.1-RELEASE + 2.2.2-RELEASE jar diff --git a/example/basic-example/plugins/pom.xml b/example/basic-example/plugins/pom.xml index 87e6f5f1702ab71246226a89edd100c013fdfe18..bb9d2b37b68c11675ced4f457ce7cfbe625f37ac 100644 --- a/example/basic-example/plugins/pom.xml +++ b/example/basic-example/plugins/pom.xml @@ -7,7 +7,7 @@ com.gitee.starblues basic-example-plugin-parent - 2.2.1-RELEASE + 2.2.2-RELEASE pom diff --git a/example/basic-example/pom.xml b/example/basic-example/pom.xml index a9322adc9a3455b13a31f1839d391ce8fb7eb7fd..39c94007f24198423216fb8f146f18d0a483d766 100644 --- a/example/basic-example/pom.xml +++ b/example/basic-example/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues basic-example - 2.2.1-RELEASE + 2.2.2-RELEASE pom 基本案例 diff --git a/example/integration-mybatis/integration-mybatis-main/pom.xml b/example/integration-mybatis/integration-mybatis-main/pom.xml index 7522e6e6347f67e275f662e72aa72c555559cb0a..f74887412a674cf80e79cb8295a9e99c5fdc5a63 100644 --- a/example/integration-mybatis/integration-mybatis-main/pom.xml +++ b/example/integration-mybatis/integration-mybatis-main/pom.xml @@ -14,14 +14,14 @@ com.gitee.starblues integration-mybatis-main - 2.2.1-RELEASE + 2.2.2-RELEASE jar 主程序模块 - 2.2.1-RELEASE - 2.2.1-RELEASE - 2.2.1-RELEASE + 2.2.2-RELEASE + 2.2.2-RELEASE + 2.2.2-RELEASE 2.0.1 2.7.0 1.6 diff --git a/example/integration-mybatis/integration-mybatis-plugin-parent/pom.xml b/example/integration-mybatis/integration-mybatis-plugin-parent/pom.xml index 43e83ac7c8b1f3726cb519900a0244d09e0f4246..269bb1fb804874b557d8b88213625700ca59479b 100644 --- a/example/integration-mybatis/integration-mybatis-plugin-parent/pom.xml +++ b/example/integration-mybatis/integration-mybatis-plugin-parent/pom.xml @@ -7,7 +7,7 @@ com.gitee.starblues integration-mybatis-plugin-parent - 2.2.1-RELEASE + 2.2.2-RELEASE pom diff --git a/example/integration-mybatis/integration-mybatis-runner/pom.xml b/example/integration-mybatis/integration-mybatis-runner/pom.xml index 5ce252935131d298083e580178c27c3ea3f9fdcb..afa68f0867134a78bd02fe6a8b3aaf652dfa2a42 100644 --- a/example/integration-mybatis/integration-mybatis-runner/pom.xml +++ b/example/integration-mybatis/integration-mybatis-runner/pom.xml @@ -14,7 +14,7 @@ com.gitee.starblues integration-mybatis-runner - 2.2.1-RELEASE + 2.2.2-RELEASE jar 启动程序模块。将启动类配置到该模块下 diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin1/plugin.properties b/example/integration-mybatis/plugins/integration-mybatis-plugin1/plugin.properties index e212961727df2403c0ece71f1d9851b14314fd15..086145a78b317607b74ce5a14fea7a831d218022 100644 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin1/plugin.properties +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin1/plugin.properties @@ -1,4 +1,4 @@ plugin.id=integration-mybatis-plugin1 plugin.class=com.mybatis.plugin1.ExamplePlugin1 -plugin.version=2.2.1-RELEASE +plugin.version=2.2.2-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin1/pom.xml b/example/integration-mybatis/plugins/integration-mybatis-plugin1/pom.xml index f4f6752b8c1018fbdb5a96a1d6521f030fdee408..51ed59e0cf99fcf6177c82646e1feb3be4217958 100644 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin1/pom.xml +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin1/pom.xml @@ -8,12 +8,12 @@ com.gitee.starblues integration-mybatis-plugin-parent - 2.2.1-RELEASE + 2.2.2-RELEASE ../../integration-mybatis-plugin-parent/pom.xml integration-mybatis-plugin1 - 2.2.1-RELEASE + 2.2.2-RELEASE jar diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin2/plugin.properties b/example/integration-mybatis/plugins/integration-mybatis-plugin2/plugin.properties index f6564bc69d7b08868df7587c7b9e7ce0bd7a6950..ae4c430545b6a0171047a0ade978bcc6ce9ac4c5 100644 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin2/plugin.properties +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin2/plugin.properties @@ -1,4 +1,4 @@ plugin.id=integration-mybatis-plugin2 plugin.class=com.mybatis.plugin2.ExamplePlugin2 -plugin.version=2.2.1-RELEASE +plugin.version=2.2.2-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin2/pom.xml b/example/integration-mybatis/plugins/integration-mybatis-plugin2/pom.xml index b935610d63261db6f7fcd2b084138741bac02773..d4580c8c4cd375d37f62bbc5f1bd52ac1ed24f34 100644 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin2/pom.xml +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin2/pom.xml @@ -8,12 +8,12 @@ com.gitee.starblues integration-mybatis-plugin-parent - 2.2.1-RELEASE + 2.2.2-RELEASE ../../integration-mybatis-plugin-parent/pom.xml integration-mybatis-plugin2 - 2.2.1-RELEASE + 2.2.2-RELEASE jar diff --git a/example/integration-mybatis/pom.xml b/example/integration-mybatis/pom.xml index 28407e334e4330b4a25a6dfb592f9f46cb4f6e51..4f25fa28917037e7e4b83a39493b67ff039a3404 100644 --- a/example/integration-mybatis/pom.xml +++ b/example/integration-mybatis/pom.xml @@ -7,7 +7,7 @@ com.gitee.starblues integration-mybatis - 2.2.1-RELEASE + 2.2.2-RELEASE pom 集成mybatis案例 diff --git a/example/integration-mybatisplus/integration-mybatisplus-main/pom.xml b/example/integration-mybatisplus/integration-mybatisplus-main/pom.xml index 62a75eb6dd41930e25550cc18f69e02d1e7e1989..5868bc659c8e31467ac8e8b3a258570106d1e687 100644 --- a/example/integration-mybatisplus/integration-mybatisplus-main/pom.xml +++ b/example/integration-mybatisplus/integration-mybatisplus-main/pom.xml @@ -13,7 +13,7 @@ com.gitee.starblues - 2.2.1-RELEASE + 2.2.2-RELEASE integration-mybatisplus-main jar 集成mybatis-plus 案例--主程序 @@ -27,8 +27,8 @@ 2.0.1 3.2.0 - 2.2.1-RELEASE - 2.2.1-RELEASE + 2.2.2-RELEASE + 2.2.2-RELEASE 2.7.0 1.6 diff --git a/example/integration-mybatisplus/plugins/integration-mybatisplus-plugin/plugin.properties b/example/integration-mybatisplus/plugins/integration-mybatisplus-plugin/plugin.properties index 8641d6820ae28104b59d407a9cd2a40827b06cf4..88aa9fe6e1e1ade077e5adad241f48362834caa8 100644 --- a/example/integration-mybatisplus/plugins/integration-mybatisplus-plugin/plugin.properties +++ b/example/integration-mybatisplus/plugins/integration-mybatisplus-plugin/plugin.properties @@ -1,4 +1,4 @@ plugin.id=integration-mybatisplus-plugin plugin.class=com.mybatisplus.plugin.MybatisPlusPlugin -plugin.version=2.2.1-RELEASE +plugin.version=2.2.2-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/integration-mybatisplus/plugins/integration-mybatisplus-plugin/pom.xml b/example/integration-mybatisplus/plugins/integration-mybatisplus-plugin/pom.xml index 1aa6ef831367a53af2fc320cff111e519c1dafc1..b8cce26281f2791a04f4e3af8af05e313497727c 100644 --- a/example/integration-mybatisplus/plugins/integration-mybatisplus-plugin/pom.xml +++ b/example/integration-mybatisplus/plugins/integration-mybatisplus-plugin/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-mybatisplus-plugin - 2.2.1-RELEASE + 2.2.2-RELEASE jar diff --git a/example/integration-mybatisplus/pom.xml b/example/integration-mybatisplus/pom.xml index a22d0354aea4a596957ec25cbbfe61ddc9d88269..71cdb2429c3b39ea52886c02518465387507dfc3 100644 --- a/example/integration-mybatisplus/pom.xml +++ b/example/integration-mybatisplus/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-mybatisplus - 2.2.1-RELEASE + 2.2.2-RELEASE pom 集成mybatis-plus案例 diff --git a/example/pom.xml b/example/pom.xml index 0df8b75cae7539253c0ad7768d7715937c89f224..1cac07c4283e1ee87ed540c95a70baa12aeb07ab 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues springboot-plugin-framework-example - 2.2.1-RELEASE + 2.2.2-RELEASE pom diff --git a/pom.xml b/pom.xml index 8d0eb1090d61f6a61e2f6705ec83c8d56a15eb81..a03328d3951df541e5a15e79c5b31f850cce2394 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.gitee.starblues springboot-plugin-framework-parent pom - 2.2.1-RELEASE + 2.2.2-RELEASE spring boot 插件开发集成包 diff --git a/springboot-plugin-framework-extension/pom.xml b/springboot-plugin-framework-extension/pom.xml index 20a47ff134b66dbfa8f83629708ab6d04004dffa..c05a6f640a8c5bc517ae26270f343324f0112657 100644 --- a/springboot-plugin-framework-extension/pom.xml +++ b/springboot-plugin-framework-extension/pom.xml @@ -9,7 +9,7 @@ com.gitee.starblues springboot-plugin-framework-extension pom - 2.2.1-RELEASE + 2.2.2-RELEASE spring boot 插件式开发集成包--扩展模块 diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/pom.xml b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/pom.xml index 5e6254362d60551e9e3f02c66cbe3a9388a8be05..49b92e18fca0777ac8defa7ff0c74d0904211ea8 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/pom.xml +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/pom.xml @@ -13,7 +13,7 @@ com.gitee.starblues springboot-plugin-framework-extension-mybatis - 2.2.1-RELEASE + 2.2.2-RELEASE jar 插件扩展-spring boot mybatis 集成扩展 @@ -64,7 +64,7 @@ 3.1.0 1.6 - 2.2.1-RELEASE + 2.2.2-RELEASE 2.0.1 3.2.0 diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/PluginMybatisXmlLoader.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/PluginMybatisXmlLoader.java index 18177aa96197abc9347c4ecfd45e37954d3a86e8..19c56a1358db4da0aa23fa012982181fb7f3918f 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/PluginMybatisXmlLoader.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/PluginMybatisXmlLoader.java @@ -67,7 +67,9 @@ public class PluginMybatisXmlLoader implements PluginResourceLoader { resources.addAll(loadResources); } } - return new ResourceWrapper(resources); + ResourceWrapper resourceWrapper = new ResourceWrapper(); + resourceWrapper.addResources(resources); + return resourceWrapper; } @Override diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/pom.xml b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/pom.xml index 1fda0a1a605d2f6395d0bb56b936e63f6a243dd1..b2c77d02b1b3035dd7189f49f5aa7c400ed04269 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/pom.xml +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/pom.xml @@ -13,7 +13,7 @@ com.gitee.starblues springboot-plugin-framework-extension-resources - 2.2.1-RELEASE + 2.2.2-RELEASE jar 插件扩展-通过url读取插件中的静态资源 @@ -69,7 +69,7 @@ 5.0.7.RELEASE 4.0.1 - 2.2.1-RELEASE + 2.2.2-RELEASE 2.1.1.RELEASE diff --git a/springboot-plugin-framework/pom.xml b/springboot-plugin-framework/pom.xml index 2b9024f41419fc99e9811efac6e054e161a20c0b..4d2d893d062ddc783448aa536e9f87bdfd6c6d57 100644 --- a/springboot-plugin-framework/pom.xml +++ b/springboot-plugin-framework/pom.xml @@ -13,7 +13,7 @@ com.gitee.starblues springboot-plugin-framework jar - 2.2.1-RELEASE + 2.2.2-RELEASE spring boot 插件式开发集成包 @@ -65,7 +65,7 @@ 1.6 3.1.0 - 2.9.9 + 2.10.1 5.0.7.RELEASE 4.0.1 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/DefaultPluginFactory.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/DefaultPluginFactory.java index ac46f812a5416cf0f370af2eb0e215d8d34ed522..e573e5bbd2d628afd7c2ed3bed98269626032cd7 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/DefaultPluginFactory.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/DefaultPluginFactory.java @@ -20,7 +20,7 @@ import java.util.Map; * 默认的插件处理者 * * @author zhangzhuo - * @version 2.1.0 + * @version 2.2.2 */ public class DefaultPluginFactory implements PluginFactory { @@ -71,8 +71,8 @@ public class DefaultPluginFactory implements PluginFactory { throw new IllegalArgumentException("Parameter:pluginWrapper cannot be null"); } if(registerPluginInfoMap.containsKey(pluginWrapper.getPluginId())){ - throw new IllegalAccessException("The plugin " - + pluginWrapper.getPluginId() +"already exists, Can't register"); + throw new IllegalAccessException("The plugin '" + + pluginWrapper.getPluginId() +"' already exists, Can't register"); } if(!buildContainer.isEmpty() && buildType == 2){ throw new IllegalAccessException("Unable to Registry operate. Because there's no build"); @@ -97,7 +97,7 @@ public class DefaultPluginFactory implements PluginFactory { public synchronized PluginFactory unRegistry(String pluginId) throws Exception { PluginRegistryInfo registerPluginInfo = registerPluginInfoMap.get(pluginId); if(registerPluginInfo == null){ - throw new Exception("Not found plugin " + pluginId + " registered"); + throw new Exception("Not found plugin '" + pluginId + "' registered"); } if(!buildContainer.isEmpty() && buildType == 1){ throw new Exception("Unable to UnRegistry operate. Because there's no build"); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginPipeProcessorFactory.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginPipeProcessorFactory.java index e5298087f6e462fc67e03f03e08eb8e59b6f6eee..f82d98b9483c65241b38b8ffb10fd3e49dc1613b 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginPipeProcessorFactory.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginPipeProcessorFactory.java @@ -4,6 +4,7 @@ import com.gitee.starblues.extension.ExtensionInitializer; import com.gitee.starblues.factory.PluginRegistryInfo; import com.gitee.starblues.factory.process.pipe.bean.BasicBeanProcessor; import com.gitee.starblues.factory.process.pipe.bean.ConfigBeanProcessor; +import com.gitee.starblues.factory.process.pipe.bean.ConfigFileBeanProcessor; import com.gitee.starblues.factory.process.pipe.bean.OneselfListenerStopEventProcessor; import com.gitee.starblues.factory.process.pipe.classs.PluginClassProcess; import org.slf4j.Logger; @@ -17,7 +18,7 @@ import java.util.List; * 插件的pipe处理者工厂 * * @author zhangzhuo - * @version 2.1.0 + * @version 2.2.2 */ public class PluginPipeProcessorFactory implements PluginPipeProcessor { @@ -37,8 +38,11 @@ public class PluginPipeProcessorFactory implements PluginPipeProcessor { // OneselfListenerStopEventProcessor 触发停止事件, 必须第一个执行 pluginPipeProcessors.add(new OneselfListenerStopEventProcessor(applicationContext)); pluginPipeProcessors.add(new PluginClassProcess()); - pluginPipeProcessors.add(new BasicBeanProcessor(applicationContext)); + // 配置文件在所有bean中第一个初始化。 + pluginPipeProcessors.add(new ConfigFileBeanProcessor(applicationContext)); + // 接下来初始化插件中配置bean的初始化 pluginPipeProcessors.add(new ConfigBeanProcessor(applicationContext)); + pluginPipeProcessors.add(new BasicBeanProcessor(applicationContext)); // 添加扩展 pluginPipeProcessors.addAll(ExtensionInitializer.getPipeProcessorExtends()); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigBeanProcessor.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigBeanProcessor.java index e141c93e41fa1fe0f9d59e7468d0a39b54bc0397..802b30d04bc13a742a472bd9c7e3c19b657c607c 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigBeanProcessor.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigBeanProcessor.java @@ -1,42 +1,33 @@ package com.gitee.starblues.factory.process.pipe.bean; -import com.gitee.starblues.annotation.ConfigDefinition; -import com.gitee.starblues.factory.PluginInfoContainer; import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.SpringBeanRegister; import com.gitee.starblues.factory.process.pipe.PluginPipeProcessor; -import com.gitee.starblues.factory.process.pipe.bean.configuration.ConfigurationParser; -import com.gitee.starblues.factory.process.pipe.bean.configuration.PluginConfigDefinition; -import com.gitee.starblues.factory.process.pipe.bean.configuration.YamlConfigurationParser; -import com.gitee.starblues.factory.process.pipe.classs.group.ConfigDefinitionGroup; -import com.gitee.starblues.integration.IntegrationConfiguration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import com.gitee.starblues.factory.process.pipe.classs.group.ConfigBeanGroup; +import com.gitee.starblues.realize.ConfigBean; import org.springframework.context.ApplicationContext; -import org.springframework.util.StringUtils; import java.util.HashSet; import java.util.List; import java.util.Set; /** - * 插件Controller bean注册者 + * 插件中实现 ConfigBean 接口的的处理者 + * @see ConfigBean + * * @author zhangzhuo - * @version 2.1.0 + * @version 2.2.2 */ public class ConfigBeanProcessor implements PluginPipeProcessor { private static final String KEY = "ConfigBeanProcessor"; - private final ConfigurationParser configurationParser; - private final DefaultListableBeanFactory defaultListableBeanFactory; + private final SpringBeanRegister springBeanRegister; + private final ApplicationContext applicationContext; - public ConfigBeanProcessor(ApplicationContext mainApplicationContext) { - IntegrationConfiguration integrationConfiguration = - mainApplicationContext.getBean(IntegrationConfiguration.class); - this.configurationParser = new YamlConfigurationParser(integrationConfiguration); - this.defaultListableBeanFactory = (DefaultListableBeanFactory) - mainApplicationContext.getAutowireCapableBeanFactory(); + public ConfigBeanProcessor(ApplicationContext applicationContext) { + this.springBeanRegister = new SpringBeanRegister(applicationContext); + this.applicationContext = applicationContext; } @@ -47,18 +38,22 @@ public class ConfigBeanProcessor implements PluginPipeProcessor { @Override public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception { - List> configDefinitions = - pluginRegistryInfo.getGroupClasses(ConfigDefinitionGroup.GROUP_ID); - if(configDefinitions == null || configDefinitions.isEmpty()){ + List> configBeans = + pluginRegistryInfo.getGroupClasses(ConfigBeanGroup.GROUP_ID); + if(configBeans == null || configBeans.isEmpty()){ return; } String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); Set beanNames = new HashSet<>(); - for (Class aClass : configDefinitions) { - String beanName = registry(pluginRegistryInfo, aClass); - if(!StringUtils.isEmpty(beanName)){ - beanNames.add(beanName); - PluginInfoContainer.addRegisterBeanName(pluginId, beanName); + for (Class aClass : configBeans) { + if(aClass == null){ + continue; + } + String name = springBeanRegister.register(pluginId, aClass); + beanNames.add(name); + Object bean = applicationContext.getBean(name); + if(bean instanceof ConfigBean){ + ((ConfigBean)bean).initialize(); } } pluginRegistryInfo.addProcessorInfo(KEY, beanNames); @@ -70,43 +65,13 @@ public class ConfigBeanProcessor implements PluginPipeProcessor { if(beanNames == null){ return; } - String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); for (String beanName : beanNames) { - if(defaultListableBeanFactory.containsSingleton(beanName)){ - defaultListableBeanFactory.destroySingleton(beanName); - PluginInfoContainer.removeRegisterBeanName(pluginId, beanName); + Object bean = applicationContext.getBean(beanName); + if(bean instanceof ConfigBean){ + ((ConfigBean)bean).destroy(); } } } - /** - * 注册配置文件 - * @param pluginRegistryInfo 插件注册的信息 - * @param aClass 配置文件类 - * @return 注册的bean名称 - * @throws Exception Exception - */ - private String registry(PluginRegistryInfo pluginRegistryInfo, Class aClass) throws Exception{ - ConfigDefinition configDefinition = aClass.getAnnotation(ConfigDefinition.class); - if(configDefinition == null){ - return null; - } - String fileName = configDefinition.value(); - if(StringUtils.isEmpty(fileName)){ - throw new IllegalArgumentException(aClass.getName() + " configDefinition value is null"); - } - PluginConfigDefinition pluginConfigDefinition = - new PluginConfigDefinition(fileName, aClass); - Object parseObject = configurationParser.parse(pluginRegistryInfo.getBasePlugin(), - pluginConfigDefinition); - String name = configDefinition.name(); - if(StringUtils.isEmpty(name)){ - name = aClass.getName(); - } - if(!defaultListableBeanFactory.containsSingleton(name)){ - defaultListableBeanFactory.registerSingleton(name, parseObject); - } - return name; - } } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigFileBeanProcessor.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigFileBeanProcessor.java new file mode 100644 index 0000000000000000000000000000000000000000..ca34eea61af302d65871bf88b734c1f2678ce214 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigFileBeanProcessor.java @@ -0,0 +1,110 @@ +package com.gitee.starblues.factory.process.pipe.bean; + +import com.gitee.starblues.annotation.ConfigDefinition; +import com.gitee.starblues.factory.PluginInfoContainer; +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.process.pipe.PluginPipeProcessor; +import com.gitee.starblues.factory.process.pipe.bean.configuration.ConfigurationParser; +import com.gitee.starblues.factory.process.pipe.bean.configuration.PluginConfigDefinition; +import com.gitee.starblues.factory.process.pipe.bean.configuration.YamlConfigurationParser; +import com.gitee.starblues.factory.process.pipe.classs.group.ConfigDefinitionGroup; +import com.gitee.starblues.integration.IntegrationConfiguration; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.util.StringUtils; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * 插件中配置文件 bean 的处理者。包括配置文件 + * @author zhangzhuo + * @version 2.2.2 + */ +public class ConfigFileBeanProcessor implements PluginPipeProcessor { + + private static final String KEY = "ConfigFileBeanProcessor"; + + private final ConfigurationParser configurationParser; + private final DefaultListableBeanFactory defaultListableBeanFactory; + + public ConfigFileBeanProcessor(ApplicationContext mainApplicationContext) { + IntegrationConfiguration integrationConfiguration = + mainApplicationContext.getBean(IntegrationConfiguration.class); + this.configurationParser = new YamlConfigurationParser(integrationConfiguration); + this.defaultListableBeanFactory = (DefaultListableBeanFactory) + mainApplicationContext.getAutowireCapableBeanFactory(); + } + + + @Override + public void initialize() throws Exception { + + } + + @Override + public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + List> configDefinitions = + pluginRegistryInfo.getGroupClasses(ConfigDefinitionGroup.GROUP_ID); + if(configDefinitions == null || configDefinitions.isEmpty()){ + return; + } + String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); + Set beanNames = new HashSet<>(); + for (Class aClass : configDefinitions) { + String beanName = registry(pluginRegistryInfo, aClass); + if(!StringUtils.isEmpty(beanName)){ + beanNames.add(beanName); + PluginInfoContainer.addRegisterBeanName(pluginId, beanName); + } + } + pluginRegistryInfo.addProcessorInfo(KEY, beanNames); + } + + @Override + public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + Set beanNames = pluginRegistryInfo.getProcessorInfo(KEY); + if(beanNames == null){ + return; + } + String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); + for (String beanName : beanNames) { + if(defaultListableBeanFactory.containsSingleton(beanName)){ + defaultListableBeanFactory.destroySingleton(beanName); + PluginInfoContainer.removeRegisterBeanName(pluginId, beanName); + } + } + } + + /** + * 注册配置文件 + * @param pluginRegistryInfo 插件注册的信息 + * @param aClass 配置文件类 + * @return 注册的bean名称 + * @throws Exception Exception + */ + private String registry(PluginRegistryInfo pluginRegistryInfo, Class aClass) throws Exception{ + ConfigDefinition configDefinition = aClass.getAnnotation(ConfigDefinition.class); + if(configDefinition == null){ + return null; + } + String fileName = configDefinition.value(); + if(StringUtils.isEmpty(fileName)){ + throw new IllegalArgumentException(aClass.getName() + " configDefinition value is null"); + } + PluginConfigDefinition pluginConfigDefinition = + new PluginConfigDefinition(fileName, aClass); + Object parseObject = configurationParser.parse(pluginRegistryInfo.getBasePlugin(), + pluginConfigDefinition); + String name = configDefinition.name(); + if(StringUtils.isEmpty(name)){ + name = aClass.getName(); + } + if(!defaultListableBeanFactory.containsSingleton(name)){ + defaultListableBeanFactory.registerSingleton(name, parseObject); + } + return name; + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/AbstractConfigurationParser.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/AbstractConfigurationParser.java index c82c8fa973920278bdbc74be10355a5fb5c65a7a..6e45f3201122580a6b9f3a27430967b381586c44 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/AbstractConfigurationParser.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/AbstractConfigurationParser.java @@ -37,12 +37,11 @@ public abstract class AbstractConfigurationParser implements ConfigurationParser "fileName can not be empty"); } - PluginResourceLoader pluginResourceLoader = new PluginConfigFileLoader( + PluginResourceLoader resourceLoader = new PluginConfigFileLoader( configuration.pluginConfigFilePath(), - fileName, - configuration.environment() + fileName ); - ResourceWrapper resourceWrapper = pluginResourceLoader.load(basePlugin); + ResourceWrapper resourceWrapper = resourceLoader.load(basePlugin); if(resourceWrapper == null){ return null; } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/YamlConfigurationParser.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/YamlConfigurationParser.java index 6920e34caecd1ff135c42b20d7c2907ad3b60ee7..9405c4e90bf1b1e27989f1a97407eb05b1a56699 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/YamlConfigurationParser.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/YamlConfigurationParser.java @@ -26,7 +26,7 @@ public class YamlConfigurationParser extends AbstractConfigurationParser { super(configuration); this.yamlFactory = new YAMLFactory(); this.objectMapper = new ObjectMapper(); - objectMapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } @@ -35,7 +35,7 @@ public class YamlConfigurationParser extends AbstractConfigurationParser { throws Exception{ InputStream inputStream = null; try { - inputStream = new FileInputStream(resource.getFile()); + inputStream = resource.getInputStream(); YAMLParser yamlParser = yamlFactory.createParser(inputStream); final JsonNode node = objectMapper.readTree(yamlParser); if(node == null){ diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/PluginClassProcess.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/PluginClassProcess.java index da4a4fd830c542d208a4f58ce4bc087055ad656f..4e35d944ddd548bf493222667e21434974243ff0 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/PluginClassProcess.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/PluginClassProcess.java @@ -1,6 +1,5 @@ package com.gitee.starblues.factory.process.pipe.classs; -import com.gitee.starblues.extension.ExtensionFactory; import com.gitee.starblues.extension.ExtensionInitializer; import com.gitee.starblues.factory.PluginRegistryInfo; import com.gitee.starblues.factory.process.pipe.PluginPipeProcessor; @@ -11,19 +10,18 @@ import com.gitee.starblues.loader.load.PluginClassLoader; import com.gitee.starblues.realize.BasePlugin; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.context.ApplicationContext; import org.springframework.core.io.Resource; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; +import java.util.Set; /** * 插件类加载处理者 * * @author zhangzhuo - * @version 2.1.0 + * @version 2.2.2 */ public class PluginClassProcess implements PluginPipeProcessor { @@ -49,6 +47,7 @@ public class PluginClassProcess implements PluginPipeProcessor { pluginClassGroups.add(new RepositoryGroup()); pluginClassGroups.add(new ConfigurationGroup()); pluginClassGroups.add(new ConfigDefinitionGroup()); + pluginClassGroups.add(new ConfigBeanGroup()); pluginClassGroups.add(new SupplierGroup()); pluginClassGroups.add(new CallerGroup()); pluginClassGroups.add(new OneselfListenerGroup()); @@ -77,12 +76,9 @@ public class PluginClassProcess implements PluginPipeProcessor { e.getMessage(), e); } } - for (Resource pluginResource : pluginResources) { - String path = pluginResource.getURL().getPath(); - String packageName = path.substring(0, path.indexOf(".class")) - .replace("/", "."); - packageName = packageName.substring(packageName.indexOf(basePlugin.scanPackage())); - Class aClass = Class.forName(packageName, false, + Set classPackageNames = resourceWrapper.getClassPackageNames(); + for (String classPackageName : classPackageNames) { + Class aClass = Class.forName(classPackageName, false, basePlugin.getWrapper().getPluginClassLoader()); if(aClass == null){ continue; diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ConfigBeanGroup.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ConfigBeanGroup.java new file mode 100644 index 0000000000000000000000000000000000000000..32a635094169f300d7225630ac53532738fa8faf --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ConfigBeanGroup.java @@ -0,0 +1,42 @@ +package com.gitee.starblues.factory.process.pipe.classs.group; + +import com.gitee.starblues.factory.process.pipe.classs.PluginClassGroup; +import com.gitee.starblues.realize.BasePlugin; +import com.gitee.starblues.realize.ConfigBean; +import org.springframework.util.ClassUtils; + +import java.util.Set; + +/** + * 对接口ConfigBean实现的类分组 + * @see ConfigBean + * + * @author zhangzhuo + * @version 2.2.2 + */ +public class ConfigBeanGroup implements PluginClassGroup { + + + public static final String GROUP_ID = "config_bean"; + + + @Override + public String groupId() { + return GROUP_ID; + } + + @Override + public void initialize(BasePlugin basePlugin) { + + } + + @Override + public boolean filter(Class aClass) { + if(aClass == null){ + return false; + } + Set> allInterfacesForClassAsSet = ClassUtils.getAllInterfacesForClassAsSet(aClass); + return allInterfacesForClassAsSet.contains(ConfigBean.class); + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java index dcba0712e210ef5350ac86d34e3b26474e6e334a..217410724c9da62ee7785120c785ee6944d6eaab 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java @@ -22,8 +22,8 @@ public class AutoPluginApplication extends DefaultPluginApplication super(); } - public AutoPluginApplication(Pf4jFactory integrationFactory) { - super(integrationFactory); + public AutoPluginApplication(Pf4jFactory pf4jFactory) { + super(pf4jFactory); } /** diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java index 882c753c522b7059b5eb65ff2b781c173a9f1c4a..12e29a518ec8df4a784ee416a3f376b3c1a2b293 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java @@ -1,6 +1,6 @@ package com.gitee.starblues.integration.application; -import com.gitee.starblues.integration.pf4j.DefaultPf4JFactory; +import com.gitee.starblues.integration.pf4j.DefaultPf4jFactory; import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.integration.pf4j.Pf4jFactory; import com.gitee.starblues.integration.listener.PluginInitializerListener; @@ -24,9 +24,10 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class DefaultPluginApplication extends AbstractPluginApplication { - private final Logger log = LoggerFactory.getLogger(DefaultPluginApplication.class); + private final Logger log = LoggerFactory.getLogger(this.getClass()); + + protected Pf4jFactory integrationFactory; - private Pf4jFactory integrationFactory; private PluginUser pluginUser; private PluginOperator pluginOperator; @@ -49,16 +50,11 @@ public class DefaultPluginApplication extends AbstractPluginApplication { } IntegrationConfiguration configuration = getConfiguration(applicationContext); if(integrationFactory == null){ - integrationFactory = new DefaultPf4JFactory(configuration); + integrationFactory = new DefaultPf4jFactory(configuration); } PluginManager pluginManager = integrationFactory.getPluginManager(); - pluginUser = new DefaultPluginUser(applicationContext, pluginManager); - pluginOperator = new DefaultPluginOperator( - applicationContext, - configuration, - pluginManager, - this.listenerFactory - ); + pluginUser = createPluginUser(applicationContext, pluginManager); + pluginOperator = createPluginOperator(applicationContext, pluginManager, configuration); try { pluginOperator.initPlugins(listener); beInitialized.set(true); @@ -67,6 +63,35 @@ public class DefaultPluginApplication extends AbstractPluginApplication { } } + /** + * 创建插件使用者。子类可扩展 + * @param applicationContext Spring ApplicationContext + * @param pluginManager 插件管理器 + * @return PluginUser + */ + protected PluginUser createPluginUser(ApplicationContext applicationContext, + PluginManager pluginManager){ + return new DefaultPluginUser(applicationContext, pluginManager); + } + + /** + * 创建插件操作者。子类可扩展 + * @param applicationContext Spring ApplicationContext + * @param pluginManager 插件管理器 + * @param configuration 当前集成的配置 + * @return PluginOperator + */ + protected PluginOperator createPluginOperator(ApplicationContext applicationContext, + PluginManager pluginManager, + IntegrationConfiguration configuration){ + return new DefaultPluginOperator( + applicationContext, + configuration, + pluginManager, + this.listenerFactory + ); + } + @Override public PluginOperator getPluginOperator() { diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java index 3b71042f87fe90ff5fb4241c43e7280b2c33f187..54a008a003956e1b2a1961a50b3c6af22e0d51fc 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/DefaultPluginOperator.java @@ -6,7 +6,7 @@ import com.gitee.starblues.integration.listener.PluginInitializerListenerFactory import com.gitee.starblues.integration.listener.PluginListenerFactory; import com.gitee.starblues.integration.operator.module.PluginInfo; import com.gitee.starblues.integration.operator.verify.PluginLegalVerify; -import com.gitee.starblues.integration.operator.verify.PluginUploadVerify; +import com.gitee.starblues.integration.operator.verify.DefaultPluginVerify; import com.gitee.starblues.factory.DefaultPluginFactory; import com.gitee.starblues.factory.PluginFactory; import com.gitee.starblues.utils.GlobalRegistryInfo; @@ -34,12 +34,12 @@ import java.util.stream.Collectors; /** * 默认的插件操作者 * @author zhangzhuo - * @version 2.2.0 + * @version 2.2.2 */ public class DefaultPluginOperator implements PluginOperator { private boolean isInit = false; - private final Logger log = LoggerFactory.getLogger(this.getClass()); + protected final Logger log = LoggerFactory.getLogger(this.getClass()); private final static DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); @@ -48,8 +48,8 @@ public class DefaultPluginOperator implements PluginOperator { protected final PluginManager pluginManager; protected final PluginFactory pluginFactory; protected final PluginInitializerListenerFactory pluginInitializerListenerFactory; - protected final PluginDescriptorFinder pluginDescriptorFinder; - protected final PluginLegalVerify uploadPluginVerify; + + protected PluginLegalVerify pluginLegalVerify; public DefaultPluginOperator(ApplicationContext applicationContext, @@ -63,10 +63,18 @@ public class DefaultPluginOperator implements PluginOperator { this.pluginFactory = new DefaultPluginFactory(applicationContext, pluginListenerFactory); this.pluginInitializerListenerFactory = new PluginInitializerListenerFactory(applicationContext); - this.pluginDescriptorFinder = new ManifestPluginDescriptorFinder(); - this.uploadPluginVerify = new PluginUploadVerify(this.pluginDescriptorFinder, pluginManager); + this.pluginLegalVerify = new DefaultPluginVerify(pluginManager); } + /** + * 设置插件校验器 + * @param uploadPluginVerify uploadPluginVerify + */ + public void setUploadPluginVerify(PluginLegalVerify uploadPluginVerify) { + if(uploadPluginVerify != null){ + this.pluginLegalVerify = uploadPluginVerify; + } + } @Override public synchronized boolean initPlugins(PluginInitializerListener pluginInitializerListener) throws Exception { @@ -105,13 +113,13 @@ public class DefaultPluginOperator implements PluginOperator { } } pluginFactory.build(); + isInit = true; if(isFoundException){ log.error("Plugins initialize failure"); return false; } else { log.info("Plugins initialize success"); pluginInitializerListenerFactory.complete(); - isInit = true; return true; } } catch (Exception e){ @@ -132,7 +140,7 @@ public class DefaultPluginOperator implements PluginOperator { throw new FileNotFoundException("Not found this path " + path); } // 校验插件文件 - uploadPluginVerify.verify(path); + pluginLegalVerify.verify(path); Path pluginsRoot = pluginManager.getPluginsRoot(); if(path.getParent().compareTo(pluginsRoot) == 0){ // 说明该插件文件存在于插件root目录下。直接加载该插件 @@ -146,6 +154,7 @@ public class DefaultPluginOperator implements PluginOperator { // 如果存在该文件, 则移动备份 backup(targetPath, "install-backup", 1); } + PluginFileUtils.createExistFile(targetPath); Files.copy(path, targetPath, StandardCopyOption.REPLACE_EXISTING); pluginId = pluginManager.loadPlugin(targetPath); } @@ -163,18 +172,19 @@ public class DefaultPluginOperator implements PluginOperator { } } catch (Exception e){ // 说明load成功, 但是没有启动成功, 则卸载该插件 - log.error("Plugin '{}' install failure. {}", pluginId, e.getMessage()); - log.info("Start uninstall plugin '{}' failure", pluginId); - try { - if(!StringUtils.isEmpty(pluginId)){ + if(!StringUtils.isEmpty(pluginId)){ + log.error("Plugin '{}' install failure. {}", pluginId, e.getMessage()); + log.info("Start uninstall plugin '{}' failure", pluginId); + try { uninstall(pluginId, false); + } catch (Exception uninstallException){ + log.error("Plugin '{}' uninstall failure. {}", pluginId, uninstallException.getMessage()); } - } catch (Exception uninstallException){ - log.error("Plugin '{}' uninstall failure. {}", pluginId, e.getMessage()); } + throw e; } finally { - if(pluginId != null){ + if(!StringUtils.isEmpty(pluginId)){ GlobalRegistryInfo.setOperatorPluginInfo(pluginId, false); } } @@ -189,6 +199,9 @@ public class DefaultPluginOperator implements PluginOperator { if(pluginWrapper == null){ throw new Exception("Plugin uninstall failure, Not found plugin '" + pluginId + "'"); } + if(pluginWrapper.getPluginState() != PluginState.STARTED){ + throw new Exception("This plugin '" + pluginId + "' is not started"); + } Exception exception = null; try { pluginFactory.unRegistry(pluginId); @@ -236,7 +249,7 @@ public class DefaultPluginOperator implements PluginOperator { } try { PluginState pluginState = pluginManager.startPlugin(pluginId); - if(pluginState == PluginState.STARTED){ + if(pluginState != null && pluginState == PluginState.STARTED){ GlobalRegistryInfo.addOperatorPluginInfo(pluginId, PluginOperatorInfo.OperatorType.START, false); pluginFactory.registry(pluginWrapper); pluginFactory.build(); @@ -407,7 +420,7 @@ public class DefaultPluginOperator implements PluginOperator { * @return 返回上传的插件路径 * @throws Exception 异常信息 */ - private Path uploadPlugin(MultipartFile pluginFile) throws Exception { + protected Path uploadPlugin(MultipartFile pluginFile) throws Exception { if(pluginFile == null){ throw new IllegalArgumentException("Method:uploadPlugin param 'pluginFile' can not be null"); } @@ -425,7 +438,7 @@ public class DefaultPluginOperator implements PluginOperator { Path tempPath = PluginFileUtils.createExistFile(Paths.get(tempPathString)); Files.write(tempPath, pluginFile.getBytes()); try { - Path verifyPath = uploadPluginVerify.verify(tempPath); + Path verifyPath = pluginLegalVerify.verify(tempPath); if(verifyPath != null){ String targetPathString = pluginManager.getPluginsRoot().toString() + File.separator + fileName; @@ -459,7 +472,7 @@ public class DefaultPluginOperator implements PluginOperator { * @return PluginWrapper * @throws Exception 插件装配异常 */ - private PluginWrapper getPluginWrapper(String pluginId, String errorMsg) throws Exception { + protected PluginWrapper getPluginWrapper(String pluginId, String errorMsg) throws Exception { PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId); if (pluginWrapper == null) { throw new Exception(errorMsg + " -> Not found plugin " + pluginId); @@ -475,7 +488,7 @@ public class DefaultPluginOperator implements PluginOperator { * @param e 异常信息 * @throws Exception Exception */ - private void verifyFailureDelete(Path tempPluginFile, Exception e) throws Exception { + protected void verifyFailureDelete(Path tempPluginFile, Exception e) throws Exception { try { Files.deleteIfExists(tempPluginFile); }catch (IOException e1){ @@ -490,7 +503,7 @@ public class DefaultPluginOperator implements PluginOperator { * @param type 类型 1移动 2拷贝 * @return 结果 */ - private boolean backup(Path sourcePath, String sign, int type) { + protected boolean backup(Path sourcePath, String sign, int type) { try { if(isDev()){ // 如果是开发环境, 则不进行备份 @@ -536,7 +549,7 @@ public class DefaultPluginOperator implements PluginOperator { * 获取现在的时间 * @return String */ - private String getNowTimeByFormat(){ + protected String getNowTimeByFormat(){ LocalDateTime localDateTime = LocalDateTime.now(); return FORMAT.format(localDateTime); } @@ -545,7 +558,7 @@ public class DefaultPluginOperator implements PluginOperator { * 是否是开发环境 * @return bolean */ - private boolean isDev(){ + protected boolean isDev(){ return integrationConfiguration.environment() == RuntimeMode.DEVELOPMENT; } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/PluginUploadVerify.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/DefaultPluginVerify.java similarity index 78% rename from springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/PluginUploadVerify.java rename to springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/DefaultPluginVerify.java index 7f3ea59461363580009b491128e9408a3993b963..e156964bc01e634aea53eaacb316499f84f864ce 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/PluginUploadVerify.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/DefaultPluginVerify.java @@ -6,17 +6,18 @@ import java.nio.file.Path; import java.util.Objects; /** - * 校验上传的插件包 + * 默认的插件校验器 * @author zhangzhuo - * @version 1.0 + * @version 2.2.2 */ -public class PluginUploadVerify extends PluginLegalVerify{ +public class DefaultPluginVerify extends PluginLegalVerify{ private final PluginManager pluginManager; - public PluginUploadVerify(PluginDescriptorFinder pluginDescriptorFinder, - PluginManager pluginManager) { - super(pluginDescriptorFinder); + public DefaultPluginVerify(PluginManager pluginManager) { + super(new CompoundPluginDescriptorFinder() + .add(new ManifestPluginDescriptorFinder()) + .add(new PropertiesPluginDescriptorFinder())); Objects.requireNonNull(pluginManager); this.pluginManager = pluginManager; } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/PluginVerify.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/PluginVerify.java index fcd967fc6898ceafb74ee95534bcd1d59ef65698..be0a89edac31972ad57e49bb9ccf74ba07b64c53 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/PluginVerify.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/PluginVerify.java @@ -7,7 +7,7 @@ import java.nio.file.Path; * 插件合法校验接口 * @author zhangzhuo * @version 1.0 - * @see PluginUploadVerify + * @see DefaultPluginVerify * @see PluginLegalVerify */ public interface PluginVerify { diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/DefaultPf4JFactory.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/DefaultPf4jFactory.java similarity index 83% rename from springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/DefaultPf4JFactory.java rename to springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/DefaultPf4jFactory.java index e52cf7cff58d0197947e0e4bb2b01717fdf8d2f4..6c11cfb9a46ece2c6fa524caf3bf76768e32bf4f 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/DefaultPf4JFactory.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/DefaultPf4jFactory.java @@ -12,11 +12,11 @@ import java.util.Objects; * @author zhangzhuo * @version 2.2.0 */ -public class DefaultPf4JFactory implements Pf4jFactory { +public class DefaultPf4jFactory implements Pf4jFactory { private final IntegrationConfiguration configuration; - public DefaultPf4JFactory(IntegrationConfiguration configuration) { + public DefaultPf4jFactory(IntegrationConfiguration configuration) { this.configuration = configuration; } @@ -39,6 +39,13 @@ public class DefaultPf4JFactory implements Pf4jFactory { System.setProperty("pf4j.mode", RuntimeMode.DEVELOPMENT.toString()); return RuntimeMode.DEVELOPMENT; } + + @Override + protected PluginDescriptorFinder createPluginDescriptorFinder() { + return new CompoundPluginDescriptorFinder() + .add(new ResolvePropertiesPluginDescriptorFinder()) + .add(new ManifestPluginDescriptorFinder()); + } }; } else if(RuntimeMode.DEPLOYMENT == environment){ // 运行环境下的插件管理者 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/ResolvePropertiesPluginDescriptorFinder.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/ResolvePropertiesPluginDescriptorFinder.java new file mode 100644 index 0000000000000000000000000000000000000000..23ca6339c92fafaaec29ff47b79b3c7fcfdeb091 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/ResolvePropertiesPluginDescriptorFinder.java @@ -0,0 +1,46 @@ +package com.gitee.starblues.integration.pf4j; + +import org.pf4j.PluginRuntimeException; +import org.pf4j.PropertiesPluginDescriptorFinder; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +/** + * description + * + * @author zhangzhuo + * @version 1.0 + */ +class ResolvePropertiesPluginDescriptorFinder extends PropertiesPluginDescriptorFinder { + + + @Override + protected Properties readProperties(Path pluginPath) { + Path propertiesPath = getPropertiesPath(pluginPath, propertiesFileName); + if (propertiesPath == null) { + throw new PluginRuntimeException("Cannot find the properties path"); + } + + + if (Files.notExists(propertiesPath)) { + throw new PluginRuntimeException("Cannot find '{}' path", propertiesPath); + } + + Properties properties = new Properties(); + + try (InputStreamReader input = new InputStreamReader(Files.newInputStream(propertiesPath), + StandardCharsets.UTF_8);) { + properties.load(input); + } catch (IOException e) { + throw new PluginRuntimeException(e); + } + + return properties; + } +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/user/DefaultPluginUser.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/user/DefaultPluginUser.java index 3638eac42b93ec9d6895d33e8bf8c27d1f749fd3..59bc9b1b462cbd2a974c25e138f024016adbef7f 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/user/DefaultPluginUser.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/user/DefaultPluginUser.java @@ -2,6 +2,8 @@ package com.gitee.starblues.integration.user; import com.gitee.starblues.factory.PluginInfoContainer; import org.pf4j.PluginManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericApplicationContext; @@ -11,12 +13,14 @@ import java.util.stream.Collectors; /** * 默认插件使用者 * @author zhangzhuo - * @version 2.0.2 + * @version 2.2.2 */ public class DefaultPluginUser implements PluginUser{ - private final GenericApplicationContext applicationContext; - private final PluginManager pluginManager; + protected final Logger log = LoggerFactory.getLogger(this.getClass()); + + protected final GenericApplicationContext applicationContext; + protected final PluginManager pluginManager; public DefaultPluginUser(ApplicationContext applicationContext, PluginManager pluginManager) { Objects.requireNonNull(applicationContext, "ApplicationContext can't be null"); @@ -122,6 +126,20 @@ public class DefaultPluginUser implements PluginUser{ return beans; } + @Override + public T generateNewInstance(T object) { + if(object == null){ + return null; + } + try { + Object newObject = applicationContext.getDefaultListableBeanFactory() + .createBean(object.getClass()); + return (T) newObject; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /** * 得到插件扩展接口实现的bean。(非Spring管理) @@ -139,7 +157,7 @@ public class DefaultPluginUser implements PluginUser{ * @param beanName bean名称 * @return boolean */ - private boolean isPluginBean(String beanName){ + protected boolean isPluginBean(String beanName){ if(beanName == null){ return false; } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/user/PluginUser.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/user/PluginUser.java index 52895fb610b6900df92e743ce4ccc51d4c6ad9a0..5542b4e5fee84e0b81115ff5691373fbe4916a90 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/user/PluginUser.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/user/PluginUser.java @@ -7,7 +7,7 @@ import java.util.Map; * 该接口用于在主程序操作Spring管理的插件bean. * 主要用途: 在主程序定义接口。插件中实现该接口做扩展, 主程序通过接口class可以获取到插件中的实现类。 * @author zhangzhuo - * @version 2.0.2 + * @version 2.2.2 */ public interface PluginUser { @@ -86,6 +86,15 @@ public interface PluginUser { */ List getPluginBeans(String pluginId, Class aClass); + /** + * 生成一个新的实例 + * @param object 元实例对象 + * @param 实例泛型 + * @return 新实例对象 + */ + T generateNewInstance(T object); + + /** * 使用场景: * 1. 在主程序定义接口(该接口需要继承 ExtensionPoint 接口)。 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/ResourceWrapper.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/ResourceWrapper.java index 74749ffc3cd33a53cbb6b68baf432d538a9cc531..a44b259a856b70e526e15b6be73619cd994f228c 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/ResourceWrapper.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/ResourceWrapper.java @@ -1,6 +1,7 @@ package com.gitee.starblues.loader; import org.springframework.core.io.Resource; +import org.springframework.util.StringUtils; import java.util.*; @@ -12,32 +13,44 @@ import java.util.*; */ public class ResourceWrapper { - private final List resources; + private final List resources = new ArrayList<>(); + private final Set classPackageNames = new HashSet<>(); private final Map extensions = new HashMap<>(); + public void addResource(Resource resource){ + if(resource == null){ + return; + } + resources.add(resource); + } - public ResourceWrapper() { - this.resources = new ArrayList<>(0); + public void addResources(List resources){ + if(resources == null || resources.isEmpty()){ + return; + } + this.resources.addAll(resources); } - public ResourceWrapper(List resources) { - if(resources == null){ - this.resources = new ArrayList<>(0); - } else { - this.resources = resources; + public List getResources(){ + return Collections.unmodifiableList(resources); + } + + public void addClassPackageName(String classFullName){ + if(StringUtils.isEmpty(classFullName)){ + return; } + classPackageNames.add(classFullName); } - public ResourceWrapper(Resource[] resources) { - if(resources != null){ - this.resources = Arrays.asList(resources); - } else { - this.resources = new ArrayList<>(0); + public void addClassPackageNames(Set classPackageNames){ + if(classPackageNames == null || classPackageNames.isEmpty()){ + return; } + this.classPackageNames.addAll(classPackageNames); } - public List getResources(){ - return resources; + public Set getClassPackageNames(){ + return Collections.unmodifiableSet(classPackageNames); } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/load/PluginClassLoader.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/load/PluginClassLoader.java index 6b6e87baf18e0b0c85482448b9264213b05e54ba..2c816b29c694b85545a666258d6cb902de9e3ca3 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/load/PluginClassLoader.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/load/PluginClassLoader.java @@ -3,22 +3,17 @@ package com.gitee.starblues.loader.load; import com.gitee.starblues.loader.PluginResourceLoader; import com.gitee.starblues.loader.ResourceWrapper; import com.gitee.starblues.realize.BasePlugin; -import com.gitee.starblues.utils.OrderExecution; import com.gitee.starblues.utils.OrderPriority; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.springframework.util.ClassUtils; +import com.gitee.starblues.utils.ScanUtils; +import org.pf4j.RuntimeMode; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.Set; /** * 插件类文件加载者 * * @author zhangzhuo - * @version 2.2.0 + * @version 2.2.2 */ public class PluginClassLoader implements PluginResourceLoader { @@ -31,17 +26,21 @@ public class PluginClassLoader implements PluginResourceLoader { @Override public ResourceWrapper load(BasePlugin basePlugin) throws Exception{ - String scanPackage = basePlugin.scanPackage(); - String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + - ClassUtils.convertClassNameToResourcePath(scanPackage) + - "/**/*.class"; - ResourcePatternResolver resourcePatternResolver = - new PathMatchingResourcePatternResolver(basePlugin.getWrapper().getPluginClassLoader()); - Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); - if(resources == null){ - return new ResourceWrapper(); + RuntimeMode runtimeMode = basePlugin.getWrapper().getRuntimeMode(); + Set classPackageName = null; + if(runtimeMode == RuntimeMode.DEPLOYMENT){ + // 生产环境 + classPackageName = ScanUtils.scanClassPackageName( + basePlugin.scanPackage(), basePlugin.getWrapper().getPluginClassLoader()); + + } else if(runtimeMode == RuntimeMode.DEVELOPMENT){ + // 开发环境 + classPackageName = ScanUtils.scanClassPackageName( + basePlugin.scanPackage(), basePlugin.getClass()); } - return new ResourceWrapper(resources); + ResourceWrapper resourceWrapper = new ResourceWrapper(); + resourceWrapper.addClassPackageNames(classPackageName); + return resourceWrapper; } @Override diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/load/PluginConfigFileLoader.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/load/PluginConfigFileLoader.java index 5f2bdc21c82d8f8d15771f60ef8270a3b04ae1ae..518ce23b888a24b96c2f2d95bd2b890ff1b1e2f4 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/load/PluginConfigFileLoader.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/loader/load/PluginConfigFileLoader.java @@ -3,35 +3,37 @@ package com.gitee.starblues.loader.load; import com.gitee.starblues.loader.PluginResourceLoader; import com.gitee.starblues.loader.ResourceWrapper; import com.gitee.starblues.realize.BasePlugin; -import com.gitee.starblues.utils.OrderExecution; import com.gitee.starblues.utils.OrderPriority; -import org.pf4j.RuntimeMode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.FileUrlResource; +import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import java.io.File; +import java.io.FileNotFoundException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.function.Supplier; /** * 插件配置文件加载者 * * @author zhangzhuo - * @version 2.2.0 + * @version 2.2.2 */ public class PluginConfigFileLoader implements PluginResourceLoader { + private final static Logger log = LoggerFactory.getLogger(PluginConfigFileLoader.class); + private final String configFilePath; private final String fileName; - private final RuntimeMode runtimeMode; public PluginConfigFileLoader(String configFilePath, - String fileName, - RuntimeMode runtimeMode) { + String fileName) { this.configFilePath = configFilePath; this.fileName = fileName; - this.runtimeMode = runtimeMode; } @@ -42,20 +44,27 @@ public class PluginConfigFileLoader implements PluginResourceLoader { @Override public ResourceWrapper load(BasePlugin basePlugin) throws Exception { - Resource resource; - if(runtimeMode == RuntimeMode.DEVELOPMENT){ - // 开发环境下 - // 加载输入流 - String path = configFilePath + fileName; - resource = new ClassPathResource("/" + path, basePlugin.getWrapper().getPluginClassLoader()); - } else { - // 生产环境下 - String path = configFilePath + File.separatorChar + fileName; - resource = new FileUrlResource(path); + List> suppliers = new ArrayList<>(); + suppliers.add(findConfigRoot()); + suppliers.add(findPluginRoot(basePlugin)); + suppliers.add(findClassPath(basePlugin)); + + + for (Supplier supplier : suppliers) { + SupplierBean supplierBean = supplier.get(); + Resource resource = supplierBean.getResource(); + if(resource.exists()){ + List resources = new ArrayList<>(); + resources.add(resource); + ResourceWrapper resourceWrapper = new ResourceWrapper(); + resourceWrapper.addResources(resources); + log.info("Load the plugin '{}' config file '{}' from '{}'", + basePlugin.getWrapper().getPluginId(), fileName, supplierBean.getPath()); + return resourceWrapper; + } } - List resources = new ArrayList<>(); - resources.add(resource); - return new ResourceWrapper(resources); + throw new FileNotFoundException("Not found plugin '" + basePlugin.getWrapper().getPluginId() + "' " + + "config file : " + fileName); } @Override @@ -67,4 +76,64 @@ public class PluginConfigFileLoader implements PluginResourceLoader { public OrderPriority order() { return OrderPriority.getHighPriority().down(20); } + + /** + * 从插件文件的根目录查找配置文件 + * @param basePlugin basePlugin + * @return 返回resource + */ + private Supplier findPluginRoot(BasePlugin basePlugin){ + return ()->{ + Path pluginPath = basePlugin.getWrapper().getPluginPath(); + String rootPath = pluginPath.getParent().toString(); + String configPath = rootPath + File.separatorChar + fileName; + Resource resource = new FileSystemResource(configPath); + return new SupplierBean(rootPath, resource); + }; + } + + + /** + * 从插件配置文件 pluginConfigFilePath 的路径下查找配置文件 + * @return 返回resource + */ + private Supplier findConfigRoot(){ + return ()->{ + String filePath = configFilePath + File.separatorChar + fileName; + Resource resource = new FileSystemResource(filePath); + return new SupplierBean(configFilePath, resource); + }; + } + + /** + * 从ClassPath 中查找配置文件 + * @param basePlugin basePlugin + * @return 返回resource + */ + private Supplier findClassPath(BasePlugin basePlugin){ + return ()->{ + Resource resource = new ClassPathResource("/" + fileName, basePlugin.getWrapper().getPluginClassLoader()); + return new SupplierBean("classPath", resource); + }; + } + + private class SupplierBean{ + private String path; + private Resource resource; + + public SupplierBean(String path, Resource resource) { + this.path = path; + this.resource = resource; + } + + public String getPath() { + return path; + } + + public Resource getResource() { + return resource; + } + } + + } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/ConfigBean.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/ConfigBean.java new file mode 100644 index 0000000000000000000000000000000000000000..cc6848efde6f52fbd18619cb95268f39a78a15be --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/ConfigBean.java @@ -0,0 +1,26 @@ +package com.gitee.starblues.realize; + +/** + * 插件可配置自定义bean的接口。 + * 注意:该实现类只能注入插件中的配置文件和主程序bean. 不能注入插件中其他的组件bean。 + * bean 指的是Spring 容器中管理的bean + * + * @author zhangzhuo + * @version 2.2.2 + */ +public interface ConfigBean { + + + /** + * 初始化。所有bean的初始化工作在此处实现 + * @throws Exception 初始化异常 + */ + void initialize() throws Exception; + + /** + * 销毁实现 + * @throws Exception 销毁异常 + */ + void destroy() throws Exception; + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/ScanUtils.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/ScanUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..d9ef336a4fab90026d814c787d8a9f48fc43bea2 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/ScanUtils.java @@ -0,0 +1,105 @@ +package com.gitee.starblues.utils; + + +import java.io.File; +import java.io.IOException; +import java.net.JarURLConnection; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.stream.Collectors; + +/** + * 扫描工具类 + * + * @author zhangzhuo + * @version 2.2.2 + */ +public class ScanUtils { + + /** + * 扫描指定包中的类。包括子包中的类 + * @param basePackage 包名 + * @param baseClass 当前操作的基础类 + * @return 类全路径 + * @throws IOException 扫描异常 + */ + public static Set scanClassPackageName(String basePackage, Class baseClass) throws IOException { + String classpathRootPath = baseClass.getResource("/").getPath(); + final String classpath = classpathRootPath + .replace("/","\\") + .replaceFirst("\\\\",""); + // 把包名 packageName 转换为路径名 + basePackage = basePackage.replace(".", File.separator); + // class 文件全路径 + String fullPath = classpath + basePackage; + + return Files.walk(Paths.get(fullPath)) + .filter(path -> path != null) + .filter(Files::isRegularFile) + .filter(path -> { + String fileName = path.getFileName().toString(); + if(fileName == null){ + return false; + } + return fileName.endsWith(".class"); + }) + .map(path -> { + String pathString = path.toString(); + return pathString + .replace(classpath, "") + .replace("\\",".") + .replace(".class",""); + }).collect(Collectors.toSet()); + } + + + /** + * 扫描jar包中的类。 + * @param basePackage 包名 + * @param classLoader jar的ClassLoader + * @return 类全路径 + * @throws IOException 扫描异常 + */ + public static Set scanClassPackageName(String basePackage, ClassLoader classLoader) throws IOException { + Enumeration urlEnumeration = classLoader.getResources(basePackage.replace(".", "/")); + Set classPackageNames = new HashSet<>(); + while (urlEnumeration.hasMoreElements()) { + URL url = urlEnumeration.nextElement(); + String protocol = url.getProtocol(); + if (!"jar".equalsIgnoreCase(protocol)) { + // 不是jar协议 + return classPackageNames; + } + JarURLConnection connection = (JarURLConnection) url.openConnection(); + if (connection == null) { + return classPackageNames; + } + JarFile jarFile = connection.getJarFile(); + if (jarFile == null) { + return classPackageNames; + } + Enumeration jarEntryEnumeration = jarFile.entries(); + // 迭代 + while (jarEntryEnumeration.hasMoreElements()) { + JarEntry entry = jarEntryEnumeration.nextElement(); + String jarEntryName = entry.getName(); + if (jarEntryName.contains(".class") && + jarEntryName.replaceAll("/",".").startsWith(basePackage)) { + String className = jarEntryName + .substring(0, jarEntryName.lastIndexOf(".")) + .replace("/", "."); + classPackageNames.add(className); + } + } + } + return classPackageNames; + } + + +}