001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.util;
020
021import java.io.PrintStream;
022import java.io.PrintWriter;
023
024import org.apache.hadoop.hbase.Version;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * This class finds the Version information for HBase.
031 */
032@InterfaceAudience.Public
033public class VersionInfo {
034  private static final Logger LOG = LoggerFactory.getLogger(VersionInfo.class.getName());
035
036  // If between two dots there is not a number, we regard it as a very large number so it is
037  // higher than any numbers in the version.
038  private static final int VERY_LARGE_NUMBER = 100000;
039
040  /**
041   * Get the hbase version.
042   * @return the hbase version string, eg. "0.6.3-dev"
043   */
044  public static String getVersion() {
045    return Version.version;
046  }
047
048  /**
049   * Get the subversion revision number for the root directory
050   * @return the revision number, eg. "451451"
051   */
052  public static String getRevision() {
053    return Version.revision;
054  }
055
056  /**
057   * The date that hbase was compiled.
058   * @return the compilation date in unix date format
059   */
060  public static String getDate() {
061    return Version.date;
062  }
063
064  /**
065   * The user that compiled hbase.
066   * @return the username of the user
067   */
068  public static String getUser() {
069    return Version.user;
070  }
071
072  /**
073   * Get the subversion URL for the root hbase directory.
074   * @return the url
075   */
076  public static String getUrl() {
077    return Version.url;
078  }
079
080  static String[] versionReport() {
081    return new String[] {
082      "HBase " + getVersion(),
083      "Source code repository " + getUrl() + " revision=" + getRevision(),
084      "Compiled by " + getUser() + " on " + getDate(),
085      "From source with checksum " + getSrcChecksum()
086      };
087  }
088
089  /**
090   * Get the checksum of the source files from which Hadoop was compiled.
091   * @return a string that uniquely identifies the source
092   **/
093  public static String getSrcChecksum() {
094    return Version.srcChecksum;
095  }
096
097  public static void writeTo(PrintWriter out) {
098    for (String line : versionReport()) {
099      out.println(line);
100    }
101  }
102
103  public static void writeTo(PrintStream out) {
104    for (String line : versionReport()) {
105      out.println(line);
106    }
107  }
108
109  public static void logVersion() {
110    for (String line : versionReport()) {
111      LOG.info(line);
112    }
113  }
114
115  public static int compareVersion(String v1, String v2) {
116    //fast compare equals first
117    if (v1.equals(v2)) {
118      return 0;
119    }
120
121    Object[] v1Comps = getVersionComponents(v1); //1.2.3-hotfix -> [1, 2, 3, hotfix]
122    Object[] v2Comps = getVersionComponents(v2);
123    int index = 0;
124    while (index < v1Comps.length && index < v2Comps.length) {
125      int va = v1Comps[index] instanceof Integer ? (Integer)v1Comps[index] : VERY_LARGE_NUMBER;
126      int vb = v2Comps[index] instanceof Integer ? (Integer)v2Comps[index] : VERY_LARGE_NUMBER;
127
128      if (va != vb) {
129        return va - vb;
130      }
131      if (va == VERY_LARGE_NUMBER) {
132        // here, va and vb components must be same and Strings, compare as String
133        int c = ((String)v1Comps[index]).compareTo((String)v2Comps[index]);
134        if (c != 0) {
135          return c;
136        }
137      }
138      index++;
139    }
140    if (index < v1Comps.length) {
141      // v1 is longer
142      return 1;
143    }
144    //v2 is longer
145    return -1;
146  }
147
148  /**
149   * Returns the version components as Integer and String objects
150   * Examples: "1.2.3" returns [1, 2, 3], "4.5.6-SNAPSHOT" returns [4, 5, 6, "SNAPSHOT"]
151   * @return the components of the version string
152   */
153  static Object[] getVersionComponents(final String version) {
154    assert(version != null);
155    Object[] strComps = version.split("[\\.-]");
156    assert(strComps.length > 0);
157
158    Object[] comps = new Object[strComps.length];
159    for (int i = 0; i < strComps.length; ++i) {
160      try {
161        comps[i] = Integer.parseInt((String) strComps[i]);
162      } catch (NumberFormatException e) {
163        comps[i] = strComps[i];
164      }
165    }
166    return comps;
167  }
168
169  public static void main(String[] args) {
170    writeTo(System.out);
171  }
172}