From e8fff9d7c300d1e7d59b8e8223b4ac795cdb50cf Mon Sep 17 00:00:00 2001 From: richie Date: Tue, 17 Sep 2019 17:16:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=9B=91=E5=90=AC+=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E6=8F=92=E4=BB=B6=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fanruan/api/conf/ConfigurationKit.java | 61 +++++++++++++++++++ .../java/com/fanruan/api/generic/Matcher.java | 11 ++++ .../fanruan/api/{conf => generic}/Runner.java | 2 +- .../com/fanruan/api/runtime/PluginKit.java | 20 ------ .../com/fanruan/api/plugin/PluginKitTest.java | 13 ---- 5 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/fanruan/api/conf/ConfigurationKit.java create mode 100644 src/main/java/com/fanruan/api/generic/Matcher.java rename src/main/java/com/fanruan/api/{conf => generic}/Runner.java (78%) delete mode 100644 src/main/java/com/fanruan/api/runtime/PluginKit.java delete mode 100644 src/test/java/com/fanruan/api/plugin/PluginKitTest.java diff --git a/src/main/java/com/fanruan/api/conf/ConfigurationKit.java b/src/main/java/com/fanruan/api/conf/ConfigurationKit.java new file mode 100644 index 0000000..86cebc0 --- /dev/null +++ b/src/main/java/com/fanruan/api/conf/ConfigurationKit.java @@ -0,0 +1,61 @@ +package com.fanruan.api.conf; + +import com.fanruan.api.generic.Matcher; +import com.fanruan.api.generic.Runner; +import com.fr.config.Configuration; +import com.fr.config.holder.ConfigChangeListener; +import com.fr.transaction.Configurations; +import com.fr.transaction.ValidateProxy; +import com.fr.transaction.WorkerFacade; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-17 + * 配置管理工具类 + */ +public class ConfigurationKit { + + /** + * 兼容配置缓存失效 + * + * @param matcher 匹配器 + * @param runner 匹配后执行的动作 + */ + public static void listenCacheChange(final Matcher> matcher, final Runner runner) { + ValidateProxy.getInstance().getValidateManager().registerListener(new ConfigChangeListener() { + + @Override + public boolean accept(Class clazz) { + if (matcher == null) { + return true; + } + return matcher.match(clazz); + } + + @Override + public void change() { + if (matcher != null) { + runner.run(); + } + } + }); + } + + /** + * 在一个事务中修改配置 + * + * @param configType 配置类 + * @param runner 执行器 + */ + public static void modify(Class configType, Runner runner) { + Configurations.modify(new WorkerFacade(configType) { + @Override + public void run() { + if (runner != null) { + runner.run(); + } + } + }); + } +} diff --git a/src/main/java/com/fanruan/api/generic/Matcher.java b/src/main/java/com/fanruan/api/generic/Matcher.java new file mode 100644 index 0000000..33f8188 --- /dev/null +++ b/src/main/java/com/fanruan/api/generic/Matcher.java @@ -0,0 +1,11 @@ +package com.fanruan.api.generic; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-09-17 + */ +public interface Matcher { + + boolean match(T t); +} diff --git a/src/main/java/com/fanruan/api/conf/Runner.java b/src/main/java/com/fanruan/api/generic/Runner.java similarity index 78% rename from src/main/java/com/fanruan/api/conf/Runner.java rename to src/main/java/com/fanruan/api/generic/Runner.java index b80c76e..c959dd0 100644 --- a/src/main/java/com/fanruan/api/conf/Runner.java +++ b/src/main/java/com/fanruan/api/generic/Runner.java @@ -1,4 +1,4 @@ -package com.fanruan.api.conf; +package com.fanruan.api.generic; /** * @author richie diff --git a/src/main/java/com/fanruan/api/runtime/PluginKit.java b/src/main/java/com/fanruan/api/runtime/PluginKit.java deleted file mode 100644 index c6f126b..0000000 --- a/src/main/java/com/fanruan/api/runtime/PluginKit.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.fanruan.api.runtime; - -import com.fr.plugin.context.PluginContexts; - -/** - * @author richie - * @version 10.0 - * Created by richie on 2019-08-30 - * 插件相关的工具类 - */ -public class PluginKit { - - /** - * 当前插件是否可用(例如:插件授权文件过期时,该犯法会返回false) - * @return 插件可用则返回true,不可用则返回false - */ - public static boolean isCurrentPluginAvailable() { - return PluginContexts.currentContext().isAvailable(); - } -} diff --git a/src/test/java/com/fanruan/api/plugin/PluginKitTest.java b/src/test/java/com/fanruan/api/plugin/PluginKitTest.java deleted file mode 100644 index 7d5f9a5..0000000 --- a/src/test/java/com/fanruan/api/plugin/PluginKitTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.fanruan.api.plugin; - -import com.fanruan.api.runtime.PluginKit; -import org.junit.Assert; -import org.junit.Test; - -public class PluginKitTest { - @Test - public void getArray() { - Assert.assertFalse(PluginKit.isCurrentPluginAvailable()); - - } -}