1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import org.apache.commons.logging.LogFactory;
22 import java.io.PrintStream;
23 import java.io.PrintWriter;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.hadoop.hbase.Version;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30
31
32
33
34 @InterfaceAudience.Public
35 @InterfaceStability.Evolving
36 public class VersionInfo {
37 private static final Log LOG = LogFactory.getLog(VersionInfo.class.getName());
38
39
40
41 private static int VERY_LARGE_NUMBER = 100000;
42
43
44
45
46
47 public static String getVersion() {
48 return Version.version;
49 }
50
51
52
53
54
55 public static String getRevision() {
56 return Version.revision;
57 }
58
59
60
61
62
63 public static String getDate() {
64 return Version.date;
65 }
66
67
68
69
70
71 public static String getUser() {
72 return Version.user;
73 }
74
75
76
77
78
79 public static String getUrl() {
80 return Version.url;
81 }
82
83 static String[] versionReport() {
84 return new String[] {
85 "HBase " + getVersion(),
86 "Source code repository " + getUrl() + " revision=" + getRevision(),
87 "Compiled by " + getUser() + " on " + getDate(),
88 "From source with checksum " + getSrcChecksum()
89 };
90 }
91
92
93
94
95
96 public static String getSrcChecksum() {
97 return Version.srcChecksum;
98 }
99
100 public static void writeTo(PrintWriter out) {
101 for (String line : versionReport()) {
102 out.println(line);
103 }
104 }
105
106 public static void writeTo(PrintStream out) {
107 for (String line : versionReport()) {
108 out.println(line);
109 }
110 }
111
112 public static void logVersion() {
113 for (String line : versionReport()) {
114 LOG.info(line);
115 }
116 }
117
118 public static int compareVersion(String v1, String v2) {
119
120 if (v1.equals(v2)) {
121 return 0;
122 }
123 String[] v1Comps = getVersionComponents(v1);
124 String[] v2Comps = getVersionComponents(v2);
125
126 int length = Math.max(v1Comps.length, v2Comps.length);
127 for (int i = 0; i < length; i++) {
128 Integer va = i < v1Comps.length ? Integer.parseInt(v1Comps[i]) : 0;
129 Integer vb = i < v2Comps.length ? Integer.parseInt(v2Comps[i]) : 0;
130 int compare = va.compareTo(vb);
131 if (compare != 0) {
132 return compare;
133 }
134 }
135 return 0;
136 }
137
138
139
140
141
142
143
144
145 private static String[] getVersionComponents(final String version) {
146 assert(version != null);
147 String[] strComps = version.split("[\\.-]");
148 assert(strComps.length > 0);
149
150 String[] comps = new String[strComps.length];
151 for (int i = 0; i < strComps.length; ++i) {
152 if (StringUtils.isEmpty(strComps[i])) {
153 comps[i] = String.valueOf(VERY_LARGE_NUMBER);
154 } else if (StringUtils.isNumeric(strComps[i])) {
155 comps[i] = strComps[i];
156 } else {
157 if("SNAPSHOT".equals(strComps[i])) {
158 comps[i] = "-1";
159 } else if("beta".equals(strComps[i])) {
160 comps[i] = "-2";
161 } else if("alpha".equals(strComps[i])) {
162 comps[i] = "-3";
163 } else {
164 comps[i] = "-4";
165 }
166 }
167 }
168 return comps;
169 }
170
171 public static void main(String[] args) {
172 writeTo(System.out);
173 }
174 }