summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java
blob: a1a8669271db4299e2ec6ff2ae162ccd17dbbd9c (plain) (tree)
































































































                                                                                                                 
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;
	}
}