From e1bbeb8a6b3be47151f4528108a1112c4a210e8b Mon Sep 17 00:00:00 2001 From: Fangjie Hu Date: Sat, 10 Sep 2016 18:28:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=80=E6=9C=89=E4=BE=9D=E8=B5=96=E4=B8=80?= =?UTF-8?q?=E8=B5=B7=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/fr/design/extra/PluginHelper.java | 16 +- .../DownLoadDependenceUI.java | 146 +++++++++++------- .../PluginDependenceUtils.java | 11 +- 3 files changed, 108 insertions(+), 65 deletions(-) diff --git a/designer_base/src/com/fr/design/extra/PluginHelper.java b/designer_base/src/com/fr/design/extra/PluginHelper.java index 82fe55999b..eef4b02970 100644 --- a/designer_base/src/com/fr/design/extra/PluginHelper.java +++ b/designer_base/src/com/fr/design/extra/PluginHelper.java @@ -10,6 +10,7 @@ import com.fr.plugin.Plugin; import com.fr.plugin.PluginLoader; import com.fr.plugin.PluginManagerHelper; import com.fr.plugin.dependence.PluginDependence; +import com.fr.plugin.dependence.PluginDependenceException; import com.fr.plugin.dependence.PluginDependenceUnit; import com.fr.stable.ArrayUtils; import com.fr.stable.EncodeConstants; @@ -21,6 +22,7 @@ import javax.swing.*; import java.io.*; import java.net.HttpURLConnection; import java.net.URLDecoder; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.concurrent.ExecutionException; @@ -124,19 +126,29 @@ public class PluginHelper { } //将所有未配置好的资源文件依赖准备好 - private static void checkDependenceEnv(Plugin plugin) { + private static void checkDependenceEnv(Plugin plugin) throws PluginDependenceException { PluginDependence dependence = plugin.getDependence(); if (dependence == null){ return; } + + List needInstallDependence = new ArrayList(); + String currentID = dependence.getCurrentPluginID(); List list = dependence.getDependPlugins(); for (int i = 0;list != null && i < list.size(); i++){ PluginDependenceUnit dependenceUnit = list.get(i); if (!dependenceUnit.checkFileEnv()){ - PluginDependenceUtils.installDependenceOnline(currentID, dependenceUnit.getDependenceID(), dependenceUnit.getDependenceDir()); + needInstallDependence.add(dependenceUnit); } } + + if (needInstallDependence.isEmpty()){ + return; + } + + //安装插件依赖 + PluginDependenceUtils.installDependenceOnline(currentID, needInstallDependence); } /** diff --git a/designer_base/src/com/fr/design/extra/plugindependence/DownLoadDependenceUI.java b/designer_base/src/com/fr/design/extra/plugindependence/DownLoadDependenceUI.java index a09db3f0e2..4209857bcb 100644 --- a/designer_base/src/com/fr/design/extra/plugindependence/DownLoadDependenceUI.java +++ b/designer_base/src/com/fr/design/extra/plugindependence/DownLoadDependenceUI.java @@ -8,6 +8,8 @@ import com.fr.general.IOUtils; import com.fr.general.Inter; import com.fr.general.SiteCenter; import com.fr.general.http.HttpClient; +import com.fr.plugin.dependence.PluginDependenceException; +import com.fr.plugin.dependence.PluginDependenceUnit; import com.fr.stable.StableUtils; import com.fr.stable.StringUtils; @@ -22,7 +24,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; - +import java.util.ArrayList; +import java.util.List; /** * Created by hufan on 2016/9/5. */ @@ -44,8 +47,7 @@ public class DownLoadDependenceUI implements ActionListener { //安装环境相关信息 private String currentID; - private String dependenceID; - private String dependenceDir; + private List list = null; //安装结果 private boolean result = false; //链接服务器的客户端 @@ -57,10 +59,9 @@ public class DownLoadDependenceUI implements ActionListener { public DownLoadDependenceUI() { } - public DownLoadDependenceUI(String currentID, String dependenceID, String dependenceDir) { + public DownLoadDependenceUI(String currentID, List list) { this.currentID = currentID; - this.dependenceID = dependenceID; - this.dependenceDir = dependenceDir; + this.list = list; this.totalSize = getFileLength(); init(); } @@ -89,7 +90,7 @@ public class DownLoadDependenceUI implements ActionListener { timer = new Timer(100, this); frame = new JDialog(DesignerContext.getDesignerFrame(), true); - frame.setTitle(Inter.getLocText("FR-Designer-Dependence_Install_Online") + dependenceID); + frame.setTitle(Inter.getLocText("FR-Designer-Dependence_Install_Online")); frame.setSize(LOAD_WIDTH, LOAD_HEIGHT); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(screenSize.width / 2 - LOAD_WIDTH / 2, screenSize.height / 2 - LOAD_HEIGHT / 2); @@ -112,17 +113,29 @@ public class DownLoadDependenceUI implements ActionListener { //是否可以连接服务器 private boolean connectToServer() { - httpClient = new HttpClient(SiteCenter.getInstance().acquireUrlByKind(dependenceID)); - return httpClient.getResponseCode() == HttpURLConnection.HTTP_OK; + for (int i = 0; i < list.size(); i++) { + PluginDependenceUnit dependenceUnit = list.get(i); + httpClient = new HttpClient(SiteCenter.getInstance().acquireUrlByKind(dependenceUnit.getDependenceID())); + if (httpClient.getResponseCode() != HttpURLConnection.HTTP_OK){ + return false; + } + } + return true; } //获取依赖文件大小 - private int getFileLength() { - HttpClient httpClient = new HttpClient(SiteCenter.getInstance().acquireUrlByKind(dependenceID)); - if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) { - return httpClient.getContentLength(); + private int getFileLength(){ + int size = 0; + for (int i = 0; i < list.size(); i++) { + PluginDependenceUnit dependenceUnit = list.get(i); + HttpClient httpClient = new HttpClient(SiteCenter.getInstance().acquireUrlByKind(dependenceUnit.getDependenceID())); + if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) { + size += httpClient.getContentLength(); + }else { + return -1; + } } - return -1; + return size; } //安装 @@ -143,7 +156,7 @@ public class DownLoadDependenceUI implements ActionListener { try { thread.join(); } catch (InterruptedException e) { - e.printStackTrace(); + FRContext.getLogger().error(e.getMessage()); return false; } //停止时钟 @@ -151,57 +164,71 @@ public class DownLoadDependenceUI implements ActionListener { return result; } - private String downloadPluginDependenceFile() throws Exception { - httpClient = new HttpClient(SiteCenter.getInstance().acquireUrlByKind(dependenceID)); - if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) { - InputStream reader = httpClient.getResponseStream(); - String temp = StableUtils.pathJoin(PluginHelper.DOWNLOAD_PATH, PluginHelper.TEMP_FILE); - StableUtils.makesureFileExist(new File(temp)); - FileOutputStream writer = new FileOutputStream(temp); - byte[] buffer = new byte[PluginConstants.BYTES_NUM]; - int bytesRead = 0; - totalBytesRead = 0; - - while ((bytesRead = reader.read(buffer)) > 0 && flag) { - writer.write(buffer, 0, bytesRead); - buffer = new byte[PluginConstants.BYTES_NUM]; - totalBytesRead += bytesRead; - } - reader.close(); - writer.flush(); - writer.close(); + private List downloadPluginDependenceFile() throws Exception { + totalBytesRead = 0; + List pathList = new ArrayList(); + for (int i = 0; i < list.size(); i++) { + PluginDependenceUnit dependenceUnit = list.get(i); + httpClient = new HttpClient(SiteCenter.getInstance().acquireUrlByKind(dependenceUnit.getDependenceID())); + if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) { + InputStream reader = httpClient.getResponseStream(); + String temp = StableUtils.pathJoin(PluginHelper.DOWNLOAD_PATH, PluginHelper.TEMP_FILE); + StableUtils.makesureFileExist(new File(temp)); + FileOutputStream writer = new FileOutputStream(temp); + byte[] buffer = new byte[PluginConstants.BYTES_NUM]; + int bytesRead = 0; + while ((bytesRead = reader.read(buffer)) > 0 && flag) { + writer.write(buffer, 0, bytesRead); + buffer = new byte[PluginConstants.BYTES_NUM]; + totalBytesRead += bytesRead; + } + reader.close(); + writer.flush(); + writer.close(); + + //下载被取消 + if (flag == false) { + result = false; + throw new PluginDependenceException(Inter.getLocText("FR-Designer-Dependence_Install_Failed")); + } + pathList.add(temp); - //下载被取消 - if (flag == false) { + } else { result = false; - return StringUtils.EMPTY; + throw new PluginDependenceException(Inter.getLocText("FR-Designer-Dependence_Install_Failed")); } - return temp; - - } else { - result = false; - throw new com.fr.plugin.PluginVerifyException(Inter.getLocText("FR-Designer-Plugin_Connect_Server_Error")); } + return pathList; } public void installDependenceOnline() { try { - String filePath = downloadPluginDependenceFile(); - if (!StringUtils.EMPTY.equals(filePath)) { - //安装文件 - installPluginDependenceFile(filePath); - result = true; - } + List filePathList = downloadPluginDependenceFile(); + //安装文件 + installPluginDependenceFile(filePathList); } catch (Exception e) { - e.printStackTrace(); + result = false; + FRContext.getLogger().error(e.getMessage()); } } //安装已经下载好的文件,如果是服务文件,则需要复制一份到安装目录下, //以便切换远程时,使用本地的服务 //如果是服务器环境,则只会安装一份 - private void installPluginDependenceFile(String filePath){ - IOUtils.unzip(new File(filePath), FRContext.getCurrentEnv().getPath() + dependenceDir); + private void installPluginDependenceFile(List filePathList){ + if (filePathList.isEmpty()){ + result = false; + return; + } + for(int i = 0; i < filePathList.size(); i++) { + if (StringUtils.EMPTY.equals(filePathList.get(i))){ + result = false; + return; + } + PluginDependenceUnit dependenceUnit = list.get(i); + IOUtils.unzip(new File(filePathList.get(i)), FRContext.getCurrentEnv().getPath() + dependenceUnit.getDependenceDir()); + result = true; + } } public void actionPerformed(ActionEvent e) { @@ -216,23 +243,22 @@ public class DownLoadDependenceUI implements ActionListener { } } - public boolean installOnline() { - int choose = JOptionPane.showConfirmDialog(null, Inter.getLocText("FR-Designer-Plugin_Plugin") + currentID + Inter.getLocText("FR-Designer-Need") + " " + dependenceID + " " + Inter.getLocText("FR-Designer-Support") + "," + Inter.getLocText("FR-Designer-Dependence_Need_Install") + " " + dependenceID + " " + "(" + showFileLength() + " m)?", "install tooltip", JOptionPane.YES_NO_OPTION); + public void installOnline()throws PluginDependenceException { + int choose = JOptionPane.showConfirmDialog(null, Inter.getLocText("FR-Designer-Plugin_Plugin") + Inter.getLocText("FR-Designer-Need") + Inter.getLocText("FR-Designer-Dependence") + Inter.getLocText("FR-Designer-Support") + "," + Inter.getLocText("FR-Designer-Dependence_Need_Install") + "(" + showFileLength() + " m)?", "install tooltip", JOptionPane.YES_NO_OPTION); if (choose == 0) {//下载安装 if (!connectToServer()) { JOptionPane.showMessageDialog(null, Inter.getLocText("FR-Designer-Dependence_Connect_Server_Error"), "alert", JOptionPane.ERROR_MESSAGE); - return false; + throw new PluginDependenceException(Inter.getLocText("FR-Designer-Dependence_Connect_Server_Error")); } //安装依赖环境 if (install()) { - JOptionPane.showMessageDialog(null, dependenceID + Inter.getLocText("FR-Designer-Dependence_Install_Succeed") + "!!"); - return true; + JOptionPane.showMessageDialog(null, Inter.getLocText("FR-Designer-Dependence_Install_Succeed") + "!!"); } else { - JOptionPane.showMessageDialog(null, dependenceID + Inter.getLocText("FR-Designer-Dependence_Install_Failed") + "!!", "alert", JOptionPane.ERROR_MESSAGE); - return false; + JOptionPane.showMessageDialog(null, Inter.getLocText("FR-Designer-Dependence_Install_Failed") + "!!", "alert", JOptionPane.ERROR_MESSAGE); + throw new PluginDependenceException(Inter.getLocText("FR-Designer-Dependence_Install_Failed")); } - } else {//不安装。无需为用户准备环境 - return true; + }else {//不选择下载,则不安装图标插件 + throw new PluginDependenceException(Inter.getLocText("FR-Designer-Dependence_Install_Failed")); } } diff --git a/designer_base/src/com/fr/design/extra/plugindependence/PluginDependenceUtils.java b/designer_base/src/com/fr/design/extra/plugindependence/PluginDependenceUtils.java index 96784f3e32..9dba8bcf4b 100644 --- a/designer_base/src/com/fr/design/extra/plugindependence/PluginDependenceUtils.java +++ b/designer_base/src/com/fr/design/extra/plugindependence/PluginDependenceUtils.java @@ -1,14 +1,19 @@ package com.fr.design.extra.plugindependence; +import com.fr.plugin.dependence.PluginDependenceException; +import com.fr.plugin.dependence.PluginDependenceUnit; + +import java.util.List; + /** * Created by hufan on 2016/8/31. */ public class PluginDependenceUtils { - public static boolean installDependenceOnline(String currentID, String dependenceID, String dependenceDir) { - DownLoadDependenceUI ui = new DownLoadDependenceUI(currentID, dependenceID, dependenceDir); - return ui.installOnline(); + public static void installDependenceOnline(String currentID, List list) throws PluginDependenceException{ + DownLoadDependenceUI ui = new DownLoadDependenceUI(currentID, list); + ui.installOnline(); } }