[120] | 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 | package org.apache.hadoop.util; |
---|
| 19 | |
---|
| 20 | import java.io.PrintStream; |
---|
| 21 | |
---|
| 22 | import org.apache.hadoop.conf.Configuration; |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * A utility to help run {@link Tool}s. |
---|
| 26 | * |
---|
| 27 | * <p><code>ToolRunner</code> can be used to run classes implementing |
---|
| 28 | * <code>Tool</code> interface. It works in conjunction with |
---|
| 29 | * {@link GenericOptionsParser} to parse the |
---|
| 30 | * <a href="{@docRoot}/org/apache/hadoop/util/GenericOptionsParser.html#GenericOptions"> |
---|
| 31 | * generic hadoop command line arguments</a> and modifies the |
---|
| 32 | * <code>Configuration</code> of the <code>Tool</code>. The |
---|
| 33 | * application-specific options are passed along without being modified. |
---|
| 34 | * </p> |
---|
| 35 | * |
---|
| 36 | * @see Tool |
---|
| 37 | * @see GenericOptionsParser |
---|
| 38 | */ |
---|
| 39 | public class ToolRunner { |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Runs the given <code>Tool</code> by {@link Tool#run(String[])}, after |
---|
| 43 | * parsing with the given generic arguments. Uses the given |
---|
| 44 | * <code>Configuration</code>, or builds one if null. |
---|
| 45 | * |
---|
| 46 | * Sets the <code>Tool</code>'s configuration with the possibly modified |
---|
| 47 | * version of the <code>conf</code>. |
---|
| 48 | * |
---|
| 49 | * @param conf <code>Configuration</code> for the <code>Tool</code>. |
---|
| 50 | * @param tool <code>Tool</code> to run. |
---|
| 51 | * @param args command-line arguments to the tool. |
---|
| 52 | * @return exit code of the {@link Tool#run(String[])} method. |
---|
| 53 | */ |
---|
| 54 | public static int run(Configuration conf, Tool tool, String[] args) |
---|
| 55 | throws Exception{ |
---|
| 56 | if(conf == null) { |
---|
| 57 | conf = new Configuration(); |
---|
| 58 | } |
---|
| 59 | GenericOptionsParser parser = new GenericOptionsParser(conf, args); |
---|
| 60 | //set the configuration back, so that Tool can configure itself |
---|
| 61 | tool.setConf(conf); |
---|
| 62 | |
---|
| 63 | //get the args w/o generic hadoop args |
---|
| 64 | String[] toolArgs = parser.getRemainingArgs(); |
---|
| 65 | return tool.run(toolArgs); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Runs the <code>Tool</code> with its <code>Configuration</code>. |
---|
| 70 | * |
---|
| 71 | * Equivalent to <code>run(tool.getConf(), tool, args)</code>. |
---|
| 72 | * |
---|
| 73 | * @param tool <code>Tool</code> to run. |
---|
| 74 | * @param args command-line arguments to the tool. |
---|
| 75 | * @return exit code of the {@link Tool#run(String[])} method. |
---|
| 76 | */ |
---|
| 77 | public static int run(Tool tool, String[] args) |
---|
| 78 | throws Exception{ |
---|
| 79 | return run(tool.getConf(), tool, args); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Prints generic command-line argurments and usage information. |
---|
| 84 | * |
---|
| 85 | * @param out stream to write usage information to. |
---|
| 86 | */ |
---|
| 87 | public static void printGenericCommandUsage(PrintStream out) { |
---|
| 88 | GenericOptionsParser.printGenericCommandUsage(out); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | } |
---|