Skip to content

Commit 110f3d4

Browse files
committed
newui: 修复不加载 jar 到 classpath 后出现的下载插件无法启用的问题
1 parent fcc1180 commit 110f3d4

7 files changed

Lines changed: 29 additions & 36 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/controller/index/PluginManageController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.xwintop.xJavaFxTool.event.AppEvents;
55
import com.xwintop.xJavaFxTool.event.PluginEvent;
66
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
7+
import com.xwintop.xJavaFxTool.plugin.PluginClassLoader;
78
import com.xwintop.xJavaFxTool.plugin.PluginManager;
9+
import com.xwintop.xJavaFxTool.plugin.PluginParser;
810
import com.xwintop.xJavaFxTool.services.index.PluginManageService;
911
import com.xwintop.xJavaFxTool.view.index.PluginManageView;
1012
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
@@ -135,6 +137,9 @@ private void afterDownload(
135137
PluginManager.getInstance().saveToFile();
136138
TooltipUtil.showToast("插件 " + dataRow.get("nameTableColumn") + " 下载完成");
137139

140+
PluginClassLoader tempClassLoader = new PluginClassLoader(pluginJarInfo.getFile());
141+
PluginParser.parse(pluginJarInfo.getFile(), pluginJarInfo, tempClassLoader);
142+
138143
AppEvents.fire(new PluginEvent(PluginEvent.PLUGIN_DOWNLOADED, pluginJarInfo));
139144
}
140145

src/main/java/com/xwintop/xJavaFxTool/newui/NewLauncherController.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public void initialize() {
6666
startWebView.getEngine().load(IndexController.QQ_URL); // 额外再打开一个反馈页面,可关闭
6767

6868
AppEvents.addEventHandler(PluginEvent.PLUGIN_DOWNLOADED, pluginEvent -> {
69-
PluginJarInfo pluginJarInfo = pluginEvent.getPluginJarInfo();
70-
PluginParser.parse(pluginJarInfo.getFile(), pluginJarInfo);
7169
loadPlugins();
7270
});
7371
}

src/main/java/com/xwintop/xJavaFxTool/plugin/PluginClassLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ private static URL fromFile(File file) {
2121
public PluginClassLoader(ClassLoader parent, File appFile) {
2222
super(new URL[]{fromFile(appFile)}, parent);
2323
}
24+
25+
public PluginClassLoader(File appFile) {
26+
this(getSystemClassLoader(), appFile);
27+
}
2428
}

src/main/java/com/xwintop/xJavaFxTool/plugin/PluginContainer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public FXMLLoader createFXMLLoader() {
3939
FXMLLoader pluginFxmlLoader = (FXMLLoader)
4040
pluginClassLoader.loadClass("javafx.fxml.FXMLLoader").newInstance();
4141

42+
pluginFxmlLoader.setClassLoader(pluginClassLoader);
43+
4244
// 必须去掉前缀的 "/" 才能正确读取
4345
String fxmlPathFixed = StringUtils.removeStart(pluginJarInfo.getFxmlPath(), "/");
4446

src/main/java/com/xwintop/xJavaFxTool/plugin/PluginLoader.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ public static Tab loadPluginAsTab(PluginJarInfo plugin, TabPane tabPane) {
8585
*/
8686
public static Tab loadIsolatedPluginAsTab(PluginJarInfo plugin, TabPane tabPane) {
8787
try {
88-
URL resource = PluginLoader.class.getResource(plugin.getFxmlPath());
89-
if (resource == null) {
90-
FxAlerts.error("加载插件失败", "无法读取资源文件 '" + plugin.getFxmlPath() + "'");
91-
return null;
92-
}
93-
9488
Tab tab = new Tab(plugin.getTitle());
9589

9690
if (StringUtils.isNotEmpty(plugin.getIconPath())) {
@@ -119,8 +113,8 @@ public static Tab loadIsolatedPluginAsTab(PluginJarInfo plugin, TabPane tabPane)
119113
JavaFxViewUtil.setControllerOnCloseRequest(controller, event);
120114
PluginContainer container = containerRef.get();
121115
if (container != null) {
122-
log.info("插件关闭:" + pluginContainer.getPluginJarInfo().getName());
123-
pluginContainer.unload();
116+
log.info("插件关闭:" + container.getPluginJarInfo().getName());
117+
container.unload();
124118
}
125119
}
126120
);

src/main/java/com/xwintop/xJavaFxTool/plugin/PluginManager.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private void loadLocalPluginConfiguration() {
113113
}
114114

115115
/**
116-
* 解析本地插件文件(必须在这之前先加入 classpath)
116+
* 解析本地插件文件
117117
*/
118118
public void loadLocalPlugins() {
119119
this.pluginList.forEach(plugin -> {
@@ -167,23 +167,6 @@ private void addOrUpdatePlugin(PluginJarInfo pluginJarInfo, Consumer<PluginJarIn
167167

168168
////////////////////////////////////////////////////////////// 下载插件
169169

170-
public File downloadPlugin(PluginJarInfo pluginJarInfo) throws IOException {
171-
PluginJarInfo plugin = getPlugin(pluginJarInfo.getJarName());
172-
if (plugin == null) {
173-
throw new IllegalStateException("没有找到插件 " + pluginJarInfo.getJarName());
174-
}
175-
176-
File file = pluginJarInfo.getFile();
177-
HttpUtil.downloadFile(pluginJarInfo.getDownloadUrl(), file);
178-
179-
plugin.setIsDownload(true);
180-
plugin.setIsEnable(true);
181-
plugin.setLocalVersionNumber(plugin.getVersionNumber());
182-
this.saveToFile();
183-
184-
return file;
185-
}
186-
187170
public File downloadPlugin(
188171
PluginJarInfo pluginJarInfo, BiConsumer<Long, Long> onProgressUpdate
189172
) throws IOException {

src/main/java/com/xwintop/xJavaFxTool/plugin/PluginParser.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package com.xwintop.xJavaFxTool.plugin;
22

3+
import static org.apache.commons.lang.StringUtils.defaultString;
4+
35
import com.xwintop.xJavaFxTool.AppException;
46
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
57
import com.xwintop.xJavaFxTool.utils.Config;
6-
import org.dom4j.Document;
7-
import org.dom4j.DocumentException;
8-
import org.dom4j.Element;
9-
import org.dom4j.io.SAXReader;
10-
118
import java.io.File;
129
import java.io.IOException;
1310
import java.io.InputStream;
@@ -18,8 +15,10 @@
1815
import java.util.jar.JarEntry;
1916
import java.util.jar.JarFile;
2017
import java.util.stream.Collectors;
21-
22-
import static org.apache.commons.lang.StringUtils.defaultString;
18+
import org.dom4j.Document;
19+
import org.dom4j.DocumentException;
20+
import org.dom4j.Element;
21+
import org.dom4j.io.SAXReader;
2322

2423
/**
2524
* 用来解析插件文件中的 toolFxmlLoaderConfiguration.xml
@@ -32,6 +31,13 @@ public class PluginParser {
3231
* 解析插件文件,补完 pluginJarInfo 属性
3332
*/
3433
public static void parse(File pluginFile, PluginJarInfo pluginJarInfo) {
34+
parse(pluginFile, pluginJarInfo, ClassLoader.getSystemClassLoader());
35+
}
36+
37+
/**
38+
* 解析插件文件,补完 pluginJarInfo 属性
39+
*/
40+
public static void parse(File pluginFile, PluginJarInfo pluginJarInfo, ClassLoader classLoader) {
3541
try (JarFile jarFile = new JarFile(pluginFile)) {
3642

3743
JarEntry entry = jarFile.getJarEntry(ENTRY_NAME);
@@ -49,7 +55,8 @@ public static void parse(File pluginFile, PluginJarInfo pluginJarInfo) {
4955
}
5056

5157
String resourceBundleName = getChildNodeText(pluginElement, "resourceBundleName");
52-
ResourceBundle pluginResourceBundle = ResourceBundle.getBundle(resourceBundleName, Config.defaultLocale);
58+
ResourceBundle pluginResourceBundle =
59+
ResourceBundle.getBundle(resourceBundleName, Config.defaultLocale, classLoader);
5360

5461
String menuId = getChildNodeText(pluginElement, "menuParentId");
5562
String url = getChildNodeText(pluginElement, "url");

0 commit comments

Comments
 (0)