001
002package org.xbib.elasticsearch.plugin.analysis.standardnumber;
003
004import java.io.ByteArrayOutputStream;
005import java.io.InputStream;
006import java.io.StringReader;
007import java.net.URL;
008import java.util.Enumeration;
009import java.util.Properties;
010
011public class Build {
012
013    private static final Build INSTANCE;
014
015    static {
016        String version = "NA";
017        String hash = "NA";
018        String hashShort = "NA";
019        String timestamp = "NA";
020        String date = "NA";
021
022        try {
023            String pluginName = AnalysisStandardNumberPlugin.class.getName();
024            Enumeration<URL> e = AnalysisStandardNumberPlugin.class.getClassLoader().getResources("es-plugin.properties");
025            while (e.hasMoreElements()) {
026                URL url = e.nextElement();
027                InputStream in = url.openStream();
028                ByteArrayOutputStream out = new ByteArrayOutputStream();
029                byte[] buffer = new byte[1024];
030                int len;
031                while ((len = in.read(buffer)) != -1) {
032                    out.write(buffer, 0, len);
033                }
034                in.close();
035                Properties props = new Properties();
036                props.load(new StringReader(new String(out.toByteArray())));
037                String plugin = props.getProperty("plugin");
038                if (pluginName.equals(plugin)) {
039                    version = props.getProperty("version");
040                    hash = props.getProperty("hash");
041                    if (!"NA".equals(hash)) {
042                        hashShort = hash.substring(0, 7);
043                    }
044                    timestamp = props.getProperty("timestamp");
045                    date = props.getProperty("date");
046                }
047            }
048        } catch (Throwable e) {
049            // just ignore...
050        }
051        INSTANCE = new Build(version, hash, hashShort, timestamp, date);
052    }
053
054    private String version;
055
056    private String hash;
057
058    private String hashShort;
059
060    private String timestamp;
061
062    private String date;
063
064    Build(String version, String hash, String hashShort, String timestamp, String date) {
065        this.version = version;
066        this.hash = hash;
067        this.hashShort = hashShort;
068        this.timestamp = timestamp;
069        this.date = date;
070    }
071
072    public static Build getInstance() {
073        return INSTANCE;
074    }
075
076    public String getVersion() {
077        return version;
078    }
079
080    public String getHash() {
081        return hash;
082    }
083
084    public String getShortHash() {
085        return hashShort;
086    }
087
088    public String getTimestamp() {
089        return timestamp;
090    }
091
092    public String getDate() {
093        return date;
094    }
095}