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.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   * This class finds the package info for hbase and the VersionAnnotation
31   * information.  Taken from hadoop.  Only name of annotation is different.
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     * Get the meta-data for the hbase package.
47     * @return package
48     */
49    static Package getPackage() {
50      return myPackage;
51    }
52  
53    // If between two dots there is not a number, we regard it as a very large number so it is
54    // higher than any numbers in the version.
55    private static int VERY_LARGE_NUMBER = 100000;
56  
57    /**
58     * Get the hbase version.
59     * @return the hbase version string, eg. "0.6.3-dev"
60     */
61    public static String getVersion() {
62      return version != null ? version.version() : "Unknown";
63    }
64  
65    /**
66     * Get the subversion revision number for the root directory
67     * @return the revision number, eg. "451451"
68     */
69    public static String getRevision() {
70      return version != null ? version.revision() : "Unknown";
71    }
72  
73    /**
74     * The date that hbase was compiled.
75     * @return the compilation date in unix date format
76     */
77    public static String getDate() {
78      return version != null ? version.date() : "Unknown";
79    }
80  
81    /**
82     * The user that compiled hbase.
83     * @return the username of the user
84     */
85    public static String getUser() {
86      return version != null ? version.user() : "Unknown";
87    }
88  
89    /**
90     * Get the subversion URL for the root hbase directory.
91     * @return the url
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    * Get the checksum of the source files from which Hadoop was compiled.
108    * @return a string that uniquely identifies the source
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     //fast compare equals first
128     if (v1.equals(v2)) {
129       return 0;
130     }
131 
132     String s1[] = v1.split("\\.|-");//1.2.3-hotfix -> [1, 2, 3, hotfix]
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         // compare as String
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       // s1 is longer
159       return 1;
160     }
161     //s2 is longer
162     return -1;
163   }
164 
165 
166   public static void main(String[] args) {
167     logVersion();
168   }
169 }