From 6d284675870b4ad042c5ad658c7bac5d5aae15fb Mon Sep 17 00:00:00 2001 From: Bryant Date: Mon, 16 Sep 2019 09:19:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?KERNEL-418=20=E5=9C=A8=E7=BA=BF=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=8D=87=E7=BA=A7=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../design/update/actions/FileDownloader.java | 118 ----------- .../fr/design/update/actions/FileProcess.java | 55 +++++ .../fr/design/update/domain/DownloadItem.java | 102 --------- .../update/ui/dialog/UpdateMainDialog.java | 195 +----------------- 4 files changed, 62 insertions(+), 408 deletions(-) delete mode 100644 designer-base/src/main/java/com/fr/design/update/actions/FileDownloader.java create mode 100644 designer-base/src/main/java/com/fr/design/update/actions/FileProcess.java delete mode 100644 designer-base/src/main/java/com/fr/design/update/domain/DownloadItem.java diff --git a/designer-base/src/main/java/com/fr/design/update/actions/FileDownloader.java b/designer-base/src/main/java/com/fr/design/update/actions/FileDownloader.java deleted file mode 100644 index 328da27a0..000000000 --- a/designer-base/src/main/java/com/fr/design/update/actions/FileDownloader.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.fr.design.update.actions; - -import com.fr.design.update.domain.UpdateConstants; -import com.fr.locale.InterProviderFactory; -import com.fr.log.FineLoggerFactory; -import com.fr.design.update.domain.DownloadItem; -import com.fr.stable.ArrayUtils; -import com.fr.stable.StableUtils; - -import javax.swing.JOptionPane; -import javax.swing.SwingWorker; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; -import java.util.concurrent.ExecutionException; - -/** - * Created by XINZAI on 2018/8/21. - */ -public abstract class FileDownloader extends SwingWorker { - private static final int REPEAT_DOWNLOAD_TIMES = 3; - private DownloadItem[] files; - private String saveDir; - //已经完成的大小 - private long completeSize; - - public FileDownloader(DownloadItem[] files, String saveDir) { - this.files = files; - this.saveDir = saveDir; - } - - @Override - protected Boolean doInBackground() throws Exception { - if (ArrayUtils.isNotEmpty(files)) { - setCompleteSize(0L); - for (DownloadItem item : files) { - for (int i = 0; i < REPEAT_DOWNLOAD_TIMES; i++) { - item.setTotalLength(0); - item.setDownloadLength(0); - download(item); - if (item.getTotalLength() == item.getDownloadLength()) { - break; - } - } - if (item.getTotalLength() != item.getDownloadLength()) { - JOptionPane.showMessageDialog(null, - com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Updater_Download_Failed"), - InterProviderFactory.getProvider().getLocText("Fine-Design_Updater_Alert"), JOptionPane.ERROR_MESSAGE); - return false; - } else { - item.setDownloadLength(0); - completeSize += item.getTotalLength(); - } - } - } - return true; - } - - @Override - protected void done() { - boolean success = false; - try { - success = get(); - } catch (InterruptedException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - Thread.currentThread().interrupt(); - } catch (ExecutionException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - if (success) { - onDownloadSuccess(); - } else { - onDownloadFailed(); - } - } - - private void download(DownloadItem item) throws Exception { - URL url = new URL(item.getUrl()); - URLConnection connection = url.openConnection(); - int total = connection.getContentLength(); - item.setTotalLength(total); - File tempFile = new File(StableUtils.pathJoin(saveDir, item.getName())); - StableUtils.makesureFileExist(tempFile); - try ( InputStream reader = connection.getInputStream(); - FileOutputStream writer = new FileOutputStream(tempFile)) { - byte[] buffer = new byte[UpdateConstants.BYTE]; - int bytesRead = 0; - int totalBytesRead = 0; - while ((bytesRead = reader.read(buffer)) != -1) { - writer.write(buffer, 0, bytesRead); - buffer = new byte[UpdateConstants.BYTE]; - totalBytesRead += bytesRead; - item.setDownloadLength(totalBytesRead); - publish(item); - } - } - } - - /** - * 下载成功 - */ - public abstract void onDownloadSuccess(); - - /** - * 下载失败 - */ - public abstract void onDownloadFailed(); - - public long getCompleteSize() { - return completeSize; - } - - public void setCompleteSize(long completeSize) { - this.completeSize = completeSize; - } -} \ No newline at end of file diff --git a/designer-base/src/main/java/com/fr/design/update/actions/FileProcess.java b/designer-base/src/main/java/com/fr/design/update/actions/FileProcess.java new file mode 100644 index 000000000..59194f86a --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/update/actions/FileProcess.java @@ -0,0 +1,55 @@ +package com.fr.design.update.actions; + +import com.fr.decision.update.Info.UpdateCallBack; +import com.fr.decision.update.UpdateExecutor; +import com.fr.log.FineLoggerFactory; + +import javax.swing.*; +import java.util.concurrent.ExecutionException; + +/** + * @author Bryant + * @version 10.0 + * Created by Bryant on 2019-09-12 + */ +public abstract class FileProcess extends SwingWorker { + + private UpdateCallBack callBack; + + public FileProcess(UpdateCallBack callBack) { + this.callBack = callBack; + } + + @Override + protected Boolean doInBackground() throws Exception { + return UpdateExecutor.getInstance().execute(callBack); + } + + @Override + protected void done() { + boolean success = false; + try { + success = get(); + } catch (InterruptedException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + if (success) { + onDownloadSuccess(); + } else { + onDownloadFailed(); + } + } + + /** + * 下载成功 + */ + public abstract void onDownloadSuccess(); + + /** + * 下载失败 + */ + public abstract void onDownloadFailed(); +} diff --git a/designer-base/src/main/java/com/fr/design/update/domain/DownloadItem.java b/designer-base/src/main/java/com/fr/design/update/domain/DownloadItem.java deleted file mode 100644 index 220f2602d..000000000 --- a/designer-base/src/main/java/com/fr/design/update/domain/DownloadItem.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.fr.design.update.domain; - -import com.fr.general.ComparatorUtils; -import com.fr.json.JSONObject; - -import java.util.Date; - -/** - * Created by XINZAI on 2018/8/21. - */ -public class DownloadItem { - - //显示为百分比 - private static final int PERCENTAGE_RATIO = 100; - //显示kB - private static final int BYTETOKB_RATIO = 1000; - - private String name; - private String url; - private long size; - - private int totalLength; - private int downloadLength; - - public DownloadItem(JSONObject json) { - this(json.optString("name"), json.optString("url"), json.optLong("size")); - } - - public DownloadItem(String name, String url, long size) { - this.name = name; - this.url = url; - this.size = size; - } - - public String getName() { - return name; - } - - public String getUrl() { - return url + "?v=" + new Date().getTime(); - } - - - public long getSize() { - return size; - } - - public void setSize(long size) { - this.size = size; - } - - public int getTotalLength() { - return totalLength; - } - - public int getDownloadLength() { - return downloadLength; - } - - public void setTotalLength(int totalLength) { - this.totalLength = totalLength; - } - - public void setDownloadLength(int downloadLength) { - this.downloadLength = downloadLength; - } - - public int getProgressValue() { - return (int) ((downloadLength / (double) totalLength) * PERCENTAGE_RATIO); - } - - public String getProgressString() { - return downloadLength / BYTETOKB_RATIO + "KB/" + totalLength / BYTETOKB_RATIO + "KB"; - } - - /** - * 转化为字符串 - * - * @return 字符串 - */ - @Override - public String toString() { - return "name:" + name + ";download:" + getProgressString(); - } - - @Override - public boolean equals(Object obj) { - return obj instanceof DownloadItem - && ComparatorUtils.equals(((DownloadItem) obj).name, name) - && ComparatorUtils.equals(((DownloadItem) obj).url, url); - } - - /** - * 返回一个hash码 - * - * @return hash码 - */ - @Override - public int hashCode() { - return name.hashCode(); - } -} \ No newline at end of file diff --git a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java index cdce0a657..bd3f0a1dd 100644 --- a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java +++ b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java @@ -1,5 +1,7 @@ package com.fr.design.update.ui.dialog; +import com.fr.decision.update.Info.UpdateCallBack; +import com.fr.decision.update.Info.UpdateProgressCallBack; import com.fr.design.RestartHelper; import com.fr.design.constants.LayoutConstants; import com.fr.design.dialog.UIDialog; @@ -10,8 +12,7 @@ import com.fr.design.gui.itextfield.UITextField; import com.fr.design.layout.TableLayout; import com.fr.design.layout.TableLayoutHelper; import com.fr.design.mainframe.DesignerContext; -import com.fr.design.update.actions.FileDownloader; -import com.fr.design.update.domain.DownloadItem; +import com.fr.design.update.actions.FileProcess; import com.fr.design.update.domain.UpdateConstants; import com.fr.design.update.domain.UpdateInfoCachePropertyManager; import com.fr.design.update.factory.DirectoryOperationFactory; @@ -27,7 +28,6 @@ import com.fr.general.ComparatorUtils; import com.fr.general.DateUtils; import com.fr.general.GeneralContext; import com.fr.general.GeneralUtils; -import com.fr.general.IOUtils; import com.fr.general.SiteCenter; import com.fr.general.http.HttpClient; import com.fr.general.http.HttpToolbox; @@ -39,18 +39,10 @@ import com.fr.stable.EncodeConstants; import com.fr.stable.ProductConstants; import com.fr.stable.StableUtils; import com.fr.stable.StringUtils; -import com.fr.stable.project.ProjectConstants; -import com.fr.third.org.apache.commons.codec.digest.DigestUtils; import com.fr.workspace.WorkContext; import com.sun.java.swing.plaf.motif.MotifProgressBarUI; -import javax.swing.BorderFactory; -import javax.swing.JPanel; -import javax.swing.JProgressBar; -import javax.swing.RowSorter; -import javax.swing.SortOrder; -import javax.swing.SwingConstants; -import javax.swing.SwingWorker; +import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.table.TableRowSorter; @@ -67,19 +59,14 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Locale; -import java.util.Map; -import java.util.Set; import java.util.concurrent.ExecutionException; /** @@ -107,7 +94,6 @@ public class UpdateMainDialog extends UIDialog { private static final SimpleDateFormat CHANGELOG_FORMAT = new SimpleDateFormat("M/d/y, h:m:s a", Locale.ENGLISH); private static final SimpleDateFormat UPDATE_INFO_TABLE_FORMAT = new SimpleDateFormat("yyyy.MM.dd"); - private Set downloadItems = new HashSet(); private JSONObject downloadFileConfig; //最新版本标签 private LoadingLabel loadingLabel; @@ -166,14 +152,12 @@ public class UpdateMainDialog extends UIDialog { public void setAutoUpdateAfterInit() { autoUpdateAfterInit = true; } - private void initUpdateActionPane() { double[] rowUpdateSubContentPaneSize = {UPDATE_CONTENT_PANE_ROW_SIZE, TableLayout.PREFERRED, UPDATE_CONTENT_PANE_ROW_SIZE}; double[] rowUpdateContentPaneSize = {TableLayout.PREFERRED}; double[] columnUpdateSubContentPaneProgressSize = {TableLayout.FILL, TableLayout.PREFERRED}; double[] columnUpdateSubContentPaneSize = {UPDATE_CONTENT_PANE_COLUMN_SIZE, TableLayout.FILL, TableLayout.PREFERRED}; JPanel progressBarPane = new JPanel(new BorderLayout()); - progressBar = new JProgressBar(); progressBar.setUI(new MotifProgressBarUI()); progressBar.setForeground(UpdateConstants.BAR_COLOR); @@ -601,62 +585,6 @@ public class UpdateMainDialog extends UIDialog { } break; } - - initMapWithInfo(downloadFileConfig); - } - - public void initMapWithInfo(JSONObject result) { - addJarNameToMap(result, "designer"); - addJarNameToMap(result, "server"); - } - - private void addJarNameToMap(JSONObject result, String category) { - JSONArray jsonArray = result.optJSONArray(category); - if (jsonArray != null) { - for (int i = 0, len = jsonArray.length(); i < len; i++) { - JSONObject jo = jsonArray.optJSONObject(i); - String downloadName = jo.optString("name"); - String downloadUrl = jo.optString("url"); - long downloadSize = jo.optLong("size"); - if (ComparatorUtils.equals(category, "server")) { - File currentJAR = new File(StableUtils.pathJoin(WorkContext.getCurrent().getPath(), ProjectConstants.LIB_NAME, downloadName)); - String currentMD5 = getCurrentJarMD5(currentJAR); - String downloadMD5 = jo.optString("md5"); - boolean exist = currentJAR.exists() && ComparatorUtils.equals(currentJAR.length(), downloadSize) && ComparatorUtils.equals(currentMD5, downloadMD5); - if (exist) { - // 如果jar包存在且MD5值和大小与oss上的一致 不下载 - continue; - } - } - downloadItems.add(new DownloadItem(downloadName, downloadUrl, downloadSize)); - } - } - } - - /** - * 获取当前jar的md5 - * - * @param currentJAR - * @return - */ - private String getCurrentJarMD5(File currentJAR) { - String md5 = StringUtils.EMPTY; - FileInputStream input = null; - try { - input = new FileInputStream(currentJAR); - md5 = DigestUtils.md5Hex(input); - } catch (Exception ignore) { - - } finally { - if (input != null) { - try { - input.close(); - } catch (IOException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - } - } - return md5; } /** @@ -669,31 +597,19 @@ public class UpdateMainDialog extends UIDialog { if (updateSuccessful) { RestartHelper.restart(); } else { + progressBar.setVisible(true); + UpdateCallBack callBack = new UpdateProgressCallBack(progressBar); deletePreviousPropertyFile(); updateButton.setEnabled(false); - progressBar.setVisible(true); updateLabel.setVisible(false); - - new FileDownloader( - downloadItems.toArray(new DownloadItem[downloadItems.size()]), - StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DOWNLOAD_DIR)) { - @Override - protected void process(java.util.List chunks) { - DownloadItem fileInfo = chunks.get(chunks.size() - 1); - progressBar.setString(fileInfo.getName() + " " + fileInfo.getProgressString()); - progressBar.setValue(fileInfo.getProgressValue()); - } - + new FileProcess(callBack) { @Override public void onDownloadSuccess() { updateButton.setEnabled(true); progressBar.setVisible(false); - backup(); - putNewFiles(); updateSuccessful = true; updateButton.setText(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Updater_Restart_Designer")); } - @Override public void onDownloadFailed() { progressBar.setVisible(false); @@ -719,109 +635,12 @@ public class UpdateMainDialog extends UIDialog { } } - /** - * JAR包更新的时候备份老的jar包,包括设计器相关的和服务器相关的几个 - */ - private void backup() { - String installHome = StableUtils.getInstallHome(); - //jar包备份文件的目录为"backup/"+jar包当前版本号 - String todayBackupDir = StableUtils.pathJoin(installHome, getBackupDirectory(), (GeneralUtils.readBuildNO())); - backupFilesFromInstallEnv(installHome, todayBackupDir, getJARList4Server()); - backupFilesFromInstallLib(installHome, todayBackupDir, getJARList4Designer()); - jarCurrentLabel.setText(downloadFileConfig.optString("buildNO")); - } - - private void backupFilesFromInstallEnv(String installHome, String todayBackupDir, List files) { - for (String file : files) { - try { - IOUtils.copy( - new File(StableUtils.pathJoin(installHome, UpdateConstants.APPS_FOLDER_NAME, ProductConstants.getAppFolderName(), ProjectConstants.WEBINF_NAME, ProjectConstants.LIB_NAME, file)), - new File(StableUtils.pathJoin(todayBackupDir))); - } catch (IOException e) { - FineLoggerFactory.getLogger().error(e.getMessage()); - } - } - } - - private void backupFilesFromInstallLib(String installHome, String todayBackupDir, List files) { - for (String file : files) { - try { - IOUtils.copy( - new File(StableUtils.pathJoin(installHome, ProjectConstants.LIB_NAME, file)), - new File(StableUtils.pathJoin(todayBackupDir))); - } catch (IOException e) { - FineLoggerFactory.getLogger().error(e.getMessage()); - } - } - } - - private void putNewFiles() { - Map map = new HashMap(); - java.util.List list = new ArrayList(); - String installHome = StableUtils.getInstallHome(); - putNewFilesToInstallLib(installHome, getDownLoadJAR4Designer(), map, list); - putNewFilesToInstallEnv(installHome, getDownLoadJAR4Server(), map, list); - RestartHelper.saveFilesWhichToMove(map); - RestartHelper.saveFilesWhichToDelete(list.toArray(new String[list.size()])); - } - - private void putNewFilesToInstallLib(String installHome, String[] files, Map map, java.util.List list) { - for (String file : files) { - map.put(StableUtils.pathJoin(installHome, UpdateConstants.DOWNLOAD_DIR, file), - StableUtils.pathJoin(installHome, ProjectConstants.LIB_NAME, file)); - list.add(StableUtils.pathJoin(installHome, ProjectConstants.LIB_NAME, file)); - } - } - - private void putNewFilesToInstallEnv(String installHome, String[] files, Map map, java.util.List list) { - for (String file : files) { - map.put(StableUtils.pathJoin(installHome, UpdateConstants.DOWNLOAD_DIR, file), - StableUtils.pathJoin(installHome, UpdateConstants.APPS_FOLDER_NAME, ProductConstants.getAppFolderName(), ProjectConstants.WEBINF_NAME, ProjectConstants.LIB_NAME, file)); - list.add(StableUtils.pathJoin(installHome, UpdateConstants.APPS_FOLDER_NAME, ProductConstants.getAppFolderName(), ProjectConstants.WEBINF_NAME, ProjectConstants.LIB_NAME, file)); - } - } //获取备份目录 private String getBackupDirectory() { return UpdateConstants.DESIGNER_BACKUP_DIR; } - //获取服务器jar包列表 - private List getJARList4Server() { - return UpdateConstants.JARS_FOR_SERVER_X; - } - - //获取设计器jar包列表 - private List getJARList4Designer() { - return UpdateConstants.JARS_FOR_DESIGNER_X; - } - - //获取服务器jar包下载列表 - private String[] getDownLoadJAR4Server() { - ArrayList jarList = new ArrayList(); - List serverItems = getJARList4Server(); - for (DownloadItem downloadItem : downloadItems) { - String downloadItemName = downloadItem.getName(); - if (serverItems.contains(downloadItemName)) { - jarList.add(downloadItemName); - } - } - return jarList.toArray(new String[jarList.size()]); - } - - //获取设计器jar包下载列表 - private String[] getDownLoadJAR4Designer() { - ArrayList jarList = new ArrayList(); - List designerJarItems = getJARList4Designer(); - for (DownloadItem downloadItem : downloadItems) { - String downloadItemName = downloadItem.getName(); - if (designerJarItems.contains(downloadItemName)) { - jarList.add(downloadItemName); - } - } - return jarList.toArray(new String[jarList.size()]); - } - //获取更新日志缓存配置文件名 private String getUpdateCacheConfig() { return UpdateConstants.UPDATE_CACHE_CONFIG_X; From f8cd1b0f51f95e1fffe9822bcac1e614007c7b0d Mon Sep 17 00:00:00 2001 From: Bryant Date: Sat, 21 Sep 2019 13:59:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?KERNEL-418=20=E6=9B=B4=E6=96=B0=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/fr/design/update/actions/FileProcess.java | 2 +- .../java/com/fr/design/update/ui/dialog/UpdateMainDialog.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/update/actions/FileProcess.java b/designer-base/src/main/java/com/fr/design/update/actions/FileProcess.java index 59194f86a..2ed0801dc 100644 --- a/designer-base/src/main/java/com/fr/design/update/actions/FileProcess.java +++ b/designer-base/src/main/java/com/fr/design/update/actions/FileProcess.java @@ -1,6 +1,6 @@ package com.fr.design.update.actions; -import com.fr.decision.update.Info.UpdateCallBack; +import com.fr.decision.update.info.UpdateCallBack; import com.fr.decision.update.UpdateExecutor; import com.fr.log.FineLoggerFactory; diff --git a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java index bd3f0a1dd..c7eceb050 100644 --- a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java +++ b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java @@ -1,7 +1,7 @@ package com.fr.design.update.ui.dialog; -import com.fr.decision.update.Info.UpdateCallBack; -import com.fr.decision.update.Info.UpdateProgressCallBack; +import com.fr.decision.update.info.UpdateCallBack; +import com.fr.decision.update.info.UpdateProgressCallBack; import com.fr.design.RestartHelper; import com.fr.design.constants.LayoutConstants; import com.fr.design.dialog.UIDialog;