From 27d992f485da2f370d425f2bae3c5fc604f44eed Mon Sep 17 00:00:00 2001 From: DaCool <8727384-DaCool@users.noreply.gitlab.com> Date: Thu, 30 Dec 2021 17:46:20 +0000 Subject: [PATCH] Cache jarVersion and let it complain only once --- src/com/sheepit/client/Client.java | 2 +- src/com/sheepit/client/Configuration.java | 30 +++++++++---------- src/com/sheepit/client/Server.java | 2 +- .../standalone/VersionParameterHandler.java | 2 +- src/com/sheepit/client/standalone/Worker.java | 2 +- .../standalone/swing/activity/Settings.java | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/com/sheepit/client/Client.java b/src/com/sheepit/client/Client.java index d3eb902..3e3f8ae 100644 --- a/src/com/sheepit/client/Client.java +++ b/src/com/sheepit/client/Client.java @@ -623,7 +623,7 @@ import okhttp3.HttpUrl; StringBuilder logHeader = new StringBuilder() .append("====================================================================================================\n") - .append(String.format("%s / %s / %s / SheepIt v%s\n", conf.getLogin(), conf.getHostname(), OS.getOS().name(), conf.getJarVersion())) + .append(String.format("%s / %s / %s / SheepIt v%s\n", conf.getLogin(), conf.getHostname(), OS.getOS().name(), Configuration.jarVersion)) .append(String.format("%s x%d %.1f GB RAM\n", cpu.name(), conf.getNbCores(), conf.getMaxAllowedMemory() / 1024.0 / 1024.0)); if (conf.getComputeMethod() == Configuration.ComputeType.GPU || conf.getComputeMethod() == Configuration.ComputeType.CPU_GPU) { diff --git a/src/com/sheepit/client/Configuration.java b/src/com/sheepit/client/Configuration.java index 6d3fc45..8cc5488 100644 --- a/src/com/sheepit/client/Configuration.java +++ b/src/com/sheepit/client/Configuration.java @@ -40,6 +40,8 @@ import lombok.Data; @AllArgsConstructor @Data public class Configuration { + public static final String jarVersion = getJarVersion(); + public enum ComputeType { CPU_GPU, CPU, GPU } // accept job for ... @@ -300,26 +302,24 @@ import lombok.Data; return files_local; } - public String getJarVersion() { + private static String getJarVersion() { String versionPath = "/VERSION"; + String version = "6.0.0"; InputStream versionStream = Client.class.getResourceAsStream(versionPath); - if (versionStream == null) { + if (versionStream != null) { + try { + BufferedReader in = new BufferedReader(new InputStreamReader(versionStream)); + version = in.readLine(); + } + catch (IOException ex) { + System.err.println("Configuration::getJarVersion error while reading manifest file (" + versionPath + "): " + ex.getMessage()); + } + } + else { System.err.println("Configuration::getJarVersion Failed to get version file"); - return "6.0.0"; - } - - try { - InputStreamReader reader = new InputStreamReader(versionStream); - BufferedReader in = new BufferedReader(reader); - String version = in.readLine(); - - return version; - } - catch (IOException ex) { - System.err.println("Configuration::getJarVersion error while reading manifest file (" + versionPath + "): " + ex.getMessage()); - return "6.0.0"; } + return version; } public boolean checkOSisSupported() { diff --git a/src/com/sheepit/client/Server.java b/src/com/sheepit/client/Server.java index 879a63f..fc4179c 100644 --- a/src/com/sheepit/client/Server.java +++ b/src/com/sheepit/client/Server.java @@ -208,7 +208,7 @@ public class Server extends Thread { .add("os_version", os.getVersion()) .add("ram", String.valueOf(os.getTotalMemory())) .add("bits", os.getCPU().arch()) - .add("version", user_config.getJarVersion()) + .add("version", Configuration.jarVersion) .add("hostname", user_config.getHostname()) .add("ui", client.getGui().getClass().getSimpleName()) .add("extras", user_config.getExtras()) diff --git a/src/com/sheepit/client/standalone/VersionParameterHandler.java b/src/com/sheepit/client/standalone/VersionParameterHandler.java index 501b384..5c123b1 100644 --- a/src/com/sheepit/client/standalone/VersionParameterHandler.java +++ b/src/com/sheepit/client/standalone/VersionParameterHandler.java @@ -35,7 +35,7 @@ public class VersionParameterHandler extends OptionHandler { @Override public int parseArguments(Parameters params) throws CmdLineException { Configuration config = new Configuration(null, "", ""); - System.out.println("Version: " + config.getJarVersion()); + System.out.println("Version: " + Configuration.jarVersion); System.exit(0); return 0; } diff --git a/src/com/sheepit/client/standalone/Worker.java b/src/com/sheepit/client/standalone/Worker.java index 46f5328..ac08095 100644 --- a/src/com/sheepit/client/standalone/Worker.java +++ b/src/com/sheepit/client/standalone/Worker.java @@ -431,7 +431,7 @@ public class Worker { settingsLoader.markLaunchSettings(List.of(args)); } - Log.getInstance(config).debug("client version " + config.getJarVersion()); + Log.getInstance(config).debug("client version " + Configuration.jarVersion); // Hostname change will overwrite the existing one (default or read from configuration file) but changes will be lost when the client closes if (hostname != null) { diff --git a/src/com/sheepit/client/standalone/swing/activity/Settings.java b/src/com/sheepit/client/standalone/swing/activity/Settings.java index 62e064d..12bdf31 100644 --- a/src/com/sheepit/client/standalone/swing/activity/Settings.java +++ b/src/com/sheepit/client/standalone/swing/activity/Settings.java @@ -141,7 +141,7 @@ public class Settings implements Activity { Graphics gph = watermark.getGraphics(); gph.setFont(gph.getFont().deriveFont(12f)); - gph.drawString("v" + config.getJarVersion(), 335, 120); + gph.drawString("v" + Configuration.jarVersion, 335, 120); gph.dispose(); labelImage = new JLabel(new ImageIcon(watermark));