001package org.anarres.cpp;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.net.URL;
006import java.text.ParseException;
007import java.text.SimpleDateFormat;
008import java.util.Date;
009import java.util.HashMap;
010import java.util.Map;
011import java.util.Properties;
012import javax.annotation.Nonnull;
013
014/**
015 * Returns information about the build.
016 *
017 * @author shevek
018 */
019public class BuildMetadata {
020
021    public static final String RESOURCE = "/META-INF/jcpp.properties";
022    private static BuildMetadata INSTANCE;
023
024    /** @throws RuntimeException if the properties file cannot be found on the classpath. */
025    @Nonnull
026    public static synchronized BuildMetadata getInstance() {
027        try {
028            if (INSTANCE == null)
029                INSTANCE = new BuildMetadata();
030            return INSTANCE;
031        } catch (IOException e) {
032            throw new RuntimeException(e);
033        }
034    }
035
036    private final Properties properties = new Properties();
037
038    private BuildMetadata() throws IOException {
039        URL url = BuildMetadata.class.getResource(RESOURCE);
040        InputStream in = url.openStream();
041        try {
042            properties.load(in);
043        } finally {
044            in.close();
045        }
046    }
047
048    @Nonnull
049    public Map<? extends String, ? extends String> asMap() {
050        Map<String, String> out = new HashMap<String, String>();
051        for (Map.Entry<Object, Object> e : properties.entrySet())
052            out.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
053        return out;
054    }
055
056    @Nonnull
057    public com.github.zafarkhaja.semver.Version getVersion() {
058        return com.github.zafarkhaja.semver.Version.valueOf(properties.getProperty("Implementation-Version"));
059    }
060
061    @Nonnull
062    public Date getBuildDate() throws ParseException {
063        // Build-Date=2015-01-01_10:09:09
064        String text = properties.getProperty("Build-Date");
065        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
066        return format.parse(text);
067    }
068
069    public String getChangeId() {
070        return properties.getProperty("Change");
071    }
072}