diff --git a/README.md b/README.md index dc5d2e9bb801fa7213e3451676f82e79e2befa79..0fe613b53cd0e7a7acc0db9d54fc6d95dc4685f0 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,12 @@ - 支持在插件中开发Rest接口。 - 支持在插件中单独定义持久层访问等需求。 - 可以遵循主程序提供的插件接口开发任意扩展功能。 +- 支持注解进行任意业务场景扩展, 并使用定义的坐标进行场景命中。 - 插件可以根据生产和开发环境自定义独立的配置文件。目前只支持yml文件。 - 支持自定义扩展开发接口, 使用者可以在预留接口上扩展额外功能。 - 支持插件之间的通信。 - 支持插件接口文档: `Swagger`、`SpringDoc`。 +- 插件支持`拦截器`的定制开发。 ### 扩展包功能 1. `SpringBoot-Mybatis`扩展包 diff --git a/example/basic-example/basic-example-main/pom.xml b/example/basic-example/basic-example-main/pom.xml index 32c29e37a1c679c9568c04c3bb16012515569ae9..28fb781ae8681b6b4f7eab2d86b3fec57b6786dc 100644 --- a/example/basic-example/basic-example-main/pom.xml +++ b/example/basic-example/basic-example-main/pom.xml @@ -13,14 +13,14 @@ com.gitee.starblues basic-example-main - 2.4.0-RELEASE + 2.4.1-RELEASE jar 2.7.0 2.3.2 1.6 - 2.4.0-RELEASE + 2.4.1-RELEASE @@ -30,17 +30,11 @@ ${springboot-plugin-framework.version} - - org.springframework.boot - spring-boot - - org.springframework.boot spring-boot-starter-web - org.quartz-scheduler quartz diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/MainInterceptor.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/MainInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..cd0f9389e83f2e2b4ef6b35f663ddf52fc091826 --- /dev/null +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/MainInterceptor.java @@ -0,0 +1,32 @@ +package com.basic.example.main.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.config.annotation.InterceptorRegistration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @author starBlues + * @version 1.0 + */ +@Configuration +public class MainInterceptor implements HandlerInterceptor, WebMvcConfigurer { + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + System.out.println("进入主拦截器"); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + //注册TestInterceptor拦截器 + InterceptorRegistration registration = registry.addInterceptor(new MainInterceptor()); + registration.addPathPatterns("/**"); + } + +} diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/PluginConfiguration.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/PluginConfiguration.java index 16eb1c9bffa4815725819aed57604036254b2c22..3eb0e2870092c08978abeb467094eef538789222 100644 --- a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/PluginConfiguration.java +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/PluginConfiguration.java @@ -100,6 +100,11 @@ public class PluginConfiguration extends DefaultIntegrationConfiguration { return true; } + @Override + public String version() { + return "1.2.6"; + } + public String getRunMode() { return runMode; } diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/extract/ExtractExample.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/extract/ExtractExample.java new file mode 100644 index 0000000000000000000000000000000000000000..d8814ce9545895cf2dee6ba31f4ce76f7e327743 --- /dev/null +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/extract/ExtractExample.java @@ -0,0 +1,50 @@ +package com.basic.example.main.extract; + +/** + * @author starBlues + * @version 1.0 + */ +public interface ExtractExample { + + void exe(); + + void exe(String name); + + void exe(Info info); + + Info exeInfo(Info info); + + + class Info{ + private String name; + private Integer age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + @Override + public String toString() { + return "Info{" + + "name='" + name + '\'' + + ", age=" + age + + '}'; + } + } + + + + +} diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/extract/MainExtractExample.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/extract/MainExtractExample.java new file mode 100644 index 0000000000000000000000000000000000000000..5900c3c52bab9526be36b92a4e8b9120179523c6 --- /dev/null +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/extract/MainExtractExample.java @@ -0,0 +1,35 @@ +package com.basic.example.main.extract; + +import com.gitee.starblues.annotation.Extract; +import org.springframework.stereotype.Component; + +/** + * @author starBlues + * @version 1.0 + */ +@Extract(bus = "main", scene = "1", useCase = "2") +@Component +public class MainExtractExample implements ExtractExample{ + + + @Override + public void exe() { + System.out.println("Main exe"); + } + + @Override + public void exe(String name) { + System.out.println("Main exe, name=" + name); + } + + @Override + public void exe(Info info) { + System.out.println("Main exe, info=" + info); + } + + @Override + public Info exeInfo(Info info) { + info.setName(info.getName() + "-main"); + return info; + } +} diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/ExtractController.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/ExtractController.java new file mode 100644 index 0000000000000000000000000000000000000000..1a3a28b395411cc55c03af749f02320a9c06063d --- /dev/null +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/ExtractController.java @@ -0,0 +1,60 @@ +package com.basic.example.main.rest; + +import com.basic.example.main.extract.ExtractExample; +import com.gitee.starblues.factory.process.pipe.extract.ExtractCoordinate; +import com.gitee.starblues.factory.process.pipe.extract.ExtractFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * @author starBlues + * @version 1.0 + */ +@RestController +@RequestMapping("/extract") +public class ExtractController { + + @Resource + private ExtractFactory extractFactory; + + @GetMapping("getExtractCoordinates") + public Map> getExtractCoordinates(){ + return extractFactory.getExtractCoordinates(); + } + + @GetMapping("getExtractByInterClass") + public List getExtractByInterClass(){ + List extractByInterClass = extractFactory.getExtractByInterClass(ExtractExample.class); + return extractByInterClass.stream() + .map(extractExample -> extractExample.getClass().getName()) + .collect(Collectors.toList()); + } + + + @GetMapping("{name}/exeR") + public ExtractExample.Info exeInfoR(@PathVariable("name") String name){ + ExtractExample extractExample = extractFactory.getExtractByCoordinate(ExtractCoordinate.build(name)); + ExtractExample.Info info = new ExtractExample.Info(); + return extractExample.exeInfo(info); + } + + @GetMapping("/mainExtract") + public ExtractExample.Info getMainExtract(){ + ExtractExample extractExample = extractFactory.getExtractByCoordinateOfMain(ExtractCoordinate.build( + "main", "1", "2" + )); + ExtractExample.Info info = new ExtractExample.Info(); + info.setName("main info"); + info.setAge(Integer.MAX_VALUE); + return extractExample.exeInfo(info); + } + +} diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/HelloResource.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/HelloResource.java index d098630d6142ec9063724477341fae4be629d985..beefe2c27d4ed6d1b18d0f265907443e139a43f8 100644 --- a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/HelloResource.java +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/HelloResource.java @@ -25,9 +25,9 @@ public class HelloResource { private final PluginUser pluginUser; private final ConsoleNameFactory consoleNameFactory; - public HelloResource(PluginApplication pluginApplication, + public HelloResource(PluginUser pluginUser, ConsoleNameFactory consoleNameFactory) { - this.pluginUser = pluginApplication.getPluginUser(); + this.pluginUser = pluginUser; this.consoleNameFactory = consoleNameFactory; } diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/PluginResource.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/PluginResource.java index 9f9d839ac7b86287c89530816ad13b96db668fcb..6936ce89da1738ff0ff53c362b62dba0c3bd4a80 100644 --- a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/PluginResource.java +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/rest/PluginResource.java @@ -24,8 +24,8 @@ public class PluginResource { private final PluginOperator pluginOperator; @Autowired - public PluginResource(PluginApplication pluginApplication) { - this.pluginOperator = pluginApplication.getPluginOperator(); + public PluginResource(PluginOperator pluginOperator) { + this.pluginOperator = pluginOperator; } /** * 获取插件信息 diff --git a/example/basic-example/basic-example-runner/pom.xml b/example/basic-example/basic-example-runner/pom.xml index 8812ae4daffc110e840b77326b185e77b3349102..fd6f04a7f59b294e60bb6c26e1736ebd6264b7e9 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.4.0-RELEASE + 2.4.1-RELEASE pom diff --git a/example/basic-example/plugins/basic-example-plugin1/pom.xml b/example/basic-example/plugins/basic-example-plugin1/pom.xml index e62f20ddb6998b6c5af08a449db45d39d4f19826..59e5eebe9ac753ee55cc25a74c535e97c0f83f42 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.4.0-RELEASE + 2.4.1-RELEASE ../pom.xml basic-example-plugin1 - 2.4.0-RELEASE + 2.4.1-RELEASE jar diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/PluginInterceptor1.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/PluginInterceptor1.java new file mode 100644 index 0000000000000000000000000000000000000000..f58fdaa50983981fb5a80baa58438f726407d581 --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/PluginInterceptor1.java @@ -0,0 +1,22 @@ +package com.basic.example.plugin1; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @author starBlues + * @version 1.0 + */ +@Component +public class PluginInterceptor1 implements HandlerInterceptor { + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + System.out.println("进入插件1拦截器"); + } + +} diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/PluginInterceptorR.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/PluginInterceptorR.java new file mode 100644 index 0000000000000000000000000000000000000000..088513bb2a0f20d607e1f9936218af739d117891 --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/PluginInterceptorR.java @@ -0,0 +1,26 @@ +package com.basic.example.plugin1; + +import com.gitee.starblues.factory.process.pipe.interceptor.PluginInterceptorRegister; +import com.gitee.starblues.factory.process.pipe.interceptor.PluginInterceptorRegistration; +import com.gitee.starblues.factory.process.pipe.interceptor.PluginInterceptorRegistry; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; + +import javax.annotation.Resource; + +/** + * @author starBlues + * @version 1.0 + */ +@Component +public class PluginInterceptorR implements PluginInterceptorRegister { + + @Resource + private PluginInterceptor1 pluginInterceptor1; + + @Override + public void registry(PluginInterceptorRegistry registry) { + registry.addInterceptor(pluginInterceptor1, PluginInterceptorRegistry.Type.PLUGIN) + .addPathPatterns("plugin1/**"); + } +} diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/extract/PluginExtract.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/extract/PluginExtract.java new file mode 100644 index 0000000000000000000000000000000000000000..2278fc90ea9cc85d02775973cb6f584e4f52fdb5 --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/extract/PluginExtract.java @@ -0,0 +1,33 @@ +package com.basic.example.plugin1.extract; + +import com.basic.example.main.extract.ExtractExample; +import com.gitee.starblues.annotation.Extract; + +/** + * @author starBlues + * @version 1.0 + */ +@Extract(bus = "PluginExtract1") +public class PluginExtract implements ExtractExample { + @Override + public void exe() { + System.out.println(PluginExtract.class.getName()); + } + + @Override + public void exe(String name) { + System.out.println(PluginExtract.class.getName() + ": name"); + } + + @Override + public void exe(Info info) { + System.out.println(PluginExtract.class.getName() + ": " + info); + } + + @Override + public Info exeInfo(Info info) { + info.setName("Plugin1-PluginExtract1"); + info.setAge(0); + return info; + } +} diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/resources/plugin.properties b/example/basic-example/plugins/basic-example-plugin1/src/main/resources/plugin.properties index ab2b15be7f5b46142d81f9dee4eecae6c61b5a53..d456f7e6919ccad159c49b340ce54f35263278e7 100644 --- a/example/basic-example/plugins/basic-example-plugin1/src/main/resources/plugin.properties +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/resources/plugin.properties @@ -1,4 +1,5 @@ plugin.id=basic-example-plugin1 plugin.class=com.basic.example.plugin1.DefinePlugin -plugin.version=2.4.0-RELEASE -plugin.provider=StarBlues \ No newline at end of file +plugin.version=2.4.1-RELEASE +plugin.provider=StarBlues +plugin.requires=1.2.6 \ 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 3440a91dcc4f0f4423a6030fc08c63ccf8257f4d..1c77458d169ba5206c0396351a128b70d339c799 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.4.0-RELEASE + 2.4.1-RELEASE ../pom.xml basic-example-plugin2 - 2.4.0-RELEASE + 2.4.1-RELEASE jar diff --git a/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/PluginInterceptorR.java b/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/PluginInterceptorR.java new file mode 100644 index 0000000000000000000000000000000000000000..987ccf41084e89989dbe8dbeb764aaaed8a509ee --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/PluginInterceptorR.java @@ -0,0 +1,29 @@ +package com.basic.example.plugin2; + +import com.gitee.starblues.factory.process.pipe.interceptor.PluginInterceptorRegister; +import com.gitee.starblues.factory.process.pipe.interceptor.PluginInterceptorRegistry; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @author starBlues + * @version 1.0 + */ +@Component +public class PluginInterceptorR implements PluginInterceptorRegister, HandlerInterceptor { + + @Override + public void registry(PluginInterceptorRegistry registry) { + registry.addInterceptor(this, PluginInterceptorRegistry.Type.PLUGIN); + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + System.out.println("拦截器进入插件2"); + } +} diff --git a/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/extract/PluginExtract.java b/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/extract/PluginExtract.java new file mode 100644 index 0000000000000000000000000000000000000000..74de2d66fee929f53963ac1b93fc85ce9f675d1a --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/extract/PluginExtract.java @@ -0,0 +1,33 @@ +package com.basic.example.plugin2.extract; + +import com.basic.example.main.extract.ExtractExample; +import com.gitee.starblues.annotation.Extract; + +/** + * @author starBlues + * @version 1.0 + */ +@Extract(bus = "PluginExtract2") +public class PluginExtract implements ExtractExample { + @Override + public void exe() { + System.out.println(PluginExtract.class.getName()); + } + + @Override + public void exe(String name) { + System.out.println(PluginExtract.class.getName() + ": name"); + } + + @Override + public void exe(Info info) { + System.out.println(PluginExtract.class.getName() + ": " + info); + } + + @Override + public Info exeInfo(Info info) { + info.setName("plugin2-PluginExtract2"); + info.setAge(0); + return info; + } +} diff --git a/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/rest/ExtractController.java b/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/rest/ExtractController.java new file mode 100644 index 0000000000000000000000000000000000000000..eca46ae9458082057abb3f5f715ad2ba3742a3d2 --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/rest/ExtractController.java @@ -0,0 +1,57 @@ +package com.basic.example.plugin2.rest; + +import com.basic.example.main.extract.ExtractExample; +import com.gitee.starblues.factory.process.pipe.extract.ExtractCoordinate; +import com.gitee.starblues.factory.process.pipe.extract.ExtractFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * @author starBlues + * @version 1.0 + */ +@RestController +@RequestMapping("/extract") +public class ExtractController { + + @Resource + private ExtractFactory extractFactory; + + + @GetMapping("{name}/exe") + public void exe(@PathVariable("name") String name){ + ExtractExample extractExample = extractFactory.getExtractByCoordinate(ExtractCoordinate.build(name)); + extractExample.exe(); + } + + @GetMapping("{name}/exeName") + public void exeName(@PathVariable("name") String name){ + ExtractExample extractExample = extractFactory.getExtractByCoordinate(ExtractCoordinate.build(name)); + extractExample.exe("name"); + } + + + @GetMapping("{name}/exeInfo") + public void exeInfo(@PathVariable("name") String name){ + ExtractExample extractExample = extractFactory.getExtractByCoordinate(ExtractCoordinate.build(name)); + ExtractExample.Info info = new ExtractExample.Info(); + info.setName("plugin2"); + info.setAge(3); + extractExample.exe(info); + } + + + @GetMapping("{name}/exeR") + public ExtractExample.Info exeInfoR(@PathVariable("name") String name){ + ExtractExample extractExample = extractFactory.getExtractByCoordinate(ExtractCoordinate.build(name)); + ExtractExample.Info info = new ExtractExample.Info(); + return extractExample.exeInfo(info); + } + + + +} diff --git a/example/basic-example/plugins/basic-example-plugin2/src/main/resources/plugin.properties b/example/basic-example/plugins/basic-example-plugin2/src/main/resources/plugin.properties index 2f641c95aa503d0cf56b05539c64214aa2f6194e..7ad556ee8bba1a0ea6c4104dec46c6d13f0e6363 100644 --- a/example/basic-example/plugins/basic-example-plugin2/src/main/resources/plugin.properties +++ b/example/basic-example/plugins/basic-example-plugin2/src/main/resources/plugin.properties @@ -1,4 +1,4 @@ plugin.id=basic-example-plugin2 plugin.class=com.basic.example.plugin2.DefinePlugin -plugin.version=2.4.0-RELEASE +plugin.version=2.4.1-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/basic-example/plugins/pom.xml b/example/basic-example/plugins/pom.xml index 0f61ecf9e0da4866b043c143dc446eb19497a131..146dc3634cf374b0100077803ab0b2775f980ca5 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.4.0-RELEASE + 2.4.1-RELEASE pom diff --git a/example/basic-example/pom.xml b/example/basic-example/pom.xml index b636b93478109ffb21e52d15d79fb03df3f36985..9e2cbe0c5f5fb3cb093252101bd8aed203b3ca88 100644 --- a/example/basic-example/pom.xml +++ b/example/basic-example/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues basic-example - 2.4.0-RELEASE + 2.4.1-RELEASE pom 基本案例 diff --git a/example/integration-mybatis/integration-mybatis-main/pom.xml b/example/integration-mybatis/integration-mybatis-main/pom.xml index 7b174d627b1f14123a91fcae52b9da93c1efbb80..c9aeda2470294efa15c0d8366e264a6b50f67e81 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.4.0-RELEASE + 2.4.1-RELEASE jar 主程序模块 - 2.4.0-RELEASE - 2.4.0-RELEASE - 2.4.0-RELEASE + 2.4.1-RELEASE + 2.4.1-RELEASE + 2.4.1-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 d48c4273a04c10b352103b0f2a9005d970cfff6f..e4cc482b0df1f13dc7732a4607094f3f27e1426a 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.4.0-RELEASE + 2.4.1-RELEASE pom diff --git a/example/integration-mybatis/integration-mybatis-runner/pom.xml b/example/integration-mybatis/integration-mybatis-runner/pom.xml index 91ba7d8c8bc9d4ad1243f303a198e8b0f3f34d84..71646f7ee37c7dee672a2d411e4b590c1259faca 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.4.0-RELEASE + 2.4.1-RELEASE jar 启动程序模块。将启动类配置到该模块下 diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin1/pom.xml b/example/integration-mybatis/plugins/integration-mybatis-plugin1/pom.xml index 41fa0f398e298ed261f5b48b37bc050c33d2d081..5483da32d27b51998341b6c3da936c56d79bf52c 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.4.0-RELEASE + 2.4.1-RELEASE ../../integration-mybatis-plugin-parent/pom.xml integration-mybatis-plugin1 - 2.4.0-RELEASE + 2.4.1-RELEASE jar diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/resources/plugin.properties b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/resources/plugin.properties index 08af4cddac2dc72d3c620292d95fdc41552c7b39..1d2b72d5fa7b9f37b6db37d800e9735d039c3451 100644 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/resources/plugin.properties +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/resources/plugin.properties @@ -1,4 +1,4 @@ plugin.id=integration-mybatis-plugin1 plugin.class=com.mybatis.plugin1.ExamplePlugin1 -plugin.version=2.4.0-RELEASE +plugin.version=2.4.1-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin2/plugin.properties b/example/integration-mybatis/plugins/integration-mybatis-plugin2/plugin.properties index f47e5c93a2881e0cf336c71f79431c9f49d2339f..72c1ab5282b7bc0c1a55a50cfe22bc2287e4a979 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.4.0-RELEASE +plugin.version=2.4.1-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 03f1925c1ad4e771a5ff2367d3e2c9bf72264428..688e2464465bbe59a27c5e64bba21d31bd55ca0d 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.4.0-RELEASE + 2.4.1-RELEASE ../../integration-mybatis-plugin-parent/pom.xml integration-mybatis-plugin2 - 2.4.0-RELEASE + 2.4.1-RELEASE jar diff --git a/example/integration-mybatis/pom.xml b/example/integration-mybatis/pom.xml index c17c26d5f398716671685094a0247a4e3c882f65..271a5eac9c64d3594cc98e66cd791a802b4a7ccf 100644 --- a/example/integration-mybatis/pom.xml +++ b/example/integration-mybatis/pom.xml @@ -7,7 +7,7 @@ com.gitee.starblues integration-mybatis - 2.4.0-RELEASE + 2.4.1-RELEASE pom 集成mybatis案例 diff --git a/example/integration-mybatisplus/integration-mybatisplus-main/pom.xml b/example/integration-mybatisplus/integration-mybatisplus-main/pom.xml index d6343a5abf0b50d3ab07be0092aeca6e51d31827..dee453b9b2cdeef60138e43370c8e9e046aea800 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.4.0-RELEASE + 2.4.1-RELEASE integration-mybatisplus-main jar 集成mybatis-plus 案例--主程序 @@ -27,8 +27,8 @@ 2.0.1 3.2.0 - 2.4.0-RELEASE - 2.4.0-RELEASE + 2.4.1-RELEASE + 2.4.1-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 77783a54d31724aca52f779b0270217bc25330a3..767beb9c203da3cd261dd6b88934313e7ea3c37a 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.4.0-RELEASE +plugin.version=2.4.1-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 fdc1aa10eaae670e355c14b870c0fcd9946c072e..6988f1557aad2bf9fab7b35462a8b7a6523ed1e3 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.4.0-RELEASE + 2.4.1-RELEASE jar diff --git a/example/integration-mybatisplus/pom.xml b/example/integration-mybatisplus/pom.xml index 58eb3eae20fc3263e3f202bb8d62579c0740d944..886c739eca96678aea6c3f679da359563537570b 100644 --- a/example/integration-mybatisplus/pom.xml +++ b/example/integration-mybatisplus/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-mybatisplus - 2.4.0-RELEASE + 2.4.1-RELEASE pom 集成mybatis-plus案例 diff --git a/example/integration-tkmybatis/integration-tkmybatis-main/pom.xml b/example/integration-tkmybatis/integration-tkmybatis-main/pom.xml index 2fa3f54f1f111f8628c9ca647033faab76f08c36..8746af465a413b5fdff5209b9d371ae4414938a9 100644 --- a/example/integration-tkmybatis/integration-tkmybatis-main/pom.xml +++ b/example/integration-tkmybatis/integration-tkmybatis-main/pom.xml @@ -13,7 +13,7 @@ com.gitee.starblues - 2.4.0-RELEASE + 2.4.1-RELEASE integration-tkmybatis-main jar 集成mybatis-plus 案例--主程序 diff --git a/example/integration-tkmybatis/plugins/integration-tkmybatis-plugin/plugin.properties b/example/integration-tkmybatis/plugins/integration-tkmybatis-plugin/plugin.properties index 0207ba31093fa28b14ab697a4d50134c1e9bc00f..01d0a2e675b98466e205e31197e633736fa938b7 100644 --- a/example/integration-tkmybatis/plugins/integration-tkmybatis-plugin/plugin.properties +++ b/example/integration-tkmybatis/plugins/integration-tkmybatis-plugin/plugin.properties @@ -1,4 +1,4 @@ plugin.id=integration-tkmybatis-plugin plugin.class=com.tkmybatis.plugin.TkMybatisPlugin -plugin.version=2.4.0-RELEASE +plugin.version=2.4.1-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/integration-tkmybatis/plugins/integration-tkmybatis-plugin/pom.xml b/example/integration-tkmybatis/plugins/integration-tkmybatis-plugin/pom.xml index 326a9fa4176092d8941039173fc0ed2d321e74d8..678e485eca031adf82042043fc4ef67d6387c0af 100644 --- a/example/integration-tkmybatis/plugins/integration-tkmybatis-plugin/pom.xml +++ b/example/integration-tkmybatis/plugins/integration-tkmybatis-plugin/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-tkmybatis-plugin - 2.4.0-RELEASE + 2.4.1-RELEASE jar diff --git a/example/integration-tkmybatis/pom.xml b/example/integration-tkmybatis/pom.xml index 4895172e2f64dd8a4509ec62b280f854b2228cdf..81b2604c5f7ae4652a26cb5b0274d73cfd73b63b 100644 --- a/example/integration-tkmybatis/pom.xml +++ b/example/integration-tkmybatis/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-tkmybatis - 2.4.0-RELEASE + 2.4.1-RELEASE pom 集成mybatis-plus案例 diff --git a/example/pom.xml b/example/pom.xml index 2722f4e3ef44503017f32dbf1958c02e154264dd..3d16ee68faca35fa5c2e48692a5eced820071c8e 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues springboot-plugin-framework-example - 2.4.0-RELEASE + 2.4.1-RELEASE pom diff --git a/pom.xml b/pom.xml index 391f04475927b6d2b0d9ada8091a7f356d0bc5b9..842d5dca36e231a6b1ec17ca89f6bac49fd9a1a0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.gitee.starblues springboot-plugin-framework-parent pom - 2.4.0-RELEASE + 2.4.1-RELEASE spring boot 插件开发集成包 diff --git a/springboot-plugin-framework-extension/pom.xml b/springboot-plugin-framework-extension/pom.xml index bf68db69573b3f2a7fbd07fc4f8f1377801aac47..5a6ae27d377e51ac78f2a97ae91bace96f4a1bb8 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.4.0-RELEASE + 2.4.1-RELEASE spring boot 插件式开发集成包--扩展模块 diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/README.md b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/README.md index ecb5844e39a8f65f95dc0fc289a4d098f788caca..ec5f526caf1d9317957fc5f52ff60729fe42c280 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/README.md +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/README.md @@ -1,24 +1,21 @@ -### 特性 -- 支持在插件中自定义Mapper接口、Mapper xml 以及对应的实体bean -- 支持插件独立定义数据源 -- 支持集成 `Mybatis` -- 支持集成 `Mybatis-Plus` -- 支持集成`Tk-Mybatis` +### 集成Mybatis扩展 -### maven 仓库地址 +## 包含集成 +- 可集成`Mybatis` +- 可集成`Mybatis-Plus` +- 可集成`Tk-Mybatis` -https://mvnrepository.com/artifact/com.gitee.starblues/springboot-plugin-framework-extension-mybatis +## maven 仓库地址 -### 集成步骤 +[maven 仓库地址](https://mvnrepository.com/artifact/com.gitee.starblues/springboot-plugin-framework-extension-mybatis) -#### 主程序集成步骤 - -一. 引入依赖 +## 主程序集成步骤 +#### 引入依赖 ```xmml com.gitee.starblues springboot-plugin-framework-extension-mybatis - ${latest.version} + 2.4.0-RELEASE @@ -44,7 +41,7 @@ https://mvnrepository.com/artifact/com.gitee.starblues/springboot-plugin-framewo ``` -二. 配置扩展 +#### 配置扩展 定义PluginApplication bean时, 新增该扩展。 ```java @@ -63,10 +60,9 @@ public PluginApplication pluginApplication(){ - Mybatis-Plus类型为:`SpringBootMybatisExtension.Type.MYBATIS_PLUS` - Tk-Mybatis类型为:`SpringBootMybatisExtension.Type.TK_MYBATIS` +## 插件集成步骤 -#### 插件程序集成步骤 - -一. 以provided方式引入主程序依赖, 例如: +#### 以provided方式引入主程序依赖, 例如: ```xml com.gitee.starblues @@ -76,15 +72,15 @@ public PluginApplication pluginApplication(){ ``` -二. 进行配置 +#### 进行配置 -- 如果集成`mybatis`, 则实现接口:`com.gitee.starblues.extension.mybatis.SpringBootMybatisConfig` -- 如果集成`mybatis-plus`, 则实现接口:`com.gitee.starblues.extension.mybatis.SpringBootMybatisPlusConfig` -- 如果集成`tkmybatis`, 则实现接口:`com.gitee.starblues.extension.mybatis.SpringBootTkMybatisConfig` +- 如果集成`Mybatis`, 则实现接口:`com.gitee.starblues.extension.mybatis.SpringBootMybatisConfig` +- 如果集成`Mybatis-Plus`, 则实现接口:`com.gitee.starblues.extension.mybatis.SpringBootMybatisPlusConfig` +- 如果集成`TkMybatis`, 则实现接口:`com.gitee.starblues.extension.mybatis.SpringBootTkMybatisConfig` -- 以上实现类添加注解`@ConfigDefinition` +以上实现类添加注解`@ConfigDefinition` -例如集成mybatis-plus: +例如集成`Mybatis-Plus`: ```java @ConfigDefinition @@ -122,7 +118,7 @@ classpath路径-> classpath: xml/mapper/*PluginMapper.xml ``` -三. 定义的Mapper 接口需要加上注解 @Mapper +#### 定义的Mapper 接口需要加上注解 @Mapper 注解位置: org.apache.ibatis.annotations.Mapper @@ -154,7 +150,7 @@ public interface Plugin1Mapper { } ``` -### 如果插件不想使用主程序的配置或者数据源, 插件可自定义配置, 配置说明如下: +#### 如果插件不想使用主程序的配置或者数据源, 插件可自定义配置, 配置说明如下: 1. 实现`enableOneselfConfig`方法, 并设置返回值为true 2. 实现`oneselfConfig(xx)`方法进行独立配置 - Mybatis独立配置: @@ -205,28 +201,35 @@ public void oneselfConfig(Config config){ ``` -#### 集成Mybatis-Plus说明 +#### 重新主程序配置 +1. 实现各个配置的 `reSetMainConfig` 方法进行重写 +2. 重写后不影响主程序的配置, 只在当前插件中起作用 + +### 集成Mybatis-Plus说明 - 集成mybatis-plus后,在插件中无法使用 `LambdaQueryWrapper` 条件构造器 -### 版本升级 -#### 2.4.0 版本 -- 修改扩展功能中配置实现类,必须新增`@ConfigDefinition` 注解 +## 版本说明 +### `2.4.1-RELEASE` 版本 +1. 新增`Mybatis`、`Mybatis-Plus`、`Tk-Mybatis`扩展新增可重写覆盖主程序的配置(重写后会当前插件私有, 不影响主程序) +2. 修复`Mybatis-Plus`批量插入的bug -#### 2.2.5 版本 +### `2.4.0-RELEASE` 版本 +1. 修改扩展功能中配置实现类,必须新增`@ConfigDefinition` 注解 + +### `2.2.5-RELEASE` 版本 全新升级该扩展 1. 对 `Mybatis`、`Mybatis-Plus`、`Tk-Mybatis` 进行支持 2. 支持动态卸载和安装 3. 支持插件可独立进行配置, 与主程序和其他插件进行环境隔离 - -#### 2.1.3 版本 +### `2.1.3-RELEASE` 版本 跟随 springboot-plugin-framework 版本的部分类修改。升级到 2.1.3 -#### 2.1.1 版本 +### `2.1.1-RELEASE` 版本 1. 新增支持 Mybatis-Plus ServiceImpl的包装类。ServiceImplWrapper。使用详见`集成Mybatis-Plus说明` 2. 修复 Mapper.xml 中定义的 resultType 类型无法定义的bug。 -#### 2.0.3 版本 -1. 修复Mapper无法注入的bug. (由于springboot-plugin-framework 2.0.3 版本升级导致) \ No newline at end of file +### `2.0.3-RELEASE` 版本 +1. 修复Mapper无法注入的bug. (由于springboot-plugin-framework 2.0.3 版本升级导致) 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 99cd8e74d808dd5a1521697be996656651827c35..c9f36c4af52f1ff6e4558ea80ada89733a7ae412 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.4.0-RELEASE + 2.4.1-RELEASE jar 插件扩展-spring boot mybatis 集成扩展 @@ -64,7 +64,7 @@ 3.1.0 1.6 - 2.4.0-RELEASE + 2.4.1-RELEASE 2.0.1 3.4.1 2.1.5 diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/MybatisProcessor.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/MybatisProcessor.java index bcbd6c0388ea0ce45c7d3ffb147074895ebcc287..db83b7324b5387c644b0db8feb34407c3f68bbe3 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/MybatisProcessor.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/MybatisProcessor.java @@ -66,6 +66,7 @@ public class MybatisProcessor implements PluginBeanRegistrarExtend { configuration.getLanguageRegistry().register(languageDriver); } } + } PluginResourceFinder pluginResourceFinder = new PluginResourceFinder(pluginRegistryInfo); @@ -81,7 +82,7 @@ public class MybatisProcessor implements PluginBeanRegistrarExtend { ClassLoader defaultClassLoader = Resources.getDefaultClassLoader(); try { - Resources.setDefaultClassLoader(pluginRegistryInfo.getDefaultPluginClassLoader()); + Resources.setDefaultClassLoader(pluginRegistryInfo.getPluginClassLoader()); SqlSessionFactory sqlSessionFactory = factory.getObject(); if(sqlSessionFactory == null){ throw new Exception("Get mybatis sqlSessionFactory is null"); diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/PluginResourceFinder.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/PluginResourceFinder.java index 8756ad0bf1beaa77c0c33c55f8239eaa4628c2d6..45f19c6ee13025f75f4a47f0bf65729455e1f3b0 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/PluginResourceFinder.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/PluginResourceFinder.java @@ -36,7 +36,7 @@ public class PluginResourceFinder { public PluginResourceFinder(PluginRegistryInfo pluginRegistryInfo) { - this.classLoader = pluginRegistryInfo.getDefaultPluginClassLoader(); + this.classLoader = pluginRegistryInfo.getPluginClassLoader(); this.resourcePatternResolver = new PathMatchingResourcePatternResolver(classLoader);; } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/SpringBootMybatisConfig.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/SpringBootMybatisConfig.java index ab86ccb716c0779c3ce6360d1d0c24d174a93bc4..75ec57fe4b843fe174ba226cb29c56da4818f0fd 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/SpringBootMybatisConfig.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/SpringBootMybatisConfig.java @@ -1,5 +1,6 @@ package com.gitee.starblues.extension.mybatis; +import org.apache.ibatis.session.Configuration; import org.mybatis.spring.SqlSessionFactoryBean; /** @@ -18,5 +19,12 @@ public interface SpringBootMybatisConfig extends MybatisCommonConfig{ default void oneselfConfig(SqlSessionFactoryBean sqlSessionFactoryBean){ } + /** + * 重写配置当前跟随主程序的配置 + * 只有 enableOneselfConfig 返回 false, 实现该方法才生效 + * @param configuration Mybatis Configuration 的配置 + */ + default void reSetMainConfig(Configuration configuration){ + } } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/mybatisplus/MybatisPlusProcessor.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/mybatisplus/MybatisPlusProcessor.java index 32f808dbed5570e85be56f2946a87aabea5a8846..e372ec62660bd7fc2d2c66335cc85dfbc866e3f3 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/mybatisplus/MybatisPlusProcessor.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/mybatisplus/MybatisPlusProcessor.java @@ -1,10 +1,12 @@ package com.gitee.starblues.extension.mybatis.mybatisplus; import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties; +import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.core.config.GlobalConfig; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator; import com.baomidou.mybatisplus.core.injector.ISqlInjector; +import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.gitee.starblues.extension.mybatis.MapperHandler; import com.gitee.starblues.extension.mybatis.PluginFollowCoreConfig; @@ -20,6 +22,7 @@ import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.BeanUtils; import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.io.Resource; @@ -27,7 +30,7 @@ import org.springframework.core.io.Resource; /** * springboot-mybatis plus 处理者 * @author starBlues - * @version 2.4.0 + * @version 2.4.1 */ public class MybatisPlusProcessor implements PluginBeanRegistrarExtend { @@ -59,8 +62,9 @@ public class MybatisPlusProcessor implements PluginBeanRegistrarExtend { PluginFollowCoreConfig followCoreConfig = new PluginFollowCoreConfig( pluginRegistryInfo.getMainApplicationContext() ); + MybatisConfiguration mybatisPlusConfiguration = followCoreConfig.getMybatisPlusConfiguration(); factory.setDataSource(followCoreConfig.getDataSource()); - factory.setConfiguration(followCoreConfig.getMybatisPlusConfiguration()); + factory.setConfiguration(mybatisPlusConfiguration); Interceptor[] interceptor = followCoreConfig.getInterceptor(); if(interceptor != null && interceptor.length > 0){ factory.setPlugins(interceptor); @@ -73,8 +77,9 @@ public class MybatisPlusProcessor implements PluginBeanRegistrarExtend { if(languageDriver != null){ factory.setScriptingLanguageDrivers(languageDriver); } - // 配置mybatis私有的配置 - mybatisPlusFollowCoreConfig(factory, pluginRegistryInfo.getMainApplicationContext()); + // 配置mybatis-plus私有的配置 + GlobalConfig globalConfig = mybatisPlusFollowCoreConfig(factory, pluginRegistryInfo.getMainApplicationContext()); + config.reSetMainConfig(mybatisPlusConfiguration, globalConfig); } PluginResourceFinder pluginResourceFinder = new PluginResourceFinder(pluginRegistryInfo); @@ -90,7 +95,7 @@ public class MybatisPlusProcessor implements PluginBeanRegistrarExtend { } ClassLoader defaultClassLoader = Resources.getDefaultClassLoader(); try { - Resources.setDefaultClassLoader(pluginRegistryInfo.getDefaultPluginClassLoader()); + Resources.setDefaultClassLoader(pluginRegistryInfo.getPluginClassLoader()); SqlSessionFactory sqlSessionFactory = factory.getObject(); if(sqlSessionFactory == null){ throw new Exception("Get mybatis-plus sqlSessionFactory is null"); @@ -108,35 +113,21 @@ public class MybatisPlusProcessor implements PluginBeanRegistrarExtend { - private void mybatisPlusFollowCoreConfig(MybatisSqlSessionFactoryBean factory, - GenericApplicationContext parentApplicationContext){ - MybatisPlusProperties plusProperties = parentApplicationContext.getBean(MybatisPlusProperties.class); + private GlobalConfig mybatisPlusFollowCoreConfig(MybatisSqlSessionFactoryBean factory, + GenericApplicationContext mainApplicationContext){ + MybatisPlusProperties plusProperties = mainApplicationContext.getBean(MybatisPlusProperties.class); + GlobalConfig currentGlobalConfig = new GlobalConfig(); + currentGlobalConfig.setBanner(false); GlobalConfig globalConfig = plusProperties.getGlobalConfig(); - - if (parentApplicationContext.getBeanNamesForType(IKeyGenerator.class, false, - false).length > 0) { - IKeyGenerator keyGenerator = parentApplicationContext.getBean(IKeyGenerator.class); - globalConfig.getDbConfig().setKeyGenerator(keyGenerator); - } - - if (parentApplicationContext.getBeanNamesForType(MetaObjectHandler.class, - false, false).length > 0) { - MetaObjectHandler metaObjectHandler = parentApplicationContext.getBean(MetaObjectHandler.class); - globalConfig.setMetaObjectHandler(metaObjectHandler); - } - if (parentApplicationContext.getBeanNamesForType(IKeyGenerator.class, false, - false).length > 0) { - IKeyGenerator keyGenerator = parentApplicationContext.getBean(IKeyGenerator.class); - globalConfig.getDbConfig().setKeyGenerator(keyGenerator); - } - - if (parentApplicationContext.getBeanNamesForType(ISqlInjector.class, false, - false).length > 0) { - ISqlInjector iSqlInjector = parentApplicationContext.getBean(ISqlInjector.class); - globalConfig.setSqlInjector(iSqlInjector); + if(globalConfig != null){ + currentGlobalConfig.setDbConfig(globalConfig.getDbConfig()); + currentGlobalConfig.setIdentifierGenerator(globalConfig.getIdentifierGenerator()); + currentGlobalConfig.setMetaObjectHandler(globalConfig.getMetaObjectHandler()); + currentGlobalConfig.setSqlInjector(globalConfig.getSqlInjector()); } - factory.setGlobalConfig(globalConfig); + factory.setGlobalConfig(currentGlobalConfig); + return currentGlobalConfig; } } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/mybatisplus/SpringBootMybatisPlusConfig.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/mybatisplus/SpringBootMybatisPlusConfig.java index af4d315719cc54e8901709dc956a8a31089d83a4..0eeac071ff3111451a8f759f607a751c156fa605 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/mybatisplus/SpringBootMybatisPlusConfig.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/mybatisplus/SpringBootMybatisPlusConfig.java @@ -1,5 +1,7 @@ package com.gitee.starblues.extension.mybatis.mybatisplus; +import com.baomidou.mybatisplus.core.MybatisConfiguration; +import com.baomidou.mybatisplus.core.config.GlobalConfig; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.gitee.starblues.extension.mybatis.MybatisCommonConfig; @@ -20,5 +22,15 @@ public interface SpringBootMybatisPlusConfig extends MybatisCommonConfig { default void oneselfConfig(MybatisSqlSessionFactoryBean sqlSessionFactoryBean){ } + /** + * 重写设置配置 + * 只有 enableOneselfConfig 返回 false, 实现该方法才生效 + * @param configuration 当前 MybatisConfiguration + * @param globalConfig 当前全局配置GlobalConfig + */ + default void reSetMainConfig(MybatisConfiguration configuration, GlobalConfig globalConfig){ + + } + } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/tkmyabtis/SpringBootTkMybatisConfig.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/tkmyabtis/SpringBootTkMybatisConfig.java index 815fea723535d19635f1db6fad152c029e93f6c8..eeeb9ef541d6aa68cbe7e79869f2718334cd6a12 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/tkmyabtis/SpringBootTkMybatisConfig.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/tkmyabtis/SpringBootTkMybatisConfig.java @@ -1,6 +1,7 @@ package com.gitee.starblues.extension.mybatis.tkmyabtis; import com.gitee.starblues.extension.mybatis.MybatisCommonConfig; +import org.apache.ibatis.session.Configuration; import org.mybatis.spring.SqlSessionFactoryBean; import tk.mybatis.mapper.entity.Config; @@ -17,17 +18,19 @@ public interface SpringBootTkMybatisConfig extends MybatisCommonConfig { * 插件自主配置Mybatis的 SqlSessionFactoryBean * SqlSessionFactoryBean 具体配置说明参考 Mybatis 官网 * @param sqlSessionFactoryBean SqlSessionFactoryBean + * @param config 插件自主配置tk的 Config 具体配置说明参考 https://gitee.com/free/Mapper/wikis/1.1-java?sort_id=208196 */ - default void oneselfConfig(SqlSessionFactoryBean sqlSessionFactoryBean){ + default void oneselfConfig(SqlSessionFactoryBean sqlSessionFactoryBean, Config config){ } /** - * 插件自主配置tk的 Config - * Config 具体配置说明参考 https://gitee.com/free/Mapper/wikis/1.1-java?sort_id=208196 - * @param config Config + * 重写配置当前跟随主程序的配置 + * 只有 enableOneselfConfig 返回 false, 实现该方法才生效 + * @param config 插件自主配置tk的 Config 具体配置说明参考 https://gitee.com/free/Mapper/wikis/1.1-java?sort_id=208196 + * @param configuration Mybatis Configuration 的配置 */ - default void oneselfConfig(Config config){ - } + default void reSetMainConfig(Configuration configuration, Config config){ + } } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/tkmyabtis/TkMybatisProcessor.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/tkmyabtis/TkMybatisProcessor.java index e735168b6f5ee731bc1aa1fb92d00deb7632fc0a..427b60ff01e3418b6a455953e9e4745eaea6d226 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/tkmyabtis/TkMybatisProcessor.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/tkmyabtis/TkMybatisProcessor.java @@ -10,6 +10,7 @@ import com.gitee.starblues.utils.SpringBeanUtils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.mapping.DatabaseIdProvider; import org.apache.ibatis.plugin.Interceptor; +import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; @@ -57,14 +58,14 @@ public class TkMybatisProcessor implements PluginBeanRegistrarExtend { Config tkConfig = null; if(config.enableOneselfConfig()){ - config.oneselfConfig(factory); tkConfig = new Config(); - config.oneselfConfig(tkConfig); + config.oneselfConfig(factory, tkConfig); } else { GenericApplicationContext mainApplicationContext = pluginRegistryInfo.getMainApplicationContext(); PluginFollowCoreConfig followCoreConfig = new PluginFollowCoreConfig(mainApplicationContext); factory.setDataSource(followCoreConfig.getDataSource()); - factory.setConfiguration(followCoreConfig.getConfiguration(SpringBootMybatisExtension.Type.TK_MYBATIS)); + Configuration configuration = followCoreConfig.getConfiguration(SpringBootMybatisExtension.Type.TK_MYBATIS); + factory.setConfiguration(configuration); Interceptor[] interceptor = followCoreConfig.getInterceptor(); if(interceptor != null && interceptor.length > 0){ factory.setPlugins(interceptor); @@ -77,6 +78,7 @@ public class TkMybatisProcessor implements PluginBeanRegistrarExtend { false, false).length > 0){ tkConfig = mainApplicationContext.getBean(Config.class); } + config.reSetMainConfig(configuration, tkConfig); } MapperHelper mapperHelper = new MapperHelper(); @@ -96,7 +98,7 @@ public class TkMybatisProcessor implements PluginBeanRegistrarExtend { if(xmlResource != null && xmlResource.length > 0){ factory.setMapperLocations(xmlResource); } - ClassLoader pluginClassLoader = pluginRegistryInfo.getDefaultPluginClassLoader(); + ClassLoader pluginClassLoader = pluginRegistryInfo.getPluginClassLoader(); ClassLoader defaultClassLoader = Resources.getDefaultClassLoader(); ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/README.md b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/README.md index e6c74e165816f263920c3c737a122930921d417a..14eb258c2e9b6c27ef58a13f086040d920285c0e 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/README.md +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/README.md @@ -1,25 +1,25 @@ -### 特性 -- 支持通过http访问插件中静态资源 -- 支持集成`Thymeleaf` +### 集成静态资源扩展 +## 包含内容 +- 可集成插件静态资源访问 +- 可集成`Thymeleaf` -### maven 仓库地址 +## maven 仓库地址 -https://mvnrepository.com/artifact/com.gitee.starblues/springboot-plugin-framework-extension-mybatis +[maven 仓库地址](https://mvnrepository.com/artifact/com.gitee.starblues/springboot-plugin-framework-extension-mybatis) -### 集成步骤 -#### 主程序集成步骤 +## 主程序集成步骤 -一. 引入依赖 +#### 引入依赖 ```xmml com.gitee.starblues springboot-plugin-framework-extension-resources - ${latest.version} + 2.4.0-RELEASE ``` -二. 配置扩展 +#### 配置扩展 定义PluginApplication bean时, 新增该扩展。 ```java @@ -39,9 +39,9 @@ public PluginApplication pluginApplication(){ **插件的http资源url访问规则为: http://ip:port/pathPrefix(上述说明配置的值)/插件id/具体插件的资源路径** -#### 插件程序集成步骤 +## 插件程序集成步骤 -一. 以provided方式引入主程序依赖, 例如: +#### 以`provided`方式引入主程序依赖, 例如: ```xml com.gitee.starblues @@ -51,7 +51,7 @@ public PluginApplication pluginApplication(){ ``` -二. 进行配置 +#### 进行配置 1. 配置集成静态资源 @@ -97,22 +97,26 @@ public class ResourceConfig implements SpringBootThymeleafConfig { } ``` +## 注意事项 +**插件中`resources`中存放的资源文件目录一定不能和主程序相同, 否则就会加载到主程序的资源** +- 例如: 主程序在`resources`中定义了 `web` 文件夹. 插件中的`resources`中不能再定义`web`文件夹来存放静态资源 -### 版本升级 +## 版本说明 -#### 2.4.0 版本 -- 修改扩展功能中配置实现类,必须新增`@ConfigDefinition` 注解 -- 修复插件中的静态资源和主程序冲突的bug +### `2.4.1-RELEASE` 版本 +1. 修复静态资源无法访问的bug -#### 2.2.5 版本 +#### `2.4.0-RELEASE` 版本 +1. 修改扩展功能中配置实现类,必须新增`@ConfigDefinition` 注解 +2. 修复插件中的静态资源和主程序冲突的bug + +#### `2.2.5-RELEASE` 版本 1. 新增`Thymeleaf`模板引擎 2. 修改插件中扩展的配置方式 -#### 2.2.1 版本 +#### `2.2.1-RELEASE` 版本 修改该扩展导致无法卸载插件的bug -#### 2.1.1 版本 -新增当前扩展 - - +#### `2.1.1-RELEASE` 版本 +新增当前扩展 \ No newline at end of file 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 159c675cada11fd581721fb59c4d11be8d2566e1..83b9e4428926e6c4ed1542ea3f3f52df8126ec94 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.4.0-RELEASE + 2.4.1-RELEASE jar 插件扩展-通过url读取插件中的静态资源 @@ -69,7 +69,7 @@ 5.0.7.RELEASE 4.0.1 - 2.4.0-RELEASE + 2.4.1-RELEASE 2.0.3.RELEASE 2.1.1.RELEASE diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/StaticResourceExtension.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/StaticResourceExtension.java index 64512ffd631c4951c359a0988b3ae1b2b1c5563f..b4c95c73917f090122dd61f50fc3d5c260090b68 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/StaticResourceExtension.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/StaticResourceExtension.java @@ -10,8 +10,7 @@ import org.springframework.http.CacheControl; import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import java.util.concurrent.TimeUnit; /** @@ -33,9 +32,18 @@ public class StaticResourceExtension extends AbstractExtension { * 访问静态资源的缓存控制。默认最大1小时。主要针对http协议的缓存。 */ private static CacheControl pluginStaticResourcesCacheControl = - CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic();; + CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic(); + private final Set includes = new HashSet<>(1); + public StaticResourceExtension(){ + } + + public StaticResourceExtension(Include... includes){ + if(includes != null){ + this.includes.addAll(Arrays.asList(includes)); + } + } @Override public String key() { @@ -54,9 +62,12 @@ public class StaticResourceExtension extends AbstractExtension { @Override public List getPluginPipeProcessor(ApplicationContext mainApplicationContext) { - final List pluginPipeProcessorExtends = new ArrayList<>(); - pluginPipeProcessorExtends.add(new ThymeleafProcessor()); - return pluginPipeProcessorExtends; + if(includes.contains(Include.THYMELEAF)){ + final List pluginPipeProcessorExtends = new ArrayList<>(1); + pluginPipeProcessorExtends.add(new ThymeleafProcessor()); + return pluginPipeProcessorExtends; + } + return null; } @Override @@ -96,4 +107,8 @@ public class StaticResourceExtension extends AbstractExtension { return pluginStaticResourcesCacheControl; } + + public enum Include{ + THYMELEAF + } } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/resolver/PluginResourceResolver.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/resolver/PluginResourceResolver.java index deee5477eac28c3ae194e8f703444c492bf3db5b..7b4a32c004c036d2caa7d666078cd4263f9ddf11 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/resolver/PluginResourceResolver.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/resolver/PluginResourceResolver.java @@ -100,7 +100,7 @@ public class PluginResourceResolver extends AbstractResourceResolver { return null; } - ClassLoader pluginClassLoader = pluginRegistryInfo.getPluginClassLoader(PluginRegistryInfo.ClassLoaderStrategy.PDA); + ClassLoader pluginClassLoader = pluginRegistryInfo.getPluginClassLoader(); for (String classPath : classPaths) { try { PluginResource resource = new PluginResource(classPath + partialPath, pluginRegistryInfo); diff --git a/springboot-plugin-framework/pom.xml b/springboot-plugin-framework/pom.xml index 8b27d1e1bae0b70cd457b2d2b9517fae980b6f38..0acd88db0953d0b3b792466d6fa8c6559f396067 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.4.0-RELEASE + 2.4.1-RELEASE spring boot 插件式开发集成包 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/annotation/Extract.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/annotation/Extract.java new file mode 100644 index 0000000000000000000000000000000000000000..bd6449987b5dbd31cbe641bf69b1f2d4481c8231 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/annotation/Extract.java @@ -0,0 +1,33 @@ +package com.gitee.starblues.annotation; + +import java.lang.annotation.*; + +/** + * 基于业务的扩展注解 + * @author starBlues + * @version 2.4.1 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Extract { + + /** + * 业务 + * @return 业务标志 + */ + String bus(); + + /** + * 场景 + * @return 场景标志 + */ + String scene() default ""; + + /** + * 用例 + * @return 用例标志 + */ + String useCase() default ""; + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/PluginRegistryInfo.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/PluginRegistryInfo.java index 7577217d12d6e88e179e523e3d2e78c92484ae0f..338ad1d0d538a01a026a8e36b6ec47c87350157f 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/PluginRegistryInfo.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/PluginRegistryInfo.java @@ -21,7 +21,7 @@ import java.util.concurrent.ConcurrentHashMap; * 注册的插件信息 * * @author starBlues - * @version 2.4.0 + * @version 2.4.1 */ public class PluginRegistryInfo { @@ -70,12 +70,6 @@ public class PluginRegistryInfo { */ private final Map processorInfo = new ConcurrentHashMap<>(8); - /** - * 自定义策略插件类加载器缓存 - */ - private final Map pluginClassLoaders = new ConcurrentHashMap<>(8); - - private PluginRegistryInfo(PluginWrapper pluginWrapper, PluginManager pluginManager, GenericApplicationContext mainApplicationContext, @@ -289,49 +283,10 @@ public class PluginRegistryInfo { } } - public ClassLoader getDefaultPluginClassLoader(){ + public ClassLoader getPluginClassLoader(){ return pluginWrapper.getPluginClassLoader(); } - public ClassLoader getPluginClassLoader(ClassLoaderStrategy strategy){ - PluginClassLoader pluginClassLoader = pluginClassLoaders.get(strategy); - if(pluginClassLoader != null){ - return pluginClassLoader; - } - ClassLoadingStrategy classLoadingStrategy = null; - switch (strategy){ - case APD: - classLoadingStrategy = ClassLoadingStrategy.APD; - break; - case ADP: - classLoadingStrategy = ClassLoadingStrategy.ADP; - break; - case PAD: - classLoadingStrategy = ClassLoadingStrategy.PAD; - break; - case DAP: - classLoadingStrategy = ClassLoadingStrategy.DAP; - break; - case DPA: - classLoadingStrategy = ClassLoadingStrategy.DPA; - break; - case PDA: - classLoadingStrategy = ClassLoadingStrategy.PDA; - break; - } - - pluginClassLoader = new PluginClassLoader(pluginManager, pluginWrapper.getDescriptor(), - this.getClass().getClassLoader(), classLoadingStrategy); - pluginClassLoader.addFile(pluginWrapper.getPluginPath().toFile()); - pluginClassLoaders.put(strategy, pluginClassLoader); - return pluginClassLoader; - } - - public List getPluginClassLoaders(){ - return Collections.unmodifiableList(new ArrayList<>(pluginClassLoaders.values())); - } - - public boolean isFollowingInitial() { return followingInitial; } @@ -346,22 +301,6 @@ public class PluginRegistryInfo { logger.error("Close plugin '{}'-ApplicationContext failure", getPluginWrapper().getPluginId(), e); } - // 关闭ClassClassLoader - try { - for (ClassLoader pluginClassLoader : pluginClassLoaders.values()) { - if (pluginClassLoader instanceof Closeable) { - try { - ((Closeable) pluginClassLoader).close(); - } catch (IOException e) { - logger.error("Close plugin '{}'-ClassLoader-'{}' failure", getPluginWrapper().getPluginId(), - pluginClassLoader.getClass().getName(), e); - } - } - } - } finally { - pluginClassLoaders.clear(); - } - // 清除数据集合 try { extensionMap.clear(); @@ -377,19 +316,11 @@ public class PluginRegistryInfo { private void closePluginApplicationContext() { try { - PluginPipeApplicationContextProcessor.removeBeanExtend(this); + getSpringBeanRegister().destroySingletons(); pluginApplicationContext.close(); } catch (Exception e){ logger.error("Close plugin '{}' ApplicationContext failure", getPluginWrapper().getPluginId(), e); } } - - public enum ClassLoaderStrategy{ - APD, ADP, PAD, DAP, DPA, PDA - } - - - - } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/SpringBeanRegister.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/SpringBeanRegister.java index 7c8f6a53d0cbd85189c9845c2e4566edf97c441b..ddb1393b8c9825e441c401ef8c911752f3209ffb 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/SpringBeanRegister.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/SpringBeanRegister.java @@ -15,21 +15,21 @@ import java.util.function.Consumer; * Spring bean注册者, 向Spring注册Bean时, 必须使用该对象进行注册 * * @author starBlues - * @version 2.4.0 + * @version 2.4.1 */ public class SpringBeanRegister { private static final Logger logger = LoggerFactory.getLogger(SpringBeanRegister.class); - private final GenericApplicationContext pluginApplicationContext; + private final GenericApplicationContext applicationContext; - public SpringBeanRegister(GenericApplicationContext pluginApplicationContext){ - this.pluginApplicationContext = pluginApplicationContext; + public SpringBeanRegister(GenericApplicationContext applicationContext){ + this.applicationContext = applicationContext; } public boolean exist(String name){ - return pluginApplicationContext.containsBean(name); + return applicationContext.containsBean(name); } /** @@ -56,9 +56,9 @@ public class SpringBeanRegister { beanDefinition.setBeanClass(aClass); BeanNameGenerator beanNameGenerator = new PluginAnnotationBeanNameGenerator(pluginId); - String beanName = beanNameGenerator.generateBeanName(beanDefinition, pluginApplicationContext); + String beanName = beanNameGenerator.generateBeanName(beanDefinition, applicationContext); - if(pluginApplicationContext.containsBean(beanName)){ + if(applicationContext.containsBean(beanName)){ String error = MessageFormat.format("Bean name {0} already exist of {1}", beanName, aClass.getName()); logger.debug(error); @@ -67,7 +67,7 @@ public class SpringBeanRegister { if(consumer != null){ consumer.accept(beanDefinition); } - pluginApplicationContext.registerBeanDefinition(beanName, beanDefinition); + applicationContext.registerBeanDefinition(beanName, beanDefinition); return beanName; } @@ -94,7 +94,7 @@ public class SpringBeanRegister { Consumer consumer) { AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(aClass); - if(pluginApplicationContext.containsBean(beanName)){ + if(applicationContext.containsBean(beanName)){ String error = MessageFormat.format("Bean name {0} already exist of {1}", beanName, aClass.getName()); throw new RuntimeException(error); @@ -102,7 +102,7 @@ public class SpringBeanRegister { if(consumer != null){ consumer.accept(beanDefinition); } - pluginApplicationContext.registerBeanDefinition(beanName, beanDefinition); + applicationContext.registerBeanDefinition(beanName, beanDefinition); } /** @@ -111,7 +111,7 @@ public class SpringBeanRegister { * @param object 对象 */ public void registerSingleton(String name, Object object){ - DefaultListableBeanFactory listableBeanFactory = pluginApplicationContext.getDefaultListableBeanFactory(); + DefaultListableBeanFactory listableBeanFactory = applicationContext.getDefaultListableBeanFactory(); if(!listableBeanFactory.containsSingleton(name)){ listableBeanFactory.registerSingleton(name, object); } @@ -123,12 +123,20 @@ public class SpringBeanRegister { * @param name 单例名称 */ public void destroySingleton(String name){ - DefaultListableBeanFactory listableBeanFactory = pluginApplicationContext.getDefaultListableBeanFactory(); + DefaultListableBeanFactory listableBeanFactory = applicationContext.getDefaultListableBeanFactory(); if(listableBeanFactory.containsSingleton(name)){ listableBeanFactory.destroySingleton(name); } } + /** + * 销毁所有单例 + */ + public void destroySingletons(){ + DefaultListableBeanFactory listableBeanFactory = applicationContext.getDefaultListableBeanFactory(); + listableBeanFactory.destroySingletons(); + } + /** * 卸载bean * @param pluginId 插件id @@ -136,7 +144,7 @@ public class SpringBeanRegister { */ public void unregister(String pluginId, String beanName){ try { - pluginApplicationContext.removeBeanDefinition(beanName); + applicationContext.removeBeanDefinition(beanName); } catch (Exception e){ logger.error("Remove plugin '{}' bean {} error. {}", pluginId, beanName, e.getMessage()); } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginInfoContainers.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginInfoContainers.java index 81fea362f88cd33b099b13d2d9fb190ecc451da4..5afe94ec9f0d9dec5069fe96873a8426ba90f2dc 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginInfoContainers.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginInfoContainers.java @@ -15,8 +15,6 @@ public class PluginInfoContainers { private final static Map PLUGIN_APPLICATION_CONTEXTS = new ConcurrentHashMap<>(); - - public static void addPluginApplicationContext(String pluginId, GenericApplicationContext applicationContext){ PLUGIN_APPLICATION_CONTEXTS.put(pluginId, applicationContext); } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginInterceptorsPipeProcessor.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginInterceptorsPipeProcessor.java new file mode 100644 index 0000000000000000000000000000000000000000..9783987a38d3bd72ee819d26da4cbdc9eeb4ee82 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginInterceptorsPipeProcessor.java @@ -0,0 +1,129 @@ +package com.gitee.starblues.factory.process.pipe; + +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.process.pipe.interceptor.PluginInterceptorRegister; +import com.gitee.starblues.factory.process.pipe.interceptor.PluginInterceptorRegistry; +import com.gitee.starblues.integration.IntegrationConfiguration; +import com.gitee.starblues.utils.ClassUtils; +import com.gitee.starblues.utils.CommonUtils; +import com.gitee.starblues.utils.SpringBeanUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.web.context.request.WebRequestInterceptor; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.handler.AbstractHandlerMapping; +import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 插件 SpringMVC 拦截器的处理 + * @author starBlues + * @version 2.4.1 + */ +public class PluginInterceptorsPipeProcessor implements PluginPipeProcessor{ + + private final ApplicationContext mainApplicationContext; + private final IntegrationConfiguration configuration; + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final static String INTERCEPTORS = "interceptors"; + + private AbstractHandlerMapping handlerMapping; + + + public PluginInterceptorsPipeProcessor(ApplicationContext mainApplicationContext){ + this.mainApplicationContext = mainApplicationContext; + this.configuration = mainApplicationContext.getBean(IntegrationConfiguration.class); + } + + @Override + public void initialize() throws Exception { + handlerMapping = SpringBeanUtils.getExistBean(mainApplicationContext, + AbstractHandlerMapping.class); + if(handlerMapping == null){ + logger.warn("Not found AbstractHandlerMapping, Plugin interceptor can't use"); + } + } + + @Override + public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + if(handlerMapping == null){ + return; + } + GenericApplicationContext pluginApplicationContext = pluginRegistryInfo.getPluginApplicationContext(); + List interceptorRegisters = SpringBeanUtils.getBeans(pluginApplicationContext, + PluginInterceptorRegister.class); + List interceptorsObjects = new ArrayList<>(); + List adaptedInterceptors = getAdaptedInterceptors(); + if(adaptedInterceptors == null){ + return; + } + String pluginRestPrefix = CommonUtils.getPluginRestPrefix(configuration, pluginRegistryInfo.getPluginWrapper().getPluginId()); + + for (PluginInterceptorRegister interceptorRegister : interceptorRegisters) { + PluginInterceptorRegistry interceptorRegistry = new PluginInterceptorRegistry(pluginRestPrefix); + interceptorRegister.registry(interceptorRegistry); + List interceptors = interceptorRegistry.getInterceptors(); + if(interceptors == null || interceptors.isEmpty()){ + continue; + } + for (Object interceptor : interceptors) { + HandlerInterceptor handlerInterceptor = adaptInterceptor(interceptor); + adaptedInterceptors.add(handlerInterceptor); + interceptorsObjects.add(handlerInterceptor); + } + } + pluginRegistryInfo.addExtension(INTERCEPTORS, interceptorsObjects); + } + + @Override + public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + if(handlerMapping == null){ + return; + } + List interceptorsObjects = pluginRegistryInfo.getExtension(INTERCEPTORS); + if(interceptorsObjects == null || interceptorsObjects.isEmpty()){ + return; + } + List adaptedInterceptors = getAdaptedInterceptors(); + if(adaptedInterceptors == null){ + return; + } + for (HandlerInterceptor interceptor : interceptorsObjects) { + adaptedInterceptors.remove(interceptor); + } + } + + /** + * 得到拦截器存储者 + * @return List + */ + private List getAdaptedInterceptors(){ + try { + return ClassUtils.getReflectionField(handlerMapping, "adaptedInterceptors", List.class); + } catch (IllegalAccessException e) { + logger.error("Can't get 'adaptedInterceptors' from AbstractHandlerMapping, so " + + "You can't use HandlerInterceptor. {} ", e.getMessage()); + return null; + } + } + + /** + * 转换拦截器 + * @param interceptor interceptor + * @return HandlerInterceptor + */ + private HandlerInterceptor adaptInterceptor(Object interceptor) { + if (interceptor instanceof HandlerInterceptor) { + return (HandlerInterceptor) interceptor; + } else if (interceptor instanceof WebRequestInterceptor) { + return new WebRequestHandlerInterceptorAdapter((WebRequestInterceptor) interceptor); + } else { + throw new IllegalArgumentException("Interceptor type not supported: " + interceptor.getClass().getName()); + } + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginPipeApplicationContextProcessor.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginPipeApplicationContextProcessor.java index 0b0757e92818e6a19b38ab0e0f5fc4aaaa677673..dc696556630a2eedc1274709b6fcbd74b2024884 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginPipeApplicationContextProcessor.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginPipeApplicationContextProcessor.java @@ -3,13 +3,15 @@ package com.gitee.starblues.factory.process.pipe; import com.gitee.starblues.extension.ExtensionInitializer; import com.gitee.starblues.factory.PluginRegistryInfo; import com.gitee.starblues.factory.process.pipe.bean.*; -import com.gitee.starblues.realize.PluginUtils; -import org.pf4j.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericApplicationContext; +import org.springframework.web.servlet.HandlerExecutionChain; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.handler.AbstractHandlerMapping; +import javax.servlet.ServletContext; import java.util.*; /** @@ -25,12 +27,14 @@ public class PluginPipeApplicationContextProcessor implements PluginPipeProcesso private final List pluginBeanDefinitionRegistrars = new ArrayList<>(); private final ApplicationContext mainApplicationContext; + public PluginPipeApplicationContextProcessor(ApplicationContext mainApplicationContext) { this.mainApplicationContext = mainApplicationContext; } @Override public void initialize() throws Exception { + pluginBeanDefinitionRegistrars.add(new PluginInsetBeanRegistrar()); pluginBeanDefinitionRegistrars.add(new ConfigBeanRegistrar()); pluginBeanDefinitionRegistrars.add(new ConfigFileBeanRegistrar(mainApplicationContext)); pluginBeanDefinitionRegistrars.add(new BasicBeanRegistrar()); @@ -45,10 +49,9 @@ public class PluginPipeApplicationContextProcessor implements PluginPipeProcesso for (PluginBeanRegistrar pluginBeanDefinitionRegistrar : pluginBeanDefinitionRegistrars) { pluginBeanDefinitionRegistrar.registry(pluginRegistryInfo); } - addBeanExtend(pluginRegistryInfo); ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { - Thread.currentThread().setContextClassLoader(pluginRegistryInfo.getDefaultPluginClassLoader()); + Thread.currentThread().setContextClassLoader(pluginRegistryInfo.getPluginClassLoader()); pluginApplicationContext.refresh(); } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); @@ -72,34 +75,4 @@ public class PluginPipeApplicationContextProcessor implements PluginPipeProcesso } } - - /** - * 向插件ApplicationContext容器中添加扩展的bean - * @param pluginRegistryInfo 插件注册信息 - */ - private void addBeanExtend(PluginRegistryInfo pluginRegistryInfo){ - GenericApplicationContext parentApplicationContext = pluginRegistryInfo.getMainApplicationContext(); - GenericApplicationContext pluginApplicationContext = pluginRegistryInfo.getPluginApplicationContext(); - PluginUtils pluginUtils = new PluginUtils(parentApplicationContext, - pluginApplicationContext, - pluginRegistryInfo.getPluginWrapper().getDescriptor()); - String name = pluginUtils.getClass().getName(); - pluginApplicationContext.getBeanFactory().registerSingleton(name, pluginUtils); - pluginRegistryInfo.addExtension("PluginUtilsName", name); - } - - - /** - * 移除扩展绑定 - * @param pluginRegistryInfo 插件注册信息 - */ - public static void removeBeanExtend(PluginRegistryInfo pluginRegistryInfo) { - String pluginUtilsName = pluginRegistryInfo.getExtension("PluginUtilsName"); - if(StringUtils.isNullOrEmpty(pluginUtilsName)){ - return; - } - GenericApplicationContext pluginApplicationContext = pluginRegistryInfo.getPluginApplicationContext(); - pluginApplicationContext.getDefaultListableBeanFactory().destroySingleton(pluginUtilsName); - } - } 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 e75b2608d05777727dd00adb47348f12b0d20e24..23a604c881fc35ddf1ae9b25443767118aa32d10 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 @@ -3,6 +3,7 @@ package com.gitee.starblues.factory.process.pipe; import com.gitee.starblues.extension.ExtensionInitializer; import com.gitee.starblues.factory.PluginRegistryInfo; import com.gitee.starblues.factory.process.pipe.classs.PluginClassProcess; +import com.gitee.starblues.factory.process.pipe.extract.PluginExtractPipeProcessor; import com.gitee.starblues.factory.process.pipe.loader.PluginResourceLoadFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,11 +22,11 @@ public class PluginPipeProcessorFactory implements PluginPipeProcessor { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private final ApplicationContext applicationContext; + private final ApplicationContext mainApplicationContext; private final List pluginPipeProcessors = new ArrayList<>(); - public PluginPipeProcessorFactory(ApplicationContext applicationContext){ - this.applicationContext = applicationContext; + public PluginPipeProcessorFactory(ApplicationContext mainApplicationContext){ + this.mainApplicationContext = mainApplicationContext; } @@ -40,9 +41,13 @@ public class PluginPipeProcessorFactory implements PluginPipeProcessor { // 添加前置扩展 pluginPipeProcessors.addAll(ExtensionInitializer.getPreProcessorExtends()); // 插件的ApplicationContext处理者 - pluginPipeProcessors.add(new PluginPipeApplicationContextProcessor(applicationContext)); + pluginPipeProcessors.add(new PluginPipeApplicationContextProcessor(mainApplicationContext)); + // 拦截器处理者 + pluginPipeProcessors.add(new PluginInterceptorsPipeProcessor(mainApplicationContext)); // 插件ConfigBean处理者 pluginPipeProcessors.add(new PluginConfigBeanPipeProcessor()); + // 插件扩展的流处理者 + pluginPipeProcessors.add(new PluginExtractPipeProcessor(mainApplicationContext)); // 添加扩展 pluginPipeProcessors.addAll(ExtensionInitializer.getPipeProcessorExtends()); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/BasicBeanRegistrar.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/BasicBeanRegistrar.java index 69077fbdf4e2ffb541b7c37094624902f0c578e8..5e8fedf474db2135bb460d7109fcc716fa7c150b 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/BasicBeanRegistrar.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/BasicBeanRegistrar.java @@ -50,7 +50,7 @@ public class BasicBeanRegistrar implements PluginBeanRegistrar { if(aClass == null){ continue; } - String beanName = springBeanRegister.register(pluginId, aClass); + springBeanRegister.register(pluginId, aClass); } } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/PluginInsetBeanRegistrar.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/PluginInsetBeanRegistrar.java new file mode 100644 index 0000000000000000000000000000000000000000..02557b19109bca6d63466d7164e08c02d10a7180 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/PluginInsetBeanRegistrar.java @@ -0,0 +1,46 @@ +package com.gitee.starblues.factory.process.pipe.bean; + +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.SpringBeanRegister; +import com.gitee.starblues.factory.process.pipe.bean.inset.ExtractFactoryInset; +import com.gitee.starblues.factory.process.pipe.bean.inset.PluginInsetBean; +import com.gitee.starblues.factory.process.pipe.bean.inset.PluginUtilsInset; +import org.pf4j.util.StringUtils; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * 系统内嵌的Bean注册者 + * @author starBlues + * @version 1.0 + */ +public class PluginInsetBeanRegistrar implements PluginBeanRegistrar{ + + private final List pluginInsetBeans = new ArrayList<>(2); + + public PluginInsetBeanRegistrar() { + this.pluginInsetBeans.add(new PluginUtilsInset()); + this.pluginInsetBeans.add(new ExtractFactoryInset()); + } + + + @Override + public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + SpringBeanRegister springBeanRegister = pluginRegistryInfo.getSpringBeanRegister(); + for (PluginInsetBean pluginInsetBean : this.pluginInsetBeans) { + String beanName = pluginInsetBean.getBeanName(); + Object bean = pluginInsetBean.getBean(pluginRegistryInfo); + if(bean == null){ + continue; + } + if(StringUtils.isNullOrEmpty(beanName)){ + beanName = bean.getClass().getName(); + } + springBeanRegister.registerSingleton(beanName, bean); + } + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/ExtractFactoryInset.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/ExtractFactoryInset.java new file mode 100644 index 0000000000000000000000000000000000000000..26db0afce34003ff921f9473d1b775754f7f9436 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/ExtractFactoryInset.java @@ -0,0 +1,22 @@ +package com.gitee.starblues.factory.process.pipe.bean.inset; + +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.process.pipe.extract.ExtractFactory; + +/** + * ExtractFactory 扩展工厂注册者 + * @author starBlues + * @version 2.4.1 + */ +public class ExtractFactoryInset implements PluginInsetBean{ + + @Override + public String getBeanName() { + return ExtractFactoryInset.class.getName(); + } + + @Override + public Object getBean(PluginRegistryInfo pluginRegistryInfo) { + return ExtractFactory.getInstant(); + } +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/PluginInsetBean.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/PluginInsetBean.java new file mode 100644 index 0000000000000000000000000000000000000000..924086a61ef9cfe8df686b2ebb25813156bb04bb --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/PluginInsetBean.java @@ -0,0 +1,23 @@ +package com.gitee.starblues.factory.process.pipe.bean.inset; + +import com.gitee.starblues.factory.PluginRegistryInfo; + +/** + * @author starBlues + * @version 2.4.1 + */ +public interface PluginInsetBean { + + /** + * 得到bean名称 + * @return bean 名称 + */ + String getBeanName(); + + /** + * 得到bean对象 + * @return 对象 + */ + Object getBean(PluginRegistryInfo pluginRegistryInfo); + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/PluginUtilsInset.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/PluginUtilsInset.java new file mode 100644 index 0000000000000000000000000000000000000000..5d720d9fd446c8131a25adc58d343b58d7d0bfe2 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/inset/PluginUtilsInset.java @@ -0,0 +1,26 @@ +package com.gitee.starblues.factory.process.pipe.bean.inset; + +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.realize.PluginUtils; +import org.springframework.context.support.GenericApplicationContext; + +/** + * PluginUtils 对象注册者 + * @author starBlues + * @version 2.4.1 + */ +public class PluginUtilsInset implements PluginInsetBean{ + @Override + public String getBeanName() { + return "PluginUtilsName"; + } + + @Override + public Object getBean(PluginRegistryInfo pluginRegistryInfo) { + GenericApplicationContext parentApplicationContext = pluginRegistryInfo.getMainApplicationContext(); + GenericApplicationContext pluginApplicationContext = pluginRegistryInfo.getPluginApplicationContext(); + return new PluginUtils(parentApplicationContext, + pluginApplicationContext, + pluginRegistryInfo.getPluginWrapper().getDescriptor()); + } +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ComponentGroup.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ComponentGroup.java index fd76f16d9b8047ec7f2d9197be13b55166c714b9..99eaed96abb4ff0a54baebbafcb1c74de2828bd0 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ComponentGroup.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ComponentGroup.java @@ -1,5 +1,6 @@ package com.gitee.starblues.factory.process.pipe.classs.group; +import com.gitee.starblues.annotation.Extract; import com.gitee.starblues.factory.process.pipe.classs.PluginClassGroup; import com.gitee.starblues.realize.BasePlugin; import com.gitee.starblues.utils.AnnotationsUtils; @@ -9,6 +10,8 @@ import org.springframework.stereotype.Controller; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RestController; +import java.lang.annotation.Annotation; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; @@ -50,8 +53,10 @@ public class ComponentGroup implements PluginClassGroup { @Override public boolean filter(Class aClass) { - boolean have = AnnotationsUtils.haveAnnotations(aClass, false, Component.class, Service.class, - Controller.class, RestController.class, Configuration.class); + boolean have = AnnotationsUtils.haveAnnotations(aClass, false, + Component.class, Service.class, + Controller.class, RestController.class, Configuration.class, + Extract.class); if(!have){ return false; } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ControllerGroup.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ControllerGroup.java index eeeacd12f63d7980b14540b74e591bd566c58d60..4a9bb31e836f140b3061617bb747ee468ce4dba2 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ControllerGroup.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ControllerGroup.java @@ -18,7 +18,7 @@ public class ControllerGroup implements PluginClassGroup { /** * spring @Controller @RestController 注解bean */ - public static final String GROUP_ID= "spring_controller"; + public static final String GROUP_ID = "spring_controller"; @Override diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/ExtractCoordinate.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/ExtractCoordinate.java new file mode 100644 index 0000000000000000000000000000000000000000..85dc04c28c06446910e2166dd7be56a11f161eb8 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/ExtractCoordinate.java @@ -0,0 +1,102 @@ +package com.gitee.starblues.factory.process.pipe.extract; + +import com.gitee.starblues.annotation.Extract; +import org.pf4j.util.StringUtils; + +import java.util.Objects; + +/** + * 执行器坐标 + * @author starBlues + * @version 2.4.1 + */ +public class ExtractCoordinate { + + private String bus; + private String scene; + private String useCase; + private Class extractClass; + + ExtractCoordinate(String bus, String scene, String useCase, Class extractClass) { + this.bus = bus; + this.scene = scene; + this.useCase = useCase; + this.extractClass = extractClass; + } + + + ExtractCoordinate(Extract extract, Class extractClass) { + this.bus = extract.bus(); + this.scene = extract.scene(); + this.useCase = extract.useCase(); + this.extractClass = extractClass; + } + + public static ExtractCoordinate build(String bus) { + return new ExtractCoordinate(bus, null, null, null); + } + + public static ExtractCoordinate build(String bus, String scene) { + return new ExtractCoordinate(bus, scene, null, null); + } + + public static ExtractCoordinate build(String bus, String scene, String useCase) { + return new ExtractCoordinate(bus, scene, useCase, null); + } + + public String getBus() { + return bus; + } + + public String getScene() { + return scene; + } + + public String getUseCase() { + return useCase; + } + + public Class getExtractClass() { + return extractClass; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ExtractCoordinate)) return false; + ExtractCoordinate that = (ExtractCoordinate) o; + if(StringUtils.isNotNullOrEmpty(bus) && + StringUtils.isNotNullOrEmpty(scene) && + StringUtils.isNotNullOrEmpty(useCase)){ + return Objects.equals(getBus(), that.getBus()) && + Objects.equals(getScene(), that.getScene()) && + Objects.equals(getUseCase(), that.getUseCase()); + } + + if(StringUtils.isNotNullOrEmpty(bus) && StringUtils.isNotNullOrEmpty(scene)){ + return Objects.equals(getBus(), that.getBus()) && + Objects.equals(getScene(), that.getScene()); + } + + if(StringUtils.isNotNullOrEmpty(bus)){ + return Objects.equals(getBus(), that.getBus()); + } + + return false; + } + + @Override + public int hashCode() { + return Objects.hash(getBus(), getScene(), getUseCase()); + } + + @Override + public String toString() { + return "ExtractCoordinate{" + + "bus='" + bus + '\'' + + ", scene='" + scene + '\'' + + ", useCase='" + useCase + '\'' + + '}'; + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/ExtractFactory.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/ExtractFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..24739307321cd9667b331bc95109bf4bd77d1851 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/ExtractFactory.java @@ -0,0 +1,209 @@ +package com.gitee.starblues.factory.process.pipe.extract; + +import com.gitee.starblues.annotation.Extract; +import org.springframework.util.ClassUtils; + +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 扩展工厂 + * @author starBlues + * @version 2.4.1 + */ +public class ExtractFactory { + + public static final String MAIN_EXTRACT_KEY = ExtractFactory.class.getName() + UUID.randomUUID().toString(); + + private Map> extractMap = new ConcurrentHashMap<>(); + + private static ExtractFactory EXTRACT_FACTORY = new ExtractFactory(); + + private ExtractFactory(){} + + /** + * 得到全局的扩展工厂 + * @return ExtractFactory + */ + public static ExtractFactory getInstant(){ + return EXTRACT_FACTORY; + } + + /** + * 添加扩展 + * @param extractObject 扩展的bean + */ + void addOfMain(Object extractObject){ + add(MAIN_EXTRACT_KEY, extractObject); + } + + + /** + * 添加扩展 + * @param pluginId 插件id + * @param extractObject 扩展的bean + */ + void add(String pluginId, Object extractObject){ + if(extractObject == null){ + return; + } + Extract extract = getExtract(extractObject); + if(extract == null){ + return; + } + Map extractObjects = extractMap.computeIfAbsent(pluginId, k -> new ConcurrentHashMap<>()); + extractObjects.put(new ExtractCoordinate(extract, extractObject.getClass()), extractObject); + } + + /** + * 根据插件id来移除扩展 + * @param pluginId 插件id + */ + void remove(String pluginId){ + extractMap.remove(pluginId); + } + + /** + * 通过坐标得到扩展 + * @param coordinate 扩展的坐标 + * @param 扩展的泛型 + * @return 扩展实例, 如果不存在则抛出 RuntimeException 异常 + */ + public T getExtractByCoordinate(ExtractCoordinate coordinate){ + Objects.requireNonNull(coordinate, "ExtractCoordinate can't be null"); + for (Map value : extractMap.values()) { + Object o = value.get(coordinate); + if(o != null){ + return (T) o; + } + } + throw new RuntimeException("Not found " + coordinate); + } + + /** + * 根据插件id和坐标得到扩展 + * @param pluginId 插件id + * @param coordinate 扩展的坐标 + * @param 扩展的泛型 + * @return 扩展实例, 如果不存在则抛出 RuntimeException 异常 + */ + public T getExtractByCoordinate(String pluginId, ExtractCoordinate coordinate){ + Objects.requireNonNull(coordinate, "ExtractCoordinate can't be null"); + Map extractCoordinates = extractMap.get(pluginId); + if(extractCoordinates == null){ + throw new RuntimeException("Not found " + coordinate + " from plugin '" + pluginId + "'"); + } + Object extracts = extractCoordinates.get(coordinate); + if(extracts == null){ + throw new RuntimeException("Not found " + coordinate + " from plugin '" + pluginId + "'"); + } + return (T) extracts; + } + + + /** + * 根据坐标得到主程序的扩展 + * 主程序扩展必须使用 @Extract+@Component 进行定义 + * @param coordinate 扩展的坐标 + * @param 扩展的泛型 + * @return 扩展实例, 如果不存在则抛出 RuntimeException 异常 + */ + public T getExtractByCoordinateOfMain(ExtractCoordinate coordinate){ + Objects.requireNonNull(coordinate, "ExtractCoordinate can't be null"); + Map extractCoordinates = extractMap.get(MAIN_EXTRACT_KEY); + if(extractCoordinates == null){ + throw new RuntimeException("Not found " + coordinate + " from main"); + } + Object extracts = extractCoordinates.get(coordinate); + if(extracts == null){ + throw new RuntimeException("Not found " + coordinate + " from main"); + } + return (T) extracts; + } + + /** + * 根据接口类型获取扩展 + * @param interfaceClass 接口类类型 + * @param 接口类型泛型 + * @return 扩展实现集合 + */ + public List getExtractByInterClass(Class interfaceClass){ + if(interfaceClass == null){ + return Collections.emptyList(); + } + List extracts = new ArrayList<>(); + for (Map value : extractMap.values()) { + for (Object o : value.values()) { + Set> allInterfacesForClassAsSet = ClassUtils.getAllInterfacesForClassAsSet(o.getClass()); + if(allInterfacesForClassAsSet.contains(interfaceClass)){ + extracts.add((T)o); + } + } + } + return extracts; + } + + /** + * 根据插件id和接口类型获取扩展 + * @param pluginId 插件id + * @param interfaceClass 接口类类型 + * @param 接口类型泛型 + * @return 扩展实现集合 + */ + public List getExtractByInterClass(String pluginId, Class interfaceClass){ + if(interfaceClass == null){ + return Collections.emptyList(); + } + List extracts = new ArrayList<>(); + Map extractCoordinateObjectMap = extractMap.get(pluginId); + if(extractCoordinateObjectMap == null || extractCoordinateObjectMap.isEmpty()){ + return Collections.emptyList(); + } + for (Object o : extractCoordinateObjectMap.values()) { + Set> allInterfacesForClassAsSet = ClassUtils.getAllInterfacesForClassAsSet(o.getClass()); + if(allInterfacesForClassAsSet.contains(interfaceClass)){ + extracts.add((T)o); + } + } + return extracts; + } + + /** + * 根据接口类型获取主程序的扩展 + * 主程序扩展必须使用 @Extract+@Component 进行定义 + * @param interfaceClass 接口类类型 + * @param 接口类型泛型 + * @return 扩展实现集合 + */ + public List getExtractByInterClassOfMain(Class interfaceClass){ + return getExtractByInterClass(MAIN_EXTRACT_KEY, interfaceClass); + } + + /** + * 得到所有的扩展坐标 + * @return 扩展坐标集合, key 为插件id, 值为所有扩展坐标集合 + */ + public Map> getExtractCoordinates(){ + Map> extractCoordinateMap = new HashMap<>(extractMap.size()); + extractMap.forEach((k, v)->{ + Set extractCoordinates = new HashSet<>(v.size()); + extractCoordinates.addAll(v.keySet()); + extractCoordinateMap.put(k, extractCoordinates); + }); + return extractCoordinateMap; + } + + /** + * 得到扩展的对象注解 + * @param extractObject 扩展对象 + * @return Extract 注解 + */ + private Extract getExtract(Object extractObject){ + Extract annotation = extractObject.getClass().getAnnotation(Extract.class); + if(annotation == null){ + return null; + } + return annotation; + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/PluginExtractPipeProcessor.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/PluginExtractPipeProcessor.java new file mode 100644 index 0000000000000000000000000000000000000000..a7c3f812d8055ccc28aa093131e9369c863c235f --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/extract/PluginExtractPipeProcessor.java @@ -0,0 +1,62 @@ +package com.gitee.starblues.factory.process.pipe.extract; + +import com.gitee.starblues.annotation.Extract; +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.SpringBeanRegister; +import com.gitee.starblues.factory.process.pipe.PluginPipeProcessor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.GenericApplicationContext; + +import java.util.Map; + +/** + * 插件扩展处理者 + * @author starBlues + * @version 2.4.1 + */ +public class PluginExtractPipeProcessor implements PluginPipeProcessor { + + private final ApplicationContext mainApplicationContext; + private final SpringBeanRegister springBeanRegister; + private final ExtractFactory extractFactory; + + public PluginExtractPipeProcessor(ApplicationContext mainApplicationContext) { + this.mainApplicationContext = mainApplicationContext; + this.springBeanRegister = new SpringBeanRegister((GenericApplicationContext) mainApplicationContext); + this.extractFactory = ExtractFactory.getInstant(); + } + + @Override + public void initialize() throws Exception { + springBeanRegister.registerSingleton(ExtractFactory.class.getName(), extractFactory); + // 获取主程序的扩展 + Map extractMap = mainApplicationContext.getBeansWithAnnotation(Extract.class); + if(!extractMap.isEmpty()){ + for (Object extract : extractMap.values()) { + extractFactory.addOfMain(extract); + } + } + } + + @Override + public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + GenericApplicationContext pluginApplicationContext = pluginRegistryInfo.getPluginApplicationContext(); + Map extractMap = pluginApplicationContext.getBeansWithAnnotation(Extract.class); + if(extractMap.isEmpty()){ + return; + } + String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); + for (Object extract : extractMap.values()) { + extractFactory.add(pluginId, extract); + } + pluginRegistryInfo.getSpringBeanRegister().registerSingleton( + ExtractFactory.class.getName(), extractFactory + ); + } + + @Override + public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); + extractFactory.remove(pluginId); + } +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegister.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegister.java new file mode 100644 index 0000000000000000000000000000000000000000..56fcba37c58a77614a9a352a1c5a121e6f16a857 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegister.java @@ -0,0 +1,17 @@ +package com.gitee.starblues.factory.process.pipe.interceptor; + +/** + * 插件拦截器注册者 + * @author starBlues + * @version 2.4.1 + */ +public interface PluginInterceptorRegister { + + /** + * 拦截器注册者 + * @param registry 注册对象 + */ + void registry(PluginInterceptorRegistry registry); + + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegistration.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegistration.java new file mode 100644 index 0000000000000000000000000000000000000000..ba067c7c41ff697e5717ef47bbb4cc9169385d3d --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegistration.java @@ -0,0 +1,146 @@ +package com.gitee.starblues.factory.process.pipe.interceptor; + +import com.gitee.starblues.utils.CommonUtils; +import org.pf4j.util.StringUtils; +import org.springframework.lang.Nullable; +import org.springframework.util.PathMatcher; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.config.annotation.InterceptorRegistration; +import org.springframework.web.servlet.handler.MappedInterceptor; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 插件拦截器注册的信息 + * @author starBlues + * @version 2.4.1 + */ +public class PluginInterceptorRegistration { + + private final HandlerInterceptor interceptor; + private final PluginInterceptorRegistry.Type type; + private final String pluginRestApiPrefix; + + private final List includePatterns = new ArrayList<>(); + + private final List excludePatterns = new ArrayList<>(); + + @Nullable + private PathMatcher pathMatcher; + + private int order = 0; + + /** + * Create an {@link InterceptorRegistration} instance. + * + * @param interceptor 拦截器 + * @param type 类型 + * @param pluginRestApiPrefix 接口前缀 + */ + public PluginInterceptorRegistration(HandlerInterceptor interceptor, + PluginInterceptorRegistry.Type type, + String pluginRestApiPrefix) { + this.interceptor = interceptor; + this.type = type; + String apiPrefix = null; + if(pluginRestApiPrefix.startsWith("/")){ + apiPrefix = pluginRestApiPrefix; + } else { + apiPrefix = "/" + pluginRestApiPrefix; + } + if(apiPrefix.endsWith("/")){ + apiPrefix = apiPrefix.substring(0, apiPrefix.lastIndexOf("/")); + } + this.pluginRestApiPrefix = apiPrefix; + } + + + /** + * Add URL patterns to which the registered interceptor should apply to. + */ + public PluginInterceptorRegistration addPathPatterns(String... patterns) { + if(type == PluginInterceptorRegistry.Type.GLOBAL){ + this.includePatterns.addAll(Arrays.asList(patterns)); + } + // 局部的 + for (String pattern : patterns) { + if(StringUtils.isNullOrEmpty(pattern)){ + continue; + } + this.includePatterns.add(CommonUtils.joiningPath(pluginRestApiPrefix, pattern)); + } + return this; + } + + /** + * Add URL patterns to which the registered interceptor should not apply to. + */ + public PluginInterceptorRegistration excludePathPatterns(String... patterns) { + if(type == PluginInterceptorRegistry.Type.GLOBAL){ + this.excludePatterns.addAll(Arrays.asList(patterns)); + } + for (String pattern : patterns) { + if(StringUtils.isNullOrEmpty(pattern)){ + continue; + } + this.excludePatterns.add(CommonUtils.joiningPath(pluginRestApiPrefix, pattern)); + } + return this; + } + + + /** + * A PathMatcher implementation to use with this interceptor. This is an optional, + * advanced property required only if using custom PathMatcher implementations + * that support mapping metadata other than the Ant path patterns supported + * by default. + */ + public PluginInterceptorRegistration pathMatcher(PathMatcher pathMatcher) { + this.pathMatcher = pathMatcher; + return this; + } + + /** + * Specify an order position to be used. Default is 0. + * @since 5.0 + */ + public PluginInterceptorRegistration order(int order){ + this.order = order; + return this; + } + + /** + * Return the order position to be used. + * @since 5.0 + */ + protected int getOrder() { + return this.order; + } + + + /** + * Build the underlying interceptor. If URL patterns are provided, the returned + * type is {@link MappedInterceptor}; otherwise {@link HandlerInterceptor}. + */ + protected Object getInterceptor() { + if(type == PluginInterceptorRegistry.Type.PLUGIN){ + if(this.includePatterns.isEmpty()){ + this.includePatterns.add(CommonUtils.joiningPath(pluginRestApiPrefix, "/**")); + } + } + if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) { + return this.interceptor; + } + + String[] include = this.includePatterns.toArray(new String[]{}); + String[] exclude = this.excludePatterns.toArray(new String[]{}); + MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor); + if (this.pathMatcher != null) { + mappedInterceptor.setPathMatcher(this.pathMatcher); + } + return mappedInterceptor; + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegistry.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegistry.java new file mode 100644 index 0000000000000000000000000000000000000000..f2f5ca70a252726d38f855641cc2847c3407769a --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/interceptor/PluginInterceptorRegistry.java @@ -0,0 +1,92 @@ +package com.gitee.starblues.factory.process.pipe.interceptor; + +import org.springframework.core.OrderComparator; +import org.springframework.core.Ordered; +import org.springframework.web.context.request.WebRequestInterceptor; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.config.annotation.InterceptorRegistration; +import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 插件拦截器添加者 + * @author starBlues + * @version 2.4.1 + */ +public class PluginInterceptorRegistry { + + + private final List registrations = new ArrayList<>(); + private final String pluginRestApiPrefix; + + public PluginInterceptorRegistry(String pluginRestApiPrefix) { + this.pluginRestApiPrefix = pluginRestApiPrefix; + } + + + /** + * Adds the provided {@link HandlerInterceptor}. + * @param interceptor the interceptor to add + * @return An {@link InterceptorRegistration} that allows you optionally configure the + * registered interceptor further for example adding URL patterns it should apply to. + */ + public PluginInterceptorRegistration addInterceptor(HandlerInterceptor interceptor, Type type) { + PluginInterceptorRegistration registration = new PluginInterceptorRegistration(interceptor, + type, pluginRestApiPrefix); + this.registrations.add(registration); + return registration; + } + + /** + * Adds the provided {@link WebRequestInterceptor}. + * @param interceptor the interceptor to add + * @return An {@link InterceptorRegistration} that allows you optionally configure the + * registered interceptor further for example adding URL patterns it should apply to. + */ + public PluginInterceptorRegistration addWebRequestInterceptor(WebRequestInterceptor interceptor, Type type) { + WebRequestHandlerInterceptorAdapter adapted = new WebRequestHandlerInterceptorAdapter(interceptor); + PluginInterceptorRegistration registration = new PluginInterceptorRegistration(adapted, type, + pluginRestApiPrefix); + this.registrations.add(registration); + return registration; + } + + /** + * Return all registered interceptors. + */ + public List getInterceptors() { + return this.registrations.stream() + .sorted(INTERCEPTOR_ORDER_COMPARATOR) + .map(PluginInterceptorRegistration::getInterceptor) + .collect(Collectors.toList()); + } + + + public enum Type{ + /** + * 全局拦截器 + */ + GLOBAL, + + /** + * 插件局部拦截器, 必须设置 pluginRestPathPrefix 的值才生效 + */ + PLUGIN + } + + + private static final Comparator INTERCEPTOR_ORDER_COMPARATOR = + OrderComparator.INSTANCE.withSourceProvider(object -> { + if (object instanceof PluginInterceptorRegistration) { + return (Ordered) ((PluginInterceptorRegistration) object)::getOrder; + } + return null; + }); + + + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/loader/PluginResource.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/loader/PluginResource.java index 8df96942e4873102a6dd3d9813f6e50086756c86..59543fb0922c50e4a065f92bae409c83ab19822e 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/loader/PluginResource.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/loader/PluginResource.java @@ -47,7 +47,7 @@ public class PluginResource implements Resource { this.path = pathToUse; PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); - this.classLoader = pluginRegistryInfo.getDefaultPluginClassLoader(); + this.classLoader = pluginRegistryInfo.getPluginClassLoader(); this.pluginWrapper = pluginWrapper; this.lastModified = pluginRegistryInfo.getBasePlugin().getBasePluginExtend().getStartTimestamp(); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/loader/load/PluginConfigFileLoader.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/loader/load/PluginConfigFileLoader.java index 85497ac575352c3b0cc3fb9037917f82d59c50e0..eccbb9b494e6e63357cb85119a25a3efe59d3e0c 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/loader/load/PluginConfigFileLoader.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/loader/load/PluginConfigFileLoader.java @@ -49,7 +49,7 @@ public class PluginConfigFileLoader implements PluginResourceLoader { BasePlugin basePlugin = pluginRegistryInfo.getBasePlugin(); suppliers.add(findConfigRoot()); suppliers.add(findPluginRoot(basePlugin)); - suppliers.add(findClassPath(pluginRegistryInfo.getDefaultPluginClassLoader())); + suppliers.add(findClassPath(pluginRegistryInfo.getPluginClassLoader())); for (Supplier supplier : suppliers) { diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/bean/PluginControllerPostProcessor.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/bean/PluginControllerPostProcessor.java index eab817f1841bc5f4fa66aa19efb2c247597672a9..0928fdcc4a263e7354c2b6e28355fd827918c509 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/bean/PluginControllerPostProcessor.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/bean/PluginControllerPostProcessor.java @@ -7,6 +7,8 @@ import com.gitee.starblues.factory.process.pipe.classs.group.ControllerGroup; import com.gitee.starblues.factory.process.post.PluginPostProcessor; import com.gitee.starblues.factory.process.post.bean.model.ControllerWrapper; import com.gitee.starblues.integration.IntegrationConfiguration; +import com.gitee.starblues.utils.CommonUtils; +import org.pf4j.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; @@ -210,18 +212,9 @@ public class PluginControllerPostProcessor implements PluginPostProcessor { if(requestMapping == null){ return; } - String pathPrefix = configuration.pluginRestPathPrefix(); - if(configuration.enablePluginIdRestPathPrefix()){ - if(pathPrefix != null && !"".equals(pathPrefix)){ - pathPrefix = joiningPath(pathPrefix, pluginId); - } else { - pathPrefix = pluginId; - } - } else { - if(pathPrefix == null || "".equals(pathPrefix)){ - // 不启用插件id作为路径前缀, 并且路径前缀为空, 则直接返回。 - return; - } + String pathPrefix = CommonUtils.getPluginRestPrefix(configuration, pluginId); + if(StringUtils.isNullOrEmpty(pathPrefix)){ + return; } InvocationHandler invocationHandler = Proxy.getInvocationHandler(requestMapping); Set definePaths = new HashSet<>(); @@ -238,7 +231,7 @@ public class PluginControllerPostProcessor implements PluginPostProcessor { if(definePath.contains(pathPrefix)){ newPath[i++] = definePath; } else { - newPath[i++] = joiningPath(pathPrefix, definePath); + newPath[i++] = CommonUtils.restJoiningPath(pathPrefix, definePath); } } if(newPath.length == 0){ @@ -251,30 +244,6 @@ public class PluginControllerPostProcessor implements PluginPostProcessor { } } - /** - * 拼接路径 - * @param path1 路径1 - * @param path2 路径2 - * @return 拼接的路径 - */ - private String joiningPath(String path1, String path2){ - if(path1 != null && path2 != null){ - if(path1.endsWith("/") && path2.startsWith("/")){ - return path1 + path2.substring(1); - } else if(!path1.endsWith("/") && !path2.startsWith("/")){ - return path1 + "/" + path2; - } else { - return path1 + path2; - } - } else if(path1 != null){ - return path1; - } else if(path2 != null){ - return path2; - } else { - return ""; - } - } - /** * 方法上是否存在 @RequestMapping 注解 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/AutoIntegrationConfiguration.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/AutoIntegrationConfiguration.java index 8ac3c568248c773f6346c56ed0bfcbf5df5e906a..8c8f7e95670b6373d3bf452c7ee44f00c76936c6 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/AutoIntegrationConfiguration.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/AutoIntegrationConfiguration.java @@ -77,6 +77,22 @@ public class AutoIntegrationConfiguration extends DefaultIntegrationConfiguratio @Value("${uploadTempPath:}") private String uploadTempPath; + /** + * 当前主程序的版本号, 用于校验插件是否可安装. + * 插件中可通过插件配置信息 requires 来指定可安装的主程序版本 + * 如果为: 0.0.0 的话, 表示不校验 + */ + @Value("${version:0.0.0}") + private String version; + + /** + * 设置为true表示插件设置的requires的版本号完全匹配version版本号才可允许插件安装, 即: requires=x.y.z + * 设置为false表示插件设置的requires的版本号小于等于version值, 插件就可安装, 即requires<=x.y.z + * 默认为false + */ + @Value("${exactVersionAllowed:false}") + private Boolean exactVersionAllowed; + /** * 启用的插件id */ @@ -168,6 +184,21 @@ public class AutoIntegrationConfiguration extends DefaultIntegrationConfiguratio return enableSwaggerRefresh; } + @Override + public List sortInitPluginIds() { + return sortInitPluginIds; + } + + @Override + public String version() { + return version; + } + + @Override + public boolean exactVersionAllowed() { + return exactVersionAllowed; + } + public String getRunMode() { return runMode; } @@ -240,6 +271,22 @@ public class AutoIntegrationConfiguration extends DefaultIntegrationConfiguratio this.uploadTempPath = uploadTempPath; } + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public Boolean getExactVersionAllowed() { + return exactVersionAllowed; + } + + public void setExactVersionAllowed(Boolean exactVersionAllowed) { + this.exactVersionAllowed = exactVersionAllowed; + } + public Set getEnablePluginIds() { return enablePluginIds; } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/ConfigurationBuilder.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/ConfigurationBuilder.java index 6e973a782ce225bad11cc8b697346136dfa555bf..f937fd1856c6abb0ac1dd9d32efa377fccfae4c3 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/ConfigurationBuilder.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/ConfigurationBuilder.java @@ -1,7 +1,7 @@ package com.gitee.starblues.integration; import org.pf4j.RuntimeMode; -import org.springframework.util.StringUtils; +import org.pf4j.util.StringUtils; import java.util.List; import java.util.Objects; @@ -32,6 +32,9 @@ public class ConfigurationBuilder extends DefaultIntegrationConfiguration{ private List sortInitPluginIds; private Boolean enableSwaggerRefresh; + private String version; + private Boolean exactVersionAllowed; + public ConfigurationBuilder(Builder builder) { this.runtimeMode = Objects.requireNonNull(builder.runtimeMode, "runtimeMode can't be empty"); this.pluginPath = Objects.requireNonNull(builder.pluginPath, "pluginPath can't be empty"); @@ -44,7 +47,8 @@ public class ConfigurationBuilder extends DefaultIntegrationConfiguration{ this.enablePluginIds = builder.enablePluginIds; this.disablePluginIds = builder.disablePluginIds; this.sortInitPluginIds = builder.sortInitPluginIds; - + this.version = builder.version; + this.exactVersionAllowed = builder.exactVersionAllowed; if(builder.enable == null){ this.enable = true; } else { @@ -79,6 +83,9 @@ public class ConfigurationBuilder extends DefaultIntegrationConfiguration{ private List sortInitPluginIds; private Boolean enableSwaggerRefresh; + private String version; + private Boolean exactVersionAllowed; + public Builder runtimeMode(RuntimeMode runtimeMode){ this.runtimeMode = runtimeMode; return this; @@ -139,6 +146,16 @@ public class ConfigurationBuilder extends DefaultIntegrationConfiguration{ return this; } + public Builder version(String version){ + this.version = version; + return this; + } + + public Builder exactVersionAllowed(Boolean exactVersionAllowed){ + this.exactVersionAllowed = exactVersionAllowed; + return this; + } + public ConfigurationBuilder build(){ return new ConfigurationBuilder(this); } @@ -164,7 +181,7 @@ public class ConfigurationBuilder extends DefaultIntegrationConfiguration{ @Override public String uploadTempPath() { - if(StringUtils.isEmpty(uploadTempPath)){ + if(StringUtils.isNullOrEmpty(uploadTempPath)){ return super.uploadTempPath(); } else { return uploadTempPath; @@ -173,7 +190,7 @@ public class ConfigurationBuilder extends DefaultIntegrationConfiguration{ @Override public String backupPath() { - if(StringUtils.isEmpty(backupPath)){ + if(StringUtils.isNullOrEmpty(backupPath)){ return super.backupPath(); } else { return backupPath; @@ -182,7 +199,7 @@ public class ConfigurationBuilder extends DefaultIntegrationConfiguration{ @Override public String pluginRestPathPrefix() { - if(StringUtils.isEmpty(pluginRestPathPrefix)){ + if(StringUtils.isNullOrEmpty(pluginRestPathPrefix)){ return super.pluginRestPathPrefix(); } else { return pluginRestPathPrefix; @@ -225,4 +242,20 @@ public class ConfigurationBuilder extends DefaultIntegrationConfiguration{ } return enableSwaggerRefresh; } + + @Override + public String version() { + if(StringUtils.isNullOrEmpty(version)){ + return super.version(); + } + return version; + } + + @Override + public boolean exactVersionAllowed() { + if(exactVersionAllowed == null){ + return super.exactVersionAllowed(); + } + return exactVersionAllowed; + } } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java index 12a84e26be2fa8d6ec20a9f59fef9b1ec7185dd5..6beb6954eef15d080ce6b1c517ed55406dae5ae9 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java @@ -55,4 +55,14 @@ public abstract class DefaultIntegrationConfiguration implements IntegrationConf public List sortInitPluginIds() { return null; } + + @Override + public String version() { + return "0.0.0"; + } + + @Override + public boolean exactVersionAllowed() { + return false; + } } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/IntegrationConfiguration.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/IntegrationConfiguration.java index a15d55f79886337938b6a581fa22dc088c4af0fa..727e6ce23caecceab8e6baf613861769bf96a695 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/IntegrationConfiguration.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/IntegrationConfiguration.java @@ -91,4 +91,19 @@ public interface IntegrationConfiguration { */ List sortInitPluginIds(); + /** + * 当前主程序的版本号, 用于校验插件是否可安装. + * 插件中可通过插件配置信息 requires 来指定可安装的主程序版本 + * @return 系统版本号, 如果为: 0.0.0 的话, 表示不校验 + */ + String version(); + + /** + * 设置为true表示插件设置的requires的版本号完全匹配version版本号才可允许插件安装, 即: requires=x.y.z + * 设置为false表示插件设置的requires的版本号小于等于version值, 插件就可安装, 即requires<=x.y.z + * 默认为false + * @return true or false + */ + boolean exactVersionAllowed(); + } 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 7814b7dffc37541949d62d06f98313eff6b57b2c..dbd7906ead1f4ca079b167570810ae29d384933c 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 @@ -12,7 +12,9 @@ import com.gitee.starblues.integration.user.PluginUser; import org.pf4j.PluginManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.ApplicationContext; +import org.springframework.context.support.GenericApplicationContext; import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; @@ -57,6 +59,7 @@ public class DefaultPluginApplication extends AbstractPluginApplication { pluginUser = createPluginUser(applicationContext, pluginManager); pluginOperator = createPluginOperator(applicationContext, pluginManager, configuration); try { + setBeanFactory(applicationContext); pluginOperator.initPlugins(listener); beInitialized.set(true); } catch (Exception e) { @@ -107,6 +110,17 @@ public class DefaultPluginApplication extends AbstractPluginApplication { return pluginUser; } + /** + * 直接将 PluginOperator 和 PluginUser 注入到ApplicationContext容器中 + * @param applicationContext ApplicationContext + */ + private void setBeanFactory(ApplicationContext applicationContext){ + GenericApplicationContext genericApplicationContext = (GenericApplicationContext) applicationContext; + DefaultListableBeanFactory defaultListableBeanFactory = genericApplicationContext.getDefaultListableBeanFactory(); + defaultListableBeanFactory.registerSingleton(pluginOperator.getClass().getName(), pluginOperator); + defaultListableBeanFactory.registerSingleton(pluginUser.getClass().getName(), pluginUser); + } + /** * 检查注入 */ diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/listener/PluginListenerFactory.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/listener/PluginListenerFactory.java index 37e9fcac5e751e5da703df626c009726ac1d67c8..e59a64229d0d20406f6fd0d3f95d163989fb5c08 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/listener/PluginListenerFactory.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/listener/PluginListenerFactory.java @@ -1,10 +1,12 @@ package com.gitee.starblues.integration.listener; +import com.gitee.starblues.utils.SpringBeanUtils; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.context.support.GenericApplicationContext; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * 插件监听工厂 @@ -76,8 +78,7 @@ public class PluginListenerFactory implements PluginListener { } } - public void buildListenerClass(GenericApplicationContext - applicationContext) { + public void buildListenerClass(GenericApplicationContext applicationContext) { if (applicationContext == null) { return; } @@ -85,11 +86,30 @@ public class PluginListenerFactory implements PluginListener { if(isBuildListenerClass){ return; } + // 搜索Spring容器中的监听器 + List pluginListeners = SpringBeanUtils.getBeans(applicationContext, PluginListener.class); + if(pluginListeners.isEmpty()){ + pluginListeners = new ArrayList<>(); + } for (Class listenerClass : listenerClasses) { // 兼容 spring 4.x - applicationContext.registerBeanDefinition(listenerClass.getName(), BeanDefinitionBuilder.genericBeanDefinition(listenerClass).getBeanDefinition()); + applicationContext.registerBeanDefinition(listenerClass.getName(), + BeanDefinitionBuilder.genericBeanDefinition(listenerClass).getBeanDefinition()); T bean = applicationContext.getBean(listenerClass); - listeners.add(bean); + pluginListeners.add(bean); + } + for (PluginListener pluginListener : pluginListeners) { + boolean find = false; + for (PluginListener listener : listeners) { + if(Objects.equals(listener, pluginListener)){ + find = true; + break; + } + } + // 防止监听器重复注册 + if(!find){ + listeners.add(pluginListener); + } } listenerClasses.clear(); isBuildListenerClass = true; 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 25721583e8ec4f1235e436ef2a82e94a51fdf5df..1223c5771e029eab2df8b47befd7066ed709c5f9 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 @@ -147,7 +147,7 @@ public class DefaultPluginOperator implements PluginOperator { @Override - public boolean install(Path jarPath) throws Exception { + public synchronized boolean install(Path jarPath) throws Exception { if(isDev()){ throw new RuntimeException("Plugin cannot be installed in 'dev' environment"); } @@ -214,7 +214,7 @@ public class DefaultPluginOperator implements PluginOperator { @Override - public boolean uninstall(String pluginId, boolean isBackup) throws Exception { + public synchronized boolean uninstall(String pluginId, boolean isBackup) throws Exception { if(isDev()){ throw new RuntimeException("Plugin cannot be uninstalled in 'dev' environment"); } @@ -268,7 +268,7 @@ public class DefaultPluginOperator implements PluginOperator { } @Override - public boolean start(String pluginId) throws Exception { + public synchronized boolean start(String pluginId) throws Exception { if(StringUtils.isEmpty(pluginId)){ throw new IllegalArgumentException("Method:start param 'pluginId' can not be empty"); } @@ -302,7 +302,7 @@ public class DefaultPluginOperator implements PluginOperator { } @Override - public boolean stop(String pluginId) throws Exception { + public synchronized boolean stop(String pluginId) throws Exception { if(StringUtils.isEmpty(pluginId)){ throw new IllegalArgumentException("Method:stop param 'pluginId' can not be empty"); } @@ -329,7 +329,7 @@ public class DefaultPluginOperator implements PluginOperator { @Override - public boolean uploadPluginAndStart(MultipartFile pluginFile) throws Exception { + public synchronized boolean uploadPluginAndStart(MultipartFile pluginFile) throws Exception { if(isDev()){ throw new RuntimeException("Plugin cannot uploadPluginAndStart in the 'dev' environment"); } @@ -346,7 +346,7 @@ public class DefaultPluginOperator implements PluginOperator { } @Override - public boolean installConfigFile(Path configFilePath) throws Exception { + public synchronized boolean installConfigFile(Path configFilePath) throws Exception { if(isDev()){ throw new RuntimeException("Plugin config file cannot be installed in the 'dev' environment"); } @@ -366,7 +366,7 @@ public class DefaultPluginOperator implements PluginOperator { } @Override - public boolean uploadConfigFile(MultipartFile configFile) throws Exception { + public synchronized boolean uploadConfigFile(MultipartFile configFile) throws Exception { if(isDev()){ throw new RuntimeException("Plugin config file cannot be uploaded in the 'dev' environment"); } @@ -387,7 +387,7 @@ public class DefaultPluginOperator implements PluginOperator { } @Override - public boolean backupPlugin(Path backDirPath, String sign) throws Exception { + public synchronized boolean backupPlugin(Path backDirPath, String sign) throws Exception { if(isDev()){ throw new RuntimeException("Plugin cannot backup in the 'dev' environment"); } @@ -397,7 +397,7 @@ public class DefaultPluginOperator implements PluginOperator { @Override - public boolean backupPlugin(String pluginId, String sign) throws Exception { + public synchronized boolean backupPlugin(String pluginId, String sign) throws Exception { if(isDev()){ throw new RuntimeException("Plugin cannot backup in the 'dev' environment"); } @@ -561,9 +561,9 @@ public class DefaultPluginOperator implements PluginOperator { String fileName = sourcePath.getFileName().toString(); String targetName = integrationConfiguration.backupPath() + File.separator; if(!StringUtils.isEmpty(sign)){ - targetName = targetName + "[" + sign + "]"; + targetName = targetName + sign; } - targetName = targetName + "[" + getNowTimeByFormat() + "]"; + targetName = targetName + "_" +getNowTimeByFormat(); Path target = Paths.get(targetName + "_" + fileName); if(!Files.exists(target.getParent())){ Files.createDirectories(target.getParent()); 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 161c38aad08ffd849ed91f443eb540dc17e86b15..9a0897cd36e4960e1c2e1f54629918cf807203f1 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 @@ -32,10 +32,11 @@ public class DefaultPf4jFactory implements Pf4jFactory { throw new RuntimeException("Configuration RuntimeMode is null" + configuration.environment()); } List sortInitPluginIds = configuration.sortInitPluginIds(); + DefaultPluginManager defaultPluginManager = null; if(RuntimeMode.DEVELOPMENT == environment){ // 开发环境下的插件管理者 Path path = Paths.get(getDevPluginDir(configuration)); - return new DefaultPluginManager(path){ + defaultPluginManager = new DefaultPluginManager(path){ @Override protected void initialize() { @@ -71,7 +72,7 @@ public class DefaultPf4jFactory implements Pf4jFactory { } else if(RuntimeMode.DEPLOYMENT == environment){ // 运行环境下的插件管理者 Path path = Paths.get(getProdPluginDir(configuration)); - return new DefaultPluginManager(path){ + defaultPluginManager = new DefaultPluginManager(path){ @Override protected void initialize() { @@ -99,9 +100,13 @@ public class DefaultPf4jFactory implements Pf4jFactory { } }; - } else { + } + if(defaultPluginManager == null){ throw new RuntimeException("Not found run environment " + configuration.environment()); } + defaultPluginManager.setSystemVersion(configuration.version()); + defaultPluginManager.setExactVersionAllowed(configuration.exactVersionAllowed()); + return defaultPluginManager; } @@ -126,12 +131,12 @@ public class DefaultPf4jFactory implements Pf4jFactory { if(runtimeMode == RuntimeMode.DEPLOYMENT){ // 生产 return new CompoundPluginDescriptorFinder() - .add(new ResourcesPluginDescriptorFinder(RuntimeMode.DEPLOYMENT)) + .add(new ResourcesPluginDescriptorFinder(runtimeMode)) .add(new ManifestPluginDescriptorFinder()); } else { // 开发 return new CompoundPluginDescriptorFinder() - .add(new ResourcesPluginDescriptorFinder(RuntimeMode.DEVELOPMENT)) + .add(new ResourcesPluginDescriptorFinder(runtimeMode)) .add(new ResolvePropertiesPluginDescriptorFinder()) .add(new ManifestPluginDescriptorFinder()); } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/AnnotationsUtils.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/AnnotationsUtils.java index b747458634a4dc72320cd412a2a68824eaadfeaa..7ed5143b0ae87c93575a26017f61eb24f6df5c7c 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/AnnotationsUtils.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/AnnotationsUtils.java @@ -22,6 +22,7 @@ public class AnnotationsUtils { * @param annotationClasses 注解类 * @return boolean */ + @SafeVarargs public static boolean haveAnnotations(Class aClass, boolean isAllMatch, Class ...annotationClasses){ if(aClass == null){ diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/CommonUtils.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/CommonUtils.java index 29daaba7cf9e7bdff99124c57ddbd4cf1cad6263..54ceaed9f41930220c5c410cbb02d14dd911064c 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/CommonUtils.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/CommonUtils.java @@ -1,5 +1,8 @@ package com.gitee.starblues.utils; +import com.gitee.starblues.integration.IntegrationConfiguration; +import org.pf4j.util.StringUtils; + import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -48,4 +51,84 @@ public class CommonUtils { } + /** + * 得到插件接口前缀 + * @param configuration 配置 + * @param pluginId 插件id + * @return 接口前缀 + */ + public static String getPluginRestPrefix(IntegrationConfiguration configuration, String pluginId){ + String pathPrefix = configuration.pluginRestPathPrefix(); + if(configuration.enablePluginIdRestPathPrefix()){ + if(pathPrefix != null && !"".equals(pathPrefix)){ + pathPrefix = restJoiningPath(pathPrefix, pluginId); + } else { + pathPrefix = pluginId; + } + return pathPrefix; + } else { + if(pathPrefix == null || "".equals(pathPrefix)){ + // 不启用插件id作为路径前缀, 并且路径前缀为空, 则直接返回。 + return null; + } + } + return pathPrefix; + } + + + /** + * rest接口拼接路径 + * @param path1 路径1 + * @param path2 路径2 + * @return 拼接的路径 + */ + public static String restJoiningPath(String path1, String path2){ + if(path1 != null && path2 != null){ + if(path1.endsWith("/") && path2.startsWith("/")){ + return path1 + path2.substring(1); + } else if(!path1.endsWith("/") && !path2.startsWith("/")){ + return path1 + "/" + path2; + } else { + return path1 + path2; + } + } else if(path1 != null){ + return path1; + } else if(path2 != null){ + return path2; + } else { + return ""; + } + } + + + /** + * 拼接路径 + * @param paths 拼接的路径 + * @return 拼接的路径 + */ + public static String joiningPath(String ...paths){ + if(paths == null || paths.length == 0){ + return ""; + } + StringBuilder stringBuilder = new StringBuilder(); + int length = paths.length; + for (int i = 0; i < length; i++) { + String path = paths[i]; + if(StringUtils.isNullOrEmpty(path)) { + continue; + } + if((i < length - 1) && path.endsWith("/")){ + path = path.substring(path.lastIndexOf("/")); + } + if(path.startsWith("/")){ + stringBuilder.append(path); + } else { + stringBuilder.append("/").append(path); + } + } + + return stringBuilder.toString(); + } + + } diff --git a/springboot-plugin-framework/src/test/java/com/gitee/starblues/utils/CommonUtilsTest.java b/springboot-plugin-framework/src/test/java/com/gitee/starblues/utils/CommonUtilsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..19e945fb7e141f2785edcb9dd0d202dea45f443b --- /dev/null +++ b/springboot-plugin-framework/src/test/java/com/gitee/starblues/utils/CommonUtilsTest.java @@ -0,0 +1,21 @@ +package com.gitee.starblues.utils; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author starBlues + * @version 1.0 + */ +public class CommonUtilsTest { + + @Test + public void testJoin(){ + Assert.assertEquals(CommonUtils.joiningPath("/p1", "p2", "p3"), "/p1/p2/p3"); + Assert.assertEquals(CommonUtils.joiningPath("/p1", "p2", "p3/"), "/p1/p2/p3/"); + Assert.assertEquals(CommonUtils.joiningPath("p1", "p2", "p3/"), "/p1/p2/p3/"); + } + + + +}