Subversion Repositories general

Rev

Rev 1024 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1024 dev 1
package ak.hostadmiral.util;
2
 
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.BufferedReader;
6
import java.io.InputStreamReader;
7
import java.util.regex.Pattern;
8
import java.util.regex.Matcher;
9
 
10
/**
11
 * Project version holder. Understands version from resource ak/hostadmiral/version
12
 * in format ${Major}.${Minor}-rc${ReleaseCanditat}.${Build}${Additional},
13
 * where ${ReleaseCanditat}, ${Build} and ${Additional} are optional.
1043 dev 14
 * ${Additional} - the rest.
1024 dev 15
 * Instead of "-rc${ReleaseCanditat}" "-p${Patch}" may exist.
16
 * E.g. 1.2-rc4.1234, 2.0, 2.1.4567M, 3.3-p1.
17
 */
18
public class ProjectVersion
19
{
20
	private static boolean initialized = false;
21
	private static String  version     = null;
22
	private static int     major       = -1;
23
	private static int     minor       = -1;
24
	private static int     rcandidat   = -1;
25
	private static int     patch       = -1;
26
	private static int     build       = -1;
27
	private static String  additional  = null;
28
 
29
	private static void init()
30
	{
31
		synchronized(ProjectVersion.class) {
32
			// in this method not exceptions are expected,
33
			// so throw an runtime exception if any problems
34
 
35
			if(initialized) return;
36
 
37
	        try {
38
	        	// get class loader
39
	        	ClassLoader cl = ProjectVersion.class.getClassLoader();
40
	        	if(cl == null) cl = ClassLoader.getSystemClassLoader();
41
	        	if(cl == null)
42
	        		throw new RuntimeException("Cannot get class loader, something is really wrong");
43
 
44
	            // load
45
	        	InputStream in = cl.getResourceAsStream("ak/hostadmiral/version");
46
	        	if(in == null)
47
	        		throw new RuntimeException("Cannot get project version, resource not found");
48
 
49
				BufferedReader reader = new BufferedReader(new InputStreamReader(in));
50
				version = reader.readLine();
51
 
52
				if(version == null || version.equals(""))
53
	        		throw new RuntimeException("Cannot get project version, it's empty");
54
 
55
	        	// parse
56
	            parse(version);
57
			}
58
			catch(IOException ex) {
59
				throw new RuntimeException("Cannot get project version: " + ex.getMessage());
60
			}
61
 
62
			initialized = true;
63
		}
64
	}
65
 
66
	private static void parse(String v)
67
	{
68
    	Pattern pattern = Pattern.compile(
1043 dev 69
    		"^(\\d+)\\.(\\d+)((-rc(\\d+))|(-p(\\d+)))?(\\.(\\d+))?(.*)$");
1024 dev 70
    	Matcher matcher = pattern.matcher(v);
71
    	if(!matcher.matches())
72
    		throw new RuntimeException(
73
    			"Cannot get project version, it doesn't match pattern: [" + v + "]");
74
 
75
    	major = Integer.parseInt(matcher.group(1));
76
    	minor = Integer.parseInt(matcher.group(2));
77
 
78
    	if(matcher.group(4) == null)
79
    		rcandidat = -1;
80
    	else
81
    		rcandidat = Integer.parseInt(matcher.group(5));
82
 
83
    	if(matcher.group(6) == null)
84
    		patch = -1;
85
    	else
86
    		patch = Integer.parseInt(matcher.group(7));
87
 
88
    	if(matcher.group(8) == null)
89
    		build = -1;
90
    	else
91
	    	build = Integer.parseInt(matcher.group(9));
92
 
93
    	additional = matcher.group(10);
94
	}
95
 
96
	public static String getVersion()
97
	{
98
		init();
99
		return version;
100
	}
101
 
102
	public static int getMajor()
103
	{
104
		init();
105
		return major;
106
	}
107
 
108
	public static int getMinor()
109
	{
110
		init();
111
		return minor;
112
	}
113
 
114
	public static int getRCandidat()
115
	{
116
		init();
117
		return rcandidat;
118
	}
119
 
120
	public static int getPatch()
121
	{
122
		init();
123
		return patch;
124
	}
125
 
126
	public static int getBuild()
127
	{
128
		init();
129
		return build;
130
	}
131
 
132
	public static String getAdditional()
133
	{
134
		init();
135
		return additional;
136
	}
137
 
138
	public static boolean sufficient(int major_)
139
	{
140
		return (major >= major_);
141
	}
142
 
143
	public static boolean sufficient(int major_, int minor_)
144
	{
145
		return (major > major_) || (major == major_) && (minor >= minor_);
146
	}
147
 
148
	public static boolean sufficient(int major_, int minor_, int patch_)
149
	{
150
		return (major > major_) || (major == major_) && (minor > minor_)
151
			|| (major == major_) && (minor == minor_) && (patch >= patch_);
152
	}
153
 
154
	private static String formString()
155
	{
156
		return ("" + major + "." + minor
157
			+ (rcandidat >= 0 ? "-rc" + rcandidat : "")
158
			+ (patch >= 0 ? "-p" + patch : "")
159
			+ (build >= 0 ? "." + build : "") + additional);
160
	}
161
 
162
	private static void testParse(String v)
163
	{
164
		parse(v);
165
		System.out.println("test: [" + v + "] [" + formString() + "]");
166
	}
167
 
168
	/**
169
	 * internal tests
170
	 */
171
	public static void main(String[] args)
172
		throws Exception
173
	{
174
		System.out.println("Current version is: [" + getVersion() + "] [" + formString() + "]");
175
		testParse("1.2-rc4.1234");
176
		testParse("2.0");
177
		testParse("2.1.4567M");
178
		testParse("3.3-p1");
179
	}
180
}