VersionUtil.java

package ui.common.util;

import ui.entities.config.SidPlay2Section;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URLConnection;
import java.util.Properties;

import static ui.common.util.InternetUtil.openConnection;

public class VersionUtil {

    private static final String LOCAL_VERSION_RESOURCE = "/META-INF/maven/jsidplay2/jsidplay2/pom.properties";

    private static final String ONLINE_VERSION_RESOURCE = "https://sourceforge.net/p/jsidplay2/git/ci/master/tree/latest.properties?format=raw";

    public static final String VERSION;

    static {
        String version;
        try {
            URLConnection connection = VersionUtil.class.getResource(LOCAL_VERSION_RESOURCE).openConnection();
            version = getVersion(connection);
        } catch (Exception e) {
            version = "(beta)";
        }
        VERSION = version;
    }

    public static String fetchRemoteVersion(SidPlay2Section sidplay2Section) {
        try {
            URLConnection connection = openConnection(new URI(ONLINE_VERSION_RESOURCE).toURL(), sidplay2Section);
            return getVersion(connection);
        } catch (Exception e) {
            return null;
        }
    }

    private static String getVersion(URLConnection connection) throws IOException {
        try (InputStream inputStream = connection.getInputStream()) {
            Properties latestProperties = new Properties();
            latestProperties.load(inputStream);
            return latestProperties.getProperty("version");
        }
    }
}