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 */
018package org.apache.hadoop.hbase.util;
019
020import java.io.PrintStream;
021import java.io.PrintWriter;
022import java.util.List;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.hadoop.hbase.Version;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import org.apache.hbase.thirdparty.com.google.common.base.Splitter;
030import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
031
032/**
033 * This class finds the Version information for HBase.
034 */
035@InterfaceAudience.Public
036public class VersionInfo {
037  private static final Logger LOG = LoggerFactory.getLogger(VersionInfo.class.getName());
038
039  // If between two dots there is not a number, we regard it as a very large number so it is
040  // higher than any numbers in the version.
041  private static final int VERY_LARGE_NUMBER = 100000;
042
043  /**
044   * Get the hbase version.
045   * @return the hbase version string, eg. "0.6.3-dev"
046   */
047  public static String getVersion() {
048    return Version.version;
049  }
050
051  /**
052   * Get the subversion revision number for the root directory
053   * @return the revision number, eg. "451451"
054   */
055  public static String getRevision() {
056    return Version.revision;
057  }
058
059  /**
060   * The date that hbase was compiled.
061   * @return the compilation date in unix date format
062   */
063  public static String getDate() {
064    return Version.date;
065  }
066
067  /**
068   * The user that compiled hbase.
069   * @return the username of the user
070   */
071  public static String getUser() {
072    return Version.user;
073  }
074
075  /**
076   * Get the subversion URL for the root hbase directory.
077   * @return the url
078   */
079  public static String getUrl() {
080    return Version.url;
081  }
082
083  static String[] versionReport() {
084    return new String[] { "HBase " + getVersion(),
085      "Source code repository " + getUrl() + " revision=" + getRevision(),
086      "Compiled by " + getUser() + " on " + getDate(),
087      "From source with checksum " + getSrcChecksum() };
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 Examples: "1.2.3" returns ["1", "2", "3"],
138   * "4.5.6-SNAPSHOT" returns ["4", "5", "6", "-1"] "4.5.6-beta" returns ["4", "5", "6", "-2"],
139   * "4.5.6-alpha" returns ["4", "5", "6", "-3"] "4.5.6-UNKNOW" returns ["4", "5", "6", "-4"]
140   * @return the components of the version string
141   */
142  private static String[] getVersionComponents(final String version) {
143    assert (version != null);
144    List<String> list = Splitter.onPattern("[\\.-]").splitToList(version);
145    String[] strComps = list.toArray(new String[list.size()]);
146    assert (strComps.length > 0);
147    String[] comps = new String[strComps.length];
148    for (int i = 0; i < strComps.length; ++i) {
149      if (StringUtils.isNumeric(strComps[i])) {
150        comps[i] = strComps[i];
151      } else if (StringUtils.isEmpty(strComps[i])) {
152        comps[i] = String.valueOf(VERY_LARGE_NUMBER);
153      } else {
154        if ("SNAPSHOT".equals(strComps[i])) {
155          comps[i] = "-1";
156        } else if ("beta".equals(strComps[i])) {
157          comps[i] = "-2";
158        } else if ("alpha".equals(strComps[i])) {
159          comps[i] = "-3";
160        } else {
161          comps[i] = "-4";
162        }
163      }
164    }
165    return comps;
166  }
167
168  public static int getMajorVersion(String version) {
169    return Integer.parseInt(Iterables.get(Splitter.on('.').split(version), 0));
170  }
171
172  public static void main(String[] args) {
173    writeTo(System.out);
174  }
175}