001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2016
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see http://www.gnu.org/licenses.
027 */
028package edu.wisc.ssec.mcidasv.util;
029
030import static edu.wisc.ssec.mcidasv.Constants.PROP_VERSION_MAJOR;
031import static edu.wisc.ssec.mcidasv.Constants.PROP_VERSION_MINOR;
032import static edu.wisc.ssec.mcidasv.Constants.PROP_VERSION_RELEASE;
033import static edu.wisc.ssec.mcidasv.Constants.PROP_BUILD_DATE;
034
035import java.io.InputStream;
036import java.util.Properties;
037
038/**
039 * This class is really just a wrapper around {@link #getVersion()}.
040 *
041 * <p>Note: this class is likely to be used in contexts where we don't
042 * have logging set up, so {@link System#err} and {@link System#out} are used.
043 */
044public class GetVer {
045
046    /**
047     * Open for reading, a resource of the specified name from the search path 
048     * used to load classes. This method locates the resource through the 
049     * system class loader.
050     * 
051     * @param name Resource to read.
052     * 
053     * @return An {@code InputStream} for reading the resource given by 
054     * {@code name}, or {@code null}.
055     */
056    public InputStream getResourceAsStream(String name) {
057        return ClassLoader.getSystemResourceAsStream(name);
058    }
059
060    /**
061     * Extracts and returns McIDAS-V version information.
062     * 
063     * @return {@code String} formatted like
064     * {@literal "McIDAS-V version VERSIONHERE built BUILDDATEHERE"}.
065     * 
066     * @throws Exception if {@code build.properties} could not be loaded into
067     * a {@code Properties} object.
068     */
069    public static String getVersion() throws Exception {
070        GetVer nonStaticInstance = new GetVer();
071        String version = "Unknown";
072        String date = "Unknown";
073        Properties props = new Properties();
074        InputStream is = nonStaticInstance.getResourceAsStream("edu/wisc/ssec/mcidasv/resources/build.properties");
075        if (is != null) {
076            props.load(is);
077            String maj = props.getProperty(PROP_VERSION_MAJOR, "0");
078            String min = props.getProperty(PROP_VERSION_MINOR, "0");
079            String rel = props.getProperty(PROP_VERSION_RELEASE, "");
080            date = props.getProperty(PROP_BUILD_DATE, "");
081            version = maj.concat(".").concat(min).concat(rel);
082        }
083        return "McIDAS-V version "+version+" built "+date;
084    }
085
086    /**
087     * The main. Get McIDAS-V version and build data and print it out.
088     * 
089     * @param args Ignored.
090     */
091    public static void main(String[] args) {
092        try {
093            System.out.println(getVersion());
094        } catch (Exception e) {
095            System.err.println("Error getting McIDAS-V version: "+e);
096        }
097    }
098}