diff options
author | Simon Rettberg | 2017-08-01 15:39:40 +0200 |
---|---|---|
committer | Simon Rettberg | 2017-08-01 15:39:40 +0200 |
commit | 54098035f34918be064be906183bec77eb43d359 (patch) | |
tree | a62d3b9daead8552f8428b6f1a675b6b40fd8445 | |
parent | [client] Only try sat server in OrgCache if it is set yet (diff) | |
download | tutor-module-54098035f34918be064be906183bec77eb43d359.tar.gz tutor-module-54098035f34918be064be906183bec77eb43d359.tar.xz tutor-module-54098035f34918be064be906183bec77eb43d359.zip |
[client] Async remote version info fetching
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java | 86 |
1 files changed, 45 insertions, 41 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java index 31effe85..5ed1fe5d 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java @@ -13,6 +13,7 @@ import java.util.jar.Manifest; import org.apache.commons.io.FileUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.log4j.Logger; +import org.openslx.dozmod.App; import org.openslx.sat.thrift.version.Version; import org.openslx.util.Json; @@ -22,26 +23,14 @@ public class ClientVersion { private static long localRevisionTime = 0; private static long remoteRevisionTime = 0; - private static String localRevision = null; - private static String remoteRevision = null; - private static String changelog = null; + private static String localRevision = "???"; + private static String remoteRevision = "???"; + private static String changelog = "???"; + private static Thread remoteThread = null; - private static void init() { - if (localRevision == null) { - loadLocalVersion(); - } - if (localRevision == null) { - localRevision = "???"; - } - if (remoteRevision == null) { - loadRemoteVersion("https://bwlp-masterserver.ruf.uni-freiburg.de/dozmod/" + Version.VERSION + "/version.json"); - } - if (remoteRevision == null) { - remoteRevision = "???"; - } - if (changelog == null) { - changelog = "???"; - } + static { + loadLocalVersion(); + loadRemoteVersion("https://bwlp-masterserver.ruf.uni-freiburg.de/dozmod/" + Version.VERSION + "/version.json"); } /** @@ -50,7 +39,6 @@ public class ClientVersion { * @return id as String */ public static String getLocalRevision() { - init(); return localRevision; } @@ -58,7 +46,6 @@ public class ClientVersion { * @return */ public static long getLocalRevTimestamp() { - init(); return localRevisionTime; } @@ -68,7 +55,7 @@ public class ClientVersion { * @return id as String if loading worked, "???" otherwise */ public static String getRemoteRevision() { - init(); + waitRemote(); return remoteRevision; } @@ -78,7 +65,7 @@ public class ClientVersion { * @return timestamp as Long if loading it worked, 0L otherwise */ public static long getRemoteRevTimestamp() { - init(); + waitRemote(); return remoteRevisionTime; } @@ -88,7 +75,7 @@ public class ClientVersion { * @return log as String */ public static String getChangelog() { - init(); + waitRemote(); return changelog; } @@ -98,10 +85,10 @@ public class ClientVersion { * @return true if there is no newer version, false otherwise */ public static boolean isNewest() { - init(); // if either local or remote version is unknown, just pretend there's no update // as there most likely isn't and we'd just annoy the user // TODO: Report "fail" state so at least on manual update check we can tell that it failed + waitRemote(); if (localRevisionTime == 0 || remoteRevisionTime == 0) return true; return localRevisionTime >= remoteRevisionTime; @@ -167,24 +154,41 @@ public class ClientVersion { * { "timestamp": 1, "revision": 2 } */ private static void loadRemoteVersion(final String urlString) { - String json = null; - try (InputStream reader = new URL(urlString).openStream()) { - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - buffer.write(reader); - buffer.close(); - json = new String(buffer.toByteArray(), StandardCharsets.UTF_8); - } catch (Exception e) { - if (json == null) { - LOGGER.error("Could not fetch remote version", e); - return; + remoteThread = new Thread(new Runnable() { + @Override + public void run() { + App.waitForInit(); + String json = null; + try (InputStream reader = new URL(urlString).openStream()) { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + buffer.write(reader); + buffer.close(); + json = new String(buffer.toByteArray(), StandardCharsets.UTF_8); + } catch (Exception e) { + if (json == null) { + LOGGER.error("Could not fetch remote version", e); + return; + } + } + + VersionQuery query = Json.deserialize(json, VersionQuery.class); + remoteRevision = query.revision; + // seconds timestamp here... + remoteRevisionTime = query.timestamp; + changelog = query.changelog; } + }); + remoteThread.start(); + } + + private static synchronized void waitRemote() { + if (remoteThread == null) + return; + try { + remoteThread.join(); + } catch (InterruptedException e) { } - - VersionQuery query = Json.deserialize(json, VersionQuery.class); - remoteRevision = query.revision; - // seconds timestamp here... - remoteRevisionTime = query.timestamp; - changelog = query.changelog; + remoteThread = null; } /** |