diff --git a/example/basic-example/basic-example-main/pom.xml b/example/basic-example/basic-example-main/pom.xml index eaf35d85918457f16c144b1291990ce5e838f994..f2ca1d9dbd315834403cc3e807ce2fe502e6b5d3 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.2-RELEASE + 2.4.3-RELEASE jar 2.7.0 2.3.2 1.6 - 2.4.2-RELEASE + 2.4.3-RELEASE @@ -30,9 +30,26 @@ ${springboot-plugin-framework.version} + + com.gitee.starblues + springboot-plugin-framework-extension-log + ${springboot-plugin-framework.version} + + org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + + org.springframework.boot + spring-boot-starter-log4j2 diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/ExampleMain.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/ExampleMain.java index 8ad6f35e3c1676a387c2e9d00042ca33f8d7e042..855333ea79f9a366e7d320995d84d09419ee5ba9 100644 --- a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/ExampleMain.java +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/ExampleMain.java @@ -2,6 +2,7 @@ package com.basic.example.main; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; /** * 插件案例启动类 @@ -16,6 +17,4 @@ public class ExampleMain { } - - } diff --git a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/PluginBeanConfig.java b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/PluginBeanConfig.java index 0deb6fa684340d138c5d11944014658fc3981e13..3f9990dfd7fa46a1dff45d3487aafe3f571b6f7c 100644 --- a/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/PluginBeanConfig.java +++ b/example/basic-example/basic-example-main/src/main/java/com/basic/example/main/config/PluginBeanConfig.java @@ -1,5 +1,6 @@ package com.basic.example.main.config; +import com.gitee.starblues.extension.log.SpringBootLogExtension; import com.gitee.starblues.extension.support.SpringDocControllerProcessor; import com.gitee.starblues.integration.application.PluginApplication; import com.gitee.starblues.integration.application.AutoPluginApplication; @@ -27,6 +28,8 @@ public class PluginBeanConfig { AutoPluginApplication autoPluginApplication = new AutoPluginApplication(); autoPluginApplication.setPluginInitializerListener(pluginListener); autoPluginApplication.addListener(ExamplePluginListener.class); + // 添加插件log4j支持 + autoPluginApplication.addExtension(new SpringBootLogExtension(SpringBootLogExtension.Type.LOG4J)); return autoPluginApplication; } diff --git a/example/basic-example/basic-example-main/src/main/resources/application-dev.yml b/example/basic-example/basic-example-main/src/main/resources/application-dev.yml index 5586028e3e7314585a6024f24bc8a16390b9dcaa..c748bc04d0c0e868acc86c0d369bc0bfa155f99a 100644 --- a/example/basic-example/basic-example-main/src/main/resources/application-dev.yml +++ b/example/basic-example/basic-example-main/src/main/resources/application-dev.yml @@ -1,6 +1,9 @@ server: port: 8080 +logging: + config: classpath:log4j.xml + plugin: runMode: dev pluginPath: ./example/basic-example/plugins diff --git a/example/basic-example/basic-example-main/src/main/resources/log4j.xml b/example/basic-example/basic-example-main/src/main/resources/log4j.xml new file mode 100644 index 0000000000000000000000000000000000000000..b573b5c3b25c79f8fc93a90024ef6a9ce2144b3b --- /dev/null +++ b/example/basic-example/basic-example-main/src/main/resources/log4j.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/basic-example/basic-example-runner/pom.xml b/example/basic-example/basic-example-runner/pom.xml index ada91f7f819eca9dea6df431671b401bcbd76509..64d91c2a1596fa7b485aa0be189bd373af1c4717 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.2-RELEASE + 2.4.3-RELEASE pom @@ -26,6 +26,51 @@ com.gitee.starblues basic-example-main ${project.version} + provided + + + org.springframework.boot + spring-boot-starter-logging + + + ch.qos.logback + logback-classic + + + + + + org.springframework.boot + spring-boot-starter-quartz + 2.4.5 + provided + + + org.springframework.boot + spring-boot-starter-logging + + + ch.qos.logback + logback-classic + + + + + + org.springframework.boot + spring-boot-starter-data-redis + 2.1.5.RELEASE + provided + + + org.springframework.boot + spring-boot-starter-logging + + + ch.qos.logback + logback-classic + + diff --git a/example/basic-example/plugins/basic-example-plugin1/pom.xml b/example/basic-example/plugins/basic-example-plugin1/pom.xml index 96e72fd56d842b5f15e0a80c5188e43ea91b09f9..f54136055a2d5bab303d42d7f3a628e03d47fd20 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.2-RELEASE + 2.4.3-RELEASE ../pom.xml basic-example-plugin1 - 2.4.2-RELEASE + 2.4.3-RELEASE jar @@ -25,5 +25,19 @@ 2.8.2 + + + org.springframework.boot + spring-boot-starter-quartz + 2.4.5 + + + + org.springframework.boot + spring-boot-starter-data-redis + 2.1.5.RELEASE + + + \ No newline at end of file diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/DefinePlugin.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/DefinePlugin.java index f81aa855ea7e85aa6fca51668e18332abdc55f42..9a4ee5a4e2edbd4d60de2418a1ea3aefc6aafbab 100644 --- a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/DefinePlugin.java +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/DefinePlugin.java @@ -1,5 +1,6 @@ package com.basic.example.plugin1; +import com.gitee.starblues.annotation.ConfigDefinition; import com.gitee.starblues.realize.BasePlugin; import org.pf4j.PluginWrapper; @@ -9,6 +10,7 @@ import org.pf4j.PluginWrapper; * @author starBlues * @version 1.0 */ +@ConfigDefinition(fileName = "pluginOfSpringBoot.yml") public class DefinePlugin extends BasePlugin { public DefinePlugin(PluginWrapper wrapper) { super(wrapper); diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/ConfigBean.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/ConfigBean.java index c997edccfcdd67a585eaf4fa84a80e172f4a4ac2..f7e89aadf1d0131ef467e3921c5516cb95c763da 100644 --- a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/ConfigBean.java +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/ConfigBean.java @@ -5,11 +5,16 @@ import com.gitee.starblues.realize.PluginUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; import javax.annotation.Resource; /** - * @author starBlues + * + * * @version 1.0 */ @Configuration @@ -25,6 +30,23 @@ public class ConfigBean { return configBeanTest; } + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(factory); + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + // key采用String的序列化方式 + template.setKeySerializer(stringRedisSerializer); + // hash的key也采用String的序列化方式 + template.setHashKeySerializer(stringRedisSerializer); + // value序列化方式采用jackson + template.setValueSerializer(new StringRedisSerializer()); + // hash的value序列化方式采用jackson + template.setHashValueSerializer(new StringRedisSerializer()); + template.afterPropertiesSet(); + return template; + } + public static class ConfigBeanTest{ private String name; private Integer age; diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/PluginConfig1.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/PluginConfig1.java index 15890e08879aeb3c9d49883ad51701f6bcc85c80..322d01aa3bd9cbba88657a27a56e724d606a5ab6 100644 --- a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/PluginConfig1.java +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/PluginConfig1.java @@ -11,7 +11,7 @@ import java.util.Set; * @author starBlues * @version 1.0 */ -@ConfigDefinition("plugin1.yml") +@ConfigDefinition(fileName = "plugin1.yml") public class PluginConfig1 { private String name; diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/QuartzJobConfiguration.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/QuartzJobConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..60267c639cdb14f0b1caa014cf570660aeb64426 --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/QuartzJobConfiguration.java @@ -0,0 +1,35 @@ +package com.basic.example.plugin1.config; + +import com.basic.example.plugin1.job.QuartzJob1; +import com.gitee.starblues.factory.process.pipe.PluginPipeApplicationContextProcessor; +import org.quartz.*; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * @Description TODO + * @Author rockstal + * @Date 2021/4/22 19:18 + **/ +@Configuration +public class QuartzJobConfiguration { + + + @Bean + public JobDetail jobDetail1(){ + return JobBuilder.newJob(QuartzJob1.class).storeDurably().build(); + } + + @Bean + public Trigger trigger1(){ + SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule() + .withIntervalInSeconds(1) //每一秒执行一次 + .repeatForever(); //永久重复,一直执行下去 + return TriggerBuilder.newTrigger() + .forJob(jobDetail1()) + .withSchedule(scheduleBuilder) + .build(); + } + +} diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/SpringConfigBean.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/SpringConfigBean.java new file mode 100644 index 0000000000000000000000000000000000000000..d95257e46609df7f58ba2df65c1be0705f4feae4 --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/config/SpringConfigBean.java @@ -0,0 +1,64 @@ +package com.basic.example.plugin1.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; + +/** + * @author zhangzhuo + * @version 1.0 + * @since 2021-05-20 + */ +@Component +@ConfigurationProperties(prefix = "spring-config") +public class SpringConfigBean { + + private String name; + private Integer age; + private String sex; + private List list; + private Map map; + + + 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; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/job/QuartzJob1.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/job/QuartzJob1.java new file mode 100644 index 0000000000000000000000000000000000000000..8abae6388ee8f1493e59e293d957e5973ff040cd --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/job/QuartzJob1.java @@ -0,0 +1,28 @@ +package com.basic.example.plugin1.job; + +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.quartz.QuartzJobBean; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * @Description TODO + * @Author rockstal + * @Date 2021/4/22 19:18 + **/ +public class QuartzJob1 extends QuartzJobBean { + + private Logger logger = LoggerFactory.getLogger(QuartzJob1.class); + + @Override + protected void executeInternal(JobExecutionContext jobExecutionContext) + throws JobExecutionException { + SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + logger.info("QuartzJob1---{}", sdf.format(new Date())); + } + +} diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/rest/HelloPlugin1.java b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/rest/HelloPlugin1.java index 56c9aea392bb429b06bb48e37720b275d3571c70..142b41f520537380310c343aae39acaeba2d4d00 100644 --- a/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/rest/HelloPlugin1.java +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/java/com/basic/example/plugin1/rest/HelloPlugin1.java @@ -2,12 +2,17 @@ package com.basic.example.plugin1.rest; import com.basic.example.plugin1.config.ConfigBean; import com.basic.example.plugin1.config.PluginConfig1; +import com.basic.example.plugin1.config.SpringConfigBean; import com.basic.example.plugin1.service.HelloService; import com.gitee.starblues.realize.PluginUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.pf4j.PluginDescriptor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -35,9 +40,20 @@ public class HelloPlugin1 { @Autowired private ConfigBean.ConfigBeanTest configBeanTest; + @Value("${c.configYmlValue}") + private String configYmlValue; + + @Autowired + private SpringConfigBean springConfigBean; + + @Autowired + private ApplicationContext applicationContext; + + @GetMapping("plugin1") @ApiOperation(value = "hello", notes = "hello") public String sya(){ + System.out.println("configYmlValue="+configYmlValue); return "hello plugin1 example"; } @@ -68,4 +84,9 @@ public class HelloPlugin1 { return configBeanTest; } + @GetMapping("spring-config") + public SpringConfigBean getSpringConfigBean(){ + return springConfigBean; + } + } diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/resources/log.xml b/example/basic-example/plugins/basic-example-plugin1/src/main/resources/log.xml new file mode 100644 index 0000000000000000000000000000000000000000..b9796c9f4ea5dbf1656c0d8b49b37e91e174234a --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/resources/log.xml @@ -0,0 +1,10 @@ + + + ~\logs\ + current + INFO + 10MB + 10GB + 30 + %d{yyyy-MM-dd HH:mm:ss.SSS} -%5p --- [%t] %-40.40logger{39} : %m%n + \ No newline at end of file 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 a6f0abe0cad284dd2baf79e17e9417bc44c9fb90..16bfa60afecf5d8ab8fa4760f2fbe388060f0463 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,5 +1,5 @@ plugin.id=basic-example-plugin1 plugin.class=com.basic.example.plugin1.DefinePlugin -plugin.version=2.4.2-RELEASE +plugin.version=2.4.3-RELEASE plugin.provider=StarBlues plugin.requires=1.2.6 \ No newline at end of file diff --git a/example/basic-example/plugins/basic-example-plugin1/src/main/resources/pluginOfSpringBoot.yml b/example/basic-example/plugins/basic-example-plugin1/src/main/resources/pluginOfSpringBoot.yml new file mode 100644 index 0000000000000000000000000000000000000000..542ae399236384628d9ad52544ae03c7153a86ec --- /dev/null +++ b/example/basic-example/plugins/basic-example-plugin1/src/main/resources/pluginOfSpringBoot.yml @@ -0,0 +1,47 @@ +c: + configYmlValue: test +plugin: + log-config-location: classpath:log.xml + auto-config-class: + - org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration + - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration + +spring: + quartz: + auto-startup: false + startup-delay: 30 + scheduler-name: custom-plugin-scheduler + properties: + org: + quartz: + threadPool: + class: org.quartz.simpl.SimpleThreadPool + threadCount: 1 + threadPriority: 5 + threadsInheritContextClassLoaderOfInitializingThread: true + host: 127.0.0.1 + port: 6379 + timeout: 10000 + +logConfigLocation: classpath:log.xml + +list: + - a + - n + - c + +map: + a: 1 + b: 2 + +spring-config: + name: starblues + age: 26 + sex: 男 + list: + - a + - b + - c + map: + a: 1 + b: 2 diff --git a/example/basic-example/plugins/basic-example-plugin2/pom.xml b/example/basic-example/plugins/basic-example-plugin2/pom.xml index e062aca4d17fa9fdbf5d1913776edfd5ef3664c6..07ffe845ff9d0dd579cbf3b8e34cc41448a4eb02 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.2-RELEASE + 2.4.3-RELEASE ../pom.xml basic-example-plugin2 - 2.4.2-RELEASE + 2.4.3-RELEASE jar diff --git a/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/config/Plugin2Config.java b/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/config/Plugin2Config.java index 32f6832ad899a2a62f740b1aa66ec08ec3b89a48..bde4c87b021449720c9bc0f51b2b477ae2b80ef6 100644 --- a/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/config/Plugin2Config.java +++ b/example/basic-example/plugins/basic-example-plugin2/src/main/java/com/basic/example/plugin2/config/Plugin2Config.java @@ -11,7 +11,7 @@ import java.util.Set; * @author starBlues * @version 1.0 */ -@ConfigDefinition("plugin2.yml") +@ConfigDefinition(fileName = "plugin2.yml") public class Plugin2Config { private String name; 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 872974ae3d6c187982b4ccd5da5bd3450bc03921..a32f052e0ff28aa04d8bf41a8c54d6f6947b0d0e 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.2-RELEASE +plugin.version=2.4.3-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/basic-example/plugins/basic-example-plugin3/pom.xml b/example/basic-example/plugins/basic-example-plugin3/pom.xml index 68ef69fc3abfa3debbceb4ef8ff202788e0dcf8b..e2a246dd2e283819bd7361f2a054566cc9598af4 100644 --- a/example/basic-example/plugins/basic-example-plugin3/pom.xml +++ b/example/basic-example/plugins/basic-example-plugin3/pom.xml @@ -8,12 +8,12 @@ com.gitee.starblues basic-example-plugin-parent - 2.4.2-RELEASE + 2.4.3-RELEASE ../pom.xml basic-example-plugin3 - 2.4.2-RELEASE + 2.4.3-RELEASE jar diff --git a/example/basic-example/plugins/basic-example-plugin3/src/main/resources/plugin.properties b/example/basic-example/plugins/basic-example-plugin3/src/main/resources/plugin.properties index 403655e2af015cbc433c37bcecd48f5a0f8a238c..9e5ecaf6e841af3edcef78b8ac7c5cdd78f4b6ad 100644 --- a/example/basic-example/plugins/basic-example-plugin3/src/main/resources/plugin.properties +++ b/example/basic-example/plugins/basic-example-plugin3/src/main/resources/plugin.properties @@ -1,4 +1,4 @@ plugin.id=basic-example-plugin3 plugin.class=com.basic.example.plugin3.DefinePlugin -plugin.version=2.4.2-RELEASE +plugin.version=2.4.3-RELEASE plugin.provider=sousouki \ No newline at end of file diff --git a/example/basic-example/plugins/pom.xml b/example/basic-example/plugins/pom.xml index b726741addb1cbe2ef7aa216656a05ec26f748a0..4a77d7ee5b15d7429216d9202e1b1fb9731f9b99 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.2-RELEASE + 2.4.3-RELEASE pom diff --git a/example/basic-example/pom.xml b/example/basic-example/pom.xml index 7c64445459e6419050ff5ec9134b0e6eaa199fe8..34e4f1018a772bad13dd3498453218e5900f4798 100644 --- a/example/basic-example/pom.xml +++ b/example/basic-example/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues basic-example - 2.4.2-RELEASE + 2.4.3-RELEASE pom 基本案例 diff --git a/example/integration-log/integration-log-main/pom.xml b/example/integration-log/integration-log-main/pom.xml index a77618ac2b14b21033ce3f75476f5b70da458357..c13c2151d0d058466041e214f3c0b9416db63e56 100644 --- a/example/integration-log/integration-log-main/pom.xml +++ b/example/integration-log/integration-log-main/pom.xml @@ -12,7 +12,7 @@ com.gitee.starblues - 2.4.2-RELEASE + 2.4.3-RELEASE integration-log-main jar 集成插件日志 案例--主程序 @@ -22,8 +22,8 @@ UTF-8 UTF-8 3.7.0 - 2.4.2-RELEASE - 2.4.2-RELEASE + 2.4.3-RELEASE + 2.4.3-RELEASE diff --git a/example/integration-log/integration-log-main/src/main/java/com/log/plugin/config/PluginBeanConfig.java b/example/integration-log/integration-log-main/src/main/java/com/log/plugin/config/PluginBeanConfig.java index fc156edc792f1be116bf1ee6d15c4758995ed46c..e8dfb17bb54a7f24a0fe874fa7ad4b31f0e3544d 100644 --- a/example/integration-log/integration-log-main/src/main/java/com/log/plugin/config/PluginBeanConfig.java +++ b/example/integration-log/integration-log-main/src/main/java/com/log/plugin/config/PluginBeanConfig.java @@ -1,57 +1,17 @@ package com.log.plugin.config; import com.gitee.starblues.extension.log.SpringBootLogExtension; -import com.gitee.starblues.integration.ConfigurationBuilder; -import com.gitee.starblues.integration.IntegrationConfiguration; +import com.gitee.starblues.integration.AutoIntegrationConfiguration; import com.gitee.starblues.integration.application.AutoPluginApplication; import com.gitee.starblues.integration.application.PluginApplication; -import org.pf4j.RuntimeMode; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.stereotype.Component; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; -@Component -@ConfigurationProperties(prefix = "plugin") +@Configuration +@Import(AutoIntegrationConfiguration.class) public class PluginBeanConfig { - /** - * 运行模式 - * 开发环境: development、dev - * 生产/部署 环境: deployment、prod - */ - @Value("${runMode:dev}") - private String runMode; - - /** - * 插件的路径 - */ - @Value("${pluginPath:plugins}") - private String pluginPath; - - /** - * 插件文件的路径 - */ - @Value("${pluginConfigFilePath:pluginConfigs}") - private String pluginConfigFilePath; - - - - @Bean - public IntegrationConfiguration configuration(){ - return ConfigurationBuilder.toBuilder() - .runtimeMode(RuntimeMode.byName(runMode)) - .pluginPath(pluginPath) - .pluginConfigFilePath(pluginConfigFilePath) - .uploadTempPath("temp") - .backupPath("backupPlugin") - .pluginRestPathPrefix("/api/plugin") - .enablePluginIdRestPathPrefix(true) - .enableSwaggerRefresh(true) - .build(); - } - - /** * 定义插件应用。使用可以注入它操作插件。 * @return PluginApplication @@ -60,19 +20,8 @@ public class PluginBeanConfig { public PluginApplication pluginApplication(){ // 实例化自动初始化插件的PluginApplication PluginApplication pluginApplication = new AutoPluginApplication(); - pluginApplication.addExtension(new SpringBootLogExtension()); + pluginApplication.addExtension(new SpringBootLogExtension(SpringBootLogExtension.Type.LOGBACK)); return pluginApplication; } - public void setRunMode(String runMode) { - this.runMode = runMode; - } - - public void setPluginPath(String pluginPath) { - this.pluginPath = pluginPath; - } - - public void setPluginConfigFilePath(String pluginConfigFilePath) { - this.pluginConfigFilePath = pluginConfigFilePath; - } } diff --git a/example/integration-log/plugins/integration-log-plugin/pom.xml b/example/integration-log/plugins/integration-log-plugin/pom.xml index 811013f99b68da666d6c0cc4261be29e671a4622..8100214b5a5c77e7d56ee7df5297681acb8e4910 100644 --- a/example/integration-log/plugins/integration-log-plugin/pom.xml +++ b/example/integration-log/plugins/integration-log-plugin/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-log-plugin - 2.4.2-RELEASE + 2.4.3-RELEASE jar diff --git a/example/integration-log/plugins/integration-log-plugin/src/main/java/com/log/plugin/LogPlugin.java b/example/integration-log/plugins/integration-log-plugin/src/main/java/com/log/plugin/LogPlugin.java index 6b3a527556273901d94a17576d33136af09b0467..1647d90865e91493ffd7fa618f3dfaf6a9204950 100644 --- a/example/integration-log/plugins/integration-log-plugin/src/main/java/com/log/plugin/LogPlugin.java +++ b/example/integration-log/plugins/integration-log-plugin/src/main/java/com/log/plugin/LogPlugin.java @@ -1,25 +1,21 @@ package com.log.plugin; -import com.gitee.starblues.extension.log.config.SpringBootLogConfig; +import com.gitee.starblues.annotation.ConfigDefinition; import com.gitee.starblues.realize.BasePlugin; import org.pf4j.PluginWrapper; -import java.util.HashSet; -import java.util.Set; - /** * description * * @author sousouki - * @version 1.0 + * @version 2.4.3 */ -public class LogPlugin extends BasePlugin implements SpringBootLogConfig { +@ConfigDefinition(fileName = "config.yml") +public class LogPlugin extends BasePlugin { - private Set locations = new HashSet<>(); public LogPlugin(PluginWrapper wrapper) { super(wrapper); - locations.add("classpath:log.xml"); } @Override @@ -37,9 +33,5 @@ public class LogPlugin extends BasePlugin implements SpringBootLogConfig { } - @Override - public Set logConfigLocations() { - return locations; - } } diff --git a/example/integration-log/plugins/integration-log-plugin/src/main/java/com/log/plugin/rest/MainRest.java b/example/integration-log/plugins/integration-log-plugin/src/main/java/com/log/plugin/rest/MainRest.java index b92925a73978e4b42e9b1a7213d9a8d5b7c7a032..7b3be4fda8afee7dba2918f44e366a49d06d0273 100644 --- a/example/integration-log/plugins/integration-log-plugin/src/main/java/com/log/plugin/rest/MainRest.java +++ b/example/integration-log/plugins/integration-log-plugin/src/main/java/com/log/plugin/rest/MainRest.java @@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.*; /** * 插件jar 包测试功能 * @author sousouki - * @version 1.0 + * @version 2.4.3 */ @RestController @RequestMapping("/log/main") diff --git a/example/integration-log/plugins/integration-log-plugin/src/main/resources/config.yml b/example/integration-log/plugins/integration-log-plugin/src/main/resources/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..7fe05d5a2acc6873c8a56630d3ab17c46429b942 --- /dev/null +++ b/example/integration-log/plugins/integration-log-plugin/src/main/resources/config.yml @@ -0,0 +1,2 @@ +plugin: + logConfigLocation: classpath:log.xml \ No newline at end of file diff --git a/example/integration-log/plugins/integration-log-plugin/src/main/resources/log.xml b/example/integration-log/plugins/integration-log-plugin/src/main/resources/log.xml index 11b1e7921335652064bbb2f4924c638ff7eac440..69b2bdd3275fd6c90ea1357f7f544fb1cac1cdd4 100644 --- a/example/integration-log/plugins/integration-log-plugin/src/main/resources/log.xml +++ b/example/integration-log/plugins/integration-log-plugin/src/main/resources/log.xml @@ -1,6 +1,7 @@ - integration-log-plugin + ~\logs\ + current INFO 10MB 10GB diff --git a/example/integration-log/plugins/integration-log-plugin/src/main/resources/plugin.properties b/example/integration-log/plugins/integration-log-plugin/src/main/resources/plugin.properties index 15d57f165dc39f82fb4cd0d75d78a1e108608424..ea83962d4dccac2b41555ea977e457e0212c53eb 100644 --- a/example/integration-log/plugins/integration-log-plugin/src/main/resources/plugin.properties +++ b/example/integration-log/plugins/integration-log-plugin/src/main/resources/plugin.properties @@ -1,4 +1,4 @@ plugin.id=integration-log-plugin plugin.class=com.log.plugin.LogPlugin -plugin.version=2.4.2-RELEASE +plugin.version=2.4.3-RELEASE plugin.provider=sousouki \ No newline at end of file diff --git a/example/integration-log/pom.xml b/example/integration-log/pom.xml index 876a306d3fe77f2498db8db94535e1e5590d0825..1185d46dc919189c47763d281fb3f29ca14be28e 100644 --- a/example/integration-log/pom.xml +++ b/example/integration-log/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-log - 2.4.2-RELEASE + 2.4.3-RELEASE pom 集成插件日志案例 diff --git a/example/integration-mybatis/integration-mybatis-main/pom.xml b/example/integration-mybatis/integration-mybatis-main/pom.xml index bde1828c08c701d2747571d8dc642c0bdcb112b2..78b0cbc919c1dbd216e96b5e514c8aae1dc6c1e2 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.2-RELEASE + 2.4.3-RELEASE jar 主程序模块 - 2.4.2-RELEASE - 2.4.2-RELEASE - 2.4.2-RELEASE + 2.4.3-RELEASE + 2.4.3-RELEASE + 2.4.3-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 b46cd85ffee3018b524faf3f2da0b23a6154b318..25afa4debb3bae59f4e5e29767b87c085d1a0e17 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.2-RELEASE + 2.4.3-RELEASE pom diff --git a/example/integration-mybatis/integration-mybatis-runner/pom.xml b/example/integration-mybatis/integration-mybatis-runner/pom.xml index 3293e338ee99f265724e01fbe4b9b866a9a42719..5b3ddd2586b338ebfd562d91a2ee2330921219da 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.2-RELEASE + 2.4.3-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 049828c8d4f0859377ad48f5bbf08159599c8957..4ab5fe7956acc166f625cb683177333205bcc07d 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.2-RELEASE + 2.4.3-RELEASE ../../integration-mybatis-plugin-parent/pom.xml integration-mybatis-plugin1 - 2.4.2-RELEASE + 2.4.3-RELEASE jar diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/ExamplePlugin1.java b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/ExamplePlugin1.java index b5154fd47be393937aedf8e1b3ef77f9ffa2eb59..bd8d018dce7e80b7f2f03978466f1521248cc386 100644 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/ExamplePlugin1.java +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/ExamplePlugin1.java @@ -1,5 +1,6 @@ package com.mybatis.plugin1; +import com.gitee.starblues.annotation.ConfigDefinition; import com.gitee.starblues.realize.BasePlugin; import org.pf4j.PluginWrapper; @@ -10,6 +11,7 @@ import org.pf4j.PluginWrapper; * @author starBlues * @version 1.0 */ +@ConfigDefinition(fileName = "plugin1-spring.yml", devSuffix = "-dev") public class ExamplePlugin1 extends BasePlugin { diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/ResourceConfig.java b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/ResourceConfig.java deleted file mode 100644 index 17784c8cd48f01567be43ac4da7f870762da7d8e..0000000000000000000000000000000000000000 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/ResourceConfig.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.mybatis.plugin1; - -import com.gitee.starblues.annotation.ConfigDefinition; -import com.gitee.starblues.extension.resources.StaticResourceConfig; -import com.gitee.starblues.extension.resources.thymeleaf.SpringBootThymeleafConfig; -import com.gitee.starblues.extension.resources.thymeleaf.ThymeleafConfig; -import org.springframework.stereotype.Component; - -import java.util.HashSet; -import java.util.Set; - -/** - * @author starBlues - * @version 1.0 - * @since 2020-12-19 - */ -@ConfigDefinition -public class ResourceConfig implements StaticResourceConfig, SpringBootThymeleafConfig { - @Override - public Set locations() { - Set locations = new HashSet<>(); - locations.add("classpath:static"); - locations.add("file:D:\\aa"); - return locations; - } - - @Override - public void config(ThymeleafConfig thymeleafConfig) { - thymeleafConfig.setPrefix("tf"); - thymeleafConfig.setSuffix(".html"); - } -} diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/config/Plugin1Config.java b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/config/Plugin1Config.java index 7582238b4e7308b84997d92cb08b74147d032e6a..ffdc1ce41c597fd29dde564ab42ae94013aed8a2 100644 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/config/Plugin1Config.java +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/java/com/mybatis/plugin1/config/Plugin1Config.java @@ -8,7 +8,7 @@ import com.gitee.starblues.annotation.ConfigDefinition; * @author starBlues * @version 1.0 */ -@ConfigDefinition("plugin1.yml") +@ConfigDefinition(fileName = "plugin1.yml") public class Plugin1Config { private String name; 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 76260415a95784e0fd9f492e879eda055535d781..f0677860a6a19296cc40cc386638e9f5c835f6a5 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.2-RELEASE +plugin.version=2.4.3-RELEASE plugin.provider=StarBlues \ No newline at end of file diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/resources/plugin1-spring-dev.yml b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/resources/plugin1-spring-dev.yml new file mode 100644 index 0000000000000000000000000000000000000000..0a7353b62cb237ac8ddc180c07fccf8718846bcf --- /dev/null +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin1/src/main/resources/plugin1-spring-dev.yml @@ -0,0 +1,10 @@ +plugin: + static: + locations: + - classpath:static + - file:D:\aa + thymeleaf: + prefix: tf + suffix: .html + mode: html + encoding: utf-8 \ 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 5c9d9ff3ccc5f30397e0df16fef7c668a57fd026..f21c81d02982c86b2b63de15852162e1f4cc2e00 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.2-RELEASE +plugin.version=2.4.3-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 bf03e5f9f40f802ce987fae5d6a3b05bab5ac370..267a91386baf1c27653773051589327055cf3382 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.2-RELEASE + 2.4.3-RELEASE ../../integration-mybatis-plugin-parent/pom.xml integration-mybatis-plugin2 - 2.4.2-RELEASE + 2.4.3-RELEASE jar diff --git a/example/integration-mybatis/plugins/integration-mybatis-plugin2/src/main/java/com/mybatis/plugin2/config/Plugin2Config.java b/example/integration-mybatis/plugins/integration-mybatis-plugin2/src/main/java/com/mybatis/plugin2/config/Plugin2Config.java index 2f32fb7c86ec34f368cb76c4d5bf711fda1c04ca..fcf3cd22767369f5e46b1b530325ada71ec565bc 100644 --- a/example/integration-mybatis/plugins/integration-mybatis-plugin2/src/main/java/com/mybatis/plugin2/config/Plugin2Config.java +++ b/example/integration-mybatis/plugins/integration-mybatis-plugin2/src/main/java/com/mybatis/plugin2/config/Plugin2Config.java @@ -8,7 +8,7 @@ import com.gitee.starblues.annotation.ConfigDefinition; * @author starBlues * @version 1.0 */ -@ConfigDefinition("plugin2.yml") +@ConfigDefinition(fileName = "plugin2.yml") public class Plugin2Config { private String name; diff --git a/example/integration-mybatis/pom.xml b/example/integration-mybatis/pom.xml index c82c1ae6f2fb26df7d56620e81025a43b2e41531..9225b48bc2dad93389d710f162a5b001265c47d1 100644 --- a/example/integration-mybatis/pom.xml +++ b/example/integration-mybatis/pom.xml @@ -7,7 +7,7 @@ com.gitee.starblues integration-mybatis - 2.4.2-RELEASE + 2.4.3-RELEASE pom 集成mybatis案例 diff --git a/example/integration-mybatisplus/integration-mybatisplus-main/pom.xml b/example/integration-mybatisplus/integration-mybatisplus-main/pom.xml index a8300eff340261450cb5f796fb3b8c8c6256c809..f72791f9086c5847638b7995dc2496a978b7aa1d 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.2-RELEASE + 2.4.3-RELEASE integration-mybatisplus-main jar 集成mybatis-plus 案例--主程序 @@ -27,8 +27,8 @@ 2.0.1 3.4.1 - 2.4.2-RELEASE - 2.4.2-RELEASE + 2.4.3-RELEASE + 2.4.3-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 1af73e00ec3962ecfbac656905eb9213b6a5c0c2..fc654779dfbad1074ffa64eecd5f3f819c15eab7 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.2-RELEASE +plugin.version=2.4.3-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 943da61420066e4ffcc7a5202f216180043346c2..3e8a5b19b9b98c9741ede65c2ef2c52008b8a4bb 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.2-RELEASE + 2.4.3-RELEASE jar diff --git a/example/integration-mybatisplus/pom.xml b/example/integration-mybatisplus/pom.xml index 3119395ad449a7a9cbe2152b374b8bb0c2be324d..78e52d76021ff9af465c198cafa91b3048848cef 100644 --- a/example/integration-mybatisplus/pom.xml +++ b/example/integration-mybatisplus/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-mybatisplus - 2.4.2-RELEASE + 2.4.3-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 be29244b086b0b65696a40e905927f8a505113df..44f3208750e5db8103b94adb2d686945738c0ba5 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.2-RELEASE + 2.4.3-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 55b6531e618e123c4f7f1999bea6540c0a921925..5a4d073ef962d7290ac052beb912adfbe011340f 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.2-RELEASE +plugin.version=2.4.3-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 b4877661ec5e1d3fa14ab5538f0231a3994f9753..e7d81ba2b7bbd586032510ce30e59f4efff06726 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.2-RELEASE + 2.4.3-RELEASE jar diff --git a/example/integration-tkmybatis/pom.xml b/example/integration-tkmybatis/pom.xml index e48c1f7cddbfb896fcf7e54a8c0e30b7ef32d40f..241b9a0e31a75f924399003a80199779d3748ae1 100644 --- a/example/integration-tkmybatis/pom.xml +++ b/example/integration-tkmybatis/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues integration-tkmybatis - 2.4.2-RELEASE + 2.4.3-RELEASE pom 集成mybatis-plus案例 diff --git a/example/pom.xml b/example/pom.xml index a8acf4bf2b7b269ded68f6f82f1ece8411b3e948..fc82283485f895a6919b37227c4f84f2c76be31b 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -6,7 +6,7 @@ com.gitee.starblues springboot-plugin-framework-example - 2.4.2-RELEASE + 2.4.3-RELEASE pom diff --git a/pom.xml b/pom.xml index 2efd6ac106dfe1aa350fcee0f27a85e2ba0f7019..747da43bee76ce1c4cbd38937aafdac7f4b6191a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.gitee.starblues springboot-plugin-framework-parent pom - 2.4.2-RELEASE + 2.4.3-RELEASE spring boot 插件开发集成包 diff --git a/springboot-plugin-framework-extension/pom.xml b/springboot-plugin-framework-extension/pom.xml index 0c360834d2253595776ae0722ffc282680d86273..32b896a09027926e95836115b01a892a1b7697b2 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.2-RELEASE + 2.4.3-RELEASE spring boot 插件式开发集成包--扩展模块 diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/pom.xml b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/pom.xml index 515002a18d0a7f6d9ce9a45f338a5540f259a239..6d98075c023adf67e9c09d45bd80ab0b74ea61f0 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/pom.xml +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/pom.xml @@ -12,7 +12,7 @@ com.gitee.starblues springboot-plugin-framework-extension-log - 2.4.2-RELEASE + 2.4.3-RELEASE 插件扩展-日志模块扩展 @@ -62,9 +62,10 @@ 3.1.0 1.6 - 2.4.2-RELEASE + 2.4.3-RELEASE 5.0.7.RELEASE 1.2.3 + 2.14.1 @@ -74,17 +75,23 @@ ${logback.version} provided + + org.apache.logging.log4j + log4j-core + ${log4j.version} + provided + org.springframework spring-context ${spring-version} - compile + provided com.gitee.starblues springboot-plugin-framework ${springboot-plugin-framework.version} - compile + provided diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/LogRegistry.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/LogRegistry.java new file mode 100644 index 0000000000000000000000000000000000000000..aab1010249d0566d375b53eda64715c85f33d5c0 --- /dev/null +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/LogRegistry.java @@ -0,0 +1,31 @@ +package com.gitee.starblues.extension.log; + +import com.gitee.starblues.factory.PluginRegistryInfo; +import org.pf4j.PluginWrapper; +import org.springframework.core.io.Resource; + +import java.util.List; + +/** + * 日志注册统一接口 + * @author starBlues + * @version 2.4.3 + */ +public interface LogRegistry { + + /** + * 注册日志 + * @param resources 日志配置文件资源 + * @param pluginRegistryInfo 当前插件的信息 + * @throws Exception 注册异常 + **/ + void registry(List resources, PluginRegistryInfo pluginRegistryInfo) throws Exception; + + /** + * 注册日志 + * @param pluginRegistryInfo 当前插件的信息 + * @throws Exception 卸载异常 + **/ + void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception; + +} diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PluginLogConfigLoader.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PluginLogConfigLoader.java deleted file mode 100644 index 32c94b6b59939f07f6c9f3c04be1836e67354397..0000000000000000000000000000000000000000 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PluginLogConfigLoader.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.gitee.starblues.extension.log; - -import com.gitee.starblues.extension.log.config.SpringBootLogConfig; -import com.gitee.starblues.factory.PluginRegistryInfo; -import com.gitee.starblues.factory.process.pipe.loader.PluginResourceLoader; -import com.gitee.starblues.factory.process.pipe.loader.ResourceWrapper; -import com.gitee.starblues.realize.BasePlugin; -import com.gitee.starblues.utils.OrderPriority; -import com.gitee.starblues.utils.ResourceUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.core.io.FileSystemResource; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.springframework.util.CollectionUtils; - -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.*; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.stream.Collectors; - -/** - * 日志配置加载者 - * @author sousouki - * @version 2.4.3 - */ -public class PluginLogConfigLoader implements PluginResourceLoader { - - private Logger log = LoggerFactory.getLogger(PluginLogConfigLoader.class); - - public static final String KEY = "SpringBootLogConfigLoader"; - - @Override - public String key() { - return KEY; - } - - @Override - public ResourceWrapper load(PluginRegistryInfo pluginRegistryInfo) throws Exception { - BasePlugin basePlugin = pluginRegistryInfo.getBasePlugin(); - if (!(basePlugin instanceof SpringBootLogConfig)) { - log.warn("Plugin '{}' not implements SpringBootLogConfig, If you need to use log in the plugin," + "Please implements SpringBootLogConfig interface", basePlugin.getWrapper().getPluginId()); - return null; - } - SpringBootLogConfig springBootLogConfig = (SpringBootLogConfig) basePlugin; - Set logConfigLocations = springBootLogConfig.logConfigLocations(); - if (logConfigLocations == null || logConfigLocations.isEmpty()) { - log.warn("SpringBootLogConfig -> logConfigLocations return is empty, " + "Please check configuration"); - return new ResourceWrapper(); - } - ResourcePatternResolver resourcePatternResolver = - new PathMatchingResourcePatternResolver(pluginRegistryInfo.getPluginClassLoader()); - List resources = new ArrayList<>(); - for (String logConfigLocation : logConfigLocations) { - String matchLocation = ResourceUtils.getMatchLocation(logConfigLocation); - if (matchLocation == null || "".equals(matchLocation)) { - continue; - } - Resource[] logConfigResources = resourcePatternResolver.getResources(matchLocation); - if (logConfigResources.length != 0) { - resources.addAll(Arrays.asList(logConfigResources)); - } - } - ResourceWrapper resourceWrapper = new ResourceWrapper(); - resourceWrapper.addResources(resources); - return resourceWrapper; - } - - @Override - public void unload(PluginRegistryInfo pluginRegistryInfo, ResourceWrapper resourceWrapper) throws Exception { - - } - - @Override - public OrderPriority order() { - return OrderPriority.getHighPriority(); - } - - -} diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PluginLogConfigProcessor.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PluginLogConfigProcessor.java index 06f91c866e4fdfe4c7b2d687699d7594ec8a7f6d..71c7d021f66fd6cd60a098892a3fde185f54960c 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PluginLogConfigProcessor.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PluginLogConfigProcessor.java @@ -1,13 +1,21 @@ package com.gitee.starblues.extension.log; -import com.gitee.starblues.extension.log.util.LogConfigProcess; +import com.gitee.starblues.extension.log.log4j.Log4jLogRegistry; +import com.gitee.starblues.extension.log.logback.LogbackLogRegistry; import com.gitee.starblues.factory.PluginRegistryInfo; import com.gitee.starblues.factory.process.pipe.PluginPipeProcessorExtend; -import com.gitee.starblues.factory.process.pipe.loader.ResourceWrapper; import com.gitee.starblues.utils.OrderPriority; -import org.pf4j.PluginWrapper; +import com.gitee.starblues.utils.ResourceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; +import org.springframework.util.ObjectUtils; +import java.io.IOException; +import java.util.ArrayList; import java.util.List; /** @@ -17,11 +25,22 @@ import java.util.List; */ class PluginLogConfigProcessor implements PluginPipeProcessorExtend { - private final LogConfigProcess logConfigProcess = LogConfigProcess.getInstance(); + private final static Logger LOG = LoggerFactory.getLogger(PluginLogConfigProcessor.class); + private final LogRegistry logRegistry; + + public PluginLogConfigProcessor(SpringBootLogExtension.Type type){ + if(type == SpringBootLogExtension.Type.LOG4J){ + logRegistry = new Log4jLogRegistry(); + } else if(type == SpringBootLogExtension.Type.LOGBACK){ + logRegistry = new LogbackLogRegistry(); + } else { + logRegistry = null; + } + } @Override public String key() { - return null; + return "SpringBootLogConfigProcessor"; } @Override @@ -36,24 +55,49 @@ class PluginLogConfigProcessor implements PluginPipeProcessorExtend { @Override public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception { - if (logConfigProcess == null) { - return; - } - PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); - ResourceWrapper resourceWrapper = pluginRegistryInfo.getPluginLoadResource(PluginLogConfigLoader.KEY); - if (resourceWrapper == null) { + if (logRegistry == null) { return; } + Resource resource = getLogConfigFile(pluginRegistryInfo); + List resources = new ArrayList<>(1); + resources.add(resource); + logRegistry.registry(resources, pluginRegistryInfo); + } - List pluginResources = resourceWrapper.getResources(); - if (pluginResources == null || pluginResources.isEmpty()) { + @Override + public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + if (logRegistry == null) { return; } - logConfigProcess.loadLogConfig(pluginResources, pluginWrapper); + logRegistry.unRegistry(pluginRegistryInfo); } - @Override - public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { - logConfigProcess.unloadLogConfig(pluginRegistryInfo.getPluginWrapper()); + /** + * 加载日志配置文件资源 + * 文件路径配置为

file:D://log.xml


+ * resources路径配置为

classpath:log.xml


+ * @param pluginRegistryInfo 当前插件注册的信息 + * @throws IOException 获取不到配置文件异常 + **/ + private Resource getLogConfigFile(PluginRegistryInfo pluginRegistryInfo) throws IOException { + GenericApplicationContext pluginApplicationContext = pluginRegistryInfo.getPluginApplicationContext(); + String logConfigLocation = pluginApplicationContext.getEnvironment() + .getProperty(PropertyKey.LOG_CONFIG_LOCATION); + if (ObjectUtils.isEmpty(logConfigLocation)) { + return null; + } + String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); + String matchLocation = ResourceUtils.getMatchLocation(logConfigLocation); + if (matchLocation == null || "".equals(matchLocation)) { + LOG.warn("Plugin '{}' not match {}: {}", pluginId, PropertyKey.LOG_CONFIG_LOCATION, + logConfigLocation); + return null; + } + if(ResourceUtils.isFile(logConfigLocation)){ + return new FileSystemResource(matchLocation); + } else { + return new ClassPathResource(matchLocation, pluginRegistryInfo.getPluginClassLoader()); + } } + } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PropertyKey.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PropertyKey.java new file mode 100644 index 0000000000000000000000000000000000000000..a907a53d694cacd3710e57a17b0d3eed008ddd03 --- /dev/null +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/PropertyKey.java @@ -0,0 +1,11 @@ +package com.gitee.starblues.extension.log; + +/** + * @author zhangzhuo + * @version 2.4.3 + */ +public class PropertyKey { + + public final static String LOG_CONFIG_LOCATION = "plugin.log-config-location"; + +} diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/SpringBootLogExtension.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/SpringBootLogExtension.java index 37787d63baf4f32157e08dfb20ba832c33723e5a..93cb475eafd74ecba1be70a94ea07fc23409635b 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/SpringBootLogExtension.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/SpringBootLogExtension.java @@ -3,7 +3,6 @@ package com.gitee.starblues.extension.log; import com.gitee.starblues.extension.AbstractExtension; import com.gitee.starblues.factory.process.pipe.PluginPipeProcessorExtend; -import com.gitee.starblues.factory.process.pipe.loader.PluginResourceLoader; import org.springframework.context.ApplicationContext; import java.util.ArrayList; @@ -11,29 +10,41 @@ import java.util.List; /** * 日志扩展 - * @author sousouki + * @author sousouki starBlues * @version 2.4.3 */ public class SpringBootLogExtension extends AbstractExtension { - public static final String KEY = "SpringBootLogExtension"; + private static final String KEY = "SpringBootLogExtension"; - @Override - public String key() { - return KEY; + private final Type type; + + public SpringBootLogExtension(Type type){ + this.type = type; } + @Override - public List getPluginResourceLoader() { - List resourceLoaders = new ArrayList<>(); - resourceLoaders.add(new PluginLogConfigLoader()); - return resourceLoaders; + public String key() { + return KEY; } @Override - public List getPluginPipeProcessor(ApplicationContext applicationContext) { + public List getPluginPipeProcessor(ApplicationContext mainApplicationContext) { List pipeProcessorExtends = new ArrayList<>(); - pipeProcessorExtends.add(new PluginLogConfigProcessor()); + pipeProcessorExtends.add(new PluginLogConfigProcessor(type)); return pipeProcessorExtends; } + + public enum Type{ + /** + * 集成log4j + **/ + LOG4J, + + /** + * 集成 logback + **/ + LOGBACK + } } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/annotation/ConfigItem.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/annotation/ConfigItem.java index c868a46430ae2a0a30a7532065f3269f138cbfea..38d98117edca3edbadab1c279f4af1763491b646 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/annotation/ConfigItem.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/annotation/ConfigItem.java @@ -6,9 +6,21 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * 配置的默认值注解 + * @author sousouki + * @version 2.4.3 + */ @Documented @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface ConfigItem { + + + /** + * 默认值 + * @return String + */ String defaultValue() default ""; + } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/config/LogConfig.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/config/LogConfig.java index 4f5818738ad5381a0352678030923b6285177441..596d4e9012397179b672f485816ef60069014960 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/config/LogConfig.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/config/LogConfig.java @@ -14,32 +14,73 @@ import javax.xml.bind.annotation.XmlTransient; @XmlRootElement(name = "log") public class LogConfig { + public static final String ROOT_PLUGIN_SIGN = "~"; + + /** + * 日志存储根目录,默认为当前插件存放目录。 + * ~: 符号表示当前插件根目录 + **/ + @XmlElement(name = "rootDir") + @ConfigItem(defaultValue = ROOT_PLUGIN_SIGN + "/logs/") + private String rootDir; + + + /** + * 日志文件名称 + **/ @XmlElement(name = "fileName") private String fileName; - @ConfigItem(defaultValue = "INFO") + /** + * 日志级别 + **/ @XmlElement(name = "level") + @ConfigItem(defaultValue = "INFO") private String level; - @ConfigItem(defaultValue = "10MB") + /** + * 日志文件最大容量 + **/ @XmlElement(name = "maxFileSize") + @ConfigItem(defaultValue = "10MB") private String maxFileSize; - @ConfigItem(defaultValue = "10GB") + /** + * 日志文件总容量 + **/ @XmlElement(name = "totalFileSize") + @ConfigItem(defaultValue = "10GB") private String totalFileSize; - @ConfigItem(defaultValue = "30") + /** + * 最大保存时间 + **/ @XmlElement(name = "maxHistory") + @ConfigItem(defaultValue = "30") private Integer maxHistory; - @ConfigItem(defaultValue = "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p --- [%t] %-40.40logger{39} : %m%n") + /** + * 日志内容格式 + **/ @XmlElement(name = "pattern") + @ConfigItem(defaultValue = "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p --- [%t] %-40.40logger{39} : %m%n") private String pattern; + /** + * 包名, 自定义当前插件的日志包名, 默认为 BasePlugin 实现类的 包名 + **/ @XmlTransient private String packageName; + @XmlTransient + public String getRootDir() { + return rootDir; + } + + public void setRootDir(String rootDir) { + this.rootDir = rootDir; + } + @XmlTransient public String getFileName() { return fileName; diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/config/SpringBootLogConfig.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/config/SpringBootLogConfig.java deleted file mode 100644 index ff27d201648634e46c2f5f3360ca9236a68788f4..0000000000000000000000000000000000000000 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/config/SpringBootLogConfig.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.gitee.starblues.extension.log.config; - -import java.util.Set; - -/** - * 日志配置 - * @author sousouki - * @version 2.4.3 - */ -public interface SpringBootLogConfig { - - /** - * 日志xml配置文件的路径 - * @return Set - */ - Set logConfigLocations(); -} diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/log4j/Log4jLogRegistry.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/log4j/Log4jLogRegistry.java new file mode 100644 index 0000000000000000000000000000000000000000..c314075af12fa6065153f51580a9bb492318d31d --- /dev/null +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/log4j/Log4jLogRegistry.java @@ -0,0 +1,209 @@ +package com.gitee.starblues.extension.log.log4j; + +import com.gitee.starblues.extension.log.LogRegistry; +import com.gitee.starblues.extension.log.config.LogConfig; +import com.gitee.starblues.extension.log.logback.LogbackLogRegistry; +import com.gitee.starblues.extension.log.util.LogConfigUtil; +import com.gitee.starblues.factory.PluginRegistryInfo; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.core.*; +import org.apache.logging.log4j.core.appender.RollingFileAppender; +import org.apache.logging.log4j.core.appender.rolling.*; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.DefaultConfiguration; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.filter.AbstractFilter; +import org.apache.logging.log4j.core.layout.PatternLayout; +import org.apache.logging.log4j.message.Message; +import org.pf4j.PluginWrapper; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.Resource; + +import java.nio.charset.Charset; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 日志注册统一接口 + * @author starBlues + * @version 2.4.3 + */ +public class Log4jLogRegistry implements LogRegistry { + + private final org.slf4j.Logger log = LoggerFactory.getLogger(LogbackLogRegistry.class); + private final Map> pluginAppenderInfo = new ConcurrentHashMap<>(); + + + @Override + public void registry(List resources, PluginRegistryInfo pluginRegistryInfo) throws Exception { + PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); + LoggerContext loggerContext = (LoggerContext)LogManager.getContext(false); + Configuration configuration = loggerContext.getConfiguration(); + LoggerConfig rootLogger = configuration.getRootLogger(); + Set allAppender = new HashSet<>(); + for (Resource resource : resources) { + if(resource == null){ + continue; + } + LogConfig logConfig; + try { + logConfig = LogConfigUtil.getLogConfig(resource, pluginRegistryInfo); + } catch (Exception e){ + log.error("Failed to read log configuration.", e); + continue; + } + Set appenderSet = getAppender(pluginRegistryInfo, logConfig); + for (Appender appender : appenderSet) { + configuration.addAppender(appender); + rootLogger.addAppender(appender, Level.toLevel(logConfig.getLevel()), null); + allAppender.add(appender); + } + } + pluginAppenderInfo.put(pluginWrapper.getPluginId(), allAppender); + } + + + private Set getAppender(PluginRegistryInfo pluginRegistryInfo, LogConfig logConfig){ + PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); + Filter filter = new LogFilter(pluginRegistryInfo.getBasePlugin().scanPackage()); + PatternLayout patternLayout = PatternLayout.newBuilder() + .withPattern(logConfig.getPattern()) + .withCharset(Charset.defaultCharset()) + .build(); + + final TriggeringPolicy policy = + CompositeTriggeringPolicy.createPolicy( + SizeBasedTriggeringPolicy.createPolicy( + logConfig.getMaxFileSize() + ), + TimeBasedTriggeringPolicy.createPolicy("1", "true") + ); + + RolloverStrategy strategy = DefaultRolloverStrategy.newBuilder() + .withFileIndex(logConfig.getTotalFileSize()) + .withConfig(new DefaultConfiguration()) + .withMax(String.valueOf(logConfig.getMaxHistory())) + .build(); + + RollingFileAppender appender = RollingFileAppender.newBuilder() + .withFilter(filter) + .withName(pluginWrapper.getPluginId()) + .withLayout(patternLayout) + .withIgnoreExceptions(false) + .withFileName(LogConfigUtil.getLogFile(pluginRegistryInfo, logConfig).concat(".log")) + .withFilePattern(".%d{yyyy-MM-dd}-%i.log") + .withAppend(true) + .withPolicy(policy) + .withStrategy(strategy) + .build(); + + appender.start(); + filter.start(); + + return Collections.singleton(appender); + } + + @Override + public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + Set allAppender = pluginAppenderInfo.get(pluginRegistryInfo.getPluginWrapper().getPluginId()); + if(allAppender == null || allAppender.isEmpty()){ + return; + } + LoggerContext loggerContext = (LoggerContext)LogManager.getContext(false); + Configuration configuration = loggerContext.getConfiguration(); + for (Appender appender : allAppender) { + configuration.getAppenders().remove(appender.getName()); + } + } + + private static class LogFilter extends AbstractFilter{ + + private final String packageName; + + private LogFilter(String packageName) { + this.packageName = packageName; + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9) { + return filter(logger.getName()); + } + + @Override + public Result filter(LogEvent event) { + return filter(event.getLoggerName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1, Object p2) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1, Object p2, Object p3) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1, Object p2, Object p3, Object p4) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7) { + return filter(logger.getName()); + } + + @Override + public Result filter(Logger logger, Level level, Marker marker, String msg, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8) { + return filter(logger.getName()); + } + + private Result filter(String loggerName){ + if (loggerName.startsWith(packageName)) { + return Result.ACCEPT; + } + return Result.DENY; + } + } + + + +} diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/util/LogConfigProcess.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/logback/LogbackLogRegistry.java similarity index 40% rename from springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/util/LogConfigProcess.java rename to springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/logback/LogbackLogRegistry.java index 0c7fa912c48368bfb0166658799b4c94887f43b0..d81d723cc387a62018d2ee52fd603bd6d596d79c 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/util/LogConfigProcess.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/logback/LogbackLogRegistry.java @@ -1,254 +1,185 @@ -package com.gitee.starblues.extension.log.util; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.encoder.PatternLayoutEncoder; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.ConsoleAppender; -import ch.qos.logback.core.filter.Filter; -import ch.qos.logback.core.rolling.RollingFileAppender; -import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy; -import ch.qos.logback.core.spi.FilterReply; -import ch.qos.logback.core.util.FileSize; -import ch.qos.logback.core.util.OptionHelper; -import com.gitee.starblues.extension.log.annotation.ConfigItem; -import com.gitee.starblues.extension.log.config.LogConfig; -import org.pf4j.PluginWrapper; -import org.slf4j.LoggerFactory; -import org.springframework.core.io.Resource; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; -import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 日志配置处理者 - * @author sousouki - * @version 2.4.3 - */ -public class LogConfigProcess { - - private org.slf4j.Logger log = LoggerFactory.getLogger(LogConfigProcess.class); - - private final Map pluginLogMap = new HashMap<>(); - - private static LogConfigProcess instance = null; - - private LogConfigProcess() { - } - - public static LogConfigProcess getInstance() { - if (instance == null) { - synchronized (LogConfigProcess.class) { - if (instance == null) { - instance = new LogConfigProcess(); - } - } - } - return instance; - } - - public void loadLogConfig(List resources, PluginWrapper pluginWrapper) { - for (Resource resource : resources) { - String configText; - try { - configText = readConfigText(resource); - } catch (IOException e) { - log.error("Failed to read log configuration.", e); - return; - } - LogConfig logConfig; - try { - logConfig = (LogConfig) xml2object(configText); - } catch (Exception e) { - log.error("", e); - return; - } - checkLogConfig(logConfig, pluginWrapper.getPluginId()); - String pluginClass = pluginWrapper.getDescriptor().getPluginClass(); - String packageName = pluginClass.substring(0, pluginClass.lastIndexOf(".")); - splitPluginLog(pluginWrapper, logConfig, packageName); - String pluginId = pluginWrapper.getPluginId(); - logConfig.setPattern(packageName); - pluginLogMap.put(pluginId, logConfig); - } - } - - public void unloadLogConfig(PluginWrapper pluginWrapper) { - pluginLogMap.remove(pluginWrapper.getPluginId()); - } - - private void splitPluginLog(PluginWrapper pluginWrapper, LogConfig logConfig, String packageName) { - LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); - Logger logger = context.getLogger(packageName); - logger.detachAndStopAllAppenders(); - - ConsoleAppender consoleAppender = createConsoleAppender(pluginWrapper, logConfig, packageName); - RollingFileAppender fileAppender = createFileAppender(pluginWrapper, logConfig, packageName); - - logger.setAdditive(false); - logger.setLevel(Level.toLevel(logConfig.getLevel())); - logger.addAppender(consoleAppender); - logger.addAppender(fileAppender); - } - - private ConsoleAppender createConsoleAppender(PluginWrapper pluginWrapper, LogConfig logConfig, String packageName) { - LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); - ConsoleAppender appender = new ConsoleAppender<>(); - Filter filter = new LogFilter(packageName); - filter.start(); - appender.addFilter(filter); - appender.setContext(context); - appender.setName(pluginWrapper.getPluginId().concat("-console")); - - PatternLayoutEncoder encoder = new PatternLayoutEncoder(); - encoder.setContext(context); - encoder.setPattern(logConfig.getPattern()); - encoder.start(); - - appender.setEncoder(encoder); - appender.start(); - return appender; - } - - private RollingFileAppender createFileAppender(PluginWrapper pluginWrapper, LogConfig logConfig, String packageName) { - LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); - RollingFileAppender appender = new RollingFileAppender<>(); - Filter filter = new LogFilter(packageName); - filter.start(); - - appender.addFilter(filter); - - appender.setContext(context); - appender.setName(pluginWrapper.getPluginId().concat("-file")); - - String fileName = logConfig.getFileName(); - if (fileName == null || "".equals(fileName)) { - fileName = pluginWrapper.getPluginId(); - } - - String home = pluginWrapper.getPluginPath().toString(); - String logPrefix = home.concat("/logs/").concat(pluginWrapper.getPluginId()).concat("/").concat(fileName); - appender.setFile(OptionHelper.substVars(logPrefix.concat(".log"), context)); - - appender.setAppend(true); - appender.setPrudent(false); - - SizeAndTimeBasedRollingPolicy policy = new SizeAndTimeBasedRollingPolicy<>(); - - String fp = OptionHelper.substVars(logPrefix.concat(".%d{yyyy-MM-dd}%i.log"), context); - policy.setMaxFileSize(FileSize.valueOf(logConfig.getMaxFileSize())); - policy.setFileNamePattern(fp); - policy.setMaxHistory(logConfig.getMaxHistory()); - policy.setTotalSizeCap(FileSize.valueOf(logConfig.getTotalFileSize())); - policy.setParent(appender); - policy.setContext(context); - policy.start(); - - PatternLayoutEncoder encoder = new PatternLayoutEncoder(); - encoder.setContext(context); - encoder.setPattern(logConfig.getPattern()); - encoder.start(); - - appender.setRollingPolicy(policy); - appender.setEncoder(encoder); - appender.start(); - return appender; - } - - private static class LogFilter extends Filter { - - private final String packageName; - private final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); - - private LogFilter(String packageName) { - this.packageName = packageName; - } - - @Override - public FilterReply decide(ILoggingEvent event) { - Logger logger = loggerContext.getLogger(packageName); - if (event.getLoggerName().startsWith(packageName) && event.getLevel().isGreaterOrEqual(logger.getLevel())) { - return FilterReply.ACCEPT; - } - return FilterReply.DENY; - } - } - - private void checkLogConfig(LogConfig logConfig, String pluginId) { - String fileName = logConfig.getFileName(); - if (fileName == null || "".equals(fileName)) { - logConfig.setFileName(pluginId.concat("Log")); - } - Field[] fields = LogConfig.class.getDeclaredFields(); - Arrays.stream(fields).forEach(field -> { - if (!field.isAccessible()) { - field.setAccessible(true); - } - ConfigItem configItem = field.getDeclaredAnnotation(ConfigItem.class); - if (configItem == null) { - return; - } - try { - Object fieldValue = field.get(logConfig); - if(fieldValue == null){ - return; - } - Class fieldType = field.getType(); - if ("".equals(fieldValue.toString()) || ObjectUtil.isEmptyObject(fieldType, fieldValue)) { - String defaultValue = configItem.defaultValue(); - log.debug("Field {} is not config or invalid in log config of plugin {}, set it to default value {}.", field.getName(), defaultValue, pluginId); - Object fixedValue = ObjectUtil.parseBasicTypeValue(fieldType, defaultValue); - field.set(logConfig, fixedValue); - } - } catch (IllegalAccessException e) { - log.error("Failed to check config item {} in log config.", field.getName()); - } - }); - } - - public Map getPluginLogMap() { - return pluginLogMap; - } - - private String readConfigText(Resource resource) throws IOException { - String fileContent; - try (InputStream inputStream = resource.getInputStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - byte[] buff = new byte[1024]; - int len; - while ((len = inputStream.read(buff)) != -1) { - baos.write(buff, 0, len); - } - byte[] data = baos.toByteArray(); - fileContent = new String(data); - } - return fileContent; - } - - private Object xml2object(String xml) throws Exception { - Object object; - try { - System.out.println(xml); - JAXBContext context = JAXBContext.newInstance(LogConfig.class); - Unmarshaller unmarshaller = context.createUnmarshaller(); - StringReader stringReader = new StringReader(xml); - object = unmarshaller.unmarshal(stringReader); - } catch (JAXBException e) { - e.printStackTrace(); - throw new Exception("Invalid xml definition", e); - } - return object; - } - -} +package com.gitee.starblues.extension.log.logback; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.encoder.PatternLayoutEncoder; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.Appender; +import ch.qos.logback.core.ConsoleAppender; +import ch.qos.logback.core.filter.Filter; +import ch.qos.logback.core.rolling.RollingFileAppender; +import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy; +import ch.qos.logback.core.spi.FilterReply; +import ch.qos.logback.core.util.FileSize; +import ch.qos.logback.core.util.OptionHelper; +import com.gitee.starblues.extension.log.LogRegistry; +import com.gitee.starblues.extension.log.config.LogConfig; +import com.gitee.starblues.extension.log.util.LogConfigUtil; +import com.gitee.starblues.factory.PluginRegistryInfo; +import org.pf4j.PluginWrapper; +import org.pf4j.util.StringUtils; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.Resource; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 日志配置处理者 + * @author sousouki + * @version 2.4.3 + */ +public class LogbackLogRegistry implements LogRegistry { + + private final org.slf4j.Logger log = LoggerFactory.getLogger(LogbackLogRegistry.class); + + private final Map>> pluginAppenderInfo = new ConcurrentHashMap<>(); + + @Override + public void registry(List resources, PluginRegistryInfo pluginRegistryInfo) throws Exception { + Set> appenderSet = new HashSet<>(); + PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); + for (Resource resource : resources) { + if(resource == null){ + continue; + } + LogConfig logConfig; + try { + logConfig = LogConfigUtil.getLogConfig(resource, pluginRegistryInfo); + } catch (Exception e){ + log.error("Failed to read log configuration.", e); + continue; + } + Set> logAppenderSet = addAppender(pluginRegistryInfo, logConfig); + appenderSet.addAll(logAppenderSet); + } + pluginAppenderInfo.put(pluginWrapper.getPluginId(), appenderSet); + } + + @Override + public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); + Set> logAppenderSet = pluginAppenderInfo.get(pluginWrapper.getPluginId()); + if(logAppenderSet == null || logAppenderSet.isEmpty()){ + return; + } + String packageName = pluginRegistryInfo.getBasePlugin().scanPackage(); + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + Logger logger = context.getLogger(packageName); + for (Appender appender : logAppenderSet) { + logger.detachAppender(appender); + } + pluginAppenderInfo.remove(pluginWrapper.getPluginId()); + } + + private Set> addAppender(PluginRegistryInfo pluginRegistryInfo, LogConfig logConfig) { + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + String packageName = logConfig.getPackageName(); + Logger logger = context.getLogger(packageName); + logger.detachAndStopAllAppenders(); + + ConsoleAppender consoleAppender = createConsoleAppender(pluginRegistryInfo.getPluginWrapper(), + logConfig, packageName); + RollingFileAppender fileAppender = createFileAppender(pluginRegistryInfo, + logConfig, packageName); + + logger.setAdditive(false); + logger.setLevel(Level.toLevel(logConfig.getLevel())); + logger.addAppender(consoleAppender); + logger.addAppender(fileAppender); + + Set> appenderSet = new HashSet<>(); + appenderSet.add(consoleAppender); + appenderSet.add(fileAppender); + return appenderSet; + } + + private ConsoleAppender createConsoleAppender(PluginWrapper pluginWrapper, + LogConfig logConfig, + String packageName) { + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + ConsoleAppender appender = new ConsoleAppender<>(); + Filter filter = new LogFilter(packageName); + filter.start(); + appender.addFilter(filter); + appender.setContext(context); + appender.setName(pluginWrapper.getPluginId().concat("-console")); + + PatternLayoutEncoder encoder = new PatternLayoutEncoder(); + encoder.setContext(context); + encoder.setPattern(logConfig.getPattern()); + encoder.start(); + + appender.setEncoder(encoder); + appender.start(); + return appender; + } + + private RollingFileAppender createFileAppender(PluginRegistryInfo pluginRegistryInfo, + LogConfig logConfig, + String packageName) { + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + + RollingFileAppender appender = new RollingFileAppender<>(); + if(StringUtils.isNotNullOrEmpty(packageName)){ + Filter filter = new LogFilter(packageName); + filter.start(); + appender.addFilter(filter); + } + + PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); + appender.setContext(context); + appender.setName(pluginWrapper.getPluginId()); + + String logFilePrefix = LogConfigUtil.getLogFile(pluginRegistryInfo, logConfig); + appender.setFile(OptionHelper.substVars(logFilePrefix.concat(".log"), context)); + + appender.setAppend(true); + appender.setPrudent(false); + + SizeAndTimeBasedRollingPolicy policy = new SizeAndTimeBasedRollingPolicy<>(); + + String fp = OptionHelper.substVars(logFilePrefix.concat(".%d{yyyy-MM-dd}-%i.log"), context); + policy.setMaxFileSize(FileSize.valueOf(logConfig.getMaxFileSize())); + policy.setFileNamePattern(fp); + policy.setMaxHistory(logConfig.getMaxHistory()); + policy.setTotalSizeCap(FileSize.valueOf(logConfig.getTotalFileSize())); + policy.setParent(appender); + policy.setContext(context); + policy.start(); + + PatternLayoutEncoder encoder = new PatternLayoutEncoder(); + encoder.setContext(context); + encoder.setPattern(logConfig.getPattern()); + encoder.start(); + + appender.setRollingPolicy(policy); + appender.setEncoder(encoder); + appender.start(); + return appender; + } + + private static class LogFilter extends Filter { + + private final String packageName; + private final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); + + private LogFilter(String packageName) { + this.packageName = packageName; + } + + @Override + public FilterReply decide(ILoggingEvent event) { + Logger logger = loggerContext.getLogger(packageName); + if (event.getLoggerName().startsWith(packageName) && event.getLevel().isGreaterOrEqual(logger.getLevel())) { + return FilterReply.ACCEPT; + } + return FilterReply.DENY; + } + } + +} diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/util/LogConfigUtil.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/util/LogConfigUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..a5fcdab7639584067f2208616b1fed1cceb9eea4 --- /dev/null +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-log/src/main/java/com/gitee/starblues/extension/log/util/LogConfigUtil.java @@ -0,0 +1,136 @@ +package com.gitee.starblues.extension.log.util; + +import com.gitee.starblues.extension.log.annotation.ConfigItem; +import com.gitee.starblues.extension.log.config.LogConfig; +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.integration.IntegrationConfiguration; +import com.gitee.starblues.utils.CommonUtils; +import org.pf4j.PluginWrapper; +import org.pf4j.RuntimeMode; +import org.pf4j.util.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.Resource; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.lang.reflect.Field; + +/** + * LogConfig 对象和 xml 映射的工具类 + * @author starBlues + * @version 2.4.3 + */ +public class LogConfigUtil { + + private static final Logger LOG = LoggerFactory.getLogger(LogConfigUtil.class); + + + public static LogConfig getLogConfig(Resource xmlResource, PluginRegistryInfo pluginRegistryInfo) + throws Exception{ + PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); + String configText = readConfigText(xmlResource); + LogConfig logConfig = (LogConfig) xml2object(configText); + checkLogConfig(logConfig, pluginWrapper.getPluginId()); + String packageName = logConfig.getPackageName(); + if(StringUtils.isNullOrEmpty(packageName)){ + logConfig.setPackageName(pluginRegistryInfo.getBasePlugin().scanPackage()); + } + return logConfig; + } + + + public static String getLogFile(PluginRegistryInfo pluginRegistryInfo, LogConfig logConfig){ + String rootDir = logConfig.getRootDir(); + String home; + PluginWrapper pluginWrapper = pluginRegistryInfo.getPluginWrapper(); + IntegrationConfiguration configuration = pluginRegistryInfo.getConfiguration(); + String pluginRootDir; + RuntimeMode runtimeMode = pluginWrapper.getRuntimeMode(); + if(runtimeMode == RuntimeMode.DEVELOPMENT){ + pluginRootDir = pluginWrapper.getPluginPath().toString(); + } else { + pluginRootDir = configuration.pluginPath(); + } + if(StringUtils.isNullOrEmpty(rootDir)){ + home = CommonUtils.joiningFilePath(pluginRootDir, "logs"); + } else { + if(rootDir.startsWith(LogConfig.ROOT_PLUGIN_SIGN)){ + // 如果root路径中开始存在ROOT_PLUGIN_SIGN,则说明进行插件根路替换 + home = rootDir.replaceFirst("\\" + LogConfig.ROOT_PLUGIN_SIGN, ""); + home = CommonUtils.joiningFilePath(pluginRootDir, home); + } else { + home = rootDir; + } + } + String fileName = logConfig.getFileName(); + if (StringUtils.isNullOrEmpty(fileName)) { + fileName = pluginWrapper.getPluginId(); + } + return CommonUtils.joiningFilePath(home, pluginWrapper.getPluginId(), fileName); + } + + private static void checkLogConfig(LogConfig logConfig, String pluginId) { + String fileName = logConfig.getFileName(); + if (StringUtils.isNullOrEmpty(fileName)) { + logConfig.setFileName(pluginId.concat("-log")); + } + Field[] fields = LogConfig.class.getDeclaredFields(); + for (Field field : fields) { + if (!field.isAccessible()) { + field.setAccessible(true); + } + ConfigItem configItem = field.getDeclaredAnnotation(ConfigItem.class); + if (configItem == null) { + continue; + } + try { + Object fieldValue = field.get(logConfig); + Class fieldType = field.getType(); + if (fieldValue == null || "".equals(fieldValue.toString()) || + ObjectUtil.isEmptyObject(fieldType, fieldValue)) { + String defaultValue = configItem.defaultValue(); + LOG.debug("Field {} is not config or invalid in log config of plugin {}, set it to default value {}.", field.getName(), defaultValue, pluginId); + Object fixedValue = ObjectUtil.parseBasicTypeValue(fieldType, defaultValue); + field.set(logConfig, fixedValue); + } + } catch (IllegalAccessException e) { + LOG.error("Failed to check config item {} in log config.", field.getName()); + } + } + } + + private static String readConfigText(Resource resource) throws IOException { + String fileContent; + try (InputStream inputStream = resource.getInputStream(); + ByteArrayOutputStream stream = new ByteArrayOutputStream()) { + byte[] buff = new byte[1024]; + int len; + while ((len = inputStream.read(buff)) != -1) { + stream.write(buff, 0, len); + } + byte[] data = stream.toByteArray(); + fileContent = new String(data); + } + return fileContent; + } + + private static Object xml2object(String xml) throws Exception { + Object object; + try { + JAXBContext context = JAXBContext.newInstance(LogConfig.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + StringReader stringReader = new StringReader(xml); + object = unmarshaller.unmarshal(stringReader); + } catch (JAXBException e) { + e.printStackTrace(); + throw new Exception("Invalid xml definition"); + } + return object; + } +} 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 5bfc4fb383df33b787d24919bdeebed1c63587f5..382c251a351c2ce621dfbb4313f7950318f01d2f 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 @@ -211,7 +211,7 @@ public void oneselfConfig(Config config){ ## 版本说明 -### `2.4.2-RELEASE` 版本 +### `2.4.3-RELEASE` 版本 1. 新增`Mybatis`、`Mybatis-Plus`、`Tk-Mybatis`扩展新增可重写覆盖主程序的配置(重写后会当前插件私有, 不影响主程序) 2. 修复`Mybatis-Plus`批量插入的bug 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 52067942e1eb3e984a9dabd033c4aa944ccedd15..b5229c067cc89f04326ae19384249c1e5bd7c655 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.2-RELEASE + 2.4.3-RELEASE jar 插件扩展-spring boot mybatis 集成扩展 @@ -64,7 +64,7 @@ 3.1.0 1.6 - 2.4.2-RELEASE + 2.4.3-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/MybatisCommonConfig.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/MybatisCommonConfig.java index 5b629518c5c5229129c5c90328631597a5cc8bed..3cbdad79a62c2406b7e6af490bb9b53d9b75c55f 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/MybatisCommonConfig.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-mybatis/src/main/java/com/gitee/starblues/extension/mybatis/MybatisCommonConfig.java @@ -21,9 +21,9 @@ public interface MybatisCommonConfig { * * 匹配零个或多个字符
* ** 匹配路径中的零或多个目录
* 例如:
- * 文件路径配置为

file: D://xml/*PluginMapper.xml


- * resources路径配置为

classpath: xml/mapper/*PluginMapper.xml


- * 包路径配置为

package: com.plugin.xml.mapper.*PluginMapper.xml


+ * 文件路径配置为

file:D://xml/*PluginMapper.xml


+ * resources路径配置为

classpath:xml/mapper/*PluginMapper.xml


+ * 包路径配置为

package:com.plugin.xml.mapper.*PluginMapper.xml


* @return Set */ Set xmlLocationsMatch(); 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 1e9b69b00c85cc4dc1ec092fb7d396ee83bb2539..7bd65b0fd0c91937f2aaf9b5f5f5cc257ce91515 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 @@ -104,7 +104,7 @@ public class ResourceConfig implements SpringBootThymeleafConfig { ## 版本说明 -### `2.4.2-RELEASE` 版本 +### `2.4.3-RELEASE` 版本 1. 修复静态资源无法访问的bug #### `2.4.0-RELEASE` 版本 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 2b13e49ffb027d632493aba979e130a54a106b0a..f6c455388057231093e8209cc9bd1931bbf4ac49 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.2-RELEASE + 2.4.3-RELEASE jar 插件扩展-通过url读取插件中的静态资源 @@ -69,9 +69,11 @@ 5.0.7.RELEASE 4.0.1 - 2.4.2-RELEASE + 2.4.3-RELEASE 2.0.3.RELEASE 2.1.1.RELEASE + + 4.11 @@ -84,7 +86,6 @@ provided - org.springframework.boot spring-boot-starter-thymeleaf @@ -106,6 +107,13 @@ provided true + + + junit + junit + ${junit.version} + test + diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/PluginResourceResolverProcess.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/PluginResourceResolverProcess.java index bf1c431c8eb8ebc45189252d8e75a582cfa2096e..c930a3a7603789c4bda971ca3496c8fbcb4c6ebd 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/PluginResourceResolverProcess.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/PluginResourceResolverProcess.java @@ -7,8 +7,14 @@ import com.gitee.starblues.utils.OrderPriority; import com.gitee.starblues.utils.SpringBeanUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.context.properties.bind.Bindable; +import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.util.ObjectUtils; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * 插件资源处理器 @@ -48,13 +54,22 @@ public class PluginResourceResolverProcess implements PluginPostProcessorExtend } String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); try { - StaticResourceConfig config = SpringBeanUtils.getObjectByInterfaceClass( - pluginRegistryInfo.getConfigSingletons(), - StaticResourceConfig.class); - if(config == null){ + // 直接从配置文件获取, 后续版本移除从实现类中获取配置 + Set locations = pluginRegistryInfo.getPluginBinder() + .bind(PropertyKey.STATIC_LOCATIONS, Bindable.setOf(String.class)) + .orElseGet(()->null); + if(ObjectUtils.isEmpty(locations)){ + StaticResourceConfig config = SpringBeanUtils.getObjectByInterfaceClass( + pluginRegistryInfo.getConfigSingletons(), + StaticResourceConfig.class); + if(config != null){ + locations = config.locations(); + } + } + if(ObjectUtils.isEmpty(locations)){ return; } - PluginResourceResolver.parse(pluginRegistryInfo, config); + PluginResourceResolver.parse(pluginRegistryInfo, locations); } catch (Exception e){ LOGGER.error("Parse plugin '{}' static resource failure.", pluginId, e); } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/PropertyKey.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/PropertyKey.java new file mode 100644 index 0000000000000000000000000000000000000000..d83dbadac0403f066d55e38eb990fd81b438b28d --- /dev/null +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/PropertyKey.java @@ -0,0 +1,21 @@ +package com.gitee.starblues.extension.resources; + +/** + * @author zhangzhuo + * @version 2.4.3 + */ +public class PropertyKey { + + /** + * 静态文件路径 + * classpath: static/ + * file: D://path/test + */ + public final static String STATIC_LOCATIONS = "plugin.static.locations"; + + + /** + * Thymeleaf 配置前缀 + */ + public final static String THYMELEAF_CONFIG = "plugin.thymeleaf"; +} diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/StaticResourceConfig.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/StaticResourceConfig.java index aba9f42c74c0853bbf8c48a3c763a0f08cd0e295..917a9f4002cab072fbe9465fc7c75ce4145d3fbe 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/StaticResourceConfig.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/StaticResourceConfig.java @@ -4,10 +4,16 @@ import java.util.Set; /** * 插件静态资源的配置 - * + * 建议从配置文件中进行配置: + * plugin: + * static: + * locations: + * - classpath: static/ + * - file: D://path/test * @author starBlues * @version 2.3 */ +@Deprecated public interface StaticResourceConfig { 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 b4c95c73917f090122dd61f50fc3d5c260090b68..1f92529e42a4f32747e3e04cbc3381402f02f026 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 @@ -17,7 +17,7 @@ import java.util.concurrent.TimeUnit; * 插件静态资源访问的扩展插件 * * @author starBlues - * @version 2.3 + * @version 2.4.3 */ public class StaticResourceExtension extends AbstractExtension { @@ -80,23 +80,27 @@ public class StaticResourceExtension extends AbstractExtension { /** * 设置访问插件静态资源前缀 * @param pluginStaticResourcePathPrefix 静态资源前缀。默认为: static-plugin。 + * @return StaticResourceExtension */ - public void setPathPrefix(String pluginStaticResourcePathPrefix){ + public StaticResourceExtension setPathPrefix(String pluginStaticResourcePathPrefix){ if(pluginStaticResourcePathPrefix != null && !"".equals(pluginStaticResourcePathPrefix)){ StaticResourceExtension.pluginStaticResourcePathPrefix = pluginStaticResourcePathPrefix; } + return this; } /** * 设置缓存控制 * @param pluginStaticResourcesCacheControl 访问静态资源的缓存控制。默认最大1小时。主要针对http协议的缓存。 + * @return StaticResourceExtension */ - public void setCacheControl(CacheControl pluginStaticResourcesCacheControl){ + public StaticResourceExtension setCacheControl(CacheControl pluginStaticResourcesCacheControl){ if(pluginStaticResourcesCacheControl == null){ StaticResourceExtension.pluginStaticResourcesCacheControl = null; } else { StaticResourceExtension.pluginStaticResourcesCacheControl = pluginStaticResourcesCacheControl; } + return this; } public static String getPluginStaticResourcePathPrefix() { 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 182ea6ea43511057a1fc957cbe5be5e2371822b1..1a8d2b1d8d7baa010ca7f6f303b93c5cefadb062 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 @@ -194,17 +194,10 @@ public class PluginResourceResolver extends AbstractResourceResolver { /** * 每新增一个插件, 都需要调用该方法,来解析该插件的 StaticResourceConfig 配置。并将其保存到 StaticResourceConfig bean 中。 * @param pluginRegistryInfo 插件信息 - * @param staticResourceConfig 静态资源配置 + * @param locations 静态资源配置 */ public static synchronized void parse(PluginRegistryInfo pluginRegistryInfo, - StaticResourceConfig staticResourceConfig){ - if(pluginRegistryInfo == null || staticResourceConfig == null){ - return; - } - - String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); - - Set locations = staticResourceConfig.locations(); + Set locations){ if(locations == null || locations.isEmpty()){ return; } @@ -212,6 +205,8 @@ public class PluginResourceResolver extends AbstractResourceResolver { Set classPaths = new HashSet<>(); Set filePaths = new HashSet<>(); + String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); + for (String location : locations) { if(StringUtils.isEmpty(location)){ continue; diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/thymeleaf/ThymeleafConfig.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/thymeleaf/ThymeleafConfig.java index 7f7178dec6005a32860a814abfb4b25194e341c9..3b11388c85b0e842dc45bc6330ef8ad8de9ee7fa 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/thymeleaf/ThymeleafConfig.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/thymeleaf/ThymeleafConfig.java @@ -42,15 +42,13 @@ public class ThymeleafConfig { /** * 是否启用模板引擎的缓存 */ - private boolean cache = true; + private Boolean cache = true; /** * 模板解析器的执行顺序, 数字越小越先执行 */ private Integer templateResolverOrder; - - public String getPrefix() { return prefix; } @@ -71,23 +69,23 @@ public class ThymeleafConfig { return mode; } - public void setMode(TemplateMode mode) { - this.mode = mode; + public void setMode(String mode) { + this.mode = TemplateMode.parse(mode); } public Charset getEncoding() { return encoding; } - public void setEncoding(Charset encoding) { - this.encoding = encoding; + public void setEncoding(String encoding) { + this.encoding = Charset.forName(encoding); } - public boolean isCache() { + public Boolean getCache() { return cache; } - public void setCache(boolean cache) { + public void setCache(Boolean cache) { this.cache = cache; } @@ -99,4 +97,15 @@ public class ThymeleafConfig { this.templateResolverOrder = templateResolverOrder; } + @Override + public String toString() { + return "ThymeleafConfig{" + + "prefix='" + prefix + '\'' + + ", suffix='" + suffix + '\'' + + ", mode=" + mode.name() + + ", encoding=" + encoding.name() + + ", cache=" + cache + + ", templateResolverOrder=" + templateResolverOrder + + '}'; + } } diff --git a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/thymeleaf/ThymeleafProcessor.java b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/thymeleaf/ThymeleafProcessor.java index 5075825204b416c66401c57ce20ab944bf58bad7..c1eca8e5ee9344796779690d1e1e0d8c8eafe7f0 100644 --- a/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/thymeleaf/ThymeleafProcessor.java +++ b/springboot-plugin-framework-extension/springboot-plugin-framework-extension-resources/src/main/java/com/gitee/starblues/extension/resources/thymeleaf/ThymeleafProcessor.java @@ -1,5 +1,6 @@ package com.gitee.starblues.extension.resources.thymeleaf; +import com.gitee.starblues.extension.resources.PropertyKey; import com.gitee.starblues.factory.PluginRegistryInfo; import com.gitee.starblues.factory.process.pipe.PluginPipeProcessorExtend; import com.gitee.starblues.utils.ClassUtils; @@ -7,7 +8,10 @@ import com.gitee.starblues.utils.OrderPriority; import com.gitee.starblues.utils.SpringBeanUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.context.properties.bind.Bindable; +import org.springframework.boot.context.properties.bind.Binder; import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import org.thymeleaf.spring5.SpringTemplateEngine; @@ -51,14 +55,21 @@ public class ThymeleafProcessor implements PluginPipeProcessorExtend { if(springTemplateEngine == null){ return; } - SpringBootThymeleafConfig config = SpringBeanUtils.getObjectByInterfaceClass( - pluginRegistryInfo.getConfigSingletons(), - SpringBootThymeleafConfig.class); - if(config == null){ - return; + + ThymeleafConfig thymeleafConfig = pluginRegistryInfo.getPluginBinder() + .bind(PropertyKey.THYMELEAF_CONFIG, ThymeleafConfig.class) + .orElse(null); + if(thymeleafConfig == null){ + SpringBootThymeleafConfig config = SpringBeanUtils.getObjectByInterfaceClass( + pluginRegistryInfo.getConfigSingletons(), + SpringBootThymeleafConfig.class); + if(config == null){ + return; + } + thymeleafConfig = new ThymeleafConfig(); + config.config(thymeleafConfig); } - ThymeleafConfig thymeleafConfig = new ThymeleafConfig(); - config.config(thymeleafConfig); + String prefix = thymeleafConfig.getPrefix(); if(StringUtils.isEmpty(prefix)){ throw new IllegalArgumentException("prefix can't be empty"); @@ -83,7 +94,7 @@ public class ThymeleafProcessor implements PluginPipeProcessorExtend { resolver.setSuffix(thymeleafConfig.getSuffix()); resolver.setTemplateMode(thymeleafConfig.getMode()); - resolver.setCacheable(thymeleafConfig.isCache()); + resolver.setCacheable(thymeleafConfig.getCache()); if(thymeleafConfig.getEncoding() != null){ resolver.setCharacterEncoding(thymeleafConfig.getEncoding().name()); } diff --git a/springboot-plugin-framework/pom.xml b/springboot-plugin-framework/pom.xml index fcda2b0e65952b88373600f8a3d5e8e2863f7c93..21f170cf026fcc4c53b359a0eac3f7f59017c226 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.2-RELEASE + 2.4.3-RELEASE spring boot 插件式开发集成包 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/annotation/ConfigDefinition.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/annotation/ConfigDefinition.java index 2b477187a068023e040c7cd84b7eed10b2136c6a..42109c7f245d021ae5ebcd2c8029a47145d2b8f2 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/annotation/ConfigDefinition.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/annotation/ConfigDefinition.java @@ -15,25 +15,12 @@ import java.lang.annotation.*; public @interface ConfigDefinition { - /** - * 插件中的配置文件的名称. 建议使用 fileName 进行文件名称配置. - * @return String - */ - @Deprecated - String value() default ""; - /** * 插件中的配置文件的名称, 新版本替换 value 值 * @return String */ String fileName() default ""; - /** - * 自定义 bean 名称 - * @return String - */ - String beanName() default ""; - /** * 开发环境下文件后缀 * 如果文件名称为: xxx.yml, 根据当前配置(当前配置为-dev)在开发环境下文件后缀为: xxx-dev.yml 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 5d7ca4de501b7830ea81aaf3b52ee9b87b77f9a6..95cbb0d4535ba8739f505ae3e6864e2ea6a282a3 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 @@ -1,19 +1,19 @@ package com.gitee.starblues.factory; import com.gitee.starblues.factory.process.pipe.PluginInfoContainers; -import com.gitee.starblues.factory.process.pipe.PluginPipeApplicationContextProcessor; import com.gitee.starblues.factory.process.pipe.loader.ResourceWrapper; +import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.realize.BasePlugin; -import org.pf4j.*; +import org.pf4j.PluginManager; +import org.pf4j.PluginWrapper; import org.pf4j.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.boot.context.properties.bind.Binder; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.GenericApplicationContext; -import org.springframework.util.ClassUtils; -import java.io.Closeable; -import java.io.IOException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -29,8 +29,10 @@ public class PluginRegistryInfo { private final PluginWrapper pluginWrapper; private final PluginManager pluginManager; + private final IntegrationConfiguration configuration; private final GenericApplicationContext mainApplicationContext; private final AnnotationConfigApplicationContext pluginApplicationContext; + private final Binder pluginBinder; private final SpringBeanRegister springBeanRegister; /** @@ -79,11 +81,17 @@ public class PluginRegistryInfo { this.pluginManager = pluginManager; this.basePlugin = (BasePlugin) pluginWrapper.getPlugin(); this.mainApplicationContext = mainApplicationContext; + this.configuration = mainApplicationContext.getBean(IntegrationConfiguration.class); this.followingInitial = followingInitial; - // 生成插件Application - this.pluginApplicationContext = new AnnotationConfigApplicationContext(); - this.pluginApplicationContext.setClassLoader(basePlugin.getWrapper().getPluginClassLoader()); + ClassLoader pluginClassLoader = basePlugin.getWrapper().getPluginClassLoader(); + // 生成插件ApplicationContext-DefaultListableBeanFactory + DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory(); + this.pluginApplicationContext = new AnnotationConfigApplicationContext(defaultListableBeanFactory); + // 设置插件ApplicationContext的classLoader + this.pluginApplicationContext.setClassLoader(pluginClassLoader); + + this.pluginBinder = Binder.get(this.pluginApplicationContext.getEnvironment()); this.springBeanRegister = new SpringBeanRegister(pluginApplicationContext); } @@ -253,6 +261,14 @@ public class PluginRegistryInfo { return pluginApplicationContext; } + /** + * 得到当前插件的Binder + * @return Binder + */ + public Binder getPluginBinder() { + return pluginBinder; + } + /** * 得到当前插件Bean注册者 * @return SpringBeanRegister @@ -292,6 +308,9 @@ public class PluginRegistryInfo { return followingInitial; } + public IntegrationConfiguration getConfiguration() { + return configuration; + } void destroy(){ // 关闭ApplicationContext diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/PropertyKey.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/PropertyKey.java new file mode 100644 index 0000000000000000000000000000000000000000..8bff0dc66e7115fb058b7367bbf78a9019d7781c --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/PropertyKey.java @@ -0,0 +1,11 @@ +package com.gitee.starblues.factory; + +/** + * @author zhangzhuo + * @version 2.4.3 + */ +public class PropertyKey { + + public final static String INSTALL_AUTO_CONFIG_CLASS = "plugin.auto-config-class"; + +} 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 ddb1393b8c9825e441c401ef8c911752f3209ffb..3d59a1bc1d8c0e0f9bb9fe1365dc199e79774d9f 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 @@ -1,11 +1,11 @@ package com.gitee.starblues.factory; -import com.gitee.starblues.factory.process.pipe.bean.name.PluginAnnotationBeanNameGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.annotation.AnnotationBeanNameGenerator; import org.springframework.context.support.GenericApplicationContext; import java.text.MessageFormat; @@ -32,30 +32,43 @@ public class SpringBeanRegister { return applicationContext.containsBean(name); } + /** - * 默认注册 + * 基于class注册一个bean + * + * @param aClass 类名 + * @return 注册的bean名称 + */ + public String register(Class aClass) { + return register(aClass, null); + } + + + /** + * 基于class注册一个bean * * @param pluginId 插件id * @param aClass 类名 * @return 注册的bean名称 */ + @Deprecated public String register(String pluginId, Class aClass) { return register(pluginId, aClass, null); } + /** - * 默认注册 - * @param pluginId 插件id + * 基于class注册一个bean, 可自定义 BeanDefinition + * * @param aClass 注册的类 * @param consumer 自定义处理AnnotatedGenericBeanDefinition * @return 注册的bean名称 */ - public String register(String pluginId, Class aClass, + public String register(Class aClass, Consumer consumer) { AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(aClass); beanDefinition.setBeanClass(aClass); - BeanNameGenerator beanNameGenerator = - new PluginAnnotationBeanNameGenerator(pluginId); + BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator(); String beanName = beanNameGenerator.generateBeanName(beanDefinition, applicationContext); if(applicationContext.containsBean(beanName)){ @@ -71,25 +84,49 @@ public class SpringBeanRegister { return beanName; } + + /** + * 基于class注册一个bean, 可自定义 BeanDefinition + * + * @param pluginId 插件id + * @param aClass 注册的类 + * @param consumer 自定义处理AnnotatedGenericBeanDefinition + * @return 注册的bean名称 + */ + @Deprecated + public String register(String pluginId, Class aClass, + Consumer consumer) { + return register(aClass, consumer); + } + + /** + * 指定bean名称注册 + * @param beanName 指定的bean名称 + * @param aClass 注册的类 + */ + public void registerOfSpecifyName(String beanName, Class aClass){ + registerOfSpecifyName(beanName, aClass, null); + } + /** * 指定bean名称注册 * @param pluginId 插件id * @param beanName 指定的bean名称 * @param aClass 注册的类 */ + @Deprecated public void registerOfSpecifyName(String pluginId, String beanName, Class aClass){ registerOfSpecifyName(pluginId, beanName, aClass, null); } + /** - * 指定bean名称注册 - * @param pluginId 插件id + * 指定bean名称注册, 可自定义 BeanDefinition * @param beanName 指定的bean名称 * @param aClass 注册的类 - * @param consumer 注册异常 + * @param consumer 自定义处理AnnotatedGenericBeanDefinition */ - public void registerOfSpecifyName(String pluginId, - String beanName, + public void registerOfSpecifyName(String beanName, Class aClass, Consumer consumer) { AnnotatedGenericBeanDefinition beanDefinition = new @@ -105,6 +142,22 @@ public class SpringBeanRegister { applicationContext.registerBeanDefinition(beanName, beanDefinition); } + + /** + * 指定bean名称注册, 可自定义 BeanDefinition + * @param pluginId 插件id + * @param beanName 指定的bean名称 + * @param aClass 注册的类 + * @param consumer 自定义处理AnnotatedGenericBeanDefinition + */ + @Deprecated + public void registerOfSpecifyName(String pluginId, + String beanName, + Class aClass, + Consumer consumer) { + registerOfSpecifyName(beanName, aClass, consumer); + } + /** * 注册单例 * @param name 单例名称 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginOneselfStopEventProcessor.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginOneselfStopEventProcessor.java new file mode 100644 index 0000000000000000000000000000000000000000..e7e717d93da2535b32330f88432d167bdff9a9f3 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/PluginOneselfStopEventProcessor.java @@ -0,0 +1,51 @@ +package com.gitee.starblues.factory.process.pipe; + +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.process.post.bean.PluginOneselfStartEventProcessor; +import com.gitee.starblues.realize.BasePlugin; +import com.gitee.starblues.realize.OneselfListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * 插件中 OneselfListener 监听者处理者。主要执行监听器的停止事件。 + * @author starBlues + * @version 1.0 + * @since 2021-05-30 + */ +public class PluginOneselfStopEventProcessor implements PluginPipeProcessor{ + + private final Logger log = LoggerFactory.getLogger(this.getClass()); + + @Override + public void initialize() throws Exception { + + } + + @Override + public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + // 当前不执行启动事件 + // 由类 com.gitee.starblues.factory.process.post.bean.PluginOneselfStartEventProcessor 实现启动 + // 主要是资源全部加载注册完成后再触发启动事件 + } + + @Override + public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + List oneselfListeners = pluginRegistryInfo + .getExtension(PluginOneselfStartEventProcessor.KEY); + if(oneselfListeners == null || oneselfListeners.isEmpty()){ + return; + } + BasePlugin basePlugin = pluginRegistryInfo.getBasePlugin(); + for (OneselfListener oneselfListener : oneselfListeners) { + try { + oneselfListener.stopEvent(basePlugin); + } catch (Exception e){ + log.error("OneselfListener {} execute stopEvent exception. {}", + oneselfListener.getClass().getName(), e.getMessage(), e); + } + } + } +} 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 dc696556630a2eedc1274709b6fcbd74b2024884..a64d1f13744e054a0343e4284cf7587935e007ab 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 @@ -2,23 +2,26 @@ package com.gitee.starblues.factory.process.pipe; import com.gitee.starblues.extension.ExtensionInitializer; import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.PropertyKey; import com.gitee.starblues.factory.process.pipe.bean.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.AutoConfigurationPackages; +import org.springframework.boot.context.properties.bind.Bindable; 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 org.springframework.util.ObjectUtils; -import javax.servlet.ServletContext; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; /** * 插件的ApplicationContext 处理 * 主要进行插件bean的扫描 * @author starBlues - * @version 2.4.0 + * @version 2.4.3 */ public class PluginPipeApplicationContextProcessor implements PluginPipeProcessor{ @@ -34,6 +37,7 @@ public class PluginPipeApplicationContextProcessor implements PluginPipeProcesso @Override public void initialize() throws Exception { + pluginBeanDefinitionRegistrars.add(new SpringBootConfigFileRegistrar(mainApplicationContext)); pluginBeanDefinitionRegistrars.add(new PluginInsetBeanRegistrar()); pluginBeanDefinitionRegistrars.add(new ConfigBeanRegistrar()); pluginBeanDefinitionRegistrars.add(new ConfigFileBeanRegistrar(mainApplicationContext)); @@ -51,7 +55,7 @@ public class PluginPipeApplicationContextProcessor implements PluginPipeProcesso } ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { - Thread.currentThread().setContextClassLoader(pluginRegistryInfo.getPluginClassLoader()); + installPluginAutoConfiguration(pluginApplicationContext, pluginRegistryInfo); pluginApplicationContext.refresh(); } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); @@ -62,6 +66,36 @@ public class PluginPipeApplicationContextProcessor implements PluginPipeProcesso PluginInfoContainers.addPluginApplicationContext(pluginId, pluginApplicationContext); } + /** + * 安装插件定义的自动装载配置类 + * @param pluginApplicationContext 插件ApplicationContext + * @param pluginRegistryInfo 插件注册信息 + */ + private void installPluginAutoConfiguration(GenericApplicationContext pluginApplicationContext, + PluginRegistryInfo pluginRegistryInfo) throws ClassNotFoundException { + Set installAutoConfigClassString = pluginRegistryInfo.getPluginBinder() + .bind(PropertyKey.INSTALL_AUTO_CONFIG_CLASS, Bindable.setOf(String.class)) + .orElseGet(()->null); + if(ObjectUtils.isEmpty(installAutoConfigClassString)){ + return; + } + + // 注册AutoConfigurationPackages, 用于插件可自动配置 + AutoConfigurationPackages.register(pluginApplicationContext.getDefaultListableBeanFactory(), + pluginRegistryInfo.getBasePlugin().scanPackage()); + + Set> autoConfigurationClassSet = new HashSet<>(installAutoConfigClassString.size()); + ClassLoader pluginClassLoader = pluginRegistryInfo.getPluginClassLoader(); + for (String autoConfigClassPackage : installAutoConfigClassString) { + Class aClass = Class.forName(autoConfigClassPackage, false, pluginClassLoader); + autoConfigurationClassSet.add(aClass); + } + for (Class autoConfigurationClass : autoConfigurationClassSet) { + pluginApplicationContext.registerBean(autoConfigurationClass); + } + } + + @Override public void unRegistry(PluginRegistryInfo pluginRegistryInfo) throws Exception { for (PluginBeanRegistrar registrar : pluginBeanDefinitionRegistrars) { 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 23a604c881fc35ddf1ae9b25443767118aa32d10..ec43b3234783a409d5685cb717e0a7063a9e5d1d 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 @@ -34,7 +34,9 @@ public class PluginPipeProcessorFactory implements PluginPipeProcessor { @Override public void initialize() throws Exception{ // 以下顺序不能更改 - // 插件资源加载者, 必须放在第一位 + // 停止事件放在第一位 + pluginPipeProcessors.add(new PluginOneselfStopEventProcessor()); + // 插件资源加载者, 必须放在第二位 pluginPipeProcessors.add(new PluginResourceLoadFactory()); // 插件类处理者 pluginPipeProcessors.add(new PluginClassProcess()); 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 5e8fedf474db2135bb460d7109fcc716fa7c150b..9291a78c99eecd4246355ba57b1ad8bcb9c6e74f 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; } - springBeanRegister.register(pluginId, aClass); + springBeanRegister.register(aClass); } } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigBeanRegistrar.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigBeanRegistrar.java index eff5e403a85a9aaa794557928cc12b490bc3aaa9..b874aaea8a7a28ed463f1bc0cd291a7886762309 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigBeanRegistrar.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigBeanRegistrar.java @@ -28,13 +28,12 @@ public class ConfigBeanRegistrar implements PluginBeanRegistrar { if(configBeans == null || configBeans.isEmpty()){ return; } - String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); SpringBeanRegister springBeanRegister = pluginRegistryInfo.getSpringBeanRegister(); for (Class aClass : configBeans) { if(aClass == null){ continue; } - springBeanRegister.register(pluginId, aClass); + springBeanRegister.register(aClass); } } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigFileBeanRegistrar.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigFileBeanRegistrar.java index ecfb5aa2c2c3a913609431e96fbf179a9205a53c..b1ddce57a0ad7eddd8fb2646200d989853f08f17 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigFileBeanRegistrar.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/ConfigFileBeanRegistrar.java @@ -10,13 +10,11 @@ import com.gitee.starblues.factory.process.pipe.classs.group.ConfigDefinitionGro import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.realize.ConfigDefinitionTip; import com.gitee.starblues.utils.ClassUtils; -import org.pf4j.RuntimeMode; +import com.gitee.starblues.utils.PluginConfigUtils; import org.pf4j.util.StringUtils; -import org.springframework.beans.BeanUtils; import org.springframework.context.ApplicationContext; import org.springframework.util.ReflectionUtils; -import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.util.List; @@ -53,15 +51,14 @@ public class ConfigFileBeanRegistrar implements PluginBeanRegistrar { * 注册配置文件 * @param pluginRegistryInfo 插件注册的信息 * @param aClass 配置文件类 - * @return 注册的bean名称 * @throws Exception Exception */ - private String registry(PluginRegistryInfo pluginRegistryInfo, Class aClass) throws Exception{ + private void registry(PluginRegistryInfo pluginRegistryInfo, Class aClass) throws Exception{ ConfigDefinition configDefinition = aClass.getAnnotation(ConfigDefinition.class); if(configDefinition == null){ - return null; + return; } - String fileName = getConfigFileName(configDefinition); + String fileName = PluginConfigUtils.getConfigFileName(configDefinition, integrationConfiguration.environment()); Object parseObject = null; if(!StringUtils.isNullOrEmpty(fileName)){ PluginConfigDefinition pluginConfigDefinition = @@ -72,16 +69,11 @@ public class ConfigFileBeanRegistrar implements PluginBeanRegistrar { parseObject = aClass.newInstance(); } - String name = configDefinition.beanName(); - if(StringUtils.isNullOrEmpty(name)){ - name = aClass.getName(); - } - name = name + "@" + pluginRegistryInfo.getPluginWrapper().getPluginId(); + String name = aClass.getName(); SpringBeanRegister springBeanRegister = pluginRegistryInfo.getSpringBeanRegister(); setConfigDefinitionTip(pluginRegistryInfo, parseObject); springBeanRegister.registerSingleton(name, parseObject); pluginRegistryInfo.addConfigSingleton(parseObject); - return name; } /** @@ -100,42 +92,4 @@ public class ConfigFileBeanRegistrar implements PluginBeanRegistrar { } } - /** - * 根据项目运行环境模式来获取配置文件 - * @param configDefinition 配置的注解 - * @return 文件名称 - */ - private String getConfigFileName(ConfigDefinition configDefinition){ - // TODO 后期移除 value - String fileName = configDefinition.value(); - if(StringUtils.isNullOrEmpty(fileName)){ - fileName = configDefinition.fileName(); - if(StringUtils.isNullOrEmpty(fileName)){ - return null; - } - } - - String fileNamePrefix; - String fileNamePrefixSuffix; - - if(fileName.lastIndexOf(".") == -1) { - fileNamePrefix = fileName; - fileNamePrefixSuffix = ""; - } else { - int index = fileName.lastIndexOf("."); - fileNamePrefix = fileName.substring(0, index); - fileNamePrefixSuffix = fileName.substring(index); - } - - RuntimeMode environment = integrationConfiguration.environment(); - if(environment == RuntimeMode.DEPLOYMENT){ - // 生产环境 - fileNamePrefix = fileNamePrefix + configDefinition.prodSuffix(); - } else if(environment == RuntimeMode.DEVELOPMENT){ - // 开发环境 - fileNamePrefix = fileNamePrefix + configDefinition.devSuffix(); - } - return fileNamePrefix + fileNamePrefixSuffix; - } - } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/InvokeBeanRegistrar.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/InvokeBeanRegistrar.java index d040e4e478cc22d1d2bc442882232a4262080ba9..ceca253eb2c98c6695faed749b22fd700a4e8794 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/InvokeBeanRegistrar.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/InvokeBeanRegistrar.java @@ -53,7 +53,6 @@ public class InvokeBeanRegistrar implements PluginBeanRegistrar{ if(supperClasses.isEmpty()){ return; } - String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); SpringBeanRegister springBeanRegister = pluginRegistryInfo.getSpringBeanRegister(); Set beanNames = new HashSet<>(supperClasses.size()); for (Class supperClass : supperClasses) { @@ -71,7 +70,7 @@ public class InvokeBeanRegistrar implements PluginBeanRegistrar{ pluginRegistryInfo.getPluginWrapper().getPluginId(), beanName, supperClass.getName()); throw new Exception(error); } - springBeanRegister.registerOfSpecifyName(pluginId, beanName, supperClass); + springBeanRegister.registerOfSpecifyName(beanName, supperClass); beanNames.add(beanName); } pluginRegistryInfo.addExtension(SUPPLIER_KEY, beanNames); @@ -83,14 +82,13 @@ public class InvokeBeanRegistrar implements PluginBeanRegistrar{ if(callerClasses == null || callerClasses.isEmpty()){ return; } - String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); SpringBeanRegister springBeanRegister = pluginRegistryInfo.getSpringBeanRegister(); for (Class callerClass : callerClasses) { Caller caller = callerClass.getAnnotation(Caller.class); if(caller == null){ continue; } - springBeanRegister.register(pluginId, callerClass, (beanDefinition) ->{ + springBeanRegister.register(callerClass, (beanDefinition) ->{ beanDefinition.getPropertyValues().add("callerInterface", callerClass); beanDefinition.getPropertyValues().add("callerAnnotation", caller); beanDefinition.setBeanClass(CallerInterfaceFactory.class); 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 index 02557b19109bca6d63466d7164e08c02d10a7180..8b5e07b31d593337c67135c76f91356077f4a1c5 100644 --- 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 @@ -8,9 +8,7 @@ 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注册者 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/SpringBootConfigFileRegistrar.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/SpringBootConfigFileRegistrar.java new file mode 100644 index 0000000000000000000000000000000000000000..cf54db5fb60058cc9c2826a7cd62e510d9566841 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/SpringBootConfigFileRegistrar.java @@ -0,0 +1,201 @@ +package com.gitee.starblues.factory.process.pipe.bean; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.TreeTraversingParser; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLParser; +import com.gitee.starblues.annotation.ConfigDefinition; +import com.gitee.starblues.factory.PluginRegistryInfo; +import com.gitee.starblues.factory.process.pipe.loader.ResourceWrapper; +import com.gitee.starblues.factory.process.pipe.loader.load.PluginConfigFileLoader; +import com.gitee.starblues.integration.IntegrationConfiguration; +import com.gitee.starblues.realize.BasePlugin; +import com.gitee.starblues.utils.PluginConfigUtils; +import org.pf4j.RuntimeMode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.Resource; +import org.springframework.lang.Nullable; +import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; + +import java.util.*; + +/** + * 插件互相调用的bean注册者 + * @author starBlues + * @version 2.4.3 + */ +public class SpringBootConfigFileRegistrar implements PluginBeanRegistrar{ + + private final Logger logger = LoggerFactory.getLogger(SpringBootConfigFileRegistrar.class); + + private final YAMLFactory yamlFactory; + private final ObjectMapper objectMapper; + + private static final String[] PROP_FILE_SUFFIX = new String[]{".prop", ".PROP", ".properties", ".PROPERTIES"}; + private static final String[] YML_FILE_SUFFIX = new String[]{".yml", ".YML", "yaml", "YAML"}; + + public static final String CONFIG_PROP = "PLUGIN_SPRING_BOOT_CONFIG-"; + + + private final IntegrationConfiguration integrationConfiguration; + + public SpringBootConfigFileRegistrar(ApplicationContext mainApplicationContext){ + integrationConfiguration = + mainApplicationContext.getBean(IntegrationConfiguration.class); + yamlFactory = new YAMLFactory(); + objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } + + + @Override + public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception { + ConfigurableEnvironment environment = pluginRegistryInfo.getPluginApplicationContext().getEnvironment(); + //加载成PropertySource对象,并添加到Environment环境中 + List> propertySources = loadProfiles(pluginRegistryInfo); + if(ObjectUtils.isEmpty(propertySources)){ + return; + } + for (PropertySource propertySource : propertySources) { + environment.getPropertySources().addLast(propertySource); + } + } + + private List> loadProfiles(PluginRegistryInfo pluginRegistryInfo) throws Exception { + List resources = getResource(pluginRegistryInfo); + if(ObjectUtils.isEmpty(resources)){ + return null; + } + List> propProfiles = new ArrayList<>(); + for (Resource resource : resources) { + if(resource == null || !resource.exists()){ + continue; + } + String filename = resource.getFilename(); + if(ObjectUtils.isEmpty(filename)){ + return null; + } + + for (String propFileSuffix : PROP_FILE_SUFFIX) { + if(!filename.endsWith(propFileSuffix)){ + continue; + } + List> propertySources = getPropProfiles(resource, pluginRegistryInfo); + if(ObjectUtils.isEmpty(propertySources)){ + continue; + } + propProfiles.addAll(propertySources); + break; + } + for (String propFileSuffix : YML_FILE_SUFFIX) { + if(!filename.endsWith(propFileSuffix)){ + continue; + } + List> propertySources = getYmlProfiles(resource, pluginRegistryInfo); + if(ObjectUtils.isEmpty(propertySources)){ + continue; + } + propProfiles.addAll(propertySources); + break; + } + } + + return propProfiles; + } + + private List> getYmlProfiles(Resource resource, + PluginRegistryInfo pluginRegistryInfo) throws Exception{ + YAMLParser yamlParser = yamlFactory.createParser(resource.getInputStream()); + final JsonNode node = objectMapper.readTree(yamlParser); + Map source = objectMapper.readValue(new TreeTraversingParser(node), + new TypeReference>(){}); + Map result = new HashMap<>(); + buildFlattenedMap(result, source, null); + String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); + List> propertySources = new ArrayList<>(1); + propertySources.add(new MapPropertySource(CONFIG_PROP.concat(pluginId), result)); + return Collections.unmodifiableList(propertySources); + } + + private List> getPropProfiles(Resource resource, + PluginRegistryInfo pluginRegistryInfo) throws Exception{ + Properties properties = new Properties(); + properties.load(resource.getInputStream()); + String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId(); + return Collections.unmodifiableList( + Collections.singletonList(new PropertiesPropertySource(pluginId.concat("-config"), properties)) + ); + } + + private List getResource(PluginRegistryInfo pluginRegistryInfo) throws Exception{ + BasePlugin basePlugin = pluginRegistryInfo.getBasePlugin(); + ConfigDefinition configDefinition = basePlugin.getClass().getAnnotation(ConfigDefinition.class); + if(configDefinition == null){ + logger.warn("Plugin '{}' not config annotation : @ConfigDefinition, " + + "If you want to use the plugin spring boot configuration file, " + + "please configure the annotation: @ConfigDefinition to BasePlugin subclasses", + pluginRegistryInfo.getPluginWrapper().getPluginId()); + return null; + } + RuntimeMode runtimeMode = pluginRegistryInfo.getPluginWrapper().getRuntimeMode(); + String fileName = PluginConfigUtils.getConfigFileName(configDefinition, runtimeMode); + PluginConfigFileLoader pluginConfigFileLoader = new PluginConfigFileLoader( + integrationConfiguration.pluginConfigFilePath(), fileName + ); + ResourceWrapper resourceWrapper = pluginConfigFileLoader.load(pluginRegistryInfo); + return resourceWrapper.getResources(); + } + + private void buildFlattenedMap(Map result, + Map source, + @Nullable String path) { + source.forEach((key, value) -> { + if (StringUtils.hasText(path)) { + if (key.startsWith("[")) { + key = path + key; + } + else { + key = path + '.' + key; + } + } + if (value instanceof String) { + result.put(key, value); + } + else if (value instanceof Map) { + // Need a compound key + @SuppressWarnings("unchecked") + Map map = (Map) value; + buildFlattenedMap(result, map, key); + } + else if (value instanceof Collection) { + // Need a compound key + @SuppressWarnings("unchecked") + Collection collection = (Collection) value; + if (collection.isEmpty()) { + result.put(key, ""); + } + else { + int count = 0; + for (Object object : collection) { + buildFlattenedMap(result, Collections.singletonMap( + "[" + (count++) + "]", object), key); + } + } + } + else { + result.put(key, (value != null ? value : "")); + } + }); + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/name/PluginAnnotationBeanNameGenerator.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/name/PluginAnnotationBeanNameGenerator.java index aa5c14e5e7364352d9fcaeee902d70596d05be2e..70a13bc4bdb9c8a5db1961b55d8d00f439ea7fe6 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/name/PluginAnnotationBeanNameGenerator.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/bean/name/PluginAnnotationBeanNameGenerator.java @@ -12,6 +12,7 @@ import org.springframework.util.StringUtils; * @author starBlues * @version 1.0 */ +@Deprecated public class PluginAnnotationBeanNameGenerator extends AnnotationBeanNameGenerator { /** diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ConfigDefinitionGroup.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ConfigDefinitionGroup.java index 252c037e5679e5d8c783175d3d16714361030c3e..953a8adf325cd945a55a40086e20185a68f38574 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ConfigDefinitionGroup.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/pipe/classs/group/ConfigDefinitionGroup.java @@ -4,6 +4,7 @@ import com.gitee.starblues.annotation.ConfigDefinition; import com.gitee.starblues.factory.process.pipe.classs.PluginClassGroup; import com.gitee.starblues.realize.BasePlugin; import com.gitee.starblues.utils.AnnotationsUtils; +import org.springframework.util.ObjectUtils; /** * 分组存在注解: @ConfigDefinition @@ -31,6 +32,9 @@ public class ConfigDefinitionGroup implements PluginClassGroup { @Override public boolean filter(Class aClass) { + if(BasePlugin.class.isAssignableFrom(aClass)){ + return false; + } return AnnotationsUtils.haveAnnotations(aClass, false, ConfigDefinition.class); } 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 eccbb9b494e6e63357cb85119a25a3efe59d3e0c..1628b808810df19879526497656a0a906b84daf0 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 @@ -51,7 +51,6 @@ public class PluginConfigFileLoader implements PluginResourceLoader { suppliers.add(findPluginRoot(basePlugin)); suppliers.add(findClassPath(pluginRegistryInfo.getPluginClassLoader())); - for (Supplier supplier : suppliers) { SupplierBean supplierBean = supplier.get(); Resource resource = supplierBean.getResource(); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/PluginPostProcessorFactory.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/PluginPostProcessorFactory.java index ffa22824deec3988dd5de738bbe4061335d8d23a..778b29edb89029ea2ecc46114c44f6cd558a7755 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/PluginPostProcessorFactory.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/PluginPostProcessorFactory.java @@ -39,11 +39,11 @@ public class PluginPostProcessorFactory implements PluginPostProcessor { // 如果配置启用webSocket的功能, 则进行引入 pluginPostProcessors.add(new PluginWebSocketProcessor(mainApplicationContext)); } - // 主要触发启动监听事件,因此在最后一个执行。配合 OneselfListenerStopEventProcessor 该类触发启动、停止事件。 - pluginPostProcessors.add(new PluginOneselfStartEventProcessor()); // 添加扩展 pluginPostProcessors.addAll(ExtensionInitializer.getPostProcessorExtends()); + // 主要触发启动监听事件,因此在最后一个执行。配合 OneselfListenerStopEventProcessor 该类触发启动、停止事件。 + pluginPostProcessors.add(new PluginOneselfStartEventProcessor()); // 进行初始化 for (PluginPostProcessor pluginPostProcessor : pluginPostProcessors) { pluginPostProcessor.initialize(); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/bean/PluginOneselfStartEventProcessor.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/bean/PluginOneselfStartEventProcessor.java index 1eb90beefb92e6cc4ae9b91a9eccb6aa1a2a75de..94854ed2eb6cd0ada954d78f8cae1badae60fd2b 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/bean/PluginOneselfStartEventProcessor.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/factory/process/post/bean/PluginOneselfStartEventProcessor.java @@ -21,7 +21,7 @@ import java.util.List; public class PluginOneselfStartEventProcessor implements PluginPostProcessor { private final Logger log = LoggerFactory.getLogger(this.getClass()); - private final static String KEY = "OneselfListeners"; + public final static String KEY = "OneselfListeners"; public PluginOneselfStartEventProcessor(){ } @@ -65,21 +65,9 @@ public class PluginOneselfStartEventProcessor implements PluginPostProcessor { @Override public void unRegistry(List pluginRegistryInfos) { - for (PluginRegistryInfo pluginRegistryInfo : pluginRegistryInfos) { - BasePlugin basePlugin = pluginRegistryInfo.getBasePlugin(); - List oneselfListeners = pluginRegistryInfo.getExtension(KEY); - if(oneselfListeners == null || oneselfListeners.isEmpty()){ - continue; - } - for (OneselfListener oneselfListener : oneselfListeners) { - try { - oneselfListener.stopEvent(basePlugin); - } catch (Exception e){ - log.error("OneselfListener {} execute stopEvent exception. {}", - oneselfListener.getClass().getName(), e.getMessage(), e); - } - } - } + // 当前不执行停止事件 + // 由类 com.gitee.starblues.factory.process.pipe.PluginOneselfStopEventProcessor 实现停止 + // 主要考虑到停止时, 需要先执行, 自主来关闭当前插件某些资源 } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AbstractPluginApplication.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AbstractPluginApplication.java index 829937534a7bf3b968b967de445becbf49180f7d..f34bd1d5587228752f74b8509f918e5f889d9945 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AbstractPluginApplication.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/AbstractPluginApplication.java @@ -14,7 +14,7 @@ import java.util.List; * 公用的的插件应用 * * @author starBlues - * @version 2.2.0 + * @version 2.4.3 */ public abstract class AbstractPluginApplication implements PluginApplication { @@ -22,12 +22,13 @@ public abstract class AbstractPluginApplication implements PluginApplication { @Override - public void addExtension(AbstractExtension extension) { + public PluginApplication addExtension(AbstractExtension extension) { if(extension == null){ - return; + return this; } extension.setPluginApplication(this); ExtensionFactory.addExtension(extension); + return this; } @Override diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/PluginApplication.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/PluginApplication.java index a397f8e8ff47a0d97230ad8647ecbc9df24aff9c..3ac7b8547b7f0a2d405da17ca80f215adece9695 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/PluginApplication.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/integration/application/PluginApplication.java @@ -10,7 +10,7 @@ import org.springframework.context.ApplicationContext; /** * 插件应用。 * @author starBlues - * @version 2.2.0 + * @version 2.4.3 */ public interface PluginApplication extends PluginListenerContext { @@ -37,7 +37,8 @@ public interface PluginApplication extends PluginListenerContext { /** * 添加扩展 * @param extension 扩展类 + * @return PluginApplication */ - void addExtension(AbstractExtension extension); + PluginApplication addExtension(AbstractExtension extension); } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/BasePlugin.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/BasePlugin.java index 7bc18cf1a945b3acf3abd03cc4fccd571a58666e..9d55eabaa057287e6dcc9b05565228f6b3cba1e1 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/BasePlugin.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/BasePlugin.java @@ -12,6 +12,7 @@ import org.pf4j.PluginWrapper; public abstract class BasePlugin extends Plugin { private final BasePluginExtend basePluginExtend; + private String springBootConfigFilePath; public BasePlugin(PluginWrapper wrapper) { super(wrapper); diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/ConfigDefinitionTip.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/ConfigDefinitionTip.java index 681ce1eae2728f4963d2152b47eadf947aba8f04..dcb0a16159f29708c8a8a5aa9ce89d0ae537fb82 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/ConfigDefinitionTip.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/realize/ConfigDefinitionTip.java @@ -5,7 +5,15 @@ import com.gitee.starblues.integration.operator.module.PluginInfo; import com.gitee.starblues.utils.SpringBeanUtils; import org.pf4j.PluginManager; import org.pf4j.PluginWrapper; +import org.springframework.boot.context.properties.bind.Bindable; +import org.springframework.boot.context.properties.bind.Binder; import org.springframework.context.ApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * 给系统中所有ConfigDefinition 提供一个小工具类。可通过它获取主程序的某些bean. 和当前插件一些信息 @@ -52,4 +60,80 @@ public class ConfigDefinitionTip { configDefinitionClass); } + /** + * 将Springboot类型的配置文件中的值映射为bean + * 注意: 只针对插件扩展的 springboot 配置文件生效 + * @param prefix 配置文件中的前置, 比如: plugin.config + * @param type 配置文件中结构对应的类类型, 比如: plugin.config 下定义的键值对和type类类型一致 + * @param 类类型 + * @return T + */ + public T getConfigOfBean(String prefix, Class type){ + return pluginRegistryInfo.getPluginBinder() + .bind(prefix, Bindable.of(type)) + .orElseGet(() -> null); + } + + /** + * 将Springboot类型的配置文件中的值映射为 List + * 注意: 只针对插件扩展的 springboot 配置文件生效 + * @param prefix 配置文件中的前置, 比如: plugin.config + * @param type List元素的类类型 + * @param List中定义的类类型 + * @return List + */ + public List getConfigOfList(String prefix, Class type){ + return pluginRegistryInfo.getPluginBinder() + .bind(prefix, Bindable.listOf(type)) + .orElseGet(() -> Collections.emptyList()); + } + + /** + * 将Springboot类型的配置文件中的值映射为 Set + * 注意: 只针对插件扩展的 springboot 配置文件生效 + * @param prefix 配置文件中的前置, 比如: plugin.config + * @param type Set元素的类类型 + * @param 类类型 + * @return Set + */ + public Set getConfigOfSet(String prefix, Class type){ + return pluginRegistryInfo.getPluginBinder() + .bind(prefix, Bindable.setOf(type)) + .orElseGet(() -> Collections.emptySet()); + } + + + /** + * 将Springboot类型的配置文件中的值映射为 Map + * 注意: 只针对插件扩展的 springboot 配置文件生效 + * @param prefix 配置文件中的前置, 比如: plugin.config + * @param keyType map的key元素类型 + * @param valueType map的值元素类型 + * @param map key 元素的类类型 + * @param map value 元素的类类型 + * @return Map + */ + public Map getConfigOfSet(String prefix, Class keyType, Class valueType){ + return pluginRegistryInfo.getPluginBinder() + .bind(prefix, Bindable.mapOf(keyType, valueType)) + .orElseGet(() -> Collections.emptyMap()); + } + + + /** + * 返回当前插件的ConfigurableEnvironment + * @return ConfigurableEnvironment + */ + public ConfigurableEnvironment getPluginEnvironment(){ + return pluginRegistryInfo.getPluginApplicationContext().getEnvironment(); + } + + /** + * 返回当前插件的Binder + * @return Binder + */ + public Binder getPluginBinder(){ + return pluginRegistryInfo.getPluginBinder(); + } + } 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 54ceaed9f41930220c5c410cbb02d14dd911064c..87a270c9b5482906882d2b5f0987ee3efc7e6ecb 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 @@ -3,6 +3,7 @@ package com.gitee.starblues.utils; import com.gitee.starblues.integration.IntegrationConfiguration; import org.pf4j.util.StringUtils; +import java.io.File; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -102,7 +103,7 @@ public class CommonUtils { /** - * 拼接路径 + * 拼接url路径 * @param paths 拼接的路径 * @return 拼接的路径 */ @@ -130,5 +131,36 @@ public class CommonUtils { return stringBuilder.toString(); } + /** + * 拼接file路径 + * @param paths 拼接的路径 + * @return 拼接的路径 + */ + public static String joiningFilePath(String ...paths){ + if(paths == null || paths.length == 0){ + return ""; + } + StringBuilder stringBuilder = new StringBuilder(); + int length = paths.length; + for (int i = 0; i < length; i++) { + String path = paths[i]; + if(StringUtils.isNullOrEmpty(path)) { + continue; + } + if(i > 0){ + if(path.startsWith(File.separator) || path.startsWith("/") || + path.startsWith("\\") || path.startsWith("//")){ + stringBuilder.append(path); + } else { + stringBuilder.append(File.separator).append(path); + } + } else { + stringBuilder.append(path); + } + } + + return stringBuilder.toString(); + } + } diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginConfigUtils.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginConfigUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..2d79c79f11883dcc2cd7e813643906edd14a92d9 --- /dev/null +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginConfigUtils.java @@ -0,0 +1,53 @@ +package com.gitee.starblues.utils; + +import com.gitee.starblues.annotation.ConfigDefinition; +import org.apache.catalina.core.ApplicationContext; +import org.pf4j.RuntimeMode; +import org.pf4j.util.StringUtils; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.env.PropertySource; + +/** + * 插件配置工具类 + * @author starBlues + * @version 2.4.3 + */ +public class PluginConfigUtils { + + private PluginConfigUtils(){} + + /** + * 根据项目运行环境模式来获取配置文件名称 + * @param configDefinition 配置的注解 + * @param runtimeMode 运行模式 + * @return 文件名称 + */ + public static String getConfigFileName(ConfigDefinition configDefinition, RuntimeMode runtimeMode){ + String fileName = configDefinition.fileName(); + if(StringUtils.isNullOrEmpty(fileName)){ + return null; + } + + String fileNamePrefix; + String fileNamePrefixSuffix; + + if(fileName.lastIndexOf(".") == -1) { + fileNamePrefix = fileName; + fileNamePrefixSuffix = ""; + } else { + int index = fileName.lastIndexOf("."); + fileNamePrefix = fileName.substring(0, index); + fileNamePrefixSuffix = fileName.substring(index); + } + + if(runtimeMode == RuntimeMode.DEPLOYMENT){ + // 生产环境 + fileNamePrefix = fileNamePrefix + configDefinition.prodSuffix(); + } else if(runtimeMode == RuntimeMode.DEVELOPMENT){ + // 开发环境 + fileNamePrefix = fileNamePrefix + configDefinition.devSuffix(); + } + return fileNamePrefix + fileNamePrefixSuffix; + } + +} diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginFileUtils.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginFileUtils.java index 38d569917a59261d3835885a31bb572f1875aad2..e40b1bf381b660ba48fdfc6b7e11b16e22b400bd 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginFileUtils.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginFileUtils.java @@ -1,5 +1,9 @@ package com.gitee.starblues.utils; +import com.gitee.starblues.annotation.ConfigDefinition; +import org.pf4j.RuntimeMode; +import org.pf4j.util.StringUtils; + import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginOperatorInfo.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginOperatorInfo.java index af22bb3820fec785944afe4677de08e1db1e2816..be3b0ec9e216c12b2d635e409723549c475f1dc6 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginOperatorInfo.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/PluginOperatorInfo.java @@ -21,7 +21,7 @@ public class PluginOperatorInfo { * 该参数主要解决在安装插件时 setOperatorType 一次操作类型,但是它又调用了启动插件,而启动插件也要 setOperatorType 一次操作类型 * 所以, 使用该参数, 用于在安装插件时, 锁定 setOperatorType, 在启动时就无法再 setOperatorType 了, 就不会覆盖该值 */ - private AtomicBoolean isLock = new AtomicBoolean(false); + private final AtomicBoolean isLock = new AtomicBoolean(false); /** * 设置操作类型 diff --git a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/ResourceUtils.java b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/ResourceUtils.java index 1870294bf030cc6e8a776cb9db99f5b0edac052d..3c45b3fde41771fff0f2483f38ad8d0323664210 100644 --- a/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/ResourceUtils.java +++ b/springboot-plugin-framework/src/main/java/com/gitee/starblues/utils/ResourceUtils.java @@ -26,6 +26,7 @@ public class ResourceUtils { public final static String TYPE_CLASSPATH = "classpath"; public final static String TYPE_PACKAGE = "package"; + public final static String TYPE_SPLIT = ":"; /** * 获取匹配路绝 @@ -36,20 +37,34 @@ public class ResourceUtils { if(StringUtils.isNullOrEmpty(locationMatch)){ return null; } - String[] split = locationMatch.split(":"); - if(split.length != 2){ - return null; + String classPathType = TYPE_CLASSPATH + TYPE_SPLIT; + if(isClasspath(locationMatch)){ + return locationMatch.replaceFirst(classPathType, ""); + } + String fileType = TYPE_FILE + TYPE_SPLIT; + if(isFile(locationMatch)){ + return locationMatch.replaceFirst(fileType, ""); } - String type = split[0]; - String location = split[1]; - if(TYPE_CLASSPATH.equalsIgnoreCase(type) || TYPE_FILE.equalsIgnoreCase(type)){ - return locationMatch; - } else if(TYPE_PACKAGE.equalsIgnoreCase(type)){ + String packageType = TYPE_PACKAGE + TYPE_SPLIT; + if(isPackage(locationMatch)){ + String location = locationMatch.replaceFirst(packageType, ""); return location.replace(".", "/"); - } else { - LOGGER.error("locationMatch {} illegal", locationMatch); - return null; } + LOGGER.error("locationMatch {} illegal", locationMatch); + return null; + } + + public static boolean isClasspath(String locationMatch){ + return locationMatch.startsWith(TYPE_CLASSPATH + TYPE_SPLIT); } + public static boolean isFile(String locationMatch){ + return locationMatch.startsWith(TYPE_FILE + TYPE_SPLIT); + } + + public static boolean isPackage(String locationMatch){ + return locationMatch.startsWith(TYPE_PACKAGE + TYPE_SPLIT); + } + + } diff --git a/springboot-plugin-framework/src/test/java/com/gitee/starblues/utils/ResourceUtilsTest.java b/springboot-plugin-framework/src/test/java/com/gitee/starblues/utils/ResourceUtilsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1f2f6d15697ddcd1408300ae60bbfac42db54f93 --- /dev/null +++ b/springboot-plugin-framework/src/test/java/com/gitee/starblues/utils/ResourceUtilsTest.java @@ -0,0 +1,34 @@ +package com.gitee.starblues.utils; + + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author starBlues + * @version 2.3.4 + * @since 2021-06-01 + */ +public class ResourceUtilsTest { + + @Test + public void test(){ + String locationMatch = "file:C:\\Users\\Desktop\\plugin1-log.xml"; + String matchLocation = ResourceUtils.getMatchLocation(locationMatch); + Assert.assertEquals("C:\\Users\\Desktop\\plugin1-log.xml", + matchLocation); + + locationMatch = "classpath:C:\\Users\\Desktop\\plugin1-log.xml"; + matchLocation = ResourceUtils.getMatchLocation(locationMatch); + Assert.assertEquals("C:\\Users\\Desktop\\plugin1-log.xml", + matchLocation); + + locationMatch = "package:com.test.aa"; + matchLocation = ResourceUtils.getMatchLocation(locationMatch); + Assert.assertEquals("com/test/aa", + matchLocation); + + + } + +} \ No newline at end of file