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 // Copying into a non-final member so that it can be changed by reflection for testing 044 private static String version = Version.version; 045 046 /** 047 * Get the hbase version. 048 * @return the hbase version string, eg. "0.6.3-dev" 049 */ 050 public static String getVersion() { 051 return version; 052 } 053 054 /** 055 * Get the subversion revision number for the root directory 056 * @return the revision number, eg. "451451" 057 */ 058 public static String getRevision() { 059 return Version.revision; 060 } 061 062 /** 063 * The date that hbase was compiled. 064 * @return the compilation date in unix date format 065 */ 066 public static String getDate() { 067 return Version.date; 068 } 069 070 /** 071 * The user that compiled hbase. 072 * @return the username of the user 073 */ 074 public static String getUser() { 075 return Version.user; 076 } 077 078 /** 079 * Get the subversion URL for the root hbase directory. 080 * @return the url 081 */ 082 public static String getUrl() { 083 return Version.url; 084 } 085 086 static String[] versionReport() { 087 return new String[] { "HBase " + getVersion(), 088 "Source code repository " + getUrl() + " revision=" + getRevision(), 089 "Compiled by " + getUser() + " on " + getDate(), 090 "From source with checksum " + getSrcChecksum() }; 091 } 092 093 /** 094 * Get the checksum of the source files from which Hadoop was compiled. 095 * @return a string that uniquely identifies the source 096 **/ 097 public static String getSrcChecksum() { 098 return Version.srcChecksum; 099 } 100 101 public static void writeTo(PrintWriter out) { 102 for (String line : versionReport()) { 103 out.println(line); 104 } 105 } 106 107 public static void writeTo(PrintStream out) { 108 for (String line : versionReport()) { 109 out.println(line); 110 } 111 } 112 113 public static void logVersion() { 114 for (String line : versionReport()) { 115 LOG.info(line); 116 } 117 } 118 119 public static int compareVersion(String v1, String v2) { 120 // fast compare equals first 121 if (v1.equals(v2)) { 122 return 0; 123 } 124 String[] v1Comps = getVersionComponents(v1); 125 String[] v2Comps = getVersionComponents(v2); 126 127 int length = Math.max(v1Comps.length, v2Comps.length); 128 for (int i = 0; i < length; i++) { 129 Integer va = i < v1Comps.length ? Integer.parseInt(v1Comps[i]) : 0; 130 Integer vb = i < v2Comps.length ? Integer.parseInt(v2Comps[i]) : 0; 131 int compare = va.compareTo(vb); 132 if (compare != 0) { 133 return compare; 134 } 135 } 136 return 0; 137 } 138 139 /** 140 * Returns the version components as String objects Examples: "1.4.3" returns ["1", "4", "3"], 141 * "4.5.6-SNAPSHOT" returns ["4", "5", "6", "-1"] "4.5.6-beta" returns ["4", "5", "6", "-2"], 142 * "4.5.6-alpha" returns ["4", "5", "6", "-3"] "4.5.6-UNKNOW" returns ["4", "5", "6", "-4"] 143 * @return the components of the version string 144 */ 145 private static String[] getVersionComponents(final String version) { 146 assert (version != null); 147 List<String> list = Splitter.onPattern("[\\.-]").splitToList(version); 148 String[] strComps = list.toArray(new String[list.size()]); 149 assert (strComps.length > 0); 150 String[] comps = new String[strComps.length]; 151 for (int i = 0; i < strComps.length; ++i) { 152 if (StringUtils.isNumeric(strComps[i])) { 153 comps[i] = strComps[i]; 154 } else if (StringUtils.isEmpty(strComps[i])) { 155 comps[i] = String.valueOf(VERY_LARGE_NUMBER); 156 } else { 157 if ("SNAPSHOT".equals(strComps[i])) { 158 comps[i] = "-1"; 159 } else if ("beta".equals(strComps[i])) { 160 comps[i] = "-2"; 161 } else if ("alpha".equals(strComps[i])) { 162 comps[i] = "-3"; 163 } else { 164 comps[i] = "-4"; 165 } 166 } 167 } 168 return comps; 169 } 170 171 public static int getMajorVersion(String version) { 172 return Integer.parseInt(Iterables.get(Splitter.on('.').split(version), 0)); 173 } 174 175 public static void main(String[] args) { 176 writeTo(System.out); 177 } 178}