summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java
diff options
context:
space:
mode:
authorJonathan Bauer2015-09-07 09:44:05 +0200
committerJonathan Bauer2015-09-07 09:44:05 +0200
commite4811fd3b312229a5a7497275e66a808b14d6015 (patch)
tree529f428570ab23d0fa01bd87df9b2a135234e7c9 /dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java
parent[client] Add hints to config dialog (diff)
downloadtutor-module-e4811fd3b312229a5a7497275e66a808b14d6015.tar.gz
tutor-module-e4811fd3b312229a5a7497275e66a808b14d6015.tar.xz
tutor-module-e4811fd3b312229a5a7497275e66a808b14d6015.zip
[client] Working on Client update checks [WIP]
TODO real URL for getting newest version TODO continue update check window popup
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java97
1 files changed, 97 insertions, 0 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
new file mode 100644
index 00000000..a1a86692
--- /dev/null
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java
@@ -0,0 +1,97 @@
+package org.openslx.dozmod.util;
+
+import java.net.URL;
+import java.util.Scanner;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+import org.apache.log4j.Logger;
+
+// ClientVersion is a bad name, TODO find better suited one :)
+public class ClientVersion {
+
+ private static final Logger LOGGER = Logger.getLogger(ClientVersion.class);
+
+ private static String remoteVersion = null;
+ private static Long localRevisionTime = null;
+ private static Long remoteRevisionTime = null;
+ private static String revision = null;
+
+ private static void init() {
+ // load local version information from the jar's MANIFEST.MF
+ String manifestRev = null;
+ String manifestRevTime = null;
+ try {
+ Attributes attributes = getAttr();
+ if (attributes == null)
+ return;
+ manifestRev = attributes.getValue("Build-Revision");
+ manifestRevTime = attributes.getValue("Build-Revision-Timestamp");
+ if (manifestRev != null)
+ revision = manifestRev;
+ if (manifestRevTime != null)
+ localRevisionTime = Long.valueOf(manifestRevTime);
+ } catch ( NumberFormatException e) {
+ LOGGER.warn("Error while reading version: ", e);
+ }
+ LOGGER.info("Local version timestamp: " + localRevisionTime);
+ // load the remote version information
+ URL remoteVersionUrl = null;
+ Scanner scanner = null;
+ try {
+ remoteVersionUrl = new URL("http://132.230.4.25/dozmod.version");
+ scanner = new Scanner(remoteVersionUrl.openStream());
+ } catch (Exception e) {
+ // TODO shouldn't happen anyways
+ LOGGER.error("Could not download remote version from: " + remoteVersionUrl.getPath(), e);
+ }
+ // process only one line for now
+ Long remoteTime = null;
+ if (scanner.hasNextLong()) {
+ remoteTime = scanner.nextLong();
+ }
+ scanner.close();
+ if (remoteTime != null) {
+ remoteRevisionTime = remoteTime;
+ }
+ LOGGER.info("Remote version timestamp: " + remoteRevisionTime);
+
+ }
+
+ private static Attributes getAttr() {
+ Class clazz = ClientVersion.class;
+ String className = clazz.getSimpleName() + ".class";
+ String classPath = clazz.getResource(className).toString();
+ if (!classPath.startsWith("jar")) {
+ // Class not from JAR
+ return null;
+ }
+ String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) +
+ "/META-INF/MANIFEST.MF";
+ Manifest manifest = null;
+ try {
+ manifest = new Manifest(new URL(manifestPath).openStream());
+ } catch (Exception e) {
+ LOGGER.error("Could not open MANIFEST", e);
+ return null;
+ }
+ Attributes attr = manifest.getMainAttributes();
+ return attr;
+ }
+ public static String getNewVersion() {
+ return getRevision();
+ }
+ public static String getRevision() {
+ if (revision == null)
+ init();
+ return revision;
+ }
+
+ public static boolean isNewest() {
+ if (localRevisionTime == null || remoteRevisionTime == null)
+ init();
+ if (localRevisionTime == null || remoteRevisionTime == null)
+ return true; // hax
+ return localRevisionTime >= remoteRevisionTime;
+ }
+}