From cd749ed515ca896183f7ac767b5ecb82fc9c1ac5 Mon Sep 17 00:00:00 2001 From: pengda Date: Fri, 4 Jun 2021 16:52:08 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-51958=20=E8=BF=9C=E7=A8=8B=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E6=A3=80=E6=B5=8B=E5=8F=8A=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fr/design/VersionCheckUtils.java | 188 ++++++------------ .../java/com/fr/env/CheckServiceDialog.java | 8 +- 2 files changed, 68 insertions(+), 128 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/VersionCheckUtils.java b/designer-base/src/main/java/com/fr/design/VersionCheckUtils.java index c3d8d94e22..17dcd51a13 100644 --- a/designer-base/src/main/java/com/fr/design/VersionCheckUtils.java +++ b/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 uninstallFailed = uninstallPlugins(differentPlugins); - Map downloadURL = getPluginsDownloadURL(differentPlugins, uninstallFailed); - downloadPlugins(downloadURL, callBack); - return installPlugins(differentPlugins); + List plugins = getSyncPlugins(differentPlugins, uninstallFailed); + installPlugins(plugins); + return getPluginsSyncFailed(differentPlugins); + } + + private static List getSyncPlugins(JSONArray differentPlugins, Set uninstallFailed) { + JSONObject differentPlugin; + String id; + String version; + List 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 uninstallPlugins(JSONArray differentPlugins) { + Set pluginsNeedUninstall = new HashSet<>(); Map localPluginsMap = new HashMap<>(); List localPlugins = PluginManager.getContexts(); for (PluginContext pluginContext : localPlugins) { localPluginsMap.put(pluginContext.getID(), pluginContext); } - Set 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) { - } - - @Override - public void done(PluginTaskResult result) { - latch.countDown(); - } - }); + Set 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) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - } + }); + } + try { + latch.await(5000, TimeUnit.MILLISECONDS); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); } + return uninstallFailedID; + } + + private static JSONArray getPluginsSyncFailed(JSONArray differentPlugins){ + JSONArray pluginsNeedSync = JSONArray.create(); List localPlugins = PluginManager.getContexts(); Map 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 urls, UpdateCallBack callBack) { - CommonIOUtils.deleteFile(new File(SYNCLIB)); - UpdateProcessBean bean = new UpdateProcessBean(); + private static void installPlugins(List 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 getPluginsDownloadURL(JSONArray differentPlugins, Set uninstallFailed) { - Map 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 getPluginZipDownloadURL(Map urls) { - CloseableHttpClient httpClient; - CloseableHttpResponse httpResponse; - HttpGet httpGet = new HttpGet(); - InputStream in = null; - HashMap 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; + } } diff --git a/designer-base/src/main/java/com/fr/env/CheckServiceDialog.java b/designer-base/src/main/java/com/fr/env/CheckServiceDialog.java index fc991eddd5..b64708659a 100644 --- a/designer-base/src/main/java/com/fr/env/CheckServiceDialog.java +++ b/designer-base/src/main/java/com/fr/env/CheckServiceDialog.java @@ -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() { @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() { @Override protected JSONArray doInBackground() { - return VersionCheckUtils.syncPlugins(differentPlugins, callBack); + return VersionCheckUtils.syncPlugins(differentPlugins); } @Override