1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.util;
18
19 import java.util.Set;
20 import java.util.TreeSet;
21
22 import org.apache.commons.cli.BasicParser;
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.CommandLineParser;
25 import org.apache.commons.cli.HelpFormatter;
26 import org.apache.commons.cli.Options;
27 import org.apache.commons.cli.ParseException;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.classification.InterfaceAudience;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.HBaseConfiguration;
33 import org.apache.hadoop.util.Tool;
34 import org.apache.hadoop.util.ToolRunner;
35
36
37
38
39
40 @InterfaceAudience.Private
41 public abstract class AbstractHBaseTool implements Tool {
42
43 private static final int EXIT_SUCCESS = 0;
44 private static final int EXIT_FAILURE = 1;
45
46 private static final String HELP_OPTION = "help";
47
48 private static final Log LOG = LogFactory.getLog(AbstractHBaseTool.class);
49
50 private final Options options = new Options();
51
52 protected Configuration conf = null;
53
54 private static final Set<String> requiredOptions = new TreeSet<String>();
55
56
57
58
59
60 protected abstract void addOptions();
61
62
63
64
65 protected abstract void processOptions(CommandLine cmd);
66
67
68 protected abstract int doWork() throws Exception;
69
70 @Override
71 public Configuration getConf() {
72 return conf;
73 }
74
75 @Override
76 public void setConf(Configuration conf) {
77 this.conf = conf;
78 }
79
80 @Override
81 public final int run(String[] args) throws Exception {
82 if (conf == null) {
83 LOG.error("Tool configuration is not initialized");
84 throw new NullPointerException("conf");
85 }
86
87 CommandLine cmd;
88 try {
89
90 cmd = parseArgs(args);
91 } catch (ParseException e) {
92 LOG.error("Error when parsing command-line arguemnts", e);
93 printUsage();
94 return EXIT_FAILURE;
95 }
96
97 if (cmd.hasOption(HELP_OPTION) || !sanityCheckOptions(cmd)) {
98 printUsage();
99 return EXIT_FAILURE;
100 }
101
102 processOptions(cmd);
103
104 int ret = EXIT_FAILURE;
105 try {
106 ret = doWork();
107 } catch (Exception e) {
108 LOG.error("Error running command-line tool", e);
109 return EXIT_FAILURE;
110 }
111 return ret;
112 }
113
114 private boolean sanityCheckOptions(CommandLine cmd) {
115 boolean success = true;
116 for (String reqOpt : requiredOptions) {
117 if (!cmd.hasOption(reqOpt)) {
118 LOG.error("Required option -" + reqOpt + " is missing");
119 success = false;
120 }
121 }
122 return success;
123 }
124
125 private CommandLine parseArgs(String[] args) throws ParseException {
126 options.addOption(HELP_OPTION, false, "Show usage");
127 addOptions();
128 CommandLineParser parser = new BasicParser();
129 return parser.parse(options, args);
130 }
131
132 private void printUsage() {
133 HelpFormatter helpFormatter = new HelpFormatter();
134 helpFormatter.setWidth(80);
135 String usageHeader = "Options:";
136 String usageFooter = "";
137 String usageStr = "bin/hbase " + getClass().getName() + " <options>";
138
139 helpFormatter.printHelp(usageStr, usageHeader, options,
140 usageFooter);
141 }
142
143 protected void addRequiredOptWithArg(String opt, String description) {
144 requiredOptions.add(opt);
145 addOptWithArg(opt, description);
146 }
147
148 protected void addOptNoArg(String opt, String description) {
149 options.addOption(opt, false, description);
150 }
151
152 protected void addOptWithArg(String opt, String description) {
153 options.addOption(opt, true, description);
154 }
155
156
157
158
159 public static long parseLong(String s, long minValue, long maxValue) {
160 long l = Long.parseLong(s);
161 if (l < minValue || l > maxValue) {
162 throw new IllegalArgumentException("The value " + l
163 + " is out of range [" + minValue + ", " + maxValue + "]");
164 }
165 return l;
166 }
167
168 public static int parseInt(String s, int minValue, int maxValue) {
169 return (int) parseLong(s, minValue, maxValue);
170 }
171
172
173 protected void doStaticMain(String args[]) {
174 int ret;
175 try {
176 ret = ToolRunner.run(HBaseConfiguration.create(), this, args);
177 } catch (Exception ex) {
178 LOG.error("Error running command-line tool", ex);
179 ret = EXIT_FAILURE;
180 }
181 System.exit(ret);
182 }
183
184 }