001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
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 https://www.gnu.org/licenses/.
027 */
028
029package edu.wisc.ssec.mcidasv.util;
030
031import static edu.wisc.ssec.mcidasv.Constants.PROP_VERSION_MAJOR;
032import static edu.wisc.ssec.mcidasv.Constants.PROP_VERSION_MINOR;
033import static edu.wisc.ssec.mcidasv.Constants.PROP_VERSION_RELEASE;
034import static edu.wisc.ssec.mcidasv.Constants.PROP_BUILD_DATE;
035
036import java.io.InputStream;
037import java.util.Properties;
038
039/**
040 * This class is really just a wrapper around {@link #getVersion()}.
041 *
042 * <p>Note: this class is likely to be used in contexts where we don't
043 * have logging set up, so {@link System#err} and {@link System#out} are used.
044 */
045public class GetVer {
046
047    /**
048     * Open for reading, a resource of the specified name from the search path 
049     * used to load classes. This method locates the resource through the 
050     * system class loader.
051     * 
052     * @param name Resource to read.
053     * 
054     * @return An {@code InputStream} for reading the resource given by 
055     * {@code name}, or {@code null}.
056     */
057    public InputStream getResourceAsStream(String name) {
058        return ClassLoader.getSystemResourceAsStream(name);
059    }
060
061    /**
062     * Extracts and returns McIDAS-V version information.
063     * 
064     * @return {@code String} formatted like
065     * {@literal "McIDAS-V version VERSIONHERE built BUILDDATEHERE"}.
066     * 
067     * @throws Exception if {@code build.properties} could not be loaded into
068     * a {@code Properties} object.
069     */
070    public static String getVersion() throws Exception {
071        GetVer nonStaticInstance = new GetVer();
072        String version = "Unknown";
073        String date = "Unknown";
074        Properties props = new Properties();
075        InputStream is = nonStaticInstance.getResourceAsStream("edu/wisc/ssec/mcidasv/resources/build.properties");
076        if (is != null) {
077            props.load(is);
078            String maj = props.getProperty(PROP_VERSION_MAJOR, "0");
079            String min = props.getProperty(PROP_VERSION_MINOR, "0");
080            String rel = props.getProperty(PROP_VERSION_RELEASE, "");
081            date = props.getProperty(PROP_BUILD_DATE, "");
082            version = maj.concat(".").concat(min).concat(rel);
083        }
084        return "McIDAS-V version "+version+" built "+date;
085    }
086
087    /**
088     * The main. Get McIDAS-V version and build data and print it out.
089     * 
090     * @param args Ignored.
091     */
092    public static void main(String[] args) {
093        try {
094            System.out.println(getVersion());
095        } catch (Exception e) {
096            System.err.println("Error getting McIDAS-V version: "+e);
097        }
098    }
099}