Browse Source

REPORT-51958 远程环境检测及同步

feature/10.0
pengda 4 years ago
parent
commit
cd749ed515
  1. 172
      designer-base/src/main/java/com/fr/design/VersionCheckUtils.java
  2. 8
      designer-base/src/main/java/com/fr/env/CheckServiceDialog.java

172
designer-base/src/main/java/com/fr/design/VersionCheckUtils.java

@ -1,37 +1,28 @@
package com.fr.design;
import com.fr.base.FRContext;
import com.fr.decision.update.info.UpdateCallBack;
import com.fr.decision.update.info.UpdateProcessBean;
import com.fr.decision.update.util.UpdateFileUtils;
import com.fr.design.env.DesignerWorkspaceInfo;
import com.fr.design.env.DesignerWorkspaceType;
import com.fr.design.env.RemoteWorkspace;
import com.fr.design.i18n.Toolkit;
import com.fr.design.mainframe.DesignerContext;
import com.fr.env.VersionCheckMessageDialog;
import com.fr.general.CloudCenter;
import com.fr.general.CommonIOUtils;
import com.fr.general.ComparatorUtils;
import com.fr.general.GeneralUtils;
import com.fr.general.IOUtils;
import com.fr.general.http.HttpToolbox;
import com.fr.invoke.Reflect;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import com.fr.locale.InterProviderFactory;
import com.fr.log.FineLoggerFactory;
import com.fr.plugin.context.PluginContext;
import com.fr.plugin.context.PluginMarker;
import com.fr.plugin.manage.PluginManager;
import com.fr.plugin.manage.control.PluginControllerHelper;
import com.fr.plugin.manage.control.PluginTaskCallback;
import com.fr.plugin.manage.control.PluginTaskResult;
import com.fr.plugin.manage.control.ProgressCallback;
import com.fr.rpc.Result;
import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils;
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse;
import com.fr.third.org.apache.http.client.methods.HttpGet;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.workspace.WorkContext;
import com.fr.workspace.base.WorkspaceAPI;
import com.fr.workspace.connect.WorkspaceConnectionInfo;
@ -39,13 +30,7 @@ import com.fr.workspace.engine.base.FineObjectPool;
import com.fr.workspace.engine.channel.http.FunctionalHttpRequest;
import com.fr.workspace.engine.exception.WorkspaceConnectionException;
import com.fr.workspace.engine.rpc.WorkspaceProxyPool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.ProtocolException;
import java.net.URI;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -64,9 +49,6 @@ import java.util.concurrent.TimeUnit;
* Created on 2021-06-02
*/
public class VersionCheckUtils {
private static final String SYNCPLUGINLIB = StableUtils.getInstallHome() + "/logs/syncLib/plugins/";
private static final String SYNCLIB = StableUtils.getInstallHome() + "/logs/syncLib/";
private static final String ZIP = ".zip";
public static final String INCONSISTENCY = "inconsistency";
public static final String MISSING = "missing";
private static final String ID = "id";
@ -254,65 +236,68 @@ public class VersionCheckUtils {
return differentPlugins;
}
public static JSONArray syncPlugins(JSONArray differentPlugins, UpdateCallBack callBack) {
public static JSONArray syncPlugins(JSONArray differentPlugins) {
Set<String> uninstallFailed = uninstallPlugins(differentPlugins);
Map<String, String> downloadURL = getPluginsDownloadURL(differentPlugins, uninstallFailed);
downloadPlugins(downloadURL, callBack);
return installPlugins(differentPlugins);
List<PluginMarker> plugins = getSyncPlugins(differentPlugins, uninstallFailed);
installPlugins(plugins);
return getPluginsSyncFailed(differentPlugins);
}
private static List<PluginMarker> getSyncPlugins(JSONArray differentPlugins, Set<String> uninstallFailed) {
JSONObject differentPlugin;
String id;
String version;
List<PluginMarker> plugins = new ArrayList<>();
if (differentPlugins != null) {
for (int i = 0; i < differentPlugins.size(); i++) {
differentPlugin = differentPlugins.getJSONObject(i);
id = differentPlugin.getString(ID);
if (uninstallFailed.contains(id)) {
continue;
}
version = differentPlugin.getString(VERSION);
plugins.add(PluginMarker.create(id, version));
}
}
return plugins;
}
private static Set<String> uninstallPlugins(JSONArray differentPlugins) {
Set<PluginMarker> pluginsNeedUninstall = new HashSet<>();
Map<String, PluginContext> localPluginsMap = new HashMap<>();
List<PluginContext> localPlugins = PluginManager.getContexts();
for (PluginContext pluginContext : localPlugins) {
localPluginsMap.put(pluginContext.getID(), pluginContext);
}
Set<String> uninstallFailedID = new HashSet<>();
for (int i = 0; i < differentPlugins.size(); i++) {
String id = differentPlugins.getJSONObject(i).getString(ID);
if (localPluginsMap.containsKey(id)) {
PluginManager.getController().uninstall(localPluginsMap.get(id).getMarker(), true, new PluginTaskCallback() {
@Override
public void done(PluginTaskResult result) {
if (!result.isSuccess()) {
FineLoggerFactory.getLogger().error("uninstall plugin:" + id + " failed");
uninstallFailedID.add(id);
pluginsNeedUninstall.add(localPluginsMap.get(id).getMarker());
}
}
});
}
}
return uninstallFailedID;
}
private static JSONArray installPlugins(JSONArray differentPlugins) {
File file = new File(SYNCPLUGINLIB);
JSONArray syncFailedPlugins = JSONArray.create();
if (file.isDirectory()) {
File[] plugins = file.listFiles();
if (plugins != null && plugins.length > 0) {
CountDownLatch latch = new CountDownLatch(plugins.length);
for (File plugin : plugins) {
if (plugin.getName().endsWith(ZIP)) {
PluginManager.getController().install(plugin, new ProgressCallback() {
@Override
public void updateProgress(String description, double progress) {
}
Set<String> uninstallFailedID = new HashSet<>();
CountDownLatch latch = new CountDownLatch(pluginsNeedUninstall.size());
for (PluginMarker pluginMarker : pluginsNeedUninstall) {
PluginManager.getController().uninstall(pluginMarker, true, new PluginTaskCallback() {
@Override
public void done(PluginTaskResult result) {
latch.countDown();
if (!result.isSuccess()) {
uninstallFailedID.add(pluginMarker.getPluginID());
}
});
}
});
}
try {
latch.await(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
return uninstallFailedID;
}
}
private static JSONArray getPluginsSyncFailed(JSONArray differentPlugins){
JSONArray pluginsNeedSync = JSONArray.create();
List<PluginContext> localPlugins = PluginManager.getContexts();
Map<String, String> localPluginsInfo = new HashMap<>();
for (int i = 0; i < localPlugins.size(); i++) {
@ -324,80 +309,31 @@ public class VersionCheckUtils {
if (localPluginsInfo.containsKey(id) && ComparatorUtils.equals(plugin.getString(VERSION), localPluginsInfo.get(id))) {
continue;
}
syncFailedPlugins.add(plugin);
pluginsNeedSync.add(plugin);
}
CommonIOUtils.deleteFile(new File(SYNCLIB));
return syncFailedPlugins;
return pluginsNeedSync;
}
private static void downloadPlugins(Map<String, String> urls, UpdateCallBack callBack) {
CommonIOUtils.deleteFile(new File(SYNCLIB));
UpdateProcessBean bean = new UpdateProcessBean();
private static void installPlugins(List<PluginMarker> plugins) {
CountDownLatch latch = new CountDownLatch(plugins.size());
for (int i = 0; i < plugins.size(); i++) {
PluginControllerHelper.installOnline(plugins.get(i), new ProgressCallback() {
@Override
public void updateProgress(String description, double progress) {
for (String idAndVersion : urls.keySet()) {
try {
String url = urls.get(idAndVersion);
if (null == url) {
continue;
}
String target = SYNCPLUGINLIB + idAndVersion + ZIP;
UpdateFileUtils.downloadFile(url, target, callBack, bean,idAndVersion);
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
}
private static Map<String, String> getPluginsDownloadURL(JSONArray differentPlugins, Set<String> uninstallFailed) {
Map<String, String> downloadURL = new HashMap<>();
JSONObject differentPlugin;
String id;
String version;
if (differentPlugins != null) {
for (int i = 0; i < differentPlugins.size(); i++) {
String url = null;
differentPlugin = differentPlugins.getJSONObject(i);
id = differentPlugin.getString(ID);
if (uninstallFailed.contains(id)) {
continue;
}
version = differentPlugin.getString(VERSION);
try {
url = StringUtils.messageFormat(CloudCenter.getInstance().acquireUrlByKind("sync10.plugin"), id, version);
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
if (url != null) {
downloadURL.put(id + "-" + version, url);
}
}
@Override
public void done(PluginTaskResult result) {
latch.countDown();
}
return getPluginZipDownloadURL(downloadURL);
});
}
private static Map<String, String> getPluginZipDownloadURL(Map<String, String> urls) {
CloseableHttpClient httpClient;
CloseableHttpResponse httpResponse;
HttpGet httpGet = new HttpGet();
InputStream in = null;
HashMap<String, String> downloadURLs = new HashMap<>();
try {
for (String idAndVersion : urls.keySet()) {
httpClient = HttpToolbox.getHttpClient(urls.get(idAndVersion));
httpGet.setURI(URI.create(urls.get(idAndVersion)));
httpResponse = httpClient.execute(httpGet);
in = httpResponse.getEntity().getContent();
JSONObject urlJson = new JSONObject(IOUtils.inputStream2String(in));
String downloadURL = urlJson.getString("result");
if (downloadURL != null) {
downloadURLs.put(idAndVersion, downloadURL);
}
}
} catch (Exception e) {
latch.await();
} catch (InterruptedException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
} finally {
CommonIOUtils.close(in);
}
return downloadURLs;
}
}

8
designer-base/src/main/java/com/fr/env/CheckServiceDialog.java vendored

@ -331,11 +331,13 @@ public class CheckServiceDialog extends JDialog implements ActionListener {
public void onDownloadSuccess() {
deleteForDesignerUpdate(installLib);
//主jar下载成功之后再卸载安装插件
progressBar.setString(Toolkit.i18nText("Fine-Design_Basic_Sync_Plugins"));
progressBar.setValue(0);
new SwingWorker<JSONArray, Void>() {
@Override
protected JSONArray doInBackground() {
return VersionCheckUtils.syncPlugins(differentPlugins, callBack);
return VersionCheckUtils.syncPlugins(differentPlugins);
}
@Override
@ -367,11 +369,13 @@ public class CheckServiceDialog extends JDialog implements ActionListener {
}.execute();
} else {
//到这边说明主jar是一致的,就只尝试同步插件
progressBar.setString(Toolkit.i18nText("Fine-Design_Basic_Sync_Plugins"));
progressBar.setValue(0);
new SwingWorker<JSONArray, Void>() {
@Override
protected JSONArray doInBackground() {
return VersionCheckUtils.syncPlugins(differentPlugins, callBack);
return VersionCheckUtils.syncPlugins(differentPlugins);
}
@Override

Loading…
Cancel
Save