Browse Source

REPORT-5357 插件逻辑中的代码质量(关于使用Executor和线程池)

master
kerry 7 years ago
parent
commit
8077974e74
  1. 144
      designer_base/src/com/fr/design/extra/PluginOperateUtils.java
  2. 20
      designer_base/src/com/fr/design/extra/PluginTask.java
  3. 42
      designer_base/src/com/fr/design/extra/PluginWebBridge.java
  4. 42
      designer_base/src/com/fr/design/extra/exe/GetPluginCategoriesExecutor.java
  5. 75
      designer_base/src/com/fr/design/extra/exe/GetPluginFromStoreExecutor.java
  6. 51
      designer_base/src/com/fr/design/extra/exe/ReadUpdateOnlineExecutor.java
  7. 59
      designer_base/src/com/fr/design/extra/exe/SearchOnlineExecutor.java
  8. 35
      designer_base/src/com/fr/design/extra/exe/getPluginPrefixExecutor.java

144
designer_base/src/com/fr/design/extra/PluginOperateUtils.java

@ -2,7 +2,13 @@ package com.fr.design.extra;
import com.fr.base.ConfigManager;
import com.fr.base.FRContext;
import com.fr.design.extra.exe.callback.*;
import com.fr.design.extra.exe.callback.InstallFromDiskCallback;
import com.fr.design.extra.exe.callback.InstallOnlineCallback;
import com.fr.design.extra.exe.callback.JSCallback;
import com.fr.design.extra.exe.callback.ModifyStatusCallback;
import com.fr.design.extra.exe.callback.UninstallPluginCallback;
import com.fr.design.extra.exe.callback.UpdateFromDiskCallback;
import com.fr.design.extra.exe.callback.UpdateOnlineCallback;
import com.fr.design.gui.ilable.UILabel;
import com.fr.general.FRLogger;
import com.fr.general.Inter;
@ -20,7 +26,8 @@ import com.fr.plugin.manage.control.PluginTaskResult;
import com.fr.plugin.view.PluginView;
import com.fr.stable.StringUtils;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import java.io.File;
import java.util.List;
@ -87,11 +94,11 @@ public class PluginOperateUtils {
@Override
public void run() {
int rv = JOptionPane.showConfirmDialog(
null,
Inter.getLocText("FR-Plugin_Delete_Confirmed"),
Inter.getLocText("FR-Designer-Plugin_Warning"),
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE
null,
Inter.getLocText("FR-Plugin_Delete_Confirmed"),
Inter.getLocText("FR-Designer-Plugin_Warning"),
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE
);
if (rv == JOptionPane.OK_OPTION) {
PluginMarker pluginMarker = PluginUtils.createPluginMarker(pluginInfo);
@ -101,104 +108,18 @@ public class PluginOperateUtils {
});
}
public static void readUpdateOnline(final JSCallback jsCallback) {
new Thread(new Runnable() {
@Override
public void run() {
try {
List<PluginView> plugins = PluginsReaderFromStore.readPluginsForUpdate();
JSONArray jsonArray = new JSONArray();
for (PluginView plugin : plugins) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("pluginid", plugin.getID());
jsonArray.put(jsonObject);
}
String result = jsonArray.toString();
jsCallback.execute(result);
} catch (Exception e) {
FRLogger.getLogger().error(e.getMessage());
}
}
}).start();
}
public static void searchPlugin(final String keyword, final JSCallback jsCallback) {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (StringUtils.isBlank(keyword)) {
getRecommendPlugins(jsCallback);
return;
}
HttpClient httpClient = new HttpClient(SiteCenter.getInstance().acquireUrlByKind("shop.plugin.store") + "&keyword=" + keyword);
httpClient.asGet();
String result = httpClient.getResponseText();
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("result");
JSONArray resultJSONArray = PluginUtils.filterPluginsFromVersion(jsonArray);
jsCallback.execute(resultJSONArray.toString());
} catch (Exception e) {
FRLogger.getLogger().error(e.getMessage());
}
}
}).start();
}
public static void getPluginFromStore(final String category, final String seller, final String fee, final JSCallback jsCallback) {
new Thread(new Runnable() {
@Override
public void run() {
String plistUrl = SiteCenter.getInstance().acquireUrlByKind("shop.plugin.plist") + "?";
boolean getRecommend = StringUtils.isEmpty(category) && StringUtils.isEmpty(seller) && StringUtils.isEmpty(fee);
if (getRecommend) {
getRecommendPlugins(jsCallback);
return;
}
if (StringUtils.isNotBlank(plistUrl)) {
StringBuilder url = new StringBuilder();
url.append(plistUrl);
dealParams(url, category, seller, fee);
try {
HttpClient httpClient = new HttpClient(url.toString());
httpClient.asGet();
String result = httpClient.getResponseText();
JSONObject resultJSONObject = new JSONObject(result);
JSONArray resultArr = resultJSONObject.getJSONArray("result");
JSONArray resultJSONArray = PluginUtils.filterPluginsFromVersion(resultArr);
jsCallback.execute(resultJSONArray.toString());
} catch (Exception e) {
FRLogger.getLogger().error(e.getMessage());
}
} else {
String result = PluginConstants.CONNECTION_404;
jsCallback.execute(result);
}
}
}).start();
}
public static void getRecommendPlugins(JSCallback jsCallback) {
public static String getRecommendPlugins() {
String plistUrl = SiteCenter.getInstance().acquireUrlByKind("shop.plugin.feature");
JSONArray resultArray = JSONArray.create();
try {
HttpClient httpClient = new HttpClient(plistUrl.toString());
String result = httpClient.getResponseText();
JSONArray jsonArray = new JSONArray(result);
JSONArray resultJSONArray = PluginUtils.filterPluginsFromVersion(jsonArray);
jsCallback.execute(resultJSONArray.toString());
resultArray = PluginUtils.filterPluginsFromVersion(jsonArray);
} catch (Exception e) {
FRLogger.getLogger().error(e.getMessage());
}
return resultArray.toString();
}
public static void dealParams(StringBuilder url, String category, String seller, String fee) {
@ -233,35 +154,6 @@ public class PluginOperateUtils {
}
}
public static void getPluginCategories(final JSCallback jsCallback) {
new Thread(new Runnable() {
@Override
public void run() {
String result;
String url = SiteCenter.getInstance().acquireUrlByKind("shop.plugin.category");
if (url != null) {
HttpClient httpClient = new HttpClient(url);
result = httpClient.getResponseText();
} else {
result = PluginConstants.CONNECTION_404;
}
jsCallback.execute(result);
}
}).start();
}
public static void getPluginPrefix(final JSCallback jsCallback) {
new Thread(new Runnable() {
@Override
public void run() {
String result = SiteCenter.getInstance().acquireUrlByKind("plugin.url.prefix");
jsCallback.execute(result);
}
}).start();
}
public static void getLoginInfo(JSCallback jsCallback, UILabel uiLabel) {
String username = ConfigManager.getProviderInstance().getBbsUsername();
if (StringUtils.isEmpty(username)) {

20
designer_base/src/com/fr/design/extra/PluginTask.java

@ -9,6 +9,9 @@ import javafx.concurrent.Task;
import javafx.scene.web.WebEngine;
import netscape.javascript.JSObject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 插件安装,卸载,更新等任务
*
@ -77,8 +80,23 @@ public class PluginTask<T> extends Task<T> {
*/
private String trimText(String old) {
if (StringUtils.isNotBlank(old)) {
return old.replaceAll("\n", "").replaceAll("\"", "\\\\\"").replaceAll("\'", "\\\\\'");
String b = filterHtmlTag(old);
return b.replaceAll("\\\\n", "").replaceAll("\\\\t", "").replaceAll("\"", "\\\\\"").replaceAll("\'", "\\\\\'").replaceAll("\\\\\\\\", "\\\\\\\\\\\\");
}
return StringUtils.EMPTY;
}
/**
* 进行html标签过滤
* @param origin 原始字符串
* @return 处理之后的字符串
*/
private String filterHtmlTag(String origin) {
String regEx_html = "<[^>]+>";
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(origin);
origin = m_html.replaceAll("");
return origin;
}
}

42
designer_base/src/com/fr/design/extra/PluginWebBridge.java

@ -5,8 +5,13 @@ import com.fr.base.FRContext;
import com.fr.design.RestartHelper;
import com.fr.design.bbs.BBSLoginUtils;
import com.fr.design.dialog.UIDialog;
import com.fr.design.extra.exe.GetPluginCategoriesExecutor;
import com.fr.design.extra.exe.GetPluginFromStoreExecutor;
import com.fr.design.extra.exe.PluginLoginExecutor;
import com.fr.design.extra.exe.ReadUpdateOnlineExecutor;
import com.fr.design.extra.exe.SearchOnlineExecutor;
import com.fr.design.extra.exe.callback.JSCallback;
import com.fr.design.extra.exe.getPluginPrefixExecutor;
import com.fr.design.gui.ilable.UILabel;
import com.fr.general.FRLogger;
import com.fr.general.Inter;
@ -23,14 +28,20 @@ import javafx.scene.web.WebEngine;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import javax.swing.*;
import java.awt.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 开放给Web组件的接口,用于安装,卸载,更新以及更改插件可用状态
@ -48,6 +59,7 @@ public class PluginWebBridge {
private UILabel uiLabel;
private ExecutorService threadPoolExecutor = Executors.newSingleThreadExecutor();
/**
* 动作枚举
@ -222,8 +234,8 @@ public class PluginWebBridge {
* 已安装插件检查更新
*/
public void readUpdateOnline(final JSObject callback) {
JSCallback jsCallback = new JSCallback(webEngine, callback);
PluginOperateUtils.readUpdateOnline(jsCallback);
Task<Void> task = new PluginTask<>(webEngine, callback, new ReadUpdateOnlineExecutor());
threadPoolExecutor.submit(task);
}
/**
@ -306,8 +318,8 @@ public class PluginWebBridge {
* @param keyword 关键字
*/
public void searchPlugin(String keyword, final JSObject callback) {
JSCallback jsCallback = new JSCallback(webEngine, callback);
PluginOperateUtils.searchPlugin(keyword, jsCallback);
Task<Void> task = new PluginTask<>(webEngine, callback, new SearchOnlineExecutor(keyword));
threadPoolExecutor.submit(task);
}
/**
@ -319,14 +331,14 @@ public class PluginWebBridge {
* @param callback 回调函数
*/
public void getPluginFromStore(String category, String seller, String fee, final JSObject callback) {
JSCallback jsCallback = new JSCallback(webEngine, callback);
PluginOperateUtils.getPluginFromStore(category, seller, fee, jsCallback);
Task<Void> task = new PluginTask<>(webEngine, callback, new GetPluginFromStoreExecutor(category, seller, fee));
threadPoolExecutor.submit(task);
}
public void getPluginPrefix(final JSObject callback) {
JSCallback jsCallback = new JSCallback(webEngine, callback);
PluginOperateUtils.getPluginPrefix(jsCallback);
Task<Void> task = new PluginTask<>(webEngine, callback, new getPluginPrefixExecutor());
threadPoolExecutor.submit(task);
}
@ -336,8 +348,8 @@ public class PluginWebBridge {
* @param callback 回调函数
*/
public void getPluginCategories(final JSObject callback) {
JSCallback jsCallback = new JSCallback(webEngine, callback);
PluginOperateUtils.getPluginCategories(jsCallback);
Task<Void> task = new PluginTask<>(webEngine, callback, new GetPluginCategoriesExecutor());
threadPoolExecutor.submit(task);
}
/**
@ -502,7 +514,7 @@ public class PluginWebBridge {
*/
public void defaultLogin(String username, String password, final JSObject callback) {
Task<Void> task = new PluginTask<>(webEngine, callback, new PluginLoginExecutor(username, password));
new Thread(task).start();
threadPoolExecutor.submit(task);
}
/**

42
designer_base/src/com/fr/design/extra/exe/GetPluginCategoriesExecutor.java

@ -0,0 +1,42 @@
package com.fr.design.extra.exe;
import com.fr.design.extra.PluginConstants;
import com.fr.design.extra.Process;
import com.fr.general.SiteCenter;
import com.fr.general.http.HttpClient;
/**
* Created by vito on 16/5/16.
*/
public class GetPluginCategoriesExecutor implements Executor {
private String result = "[]";
@Override
public String getTaskFinishMessage() {
return result;
}
@Override
public Command[] getCommands() {
return new Command[]{
new Command() {
@Override
public String getExecuteMessage() {
return null;
}
@Override
public void run(Process<String> process) {
String url = SiteCenter.getInstance().acquireUrlByKind("shop.plugin.category");
if (url != null) {
HttpClient httpClient = new HttpClient(url);
result = httpClient.getResponseText();
} else {
result = PluginConstants.CONNECTION_404;
}
}
}
};
}
}

75
designer_base/src/com/fr/design/extra/exe/GetPluginFromStoreExecutor.java

@ -0,0 +1,75 @@
package com.fr.design.extra.exe;
import com.fr.design.extra.PluginConstants;
import com.fr.design.extra.PluginOperateUtils;
import com.fr.design.extra.PluginUtils;
import com.fr.design.extra.Process;
import com.fr.general.FRLogger;
import com.fr.general.SiteCenter;
import com.fr.general.http.HttpClient;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import com.fr.stable.StringUtils;
/**
* Created by vito on 16/4/18.
* 获取插件分类信息
*/
public class GetPluginFromStoreExecutor implements Executor {
private String result = "[]";
private String category;
private String seller;
private String fee;
public GetPluginFromStoreExecutor(String category, String seller, String fee) {
this.category = category;
this.seller = seller;
this.fee = fee;
}
@Override
public String getTaskFinishMessage() {
return result;
}
@Override
public Command[] getCommands() {
return new Command[]{
new Command() {
@Override
public String getExecuteMessage() {
return StringUtils.EMPTY;
}
@Override
public void run(Process<String> process) {
String plistUrl = SiteCenter.getInstance().acquireUrlByKind("shop.plugin.plist") + "?";
boolean getRecommend = StringUtils.isEmpty(category) && StringUtils.isEmpty(seller) && StringUtils.isEmpty(fee);
if (getRecommend) {
result = PluginOperateUtils.getRecommendPlugins();
return;
}
if (StringUtils.isNotBlank(plistUrl)) {
StringBuilder url = new StringBuilder();
url.append(plistUrl);
PluginOperateUtils.dealParams(url, category, seller, fee);
try {
HttpClient httpClient = new HttpClient(url.toString());
httpClient.asGet();
String responseText = httpClient.getResponseText();
JSONObject resultJSONObject = new JSONObject(responseText);
JSONArray resultArr = resultJSONObject.getJSONArray("result");
JSONArray resultJSONArray = PluginUtils.filterPluginsFromVersion(resultArr);
result = resultJSONArray.toString();
} catch (Exception e) {
FRLogger.getLogger().error(e.getMessage());
}
} else {
result = PluginConstants.CONNECTION_404;
}
}
}
};
}
}

51
designer_base/src/com/fr/design/extra/exe/ReadUpdateOnlineExecutor.java

@ -0,0 +1,51 @@
package com.fr.design.extra.exe;
import com.fr.design.extra.PluginsReaderFromStore;
import com.fr.design.extra.Process;
import com.fr.general.FRLogger;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import com.fr.plugin.view.PluginView;
import com.fr.stable.StringUtils;
import java.util.List;
/**
* Created by vito on 16/4/19.
*/
public class ReadUpdateOnlineExecutor implements Executor {
private String result = StringUtils.EMPTY;
@Override
public String getTaskFinishMessage() {
return result;
}
@Override
public Command[] getCommands() {
return new Command[]{
new Command() {
@Override
public String getExecuteMessage() {
return StringUtils.EMPTY;
}
@Override
public void run(Process<String> process) {
try {
List<PluginView> plugins = PluginsReaderFromStore.readPluginsForUpdate();
JSONArray jsonArray = new JSONArray();
for (PluginView plugin : plugins) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("pluginid", plugin.getID());
jsonArray.put(jsonObject);
}
result = jsonArray.toString();
} catch (Exception e) {
FRLogger.getLogger().error(e.getMessage());
}
}
}
};
}
}

59
designer_base/src/com/fr/design/extra/exe/SearchOnlineExecutor.java

@ -0,0 +1,59 @@
package com.fr.design.extra.exe;
import com.fr.design.extra.PluginOperateUtils;
import com.fr.design.extra.PluginUtils;
import com.fr.design.extra.Process;
import com.fr.general.FRLogger;
import com.fr.general.SiteCenter;
import com.fr.general.http.HttpClient;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import com.fr.stable.StringUtils;
/**
* Created by vito on 16/4/18.
*/
public class SearchOnlineExecutor implements Executor {
private String result = StringUtils.EMPTY;
private String keyword;
public SearchOnlineExecutor(String keyword) {
this.keyword = keyword;
}
@Override
public String getTaskFinishMessage() {
return result;
}
@Override
public Command[] getCommands() {
return new Command[]{
new Command() {
@Override
public String getExecuteMessage() {
return StringUtils.EMPTY;
}
@Override
public void run(Process<String> process) {
try {
if (StringUtils.isBlank(keyword)) {
result = PluginOperateUtils.getRecommendPlugins();
return;
}
HttpClient httpClient = new HttpClient(SiteCenter.getInstance().acquireUrlByKind("shop.plugin.store") + "&keyword=" + keyword);
httpClient.asGet();
String responseText = httpClient.getResponseText();
JSONObject jsonObject = new JSONObject(responseText);
JSONArray jsonArray = jsonObject.getJSONArray("result");
JSONArray resultJSONArray = PluginUtils.filterPluginsFromVersion(jsonArray);
result = resultJSONArray.toString();
} catch (Exception e) {
FRLogger.getLogger().error(e.getMessage());
}
}
}
};
}
}

35
designer_base/src/com/fr/design/extra/exe/getPluginPrefixExecutor.java

@ -0,0 +1,35 @@
package com.fr.design.extra.exe;
import com.fr.design.extra.Process;
import com.fr.general.SiteCenter;
import com.fr.stable.StringUtils;
/**
* Created by kerry on 2017/11/3.
*/
public class getPluginPrefixExecutor implements Executor {
private String result = StringUtils.EMPTY;
@Override
public String getTaskFinishMessage() {
return result;
}
@Override
public Command[] getCommands() {
return new Command[]{
new Command() {
@Override
public String getExecuteMessage() {
return null;
}
@Override
public void run(Process<String> process) {
result = SiteCenter.getInstance().acquireUrlByKind("plugin.url.prefix");
}
}
};
}
}
Loading…
Cancel
Save