From da1ad6e51743fecab7e30b6fb6329aac6f223f2e Mon Sep 17 00:00:00 2001 From: StarBlues Date: Tue, 12 Nov 2019 12:47:49 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=8C=85=E6=A0=A1=E9=AA=8C=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../operator/DefaultPluginOperator.java | 24 ++++++++++++------- ...adVerify.java => DefaultPluginVerify.java} | 13 +++++----- .../operator/verify/PluginVerify.java | 2 +- 3 files changed, 24 insertions(+), 15 deletions(-) rename springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/operator/verify/{PluginUploadVerify.java => DefaultPluginVerify.java} (78%) 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 3b71042..db1b397 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,7 +34,7 @@ import java.util.stream.Collectors; /** * 默认的插件操作者 * @author zhangzhuo - * @version 2.2.0 + * @version 2.2.2 */ public class DefaultPluginOperator implements PluginOperator { @@ -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 { @@ -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目录下。直接加载该插件 @@ -425,7 +433,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; 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 7f3ea59..e156964 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 fcd967f..be0a89e 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 { -- Gitee From cad88e6e9080934aeab6a18cca19d7738bd23a9f Mon Sep 17 00:00:00 2001 From: StarBlues Date: Tue, 19 Nov 2019 17:29:02 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=BB=E5=8A=A1:=20htt?= =?UTF-8?q?ps://gitee.com/starblues/springboot-plugin-framework-parent/iss?= =?UTF-8?q?ues/I14VCC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/PluginMybatisXmlLoader.java | 4 +- springboot-plugin-framework/pom.xml | 2 +- .../YamlConfigurationParser.java | 2 +- .../pipe/classs/PluginClassProcess.java | 13 +-- .../application/AutoPluginApplication.java | 4 +- .../application/DefaultPluginApplication.java | 43 +++++-- .../operator/DefaultPluginOperator.java | 16 +-- .../integration/pf4j/DefaultPf4JFactory.java | 7 ++ ...solvePropertiesPluginDescriptorFinder.java | 46 ++++++++ .../integration/user/DefaultPluginUser.java | 24 +++- .../integration/user/PluginUser.java | 9 ++ .../starblues/loader/ResourceWrapper.java | 43 ++++--- .../loader/load/PluginClassLoader.java | 32 +++--- .../loader/load/PluginConfigFileLoader.java | 4 +- .../com/gitee/starblues/utils/ScanUtils.java | 105 ++++++++++++++++++ 15 files changed, 289 insertions(+), 65 deletions(-) create mode 100644 springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/ResolvePropertiesPluginDescriptorFinder.java create mode 100644 springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/ScanUtils.java 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 18177aa..19c56a1 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/pom.xml b/springboot-plugin-framework/pom.xml index 2b9024f..c58fa83 100644 --- a/springboot-plugin-framework/pom.xml +++ b/springboot-plugin-framework/pom.xml @@ -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/process/pipe/bean/configuration/YamlConfigurationParser.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/configuration/YamlConfigurationParser.java index 6920e34..cd47431 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); } 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 da4a4fd..e2f7a2c 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,13 +10,12 @@ 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; /** * 插件类加载处理者 @@ -77,12 +75,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/integration/application/AutoPluginApplication.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AutoPluginApplication.java index dcba071..2174107 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 882c753..7646cd7 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 @@ -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 static Logger log = LoggerFactory.getLogger(DefaultPluginApplication.class); + + protected Pf4jFactory integrationFactory; - private Pf4jFactory integrationFactory; private PluginUser pluginUser; private PluginOperator pluginOperator; @@ -52,13 +53,8 @@ public class DefaultPluginApplication extends AbstractPluginApplication { 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 db1b397..f02d777 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 @@ -39,7 +39,7 @@ import java.util.stream.Collectors; 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"); @@ -113,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){ @@ -415,7 +415,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"); } @@ -467,7 +467,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); @@ -483,7 +483,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){ @@ -498,7 +498,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()){ // 如果是开发环境, 则不进行备份 @@ -544,7 +544,7 @@ public class DefaultPluginOperator implements PluginOperator { * 获取现在的时间 * @return String */ - private String getNowTimeByFormat(){ + protected String getNowTimeByFormat(){ LocalDateTime localDateTime = LocalDateTime.now(); return FORMAT.format(localDateTime); } @@ -553,7 +553,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/pf4j/DefaultPf4JFactory.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/DefaultPf4JFactory.java index e52cf7c..0375027 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 @@ -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 0000000..23ca633 --- /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 3638eac..62f8b2e 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; @@ -15,8 +17,10 @@ import java.util.stream.Collectors; */ 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 52895fb..41d55da 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 @@ -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 74749ff..a44b259 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 6b6e87b..3bdd8a2 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,16 +3,14 @@ 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.ScanUtils; import com.gitee.starblues.utils.OrderPriority; +import org.pf4j.RuntimeMode; 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 java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.Set; /** * 插件类文件加载者 @@ -31,17 +29,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 5f2bdc2..0e7c4ae 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 @@ -55,7 +55,9 @@ public class PluginConfigFileLoader implements PluginResourceLoader { } List resources = new ArrayList<>(); resources.add(resource); - return new ResourceWrapper(resources); + ResourceWrapper resourceWrapper = new ResourceWrapper(); + resourceWrapper.addResources(resources); + return resourceWrapper; } @Override 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 0000000..ad8ef9f --- /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 sun.net.www.protocol.jar.JarURLConnection; + +import java.io.File; +import java.io.IOException; +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; + } + + +} -- Gitee From a8801c8d6a7dd370a39f03a151e58fe87714bf89 Mon Sep 17 00:00:00 2001 From: StarBlues Date: Tue, 19 Nov 2019 17:50:59 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9log=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/application/DefaultPluginApplication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7646cd7..8af9b6a 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 @@ -24,7 +24,7 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class DefaultPluginApplication extends AbstractPluginApplication { - private final static Logger log = LoggerFactory.getLogger(DefaultPluginApplication.class); + private final Logger log = LoggerFactory.getLogger(this.getClass()); protected Pf4jFactory integrationFactory; -- Gitee From 9656adb1a7b7d979a77b3eb6a35f2c4a37283e1a Mon Sep 17 00:00:00 2001 From: StarBlues Date: Thu, 28 Nov 2019 11:40:10 +0800 Subject: [PATCH 4/4] # https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I14VCC --- .../basic-example/basic-example-main/pom.xml | 4 +- .../basic-example-runner/pom.xml | 2 +- .../basic-example-plugin1/plugin.properties | 2 +- .../plugins/basic-example-plugin1/pom.xml | 4 +- .../basic-example-plugin2/plugin.properties | 2 +- .../plugins/basic-example-plugin2/pom.xml | 4 +- example/basic-example/plugins/pom.xml | 2 +- example/basic-example/pom.xml | 2 +- .../integration-mybatis-main/pom.xml | 8 +- .../integration-mybatis-plugin-parent/pom.xml | 2 +- .../integration-mybatis-runner/pom.xml | 2 +- .../plugin.properties | 2 +- .../integration-mybatis-plugin1/pom.xml | 4 +- .../plugin.properties | 2 +- .../integration-mybatis-plugin2/pom.xml | 4 +- example/integration-mybatis/pom.xml | 2 +- .../integration-mybatisplus-main/pom.xml | 6 +- .../plugin.properties | 2 +- .../integration-mybatisplus-plugin/pom.xml | 2 +- example/integration-mybatisplus/pom.xml | 2 +- example/pom.xml | 2 +- pom.xml | 2 +- springboot-plugin-framework-extension/pom.xml | 2 +- .../pom.xml | 4 +- .../pom.xml | 4 +- springboot-plugin-framework/pom.xml | 2 +- .../factory/DefaultPluginFactory.java | 8 +- .../pipe/PluginPipeProcessorFactory.java | 8 +- .../pipe/bean/ConfigBeanProcessor.java | 89 +++++--------- .../pipe/bean/ConfigFileBeanProcessor.java | 110 +++++++++++++++++ .../AbstractConfigurationParser.java | 7 +- .../YamlConfigurationParser.java | 2 +- .../pipe/classs/PluginClassProcess.java | 3 +- .../pipe/classs/group/ConfigBeanGroup.java | 42 +++++++ .../application/DefaultPluginApplication.java | 4 +- .../operator/DefaultPluginOperator.java | 21 ++-- ...4JFactory.java => DefaultPf4jFactory.java} | 4 +- .../integration/user/DefaultPluginUser.java | 2 +- .../integration/user/PluginUser.java | 2 +- .../loader/load/PluginClassLoader.java | 7 +- .../loader/load/PluginConfigFileLoader.java | 113 ++++++++++++++---- .../gitee/starblues/realize/ConfigBean.java | 26 ++++ .../com/gitee/starblues/utils/ScanUtils.java | 2 +- 43 files changed, 371 insertions(+), 155 deletions(-) create mode 100644 springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigFileBeanProcessor.java create mode 100644 springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ConfigBeanGroup.java rename springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/pf4j/{DefaultPf4JFactory.java => DefaultPf4jFactory.java} (95%) create mode 100644 springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/ConfigBean.java diff --git a/example/basic-example/basic-example-main/pom.xml b/example/basic-example/basic-example-main/pom.xml index 917f4e8..8ae868c 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 7935a97..f15d476 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 48af354..a62fec6 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 01a085b..d5024f7 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 7f10c4b..b1bef3e 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 4bc0a1a..530c2eb 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 87e6f5f..bb9d2b3 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 a9322ad..39c9400 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 7522e6e..f748874 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 43e83ac..269bb1f 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 5ce2529..afa68f0 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 e212961..086145a 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 f4f6752..51ed59e 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 f6564bc..ae4c430 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 b935610..d4580c8 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 28407e3..4f25fa2 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 62a75eb..5868bc6 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 8641d68..88aa9fe 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 1aa6ef8..b8cce26 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 a22d035..71cdb24 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 0df8b75..1cac07c 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 8d0eb10..a03328d 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 20a47ff..c05a6f6 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 5e62543..49b92e1 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-resources/pom.xml b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/pom.xml index 1fda0a1..b2c77d0 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 c58fa83..4d2d893 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 插件式开发集成包 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 ac46f81..e573e5b 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 e529808..f82d98b 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 e141c93..802b30d 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 0000000..ca34eea --- /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 c82c8fa..6e45f32 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 cd47431..9405c4e 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 @@ -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 e2f7a2c..4e35d94 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 @@ -21,7 +21,7 @@ import java.util.Set; * 插件类加载处理者 * * @author zhangzhuo - * @version 2.1.0 + * @version 2.2.2 */ public class PluginClassProcess implements PluginPipeProcessor { @@ -47,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()); 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 0000000..32a6350 --- /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/DefaultPluginApplication.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/DefaultPluginApplication.java index 8af9b6a..12e29a5 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; @@ -50,7 +50,7 @@ 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 = createPluginUser(applicationContext, pluginManager); 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 f02d777..54a008a 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 @@ -154,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); } @@ -171,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); } } @@ -197,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); @@ -244,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(); 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 95% 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 0375027..6c11cfb 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; } 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 62f8b2e..59bc9b1 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 @@ -13,7 +13,7 @@ import java.util.stream.Collectors; /** * 默认插件使用者 * @author zhangzhuo - * @version 2.0.2 + * @version 2.2.2 */ public class DefaultPluginUser implements PluginUser{ 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 41d55da..5542b4e 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 { 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 3bdd8a2..2c816b2 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,12 +3,9 @@ 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.ScanUtils; import com.gitee.starblues.utils.OrderPriority; +import com.gitee.starblues.utils.ScanUtils; import org.pf4j.RuntimeMode; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; import java.util.Set; @@ -16,7 +13,7 @@ import java.util.Set; * 插件类文件加载者 * * @author zhangzhuo - * @version 2.2.0 + * @version 2.2.2 */ public class PluginClassLoader implements PluginResourceLoader { 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 0e7c4ae..518ce23 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,22 +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); - ResourceWrapper resourceWrapper = new ResourceWrapper(); - resourceWrapper.addResources(resources); - return resourceWrapper; + throw new FileNotFoundException("Not found plugin '" + basePlugin.getWrapper().getPluginId() + "' " + + "config file : " + fileName); } @Override @@ -69,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 0000000..cc6848e --- /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 index ad8ef9f..d9ef336 100644 --- 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 @@ -1,9 +1,9 @@ package com.gitee.starblues.utils; -import sun.net.www.protocol.jar.JarURLConnection; 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; -- Gitee