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.util;
019
020import java.io.PrintStream;
021
022import org.apache.hadoop.classification.InterfaceAudience;
023import org.apache.hadoop.classification.InterfaceStability;
024import org.apache.hadoop.conf.Configuration;
025
026/**
027 * A utility to help run {@link Tool}s.
028 * 
029 * <p><code>ToolRunner</code> can be used to run classes implementing 
030 * <code>Tool</code> interface. It works in conjunction with 
031 * {@link GenericOptionsParser} to parse the 
032 * <a href="{@docRoot}/org/apache/hadoop/util/GenericOptionsParser.html#GenericOptions">
033 * generic hadoop command line arguments</a> and modifies the 
034 * <code>Configuration</code> of the <code>Tool</code>. The 
035 * application-specific options are passed along without being modified.
036 * </p>
037 * 
038 * @see Tool
039 * @see GenericOptionsParser
040 */
041@InterfaceAudience.Public
042@InterfaceStability.Stable
043public class ToolRunner {
044 
045  /**
046   * Runs the given <code>Tool</code> by {@link Tool#run(String[])}, after 
047   * parsing with the given generic arguments. Uses the given 
048   * <code>Configuration</code>, or builds one if null.
049   * 
050   * Sets the <code>Tool</code>'s configuration with the possibly modified 
051   * version of the <code>conf</code>.  
052   * 
053   * @param conf <code>Configuration</code> for the <code>Tool</code>.
054   * @param tool <code>Tool</code> to run.
055   * @param args command-line arguments to the tool.
056   * @return exit code of the {@link Tool#run(String[])} method.
057   */
058  public static int run(Configuration conf, Tool tool, String[] args) 
059    throws Exception{
060    if(conf == null) {
061      conf = new Configuration();
062    }
063    GenericOptionsParser parser = new GenericOptionsParser(conf, args);
064    //set the configuration back, so that Tool can configure itself
065    tool.setConf(conf);
066    
067    //get the args w/o generic hadoop args
068    String[] toolArgs = parser.getRemainingArgs();
069    return tool.run(toolArgs);
070  }
071  
072  /**
073   * Runs the <code>Tool</code> with its <code>Configuration</code>.
074   * 
075   * Equivalent to <code>run(tool.getConf(), tool, args)</code>.
076   * 
077   * @param tool <code>Tool</code> to run.
078   * @param args command-line arguments to the tool.
079   * @return exit code of the {@link Tool#run(String[])} method.
080   */
081  public static int run(Tool tool, String[] args) 
082    throws Exception{
083    return run(tool.getConf(), tool, args);
084  }
085  
086  /**
087   * Prints generic command-line argurments and usage information.
088   * 
089   *  @param out stream to write usage information to.
090   */
091  public static void printGenericCommandUsage(PrintStream out) {
092    GenericOptionsParser.printGenericCommandUsage(out);
093  }
094  
095}