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.hadoop.hbase.Version;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  
30  /**
31   * This class finds the Version information for HBase.
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Evolving
35  public class VersionInfo {
36    private static final Log LOG = LogFactory.getLog(VersionInfo.class.getName());
37  
38    // If between two dots there is not a number, we regard it as a very large number so it is
39    // higher than any numbers in the version.
40    private static int VERY_LARGE_NUMBER = 100000;
41  
42    /**
43     * Get the hbase version.
44     * @return the hbase version string, eg. "0.6.3-dev"
45     */
46    public static String getVersion() {
47      return Version.version;
48    }
49  
50    /**
51     * Get the subversion revision number for the root directory
52     * @return the revision number, eg. "451451"
53     */
54    public static String getRevision() {
55      return Version.revision;
56    }
57  
58    /**
59     * The date that hbase was compiled.
60     * @return the compilation date in unix date format
61     */
62    public static String getDate() {
63      return Version.date;
64    }
65  
66    /**
67     * The user that compiled hbase.
68     * @return the username of the user
69     */
70    public static String getUser() {
71      return Version.user;
72    }
73  
74    /**
75     * Get the subversion URL for the root hbase directory.
76     * @return the url
77     */
78    public static String getUrl() {
79      return Version.url;
80    }
81  
82    static String[] versionReport() {
83      return new String[] {
84        "HBase " + getVersion(),
85        "Source code repository " + getUrl() + " revision=" + getRevision(),
86        "Compiled by " + getUser() + " on " + getDate(),
87        "From source with checksum " + getSrcChecksum()
88        };
89    }
90  
91    /**
92     * Get the checksum of the source files from which Hadoop was compiled.
93     * @return a string that uniquely identifies the source
94     **/
95    public static String getSrcChecksum() {
96      return Version.srcChecksum;
97    }
98  
99    public static void writeTo(PrintWriter out) {
100     for (String line : versionReport()) {
101       out.println(line);
102     }
103   }
104 
105   public static void writeTo(PrintStream out) {
106     for (String line : versionReport()) {
107       out.println(line);
108     }
109   }
110 
111   public static void logVersion() {
112     for (String line : versionReport()) {
113       LOG.info(line);
114     }
115   }
116 
117   public static int compareVersion(String v1, String v2) {
118     //fast compare equals first
119     if (v1.equals(v2)) {
120       return 0;
121     }
122 
123     String s1[] = v1.split("\\.|-");//1.2.3-hotfix -> [1, 2, 3, hotfix]
124     String s2[] = v2.split("\\.|-");
125     int index = 0;
126     while (index < s1.length && index < s2.length) {
127       int va = VERY_LARGE_NUMBER, vb = VERY_LARGE_NUMBER;
128       try {
129         va = Integer.parseInt(s1[index]);
130       } catch (Exception ingore) {
131       }
132       try {
133         vb = Integer.parseInt(s2[index]);
134       } catch (Exception ingore) {
135       }
136       if (va != vb) {
137         return va - vb;
138       }
139       if (va == VERY_LARGE_NUMBER) {
140         // compare as String
141         int c = s1[index].compareTo(s2[index]);
142         if (c != 0) {
143           return c;
144         }
145       }
146       index++;
147     }
148     if (index < s1.length) {
149       // s1 is longer
150       return 1;
151     }
152     //s2 is longer
153     return -1;
154   }
155 
156 
157   public static void main(String[] args) {
158     writeTo(System.out);
159   }
160 }