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 */
018
019package org.apache.hadoop.util;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.Properties;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.apache.hadoop.classification.InterfaceAudience;
028import org.apache.hadoop.classification.InterfaceStability;
029import org.apache.hadoop.io.IOUtils;
030
031/**
032 * This class returns build information about Hadoop components.
033 */
034@InterfaceAudience.Public
035@InterfaceStability.Stable
036public class VersionInfo {
037  private static final Log LOG = LogFactory.getLog(VersionInfo.class);
038
039  private Properties info;
040
041  protected VersionInfo(String component) {
042    info = new Properties();
043    String versionInfoFile = component + "-version-info.properties";
044    InputStream is = null;
045    try {
046      is = Thread.currentThread().getContextClassLoader()
047        .getResourceAsStream(versionInfoFile);
048      if (is == null) {
049        throw new IOException("Resource not found");
050      }
051      info.load(is);
052    } catch (IOException ex) {
053      LogFactory.getLog(getClass()).warn("Could not read '" +
054          versionInfoFile + "', " + ex.toString(), ex);
055    } finally {
056      IOUtils.closeStream(is);
057    }
058  }
059
060  protected String _getVersion() {
061    return info.getProperty("version", "Unknown");
062  }
063
064  protected String _getRevision() {
065    return info.getProperty("revision", "Unknown");
066  }
067
068  protected String _getBranch() {
069    return info.getProperty("branch", "Unknown");
070  }
071
072  protected String _getDate() {
073    return info.getProperty("date", "Unknown");
074  }
075
076  protected String _getUser() {
077    return info.getProperty("user", "Unknown");
078  }
079
080  protected String _getUrl() {
081    return info.getProperty("url", "Unknown");
082  }
083
084  protected String _getSrcChecksum() {
085    return info.getProperty("srcChecksum", "Unknown");
086  }
087
088  protected String _getBuildVersion(){
089    return getVersion() +
090      " from " + _getRevision() +
091      " by " + _getUser() +
092      " source checksum " + _getSrcChecksum();
093  }
094
095  protected String _getProtocVersion() {
096    return info.getProperty("protocVersion", "Unknown");
097  }
098
099  private static VersionInfo COMMON_VERSION_INFO = new VersionInfo("common");
100  /**
101   * Get the Hadoop version.
102   * @return the Hadoop version string, eg. "0.6.3-dev"
103   */
104  public static String getVersion() {
105    return COMMON_VERSION_INFO._getVersion();
106  }
107  
108  /**
109   * Get the Git commit hash of the repository when compiled.
110   * @return the commit hash, eg. "18f64065d5db6208daf50b02c1b5ed4ee3ce547a"
111   */
112  public static String getRevision() {
113    return COMMON_VERSION_INFO._getRevision();
114  }
115
116  /**
117   * Get the branch on which this originated.
118   * @return The branch name, e.g. "trunk" or "branches/branch-0.20"
119   */
120  public static String getBranch() {
121    return COMMON_VERSION_INFO._getBranch();
122  }
123
124  /**
125   * The date that Hadoop was compiled.
126   * @return the compilation date in unix date format
127   */
128  public static String getDate() {
129    return COMMON_VERSION_INFO._getDate();
130  }
131
132  /**
133   * The user that compiled Hadoop.
134   * @return the username of the user
135   */
136  public static String getUser() {
137    return COMMON_VERSION_INFO._getUser();
138  }
139
140  /**
141   * Get the URL for the Hadoop repository.
142   * @return the URL of the Hadoop repository
143   */
144  public static String getUrl() {
145    return COMMON_VERSION_INFO._getUrl();
146  }
147
148  /**
149   * Get the checksum of the source files from which Hadoop was built.
150   * @return the checksum of the source files
151   */
152  public static String getSrcChecksum() {
153    return COMMON_VERSION_INFO._getSrcChecksum();
154  }
155
156  /**
157   * Returns the buildVersion which includes version,
158   * revision, user and date.
159   * @return the buildVersion
160   */
161  public static String getBuildVersion(){
162    return COMMON_VERSION_INFO._getBuildVersion();
163  }
164
165  /**
166   * Returns the protoc version used for the build.
167   * @return the protoc version
168   */
169  public static String getProtocVersion(){
170    return COMMON_VERSION_INFO._getProtocVersion();
171  }
172
173  public static void main(String[] args) {
174    LOG.debug("version: "+ getVersion());
175    System.out.println("Hadoop " + getVersion());
176    System.out.println("Subversion " + getUrl() + " -r " + getRevision());
177    System.out.println("Compiled by " + getUser() + " on " + getDate());
178    System.out.println("Compiled with protoc " + getProtocVersion());
179    System.out.println("From source with checksum " + getSrcChecksum());
180    System.out.println("This command was run using " + 
181        ClassUtil.findContainingJar(VersionInfo.class));
182  }
183}