Subversion Repositories general

Rev

Rev 1024 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package ak.hostadmiral.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 * Project version holder. Understands version from resource ak/hostadmiral/version
 * in format ${Major}.${Minor}-rc${ReleaseCanditat}.${Build}${Additional},
 * where ${ReleaseCanditat}, ${Build} and ${Additional} are optional.
 * ${Additional} contains letters only.
 * Instead of "-rc${ReleaseCanditat}" "-p${Patch}" may exist.
 * E.g. 1.2-rc4.1234, 2.0, 2.1.4567M, 3.3-p1.
 */
public class ProjectVersion
{
        private static boolean initialized = false;
        private static String  version     = null;
        private static int     major       = -1;
        private static int     minor       = -1;
        private static int     rcandidat   = -1;
        private static int     patch       = -1;
        private static int     build       = -1;
        private static String  additional  = null;

        private static void init()
        {
                synchronized(ProjectVersion.class) {
                        // in this method not exceptions are expected,
                        // so throw an runtime exception if any problems

                        if(initialized) return;

                try {
                        // get class loader
                        ClassLoader cl = ProjectVersion.class.getClassLoader();
                        if(cl == null) cl = ClassLoader.getSystemClassLoader();
                        if(cl == null)
                                throw new RuntimeException("Cannot get class loader, something is really wrong");

                    // load
                        InputStream in = cl.getResourceAsStream("ak/hostadmiral/version");
                        if(in == null)
                                throw new RuntimeException("Cannot get project version, resource not found");

                                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                                version = reader.readLine();

                                if(version == null || version.equals(""))
                                throw new RuntimeException("Cannot get project version, it's empty");

                        // parse
                    parse(version);
                        }
                        catch(IOException ex) {
                                throw new RuntimeException("Cannot get project version: " + ex.getMessage());
                        }

                        initialized = true;
                }
        }

        private static void parse(String v)
        {
        Pattern pattern = Pattern.compile(
                "^(\\d+)\\.(\\d+)((-rc(\\d+))|(-p(\\d+)))?(\\.(\\d+))?([a-zA-Z]*)$");
        Matcher matcher = pattern.matcher(v);
        if(!matcher.matches())
                throw new RuntimeException(
                        "Cannot get project version, it doesn't match pattern: [" + v + "]");

        major = Integer.parseInt(matcher.group(1));
        minor = Integer.parseInt(matcher.group(2));

        if(matcher.group(4) == null)
                rcandidat = -1;
        else
                rcandidat = Integer.parseInt(matcher.group(5));

        if(matcher.group(6) == null)
                patch = -1;
        else
                patch = Integer.parseInt(matcher.group(7));

        if(matcher.group(8) == null)
                build = -1;
        else
                build = Integer.parseInt(matcher.group(9));

        additional = matcher.group(10);
        }

        public static String getVersion()
        {
                init();
                return version;
        }

        public static int getMajor()
        {
                init();
                return major;
        }

        public static int getMinor()
        {
                init();
                return minor;
        }

        public static int getRCandidat()
        {
                init();
                return rcandidat;
        }

        public static int getPatch()
        {
                init();
                return patch;
        }

        public static int getBuild()
        {
                init();
                return build;
        }

        public static String getAdditional()
        {
                init();
                return additional;
        }

        public static boolean sufficient(int major_)
        {
                return (major >= major_);
        }

        public static boolean sufficient(int major_, int minor_)
        {
                return (major > major_) || (major == major_) && (minor >= minor_);
        }

        public static boolean sufficient(int major_, int minor_, int patch_)
        {
                return (major > major_) || (major == major_) && (minor > minor_)
                        || (major == major_) && (minor == minor_) && (patch >= patch_);
        }

        private static String formString()
        {
                return ("" + major + "." + minor
                        + (rcandidat >= 0 ? "-rc" + rcandidat : "")
                        + (patch >= 0 ? "-p" + patch : "")
                        + (build >= 0 ? "." + build : "") + additional);
        }

        private static void testParse(String v)
        {
                parse(v);
                System.out.println("test: [" + v + "] [" + formString() + "]");
        }

        /**
         * internal tests
         */
        public static void main(String[] args)
                throws Exception
        {
                System.out.println("Current version is: [" + getVersion() + "] [" + formString() + "]");
                testParse("1.2-rc4.1234");
                testParse("2.0");
                testParse("2.1.4567M");
                testParse("3.3-p1");
        }
}