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.commons.lang3.StringUtils;
025import org.apache.hadoop.hbase.Version;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * This class finds the Version information for HBase.
032 */
033@InterfaceAudience.Public
034public class VersionInfo {
035  private static final Logger LOG = LoggerFactory.getLogger(VersionInfo.class.getName());
036
037  // If between two dots there is not a number, we regard it as a very large number so it is
038  // higher than any numbers in the version.
039  private static final int VERY_LARGE_NUMBER = 100000;
040
041  /**
042   * Get the hbase version.
043   * @return the hbase version string, eg. "0.6.3-dev"
044   */
045  public static String getVersion() {
046    return Version.version;
047  }
048
049  /**
050   * Get the subversion revision number for the root directory
051   * @return the revision number, eg. "451451"
052   */
053  public static String getRevision() {
054    return Version.revision;
055  }
056
057  /**
058   * The date that hbase was compiled.
059   * @return the compilation date in unix date format
060   */
061  public static String getDate() {
062    return Version.date;
063  }
064
065  /**
066   * The user that compiled hbase.
067   * @return the username of the user
068   */
069  public static String getUser() {
070    return Version.user;
071  }
072
073  /**
074   * Get the subversion URL for the root hbase directory.
075   * @return the url
076   */
077  public static String getUrl() {
078    return Version.url;
079  }
080
081  static String[] versionReport() {
082    return new String[] {
083      "HBase " + getVersion(),
084      "Source code repository " + getUrl() + " revision=" + getRevision(),
085      "Compiled by " + getUser() + " on " + getDate(),
086      "From source with checksum " + getSrcChecksum()
087      };
088  }
089
090  /**
091   * Get the checksum of the source files from which Hadoop was compiled.
092   * @return a string that uniquely identifies the source
093   **/
094  public static String getSrcChecksum() {
095    return Version.srcChecksum;
096  }
097
098  public static void writeTo(PrintWriter out) {
099    for (String line : versionReport()) {
100      out.println(line);
101    }
102  }
103
104  public static void writeTo(PrintStream out) {
105    for (String line : versionReport()) {
106      out.println(line);
107    }
108  }
109
110  public static void logVersion() {
111    for (String line : versionReport()) {
112      LOG.info(line);
113    }
114  }
115
116  public static int compareVersion(String v1, String v2) {
117    //fast compare equals first
118    if (v1.equals(v2)) {
119      return 0;
120    }
121    String[] v1Comps = getVersionComponents(v1);
122    String[] v2Comps = getVersionComponents(v2);
123
124    int length = Math.max(v1Comps.length, v2Comps.length);
125    for (int i = 0; i < length; i++) {
126      Integer va = i < v1Comps.length ? Integer.parseInt(v1Comps[i]) : 0;
127      Integer vb = i < v2Comps.length ? Integer.parseInt(v2Comps[i]) : 0;
128      int compare = va.compareTo(vb);
129      if (compare != 0) {
130        return compare;
131      }
132    }
133    return 0;
134  }
135
136  /**
137   * Returns the version components as String objects
138   * Examples: "1.2.3" returns ["1", "2", "3"], "4.5.6-SNAPSHOT" returns ["4", "5", "6", "-1"]
139   * "4.5.6-beta" returns ["4", "5", "6", "-2"], "4.5.6-alpha" returns ["4", "5", "6", "-3"]
140   * "4.5.6-UNKNOW" returns ["4", "5", "6", "-4"]
141   * @return the components of the version string
142   */
143  private static String[] getVersionComponents(final String version) {
144    assert(version != null);
145    String[] strComps = version.replace("cdh", "").split("[\\.-]");
146    assert(strComps.length > 0);
147
148    String[] comps = new String[strComps.length];
149    for (int i = 0; i < strComps.length; ++i) {
150      if (StringUtils.isNumeric(strComps[i])) {
151        comps[i] = strComps[i];
152      } else if (StringUtils.isEmpty(strComps[i]) || "x".equals(strComps[i])) {
153        comps[i] = String.valueOf(VERY_LARGE_NUMBER);
154      } else {
155        if("SNAPSHOT".equals(strComps[i])) {
156          comps[i] = "-1";
157        } else if("beta".equals(strComps[i])) {
158          comps[i] = "-2";
159        } else if("alpha".equals(strComps[i])) {
160          comps[i] = "-3";
161        } else {
162          comps[i] = "-4";
163        }
164      }
165    }
166    return comps;
167  }
168
169  public static void main(String[] args) {
170    writeTo(System.out);
171  }
172}