View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.PrintStream;
22  import java.io.PrintWriter;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.VersionAnnotation;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  
30  /**
31   * This class finds the package info for hbase and the VersionAnnotation
32   * information.  Taken from hadoop.  Only name of annotation is different.
33   */
34  @InterfaceAudience.Public
35  @InterfaceStability.Evolving
36  public class VersionInfo {
37    private static final Log LOG = LogFactory.getLog(VersionInfo.class.getName());
38    private static Package myPackage;
39    private static VersionAnnotation version;
40  
41    static {
42      myPackage = VersionAnnotation.class.getPackage();
43      version = myPackage.getAnnotation(VersionAnnotation.class);
44    }
45  
46    /**
47     * Get the meta-data for the hbase package.
48     * @return package
49     */
50    static Package getPackage() {
51      return myPackage;
52    }
53  
54    /**
55     * Get the hbase version.
56     * @return the hbase version string, eg. "0.6.3-dev"
57     */
58    public static String getVersion() {
59      return version != null ? version.version() : "Unknown";
60    }
61  
62    /**
63     * Get the subversion revision number for the root directory
64     * @return the revision number, eg. "451451"
65     */
66    public static String getRevision() {
67      return version != null ? version.revision() : "Unknown";
68    }
69  
70    /**
71     * The date that hbase was compiled.
72     * @return the compilation date in unix date format
73     */
74    public static String getDate() {
75      return version != null ? version.date() : "Unknown";
76    }
77  
78    /**
79     * The user that compiled hbase.
80     * @return the username of the user
81     */
82    public static String getUser() {
83      return version != null ? version.user() : "Unknown";
84    }
85  
86    /**
87     * Get the subversion URL for the root hbase directory.
88     * @return the url
89     */
90    public static String getUrl() {
91      return version != null ? version.url() : "Unknown";
92    }
93  
94    static String[] versionReport() {
95      return new String[] {
96        "HBase " + getVersion(),
97        "Source code repository " + getUrl() + " revision=" + getRevision(),
98        "Compiled by " + getUser() + " on " + getDate(),
99        "From source with checksum " + getSrcChecksum()
100       };
101   }
102 
103   /**
104    * Get the checksum of the source files from which Hadoop was compiled.
105    * @return a string that uniquely identifies the source
106    **/
107   public static String getSrcChecksum() {
108     return version != null ? version.srcChecksum() : "Unknown";
109   }
110 
111   public static void writeTo(PrintWriter out) {
112     for (String line : versionReport()) {
113       out.println(line);
114     }
115   }
116 
117   public static void writeTo(PrintStream out) {
118     for (String line : versionReport()) {
119       out.println(line);
120     }
121   }
122 
123   public static void logVersion() {
124     for (String line : versionReport()) {
125       LOG.info(line);
126     }
127   }
128 
129   public static void main(String[] args) {
130     writeTo(System.out);
131   }
132 }