diff options
Diffstat (limited to 'dozentenmodul/src/main/java/ftp/DownloadTask.java')
| -rw-r--r-- | dozentenmodul/src/main/java/ftp/DownloadTask.java | 162 |
1 files changed, 90 insertions, 72 deletions
diff --git a/dozentenmodul/src/main/java/ftp/DownloadTask.java b/dozentenmodul/src/main/java/ftp/DownloadTask.java index fae25c68..bb81ab51 100644 --- a/dozentenmodul/src/main/java/ftp/DownloadTask.java +++ b/dozentenmodul/src/main/java/ftp/DownloadTask.java @@ -34,7 +34,7 @@ import util.ResourceLoader; public class DownloadTask extends SwingWorker<Void, Void> { /** - * Logger instance for this class. + * Logger instance for this class. */ private final static Logger LOGGER = Logger.getLogger(DownloadTask.class); @@ -51,8 +51,8 @@ public class DownloadTask extends SwingWorker<Void, Void> { private String saveDir; private int percentCompleted; - public DownloadTask(String host, int port, String username, - String password, String downloadPath, String saveDir) { + public DownloadTask(String host, int port, String username, String password, + String downloadPath, String saveDir) { this.host = host; this.port = port; this.username = username; @@ -131,20 +131,21 @@ public class DownloadTask extends SwingWorker<Void, Void> { */ @Override protected void done() { - if (!isCancelled() && percentCompleted==100) { + if (!isCancelled() && percentCompleted == 100) { LOGGER.info("Datei erfolgreich heruntergeladen."); String vmxResult = ""; - vmxResult = generateVmx() ? "Passende VMX generiert." : "Keine passende VMX generiert!"; - JOptionPane.showMessageDialog(null, - "Datei erfolgreich heruntergeladen. " + vmxResult, "Message", - JOptionPane.INFORMATION_MESSAGE); - } else if(!isCancelled() && percentCompleted != 100){ + vmxResult = generateVmx() ? "Passende VMX generiert." + : "Keine passende VMX generiert!"; + JOptionPane.showMessageDialog(null, "Datei erfolgreich heruntergeladen. " + + vmxResult, "Message", JOptionPane.INFORMATION_MESSAGE); + } else if (!isCancelled() && percentCompleted != 100) { LOGGER.error("Datei wurde unvollständig heruntergeladen."); JOptionPane.showMessageDialog(null, - "Datei wurde unvollständig heruntergeladen. Bitte wiederholen.", "Message", - JOptionPane.INFORMATION_MESSAGE); + "Datei wurde unvollständig heruntergeladen. Bitte wiederholen.", + "Message", JOptionPane.INFORMATION_MESSAGE); } } + /** * Helper to generate the vmx for the downloaded image * @@ -159,7 +160,9 @@ public class DownloadTask extends SwingWorker<Void, Void> { LOGGER.debug("Image's ID: " + Image.image.getImageId()); Map<String, String> imageData = null; try { - imageData = models.Client.clientcon.getClient().getImageData(Image.image.getImageId(), Image.image.getVersion(), SessionData.session.getAuthToken()); + imageData = models.Client.clientcon.getClient().getImageData( + Image.image.getImageId(), Image.image.getVersion(), + SessionData.session.getAuthToken()); } catch (TException e) { LOGGER.error("Thrift exception during transfer, see trace: ", e); return false; @@ -172,89 +175,104 @@ public class DownloadTask extends SwingWorker<Void, Void> { LOGGER.error("Image's version: " + Image.image.getVersion()); return false; } - int hardwareVersion = extractHardwareVersion(saveDir + File.separator + imageData.get("path").replaceFirst("^prod/", "")); + int hardwareVersion = extractHardwareVersion(saveDir + File.separator + + imageData.get("path").replaceFirst("^prod/", "")); if (hardwareVersion == 0) { - LOGGER.error("'extractHardwareVersion' returned 0 indicating some problem. See logs."); + LOGGER + .error("'extractHardwareVersion' returned 0 indicating some problem. See logs."); LOGGER.error("Falling back to default hardware version of '10'."); hardwareVersion = 10; } // TODO: sanity checks on the content of imageData would be good here... // use the information we received about the image - vmxTemplate = vmxTemplate.replace("%VM_DISPLAY_NAME%", imageData.get("name")); + vmxTemplate = vmxTemplate.replace("%VM_DISPLAY_NAME%", + imageData.get("name")); vmxTemplate = vmxTemplate.replace("%VM_GUEST_OS%", imageData.get("os")); vmxTemplate = vmxTemplate.replace("%VM_CPU_COUNT%", imageData.get("cpu")); - vmxTemplate = vmxTemplate.replace("%VM_RAM_SIZE%", String.valueOf(Integer.valueOf(imageData.get("ram")) * 1024)); - vmxTemplate = vmxTemplate.replace("%VM_DISK_PATH%", imageData.get("path").replaceFirst("^prod/", "")); - vmxTemplate = vmxTemplate.replace("%VM_HW_VERSION%", String.valueOf(hardwareVersion)); + vmxTemplate = vmxTemplate.replace("%VM_RAM_SIZE%", + String.valueOf(Integer.valueOf(imageData.get("ram")) * 1024)); + vmxTemplate = vmxTemplate.replace("%VM_DISK_PATH%", imageData.get("path") + .replaceFirst("^prod/", "")); + vmxTemplate = vmxTemplate.replace("%VM_HW_VERSION%", + String.valueOf(hardwareVersion)); // build filename for the vmx, basicly the same as the path of the vmdk // just without the leading "prod/" and "vmx" instead of "vmdk" at the end. - String targetFilename = saveDir + File.separator + - imageData.get("path").replaceFirst("^prod/", "").replaceFirst("\\.vmdk$", "") + ".vmx"; + String targetFilename = saveDir + + File.separator + + imageData.get("path").replaceFirst("^prod/", "") + .replaceFirst("\\.vmdk$", "") + ".vmx"; try { // try to write it to file - FileUtils.writeStringToFile(new File(targetFilename), vmxTemplate, StandardCharsets.UTF_8); + FileUtils.writeStringToFile(new File(targetFilename), vmxTemplate, + StandardCharsets.UTF_8); } catch (IOException e) { - LOGGER.error("Could not write vmx-template to '" + targetFilename + "'. See trace: ", e); + LOGGER.error("Could not write vmx-template to '" + targetFilename + + "'. See trace: ", e); return false; } return true; } /** - * Helper to extract the hardware version of the VMDK file by inspecting its content. + * Helper to extract the hardware version of the VMDK file by inspecting its + * content. * - * @return value of hardware version as integer. A return value of 0 indicates an error. + * @return value of hardware version as integer. A return value of 0 indicates + * an error. */ private int extractHardwareVersion(String path) { - BufferedReader br = null; - try { - try { - br = new BufferedReader(new InputStreamReader(new FileInputStream(path))); - String line; - // first 4 characters of a VMDK file start with 'KDMV' - // first lets check if this is the case - line = br.readLine(); - if (!line.subSequence(0, 4).equals("KDMV")) { - LOGGER.error("Did not see 'KDMV' as first chars of the VMDK! Returning 0."); - LOGGER.debug("First line was: " + line); - LOGGER.debug("First 4 characters of it: " + line.subSequence(0, 4)); - return 0; - } - // only read a maximum of 20 lines, just in case... - int round = 0; - while ((line = br.readLine()) != null || round > 20) { - if (line.matches("^ddb\\.virtualHWVersion.*")) { - String[] tmp = line.split("="); - // we should get 2 strings only after the split, lets be sure - if (tmp.length != 2) { - LOGGER.debug("Splitting returned more than 2 parts, this should not happen!"); - return 0; - } - int candidate = Integer.parseInt(tmp[1].trim().replace("\"", "")); - LOGGER.debug("Considering hardware version: " + candidate); - if (candidate > 0) { - LOGGER.debug("Valid value of the candidate. Using hardware version of: " + candidate); - return candidate; - } else { - LOGGER.error("Candidate is not > 0! Returning 0."); - return 0; - } - } - round++; - } - LOGGER.error("Failed to find hardware version. Tried " + round + " rounds."); - } - finally { - br.close(); - } - } - catch (FileNotFoundException e) { - LOGGER.debug("File not found, see trace: ", e); - } - catch (IOException e) { - LOGGER.debug("I/O Exception, see trace: ", e); - } + BufferedReader br = null; + try { + try { + br = new BufferedReader( + new InputStreamReader(new FileInputStream(path))); + String line; + // first 4 characters of a VMDK file start with 'KDMV' + // first lets check if this is the case + line = br.readLine(); + if (!line.subSequence(0, 4).equals("KDMV")) { + LOGGER + .error("Did not see 'KDMV' as first chars of the VMDK! Returning 0."); + LOGGER.debug("First line was: " + line); + LOGGER.debug("First 4 characters of it: " + line.subSequence(0, 4)); + return 0; + } + // only read a maximum of 20 lines, just in case... + int round = 0; + while ((line = br.readLine()) != null && round < 20) { + if (line.matches("^ddb\\.virtualHWVersion.*")) { + String[] tmp = line.split("="); + // we should get 2 strings only after the split, lets be sure + if (tmp.length != 2) { + LOGGER + .debug("Splitting returned more than 2 parts, this should not happen!"); + return 0; + } + int candidate = Integer.parseInt(tmp[1].trim().replace("\"", "")); + LOGGER.debug("Considering hardware version: " + candidate); + if (candidate > 0) { + LOGGER + .debug("Valid value of the candidate. Using hardware version of: " + + candidate); + return candidate; + } else { + LOGGER.error("Candidate is not > 0! Returning 0."); + return 0; + } + } + round++; + } + LOGGER.error("Failed to find hardware version. Tried " + round + + " rounds."); + } finally { + br.close(); + } + } catch (FileNotFoundException e) { + LOGGER.debug("File not found, see trace: ", e); + } catch (IOException e) { + LOGGER.debug("I/O Exception, see trace: ", e); + } return 0; } } |
