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