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 org.apache.commons.logging.LogFactory;
22  import java.io.PrintWriter;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.VersionAnnotation;
27  import org.apache.commons.logging.Log;
28  
29  /**
30   * This class finds the package info for hbase and the VersionAnnotation
31   * information.  Taken from hadoop.  Only name of annotation is different.
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Evolving
35  public class VersionInfo {
36    private static final Log LOG = LogFactory.getLog(VersionInfo.class.getName());
37    private static Package myPackage;
38    private static VersionAnnotation version;
39  
40    static {
41      myPackage = VersionAnnotation.class.getPackage();
42      version = myPackage.getAnnotation(VersionAnnotation.class);
43    }
44  
45    /**
46     * Get the meta-data for the hbase package.
47     * @return package
48     */
49    static Package getPackage() {
50      return myPackage;
51    }
52  
53    /**
54     * Get the hbase version.
55     * @return the hbase version string, eg. "0.6.3-dev"
56     */
57    public static String getVersion() {
58      return version != null ? version.version() : "Unknown";
59    }
60  
61    /**
62     * Get the subversion revision number for the root directory
63     * @return the revision number, eg. "451451"
64     */
65    public static String getRevision() {
66      return version != null ? version.revision() : "Unknown";
67    }
68  
69    /**
70     * The date that hbase was compiled.
71     * @return the compilation date in unix date format
72     */
73    public static String getDate() {
74      return version != null ? version.date() : "Unknown";
75    }
76  
77    /**
78     * The user that compiled hbase.
79     * @return the username of the user
80     */
81    public static String getUser() {
82      return version != null ? version.user() : "Unknown";
83    }
84  
85    /**
86     * Get the subversion URL for the root hbase directory.
87     * @return the url
88     */
89    public static String getUrl() {
90      return version != null ? version.url() : "Unknown";
91    }
92  
93    static String[] versionReport() {
94      return new String[] {
95        "HBase " + getVersion(),
96        "Source code repository " + getUrl() + " -r " + getRevision(),
97        "Compiled by " + getUser() + " on " + getDate(),
98        "From source with checksum " + getSrcChecksum()
99        };
100   }
101 
102   /**
103    * Get the checksum of the source files from which Hadoop was compiled.
104    * @return a string that uniquely identifies the source
105    **/
106   public static String getSrcChecksum() {
107     return version != null ? version.srcChecksum() : "Unknown";
108   }
109 
110   public static void writeTo(PrintWriter out) {
111     for (String line : versionReport()) {
112       out.println(line);
113     }
114   }
115 
116   public static void logVersion() {
117     for (String line : versionReport()) {
118       LOG.info(line);
119     }
120   }
121 
122   public static void main(String[] args) {
123     logVersion();
124   }
125 }