mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
15.51.2019 91c24ab5c6bafe0b3f5a83b018ed6feb31cfc225
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
package de.micromata.borgbutler.server;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;
 
public class Version {
    private Logger log = LoggerFactory.getLogger(Version.class);
    private String appName;
    private String version;
    private String buildDateUTC;
    private Date buildDate;
    private String updateVersion;
 
    private static final Version instance = new Version();
 
    public static Version getInstance() {
        instance.init();
        return instance;
    }
 
    private void init() {
        synchronized (this) {
            if (appName != null) {
                return;
            }
            try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("version.properties")) {
                if (inputStream == null) {
                    log.warn("version.properties not found (OK, if started e. g. in IDE");
                    version = "99.0";
                    appName = "BorgButler";
                    buildDate = new Date();
                } else {
                    Properties props = new Properties();
                    props.load(inputStream);
                    appName = props.getProperty("name");
                    version = props.getProperty("version");
                    String buildDateMillisString = props.getProperty("build.date.millis");
                    long buildDateMillis = 0;
                    if (buildDateMillisString != null) {
                        try {
                            buildDateMillis = Long.parseLong(buildDateMillisString);
                        } catch (NumberFormatException ex) {
                            log.error("Can't parse build date (millis expected): " + buildDateMillisString + ": " + ex.getMessage(), ex);
                        }
                    }
                    buildDate = new Date(buildDateMillis);
                }
            } catch (Exception ex) {
                log.error("Can't load version information from classpath. File 'version.properties' not found: " + ex.getMessage(), ex);
                appName = "BorgButler";
                version = "?.?";
                buildDateUTC = "1970-01-01 00:00:00";
                return;
            }
            buildDateUTC = formatBuildDateISO(TimeZone.getTimeZone("UTC"));
            log.debug("appName=" + appName + ", version=" + version + ", buildDateUTC=" + buildDateUTC);
        }
    }
 
    public String formatBuildDateISO(TimeZone timeZone) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
        formatter.setTimeZone(timeZone);
        return formatter.format(buildDate);
    }
 
    public String getAppName() {
        return appName;
    }
 
    public String getVersion() {
        return version;
    }
 
    /**
     * Replaces -SNAPSHOT by dev for snapshot versions. For none snapshot releases the returned value is equal to the version.
     *
     * @return the version as string.
     */
    public String getShortVersion() {
        return version.replace("-SNAPSHOT", "dev");
    }
 
    public String getBuildDateUTC() {
        return buildDateUTC;
    }
 
    public Date getBuildDate() {
        return buildDate;
    }
 
    /**
     * @return Version of the available update, if exist. Otherwise null.
     */
    public String getUpdateVersion() {
        return updateVersion;
    }
 
    public void setUpdateVersion(String updateVersion) {
        this.updateVersion = updateVersion;
    }
}