View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * This class finds the Version information for HBase.
33   */
34  @InterfaceAudience.Public
35  @InterfaceStability.Evolving
36  public class VersionInfo {
37    private static final Log LOG = LogFactory.getLog(VersionInfo.class.getName());
38  
39    // If between two dots there is not a number, we regard it as a very large number so it is
40    // higher than any numbers in the version.
41    private static int VERY_LARGE_NUMBER = 100000;
42  
43    /**
44     * Get the hbase version.
45     * @return the hbase version string, eg. "0.6.3-dev"
46     */
47    public static String getVersion() {
48      return Version.version;
49    }
50  
51    /**
52     * Get the subversion revision number for the root directory
53     * @return the revision number, eg. "451451"
54     */
55    public static String getRevision() {
56      return Version.revision;
57    }
58  
59    /**
60     * The date that hbase was compiled.
61     * @return the compilation date in unix date format
62     */
63    public static String getDate() {
64      return Version.date;
65    }
66  
67    /**
68     * The user that compiled hbase.
69     * @return the username of the user
70     */
71    public static String getUser() {
72      return Version.user;
73    }
74  
75    /**
76     * Get the subversion URL for the root hbase directory.
77     * @return the url
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     * Get the checksum of the source files from which Hadoop was compiled.
94     * @return a string that uniquely identifies the source
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     //fast compare equals first
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    * Returns the version components as String objects
140    * Examples: "1.2.3" returns ["1", "2", "3"], "4.5.6-SNAPSHOT" returns ["4", "5", "6", "-1"]
141    * "4.5.6-beta" returns ["4", "5", "6", "-2"], "4.5.6-alpha" returns ["4", "5", "6", "-3"]
142    * "4.5.6-UNKNOW" returns ["4", "5", "6", "-4"]
143    * @return the components of the version string
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 }