diff --git a/modules/common/src/main/java/io/jpom/plugin/DefaultPlugin.java b/modules/common/src/main/java/io/jpom/plugin/DefaultPlugin.java index b57d516ed..8a79e8fb2 100644 --- a/modules/common/src/main/java/io/jpom/plugin/DefaultPlugin.java +++ b/modules/common/src/main/java/io/jpom/plugin/DefaultPlugin.java @@ -22,11 +22,19 @@ */ package io.jpom.plugin; +import cn.hutool.core.util.StrUtil; + /** + * 默认插件 + * * @author bwcx_jzy * @since 2021/12/22 */ public enum DefaultPlugin { + /** + * null + */ + NULL(StrUtil.EMPTY), /** * web hook */ diff --git a/modules/common/src/main/java/io/jpom/plugin/IDefaultPlugin.java b/modules/common/src/main/java/io/jpom/plugin/IDefaultPlugin.java index d43e96549..d44a31aed 100644 --- a/modules/common/src/main/java/io/jpom/plugin/IDefaultPlugin.java +++ b/modules/common/src/main/java/io/jpom/plugin/IDefaultPlugin.java @@ -30,13 +30,4 @@ package io.jpom.plugin; */ public interface IDefaultPlugin extends IPlugin { - /** - * 默认插件排序到最后 - * - * @return max - */ - @Override - default int order() { - return Integer.MAX_VALUE; - } } diff --git a/modules/common/src/main/java/io/jpom/plugin/IPlugin.java b/modules/common/src/main/java/io/jpom/plugin/IPlugin.java index 4d039c465..160089c37 100644 --- a/modules/common/src/main/java/io/jpom/plugin/IPlugin.java +++ b/modules/common/src/main/java/io/jpom/plugin/IPlugin.java @@ -53,29 +53,4 @@ public interface IPlugin { default boolean check(String type, Object main, Map parameter) { throw new RuntimeException("Not implements"); } - - /** - * 插件的名字 - * - * @return 名称 - */ - String name(); - - /** - * 插件父级 - * - * @return 父级名称 - */ - default String parent() { - return null; - } - - /** - * 排序值 - * - * @return 值越小,排到前面 正序 - */ - default int order() { - return Integer.MIN_VALUE; - } } diff --git a/modules/common/src/main/java/io/jpom/plugin/PluginConfig.java b/modules/common/src/main/java/io/jpom/plugin/PluginConfig.java new file mode 100644 index 000000000..1ff05cbe2 --- /dev/null +++ b/modules/common/src/main/java/io/jpom/plugin/PluginConfig.java @@ -0,0 +1,38 @@ +package io.jpom.plugin; + +import cn.hutool.core.util.StrUtil; + +import java.lang.annotation.*; + +/** + * 插件配置 相关属性注解 + * + * @author bwcx_jzy + * @since 2021/12/24 + */ +@Documented +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface PluginConfig { + + /** + * 是否为原生对象,原生对象将使用 默认构造方法创建单利对象 + * + * @return 默认 原生对象 + */ + boolean nativeObject() default true; + + /** + * 默认插件、该字段优先级最低 + * + * @return 插件名 + */ + DefaultPlugin plugin() default DefaultPlugin.NULL; + + /** + * 插件名、该字段优先级高于 plugin + * + * @return 插件名 + */ + String name() default StrUtil.EMPTY; +} diff --git a/modules/common/src/main/java/io/jpom/plugin/PluginFactory.java b/modules/common/src/main/java/io/jpom/plugin/PluginFactory.java index 4396022c0..f034e4f70 100644 --- a/modules/common/src/main/java/io/jpom/plugin/PluginFactory.java +++ b/modules/common/src/main/java/io/jpom/plugin/PluginFactory.java @@ -28,12 +28,12 @@ import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.JarClassLoader; import cn.hutool.core.util.ClassLoaderUtil; import cn.hutool.core.util.ClassUtil; -import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import cn.jiangzeyin.common.DefaultSystemLog; import io.jpom.common.JpomManifest; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.annotation.Order; import org.springframework.util.Assert; import java.io.File; @@ -52,7 +52,7 @@ import java.util.stream.Collectors; public class PluginFactory implements ApplicationContextInitializer { private static final List FEATURE_CALLBACKS = new ArrayList<>(); - private static final Map> PLUGIN_MAP = new ConcurrentHashMap<>(); + private static final Map> PLUGIN_MAP = new ConcurrentHashMap<>(); /** * 添加回调事件 @@ -84,10 +84,10 @@ public class PluginFactory implements ApplicationContextInitializer pluginItems = PLUGIN_MAP.get(name); - PluginItem first = CollUtil.getFirst(pluginItems); + List pluginItemWraps = PLUGIN_MAP.get(name); + PluginItemWrap first = CollUtil.getFirst(pluginItemWraps); Assert.notNull(first, "对应找到对应到插件:" + name); - return first.plugin; + return first.getPlugin(); } /** @@ -103,14 +103,12 @@ public class PluginFactory implements ApplicationContextInitializer FileUtil.isFile(pathname) && FileUtil.JAR_FILE_EXT.equalsIgnoreCase(FileUtil.extName(pathname))); if (files != null) { for (File file : files) { @@ -134,7 +133,7 @@ public class PluginFactory implements ApplicationContextInitializer> classes = ClassUtil.scanPackage("io.jpom", IPlugin.class::isAssignableFrom); - List pluginItems = classes.stream().filter(ClassUtil::isNormalClass).map(aClass -> { - PluginItem pluginItem = new PluginItem(); - IPlugin plugin = (IPlugin) ReflectUtil.newInstance(aClass); - pluginItem.plugin = plugin; - pluginItem.name = plugin.name(); - pluginItem.className = (Class) aClass; - return pluginItem; - }).collect(Collectors.toList()); + List pluginItemWraps = classes + .stream() + .filter(aClass -> ClassUtil.isNormalClass(aClass) && aClass.isAnnotationPresent(PluginConfig.class)) + .map(aClass -> new PluginItemWrap((Class) aClass)) + .filter(pluginItemWrap -> { + if (StrUtil.isEmpty(pluginItemWrap.getName())) { + DefaultSystemLog.getLog().warn("plugin config name error:{}", pluginItemWrap.getClassName()); + return false; + } + return true; + }) + .collect(Collectors.toList()); // - Map> pluginMap = CollStreamUtil.groupByKey(pluginItems, PluginItem::getName); + Map> pluginMap = CollStreamUtil.groupByKey(pluginItemWraps, PluginItemWrap::getName); pluginMap.forEach((key, value) -> { // 排序 - value.sort((o1, o2) -> Comparator.comparingInt((ToIntFunction) value1 -> value1.plugin.order()).compare(o1, o2)); + value.sort((o1, o2) -> Comparator.comparingInt((ToIntFunction) value1 -> { + Order order = value1.getClassName().getAnnotation(Order.class); + if (order == null) { + return 0; + } + return order.value(); + }).compare(o1, o2)); PLUGIN_MAP.put(key, value); }); } - - private static class PluginItem { - - /** - * 插件名 - */ - private String name; - - /** - * 插件类名 - */ - private Class className; - - /** - * 插件对象 - */ - private IPlugin plugin; - - public String getName() { - return name; - } - - public Class getClassName() { - return className; - } - - public IPlugin getPlugin() { - return plugin; - } - } } diff --git a/modules/common/src/main/java/io/jpom/plugin/PluginItemWrap.java b/modules/common/src/main/java/io/jpom/plugin/PluginItemWrap.java new file mode 100644 index 000000000..257b35a8c --- /dev/null +++ b/modules/common/src/main/java/io/jpom/plugin/PluginItemWrap.java @@ -0,0 +1,73 @@ +package io.jpom.plugin; + +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; +import cn.jiangzeyin.common.spring.SpringUtil; + +/** + * 插件端对象 + * + * @author bwcx_jzy + * @since 2021/12/24 + */ +public class PluginItemWrap { + + /** + * 配置相关 + */ + private final PluginConfig pluginConfig; + + /** + * 插件名 + */ + private final String name; + + /** + * 插件类名 + */ + private final Class className; + + /** + * 插件对象 + */ + private volatile IPlugin plugin; + + public PluginItemWrap(Class className) { + this.className = className; + this.pluginConfig = className.getAnnotation(PluginConfig.class); + if (StrUtil.isNotEmpty(this.pluginConfig.name())) { + this.name = this.pluginConfig.name(); + } else { + this.name = this.pluginConfig.plugin().getName(); + } + } + + public PluginConfig getPluginConfig() { + return pluginConfig; + } + + public String getName() { + return name; + } + + public Class getClassName() { + return className; + } + + public IPlugin getPlugin() { + if (plugin == null) { + synchronized (className) { + if (plugin == null) { + // + boolean nativeObject = this.pluginConfig.nativeObject(); + if (nativeObject) { + plugin = ReflectUtil.newInstance(className); + } else { + plugin = SpringUtil.getBean(className); + } + } + } + } + return plugin; + } +} diff --git a/modules/sub-plugin/auto-charset-jchardet/src/main/java/io/jpom/plugin/DefaultFileCharsetDetectorImpl.java b/modules/sub-plugin/auto-charset-jchardet/src/main/java/io/jpom/plugin/DefaultFileCharsetDetectorImpl.java index ccac56de8..f23f3886c 100644 --- a/modules/sub-plugin/auto-charset-jchardet/src/main/java/io/jpom/plugin/DefaultFileCharsetDetectorImpl.java +++ b/modules/sub-plugin/auto-charset-jchardet/src/main/java/io/jpom/plugin/DefaultFileCharsetDetectorImpl.java @@ -29,6 +29,7 @@ import java.util.Map; * @author bwcx_jzy * @since 2021/12/24 */ +@PluginConfig(name = "charset-detector") public class DefaultFileCharsetDetectorImpl implements IDefaultPlugin { @Override @@ -36,9 +37,4 @@ public class DefaultFileCharsetDetectorImpl implements IDefaultPlugin { File file = (File) main; return new CharsetDetector().detectChineseCharset(file); } - - @Override - public String name() { - return "charset-detector"; - } } diff --git a/modules/sub-plugin/email/src/main/java/io/jpom/email/DefaultEmailPluginImpl.java b/modules/sub-plugin/email/src/main/java/io/jpom/email/DefaultEmailPluginImpl.java index 9f0af537c..8e5fc6fa5 100644 --- a/modules/sub-plugin/email/src/main/java/io/jpom/email/DefaultEmailPluginImpl.java +++ b/modules/sub-plugin/email/src/main/java/io/jpom/email/DefaultEmailPluginImpl.java @@ -29,6 +29,7 @@ import cn.jiangzeyin.common.DefaultSystemLog; import com.alibaba.fastjson.JSONObject; import io.jpom.plugin.DefaultPlugin; import io.jpom.plugin.IDefaultPlugin; +import io.jpom.plugin.PluginConfig; import javax.mail.Session; import javax.mail.Transport; @@ -39,6 +40,7 @@ import java.util.Map; * @author bwcx_jzy * @since 2021/12/22 */ +@PluginConfig(plugin = DefaultPlugin.Email) public class DefaultEmailPluginImpl implements IDefaultPlugin { @Override @@ -107,9 +109,4 @@ public class DefaultEmailPluginImpl implements IDefaultPlugin { mailAccount.setAuth(true); return mailAccount; } - - @Override - public String name() { - return DefaultPlugin.Email.getName(); - } } diff --git a/modules/sub-plugin/svn-clone/src/main/java/io/jpom/plugin/DefaultSvnPluginImpl.java b/modules/sub-plugin/svn-clone/src/main/java/io/jpom/plugin/DefaultSvnPluginImpl.java index d01ba0af2..523d77527 100644 --- a/modules/sub-plugin/svn-clone/src/main/java/io/jpom/plugin/DefaultSvnPluginImpl.java +++ b/modules/sub-plugin/svn-clone/src/main/java/io/jpom/plugin/DefaultSvnPluginImpl.java @@ -31,6 +31,7 @@ import java.util.Map; * @author bwcx_jzy * @since 2021/12/23 */ +@PluginConfig(plugin = DefaultPlugin.SvnClone) public class DefaultSvnPluginImpl implements IDefaultPlugin { @Override @@ -38,9 +39,4 @@ public class DefaultSvnPluginImpl implements IDefaultPlugin { File savePath = (File) main; return SvnKitUtil.checkOut(parameter, savePath); } - - @Override - public String name() { - return DefaultPlugin.SvnClone.getName(); - } } diff --git a/modules/sub-plugin/webhook/src/main/java/io/jpom/webhook/DefaultWebhookPluginImpl.java b/modules/sub-plugin/webhook/src/main/java/io/jpom/webhook/DefaultWebhookPluginImpl.java index e60bc7921..a0a36fe30 100644 --- a/modules/sub-plugin/webhook/src/main/java/io/jpom/webhook/DefaultWebhookPluginImpl.java +++ b/modules/sub-plugin/webhook/src/main/java/io/jpom/webhook/DefaultWebhookPluginImpl.java @@ -29,6 +29,7 @@ import cn.hutool.http.HttpUtil; import cn.jiangzeyin.common.DefaultSystemLog; import io.jpom.plugin.DefaultPlugin; import io.jpom.plugin.IDefaultPlugin; +import io.jpom.plugin.PluginConfig; import java.util.Map; @@ -38,6 +39,7 @@ import java.util.Map; * @author bwcx_jzy * @since 2021/12/22 */ +@PluginConfig(plugin = DefaultPlugin.WebHook) public class DefaultWebhookPluginImpl implements IDefaultPlugin { @Override @@ -57,9 +59,4 @@ public class DefaultWebhookPluginImpl implements IDefaultPlugin { return "WebHooks error:" + e.getMessage(); } } - - @Override - public String name() { - return DefaultPlugin.WebHook.getName(); - } }