From 6b7b78eb68c3e1f48113fbb28935cde567a7939f Mon Sep 17 00:00:00 2001 From: rusefillc Date: Mon, 19 Aug 2024 11:05:52 -0400 Subject: [PATCH] only:extract methods --- .../com/rusefi/autoupdate/Autoupdate.java | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/java_console/autoupdate/src/main/java/com/rusefi/autoupdate/Autoupdate.java b/java_console/autoupdate/src/main/java/com/rusefi/autoupdate/Autoupdate.java index b77c7cc1f2..3f3dfd434a 100644 --- a/java_console/autoupdate/src/main/java/com/rusefi/autoupdate/Autoupdate.java +++ b/java_console/autoupdate/src/main/java/com/rusefi/autoupdate/Autoupdate.java @@ -86,6 +86,12 @@ public class Autoupdate { @NotNull String firstArgument = args.length > 0 ? args[0] : ""; + Optional downloadedAutoupdateFile = downloadFreshZipFile(args, firstArgument, bundleInfo); + URLClassLoader jarClassLoader = safeUnzipMakingSureClassloaderIsHappy(downloadedAutoupdateFile); + startConsole(args, jarClassLoader); + } + + private static Optional downloadFreshZipFile(String[] args, String firstArgument, BundleUtil.BundleInfo bundleInfo) { Optional downloadedAutoupdateFile = Optional.empty(); if (firstArgument.equalsIgnoreCase("basic-ui")) { downloadedAutoupdateFile = doDownload(bundleInfo, UpdateMode.ALWAYS); @@ -105,45 +111,51 @@ public class Autoupdate { log.info("Update mode: NEVER"); } } - downloadedAutoupdateFile.ifPresent(autoupdateFile -> { - try { - // We cannot unzip rusefi_autoupdate.jar file because we need the old one to prepare class loader below - // (otherwise we get `ZipFile invalid LOC header (bad signature)` exception, see #6777) - // TODO: extract only ConnectionAndMeta#getRusEfiConsoleJarName - FileUtil.unzip(autoupdateFile.zipFileName, new File(".."), isRusefiAutoupdateJar.negate()); - final String srecFile = findSrecFile(); - new File(srecFile == null ? FindFileHelper.FIRMWARE_BIN_FILE : srecFile) - .setLastModified(autoupdateFile.lastModified); - } catch (IOException e) { - log.error("Error unzipping bundle without rusefi_autoupdate.jar: " + e); - if (!AutoupdateUtil.runHeadless) { - JOptionPane.showMessageDialog( - null, - "Error unzipping bundle without rusefi_autoupdate.jar: " + e, - "Error", - JOptionPane.ERROR_MESSAGE - ); - } - } - }); - final URLClassLoader jarClassLoader = prepareClassLoaderToStartConsole(); + return downloadedAutoupdateFile; + } + + private static URLClassLoader safeUnzipMakingSureClassloaderIsHappy(Optional downloadedAutoupdateFile) { + downloadedAutoupdateFile.ifPresent(Autoupdate::unzipFreshConsole); + URLClassLoader jarClassLoader = prepareClassLoaderToStartConsole(); downloadedAutoupdateFile.ifPresent(autoupdateFile -> { try { // We've already prepared class loader, so now we can unzip rusefi_autoupdate.jar file (#6777) FileUtil.unzip(autoupdateFile.zipFileName, new File(".."), isRusefiAutoupdateJar); } catch (IOException e) { - log.error("Error unzipping rusefi_autoupdate.jar from bundle: " + e); + log.error("Error unzipping autoupdate from bundle: " + e); if (!AutoupdateUtil.runHeadless) { JOptionPane.showMessageDialog( null, - "Error unzipping rusefi_autoupdate.jar from bundle: " + e, + "Error unzipping autoupdate from bundle: " + e, "Error", JOptionPane.ERROR_MESSAGE ); } } }); - startConsole(args, jarClassLoader); + return jarClassLoader; + } + + private static void unzipFreshConsole(DownloadedAutoupdateFileInfo autoupdateFile) { + try { + // We cannot unzip rusefi_autoupdate.jar file because we need the old one to prepare class loader below + // (otherwise we get `ZipFile invalid LOC header (bad signature)` exception, see #6777) + // TODO: extract only ConnectionAndMeta#getRusEfiConsoleJarName + FileUtil.unzip(autoupdateFile.zipFileName, new File(".."), isRusefiAutoupdateJar.negate()); + final String srecFile = findSrecFile(); + new File(srecFile == null ? FindFileHelper.FIRMWARE_BIN_FILE : srecFile) + .setLastModified(autoupdateFile.lastModified); + } catch (IOException e) { + log.error("Error unzipping bundle without autoupdate: " + e); + if (!AutoupdateUtil.runHeadless) { + JOptionPane.showMessageDialog( + null, + "Error unzipping bundle without autoupdate: " + e, + "Error", + JOptionPane.ERROR_MESSAGE + ); + } + } } private static final Predicate isRusefiAutoupdateJar =