001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.plugin.jdbc.river;
017
018import java.io.ByteArrayOutputStream;
019import java.io.InputStream;
020import java.io.StringReader;
021import java.net.URL;
022import java.util.Enumeration;
023import java.util.Properties;
024
025public class Build {
026
027    private static final Build INSTANCE;
028
029    static {
030        String version = "NA";
031        String hash = "NA";
032        String hashShort = "NA";
033        String timestamp = "NA";
034        String date = "NA";
035
036        try {
037            String pluginName = JDBCRiverPlugin.class.getName();
038            Enumeration<URL> e = JDBCRiverPlugin.class.getClassLoader().getResources("es-plugin.properties");
039            while (e.hasMoreElements()) {
040                URL url = e.nextElement();
041                InputStream in = url.openStream();
042                ByteArrayOutputStream out = new ByteArrayOutputStream();
043                byte[] buffer = new byte[1024];
044                int len;
045                while ((len = in.read(buffer)) != -1) {
046                    out.write(buffer, 0, len);
047                }
048                in.close();
049                Properties props = new Properties();
050                props.load(new StringReader(out.toString("UTF-8")));
051                String plugin = props.getProperty("plugin");
052                if (pluginName.equals(plugin)) {
053                    version = props.getProperty("version");
054                    hash = props.getProperty("hash");
055                    if (!"NA".equals(hash)) {
056                        hashShort = hash.substring(0, 7);
057                    }
058                    timestamp = props.getProperty("timestamp");
059                    date = props.getProperty("date");
060                }
061            }
062        } catch (Throwable e) {
063            // just ignore...
064        }
065        INSTANCE = new Build(version, hash, hashShort, timestamp, date);
066    }
067
068    private String version;
069
070    private String hash;
071
072    private String hashShort;
073
074    private String timestamp;
075
076    private String date;
077
078    Build(String version, String hash, String hashShort, String timestamp, String date) {
079        this.version = version;
080        this.hash = hash;
081        this.hashShort = hashShort;
082        this.timestamp = timestamp;
083        this.date = date;
084    }
085
086    public static Build getInstance() {
087        return INSTANCE;
088    }
089
090    public String getVersion() {
091        return version;
092    }
093
094    public String getHash() {
095        return hash;
096    }
097
098    public String getShortHash() {
099        return hashShort;
100    }
101
102    public String getTimestamp() {
103        return timestamp;
104    }
105
106    public String getDate() {
107        return date;
108    }
109}