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.PrintWriter;
23
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.classification.InterfaceStability;
26 import org.apache.hadoop.hbase.VersionAnnotation;
27 import org.apache.commons.logging.Log;
28
29
30
31
32
33 @InterfaceAudience.Public
34 @InterfaceStability.Evolving
35 public class VersionInfo {
36 private static final Log LOG = LogFactory.getLog(VersionInfo.class.getName());
37 private static Package myPackage;
38 private static VersionAnnotation version;
39
40 static {
41 myPackage = VersionAnnotation.class.getPackage();
42 version = myPackage.getAnnotation(VersionAnnotation.class);
43 }
44
45
46
47
48
49 static Package getPackage() {
50 return myPackage;
51 }
52
53
54
55 private static int VERY_LARGE_NUMBER = 100000;
56
57
58
59
60
61 public static String getVersion() {
62 return version != null ? version.version() : "Unknown";
63 }
64
65
66
67
68
69 public static String getRevision() {
70 return version != null ? version.revision() : "Unknown";
71 }
72
73
74
75
76
77 public static String getDate() {
78 return version != null ? version.date() : "Unknown";
79 }
80
81
82
83
84
85 public static String getUser() {
86 return version != null ? version.user() : "Unknown";
87 }
88
89
90
91
92
93 public static String getUrl() {
94 return version != null ? version.url() : "Unknown";
95 }
96
97 static String[] versionReport() {
98 return new String[] {
99 "HBase " + getVersion(),
100 "Source code repository " + getUrl() + " revision=" + getRevision(),
101 "Compiled by " + getUser() + " on " + getDate(),
102 "From source with checksum " + getSrcChecksum()
103 };
104 }
105
106
107
108
109
110 public static String getSrcChecksum() {
111 return version != null ? version.srcChecksum() : "Unknown";
112 }
113
114 public static void writeTo(PrintWriter out) {
115 for (String line : versionReport()) {
116 out.println(line);
117 }
118 }
119
120 public static void logVersion() {
121 for (String line : versionReport()) {
122 LOG.info(line);
123 }
124 }
125
126 public static int compareVersion(String v1, String v2) {
127
128 if (v1.equals(v2)) {
129 return 0;
130 }
131
132 String s1[] = v1.split("\\.|-");
133 String s2[] = v2.split("\\.|-");
134 int index = 0;
135 while (index < s1.length && index < s2.length) {
136 int va = VERY_LARGE_NUMBER, vb = VERY_LARGE_NUMBER;
137 try {
138 va = Integer.parseInt(s1[index]);
139 } catch (Exception ingore) {
140 }
141 try {
142 vb = Integer.parseInt(s2[index]);
143 } catch (Exception ingore) {
144 }
145 if (va != vb) {
146 return va - vb;
147 }
148 if (va == VERY_LARGE_NUMBER) {
149
150 int c = s1[index].compareTo(s2[index]);
151 if (c != 0) {
152 return c;
153 }
154 }
155 index++;
156 }
157 if (index < s1.length) {
158
159 return 1;
160 }
161
162 return -1;
163 }
164
165
166 public static void main(String[] args) {
167 logVersion();
168 }
169 }