Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit 3edf0f4

Browse files
committed
fix: plugin loader
1 parent 24570a4 commit 3edf0f4

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

src/main/java/org/code4everything/wetool/controller/MainController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ public void handleEvent0(String s, Date date) {
171171
// 监听鼠标位置
172172
EventCenter.subscribeEvent(EventCenter.EVENT_MOUSE_MOTION, new MouseMotionEventHandler());
173173
multiDesktopOnWindows();
174-
WeUtils.execute(PluginLoader::loadPlugins);
175174

176175
// 注册搜索动作
177176
registerAction("退出-exit", actionEvent -> WeUtils.exitSystem());
@@ -194,6 +193,8 @@ public void handleEvent0(String s, Date date) {
194193
String name = StrUtil.removePrefix(a.getSource().toString(), "env").trim();
195194
FxDialogs.showInformation(StrUtil.format("{} 的环境变量", name), System.getenv(name));
196195
});
196+
197+
WeUtils.execute(PluginLoader::loadPlugins);
197198
}
198199

199200
private void runHutoolCmd(ActionEvent actionEvent) {

src/main/java/org/code4everything/wetool/plugin/PluginLoader.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static void loadPlugins() {
9393
}
9494
}
9595

96-
public static void addPluginForSearch(String prefix, Menu menu) {
96+
private static void addPluginForSearch(String prefix, Menu menu) {
9797
if (Objects.isNull(menu)) {
9898
menu = FxUtils.getPluginMenu();
9999
}
@@ -127,11 +127,12 @@ public static void loadPlugins(Collection<File> plugins, final boolean checkDisa
127127
loadPluginFromPrepared();
128128
}
129129

130-
public static void registerPlugin(WePluginInfo info, WePluginSupporter supporter) {
131-
registerPlugin(info, supporter, true);
130+
public static void loadPluginForTest(WePluginInfo info) {
131+
preparePlugin(FileUtil.file(FileUtils.currentWorkDir()), info, true);
132+
loadPluginFromPrepared();
132133
}
133134

134-
private void preparePlugin(File file, boolean checkDisable) {
135+
private static void preparePlugin(File file, boolean checkDisable) {
135136
if (file.exists() && file.isFile()) {
136137
// 包装成 JarFile
137138
try (JarFile jar = new JarFile(file)) {
@@ -145,35 +146,38 @@ private void preparePlugin(File file, boolean checkDisable) {
145146
String json = IoUtil.read(jar.getInputStream(entry), "utf-8");
146147
WePluginInfo info = JSON.parseObject(json, WePluginInfo.class);
147148
BeanFactory.get(WePluginConfig.class).putInitBootIfNotExists(info, false);
148-
149-
if (checkDisable && isDisabled(info)) {
150-
// 插件被禁止加载
151-
log.info("plugin {}-{}-{} disabled", info.getAuthor(), info.getName(), info.getVersion());
152-
return;
153-
}
154-
// 兼容性检测
155-
if (isIncompatible(info)) {
156-
return;
157-
}
158-
WePlugin plugin = new WePlugin(info, file);
159-
// 检测插件是否已经加载
160-
if (LOADED_PLUGINS.contains(plugin)) {
161-
// 插件已被加载
162-
log.info("plugin {}-{} already loaded", info.getAuthor(), info.getName());
163-
return;
164-
}
165-
if (info.getIsolated()) {
166-
// 隔离的插件使用单独的类加载器
167-
plugin.setLoaderName(info.getName());
168-
}
169-
replaceIfNewer(plugin);
149+
preparePlugin(file, info, checkDisable);
170150
} catch (Exception e) {
171151
FxDialogs.showException("plugin file load failed: " + file.getName(), e);
172152
}
173153
}
174154
}
175155

176-
private static boolean registerPlugin(WePluginInfo info, WePluginSupporter supporter, boolean checkCompatible) {
156+
private static void preparePlugin(File jarFile, WePluginInfo info, boolean checkDisable) {
157+
if (checkDisable && isDisabled(info)) {
158+
// 插件被禁止加载
159+
log.info("plugin {}-{}-{} disabled", info.getAuthor(), info.getName(), info.getVersion());
160+
return;
161+
}
162+
// 兼容性检测
163+
if (isIncompatible(info)) {
164+
return;
165+
}
166+
WePlugin plugin = new WePlugin(info, jarFile);
167+
// 检测插件是否已经加载
168+
if (LOADED_PLUGINS.contains(plugin)) {
169+
// 插件已被加载
170+
log.info("plugin {}-{} already loaded", info.getAuthor(), info.getName());
171+
return;
172+
}
173+
if (info.getIsolated()) {
174+
// 隔离的插件使用单独的类加载器
175+
plugin.setLoaderName(info.getName());
176+
}
177+
replaceIfNewer(plugin);
178+
}
179+
180+
private static boolean registerPlugin(WePluginInfo info, WePluginSupporter supporter) {
177181
OsInfo osInfo = SystemUtil.getOsInfo();
178182

179183
// @formatter:off
@@ -185,9 +189,6 @@ private static boolean registerPlugin(WePluginInfo info, WePluginSupporter suppo
185189
log.info("plugin {}-{}-{} not support this os", info.getAuthor(), info.getName(), info.getVersion());
186190
return false;
187191
}
188-
if (checkCompatible && isIncompatible(info)) {
189-
return false;
190-
}
191192
// 初始化
192193
if (!supporter.initialize()) {
193194
log.info("plugin {}-{}-{} initialize failed", info.getAuthor(), info.getName(), info.getVersion());
@@ -261,7 +262,7 @@ private static void loadPluginFromPrepared() {
261262
Class<?> clazz = plugin.getClassLoader().loadClass(plugin.getPluginInfo().getSupportedClass());
262263
WePluginSupporter supporter = (WePluginSupporter) ReflectUtil.newInstance(clazz);
263264
// 添加插件菜单
264-
if (registerPlugin(plugin.getPluginInfo(), supporter, false)) {
265+
if (registerPlugin(plugin.getPluginInfo(), supporter)) {
265266
LOADED_PLUGINS.add(plugin);
266267
}
267268
} catch (Exception e) {

0 commit comments

Comments
 (0)