summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java
blob: 9a124b1754cb65644e30d20dcec189eb3f3c82a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.openslx.dozmod.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.apache.log4j.Logger;

import com.google.gson.Gson;

// ClientVersion is a bad name, TODO find better suited one :)
public class ClientVersion {

	private static final Logger LOGGER = Logger.getLogger(ClientVersion.class);
	
	private static Long localRevisionTime = null;
	private static Long remoteRevisionTime = null;
	private static String localRevision = null;
	private static String remoteRevision = null;
	
	private static void init() {
		loadLocalVersion();
		loadRemoteVersion("http://132.230.4.25/dozmod.version");
	}


	/**
	 * Gets the local revision id if loading it worked, "???" otherwise. 
	 * 
	 * @return id as String
	 */
	public static String getLocalRevision() {
		if (localRevision == null) {
			init();
			if (localRevision == null)
				// fallback for dev purposes...
				localRevision = "???";
		}
		return localRevision;
	}
	/**
	 * @return
	 */
	public static long getLocalRevTimestamp() {
		if (localRevisionTime == null) {
			init();
			if (localRevisionTime == null)
				// fallback for dev purposes...
				localRevisionTime = 0L;
		}
		return localRevisionTime;
	}
	/**
	 * Gets the revision id of the latest remote version
	 * 
	 * @return id as String if loading worked, "-" otherwise
	 */
	public static String getRemoteRevision() {
		if (remoteRevision == null) {
			init();
			if (remoteRevision == null)
				remoteRevision = "-";
		}
		return remoteRevision;
	}
	/**
	 * Gets the timestamp of the latest remote version
	 * 
	 * @return timestamp as Long if loading it worked, 0L otherwise
	 */
	public static long getRemoteRevTimestamp() {
		if (remoteRevisionTime == null) {
			init();
			if (remoteRevisionTime == null)
				return 0L;
		}
		return remoteRevisionTime;
	}
	/**
	 * Checks if we are running latest bwSuite version 
	 * 
	 * @return true if there is no newer version, false otherwise
	 */
	public static boolean isNewest() {
		if (localRevisionTime == null || remoteRevisionTime == null)
			init();
		// fallback if the above did not work: fail
		if (localRevisionTime == null || remoteRevisionTime == null)
			return true;
		return localRevisionTime >= remoteRevisionTime;
	}
	/**
	 * Loads the local version information from the jar's MANIFEST.MF
	 * into the fields 'localRevision' and 'localRevisionTime'
	 */
	private static void loadLocalVersion() {
		Class clazz = ClientVersion.class;
		String className = clazz.getSimpleName() + ".class";
		String classPath = clazz.getResource(className).toString();
		if (!classPath.startsWith("jar")) {
			// Class not from JAR
			return;
		}
		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;
		}
		Attributes attributes = manifest.getMainAttributes();
		// if attr are null, then we couldn't open the jar's MANIFEST
		// since we are probably not in a jar context, just do nothing
		if (attributes == null)
			return;
		String manifestRev = null;
		String manifestRevTime = null;
		try {
			manifestRev = attributes.getValue("Build-Revision");
			manifestRevTime = attributes.getValue("Build-Revision-Timestamp");
		} catch (IllegalArgumentException e) {
			LOGGER.warn("Error while reading version: ", e);
			return;
		}
		if (manifestRev != null)
			localRevision = manifestRev;
		if (manifestRevTime != null) {
			localRevisionTime = Long.valueOf(manifestRevTime);
			// hax since we get milliseconds not seconds
			localRevisionTime = localRevisionTime / 1000L; 
		}
	}
	/**
	 * Loads the given UrlString as JSON and saves the remote information 
	 * into fields 'remoteRevision' and 'remoteRevisionTime'
	 * 
	 * The remote JSON should have 'timestamp' and 'revision', like:
	 * 		{ "timestamp": 1, "revision": 2 }
	 */
	private static void loadRemoteVersion(final String urlString) {
		Gson gson = new Gson();
		String json = null;
		BufferedReader reader = null;
		try {
			URL url = new URL(urlString);
			reader = new BufferedReader(new InputStreamReader(url.openStream()));
			StringBuffer buffer = new StringBuffer();
			int read;
			char[] chars = new char[1024];
			while ((read = reader.read(chars)) != -1)
				buffer.append(chars, 0, read); 
			json = buffer.toString();
		} catch (Exception e) {
			LOGGER.error("Could not fetch remote version", e);
			return;
		} finally {
			if (reader != null)
				try {
					reader.close();
				} catch (IOException e) {
				}
		}

		VersionQuery query = gson.fromJson(json, VersionQuery.class);
		if (query.revision != null)
			remoteRevision = query.revision;
		if (query.timestamp != null) {
			// seconds timestamp here...
			remoteRevisionTime = query.timestamp;
		}
	}

	/**
	 * Class for GSON json parsing
	 */
	static class VersionQuery {
		Long timestamp;
		String revision;
	}
}