summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ClientVersion.java
blob: f7b75abf852be9fc96d3193b60aaddb50fe14c99 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                                


                                 
                    




                                

                            



                                                                                   
        

                                                      

                                                    

                                    








                                                               
                 












                                                               
                 













                                                                            

         

                                                                            



                                                                           

                                             







                                                                                               
                               
                 




















                                                                                          
         































                                                                                             

         





                                      

         
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();
	}
	public static long getLocalRevTimestamp() {
		if (localRevisionTime == null) {
			init();
			if (localRevisionTime == null)
				// fallback for dev purposes...
				localRevisionTime = 0L;
		}
		return localRevisionTime;
	}
	public static long getRemoteRevTimestamp() {
		if (remoteRevisionTime == null)
			init();
		return remoteRevisionTime;
	}
	public static String getLocalRevision() {
		if (localRevision == null) {
			init();
			if (localRevision == null)
				// fallback for dev purposes...
				localRevision = "dev";
		}
		return localRevision;
	}
	public static String getRemoteRevision() {
		if (remoteRevision == null)
			init();
		return remoteRevision;
	}
	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;
	}

	private static void loadLocalVersion() {
		// load local version information from the jar's MANIFEST.MF
		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);

		LOGGER.info("Local revision: " + localRevision);
		LOGGER.info("Local revision timestamp: " + localRevisionTime);
	}
	// per GSON
	private static void loadRemoteVersion() {
		Gson gson = new Gson();
		String json = null;
		BufferedReader reader = null;
		try {
			URL url = new URL("http://132.230.4.25/dozmod.version");
			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)
			remoteRevisionTime = query.timestamp;
		LOGGER.debug("Remote revision: " + remoteRevision);
		LOGGER.debug("Remote timestamp: " + remoteRevisionTime);
	}

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