aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/anarres/cpp/BuildMetadata.java
blob: 79de407f4861638db1abe5406c638fadf5b5fb67 (plain)
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
package org.anarres.cpp;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Nonnull;

/**
 * Returns information about the build.
 *
 * @author shevek
 */
public class BuildMetadata {

    public static final String RESOURCE = "/META-INF/jcpp.properties";
    private static BuildMetadata INSTANCE;

    /** @throws RuntimeException if the properties file cannot be found on the classpath. */
    @Nonnull
    public static synchronized BuildMetadata getInstance() {
        try {
            if (INSTANCE == null)
                INSTANCE = new BuildMetadata();
            return INSTANCE;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private final Properties properties = new Properties();

    private BuildMetadata() throws IOException {
        URL url = BuildMetadata.class.getResource(RESOURCE);
        InputStream in = url.openStream();
        try {
            properties.load(in);
        } finally {
            in.close();
        }
    }

    @Nonnull
    public Map<? extends String, ? extends String> asMap() {
        Map<String, String> out = new HashMap<String, String>();
        for (Map.Entry<Object, Object> e : properties.entrySet())
            out.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
        return out;
    }

    @Nonnull
    public com.github.zafarkhaja.semver.Version getVersion() {
        return com.github.zafarkhaja.semver.Version.valueOf(properties.getProperty("Implementation-Version"));
    }

    @Nonnull
    public Date getBuildDate() throws ParseException {
        // Build-Date=2015-01-01_10:09:09
        String text = properties.getProperty("Build-Date");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
        return format.parse(text);
    }

    public String getChangeId() {
        return properties.getProperty("Change");
    }
}