View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import java.lang.management.RuntimeMXBean;
22  import java.lang.management.ManagementFactory;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.conf.Configured;
29  import org.apache.hadoop.hbase.HBaseConfiguration;
30  import org.apache.hadoop.util.Tool;
31  import org.apache.hadoop.util.ToolRunner;
32  
33  /**
34   * Base class for command lines that start up various HBase daemons.
35   */
36  @InterfaceAudience.Private
37  public abstract class ServerCommandLine extends Configured implements Tool {
38    private static final Log LOG = LogFactory.getLog(ServerCommandLine.class);
39  
40    /**
41     * Implementing subclasses should return a usage string to print out.
42     */
43    protected abstract String getUsage();
44  
45    /**
46     * Print usage information for this command line.
47     *
48     * @param message if not null, print this message before the usage info.
49     */
50    protected void usage(String message) {
51      if (message != null) {
52        System.err.println(message);
53        System.err.println("");
54      }
55  
56      System.err.println(getUsage());
57    }
58  
59    /**
60     * Log information about the currently running JVM.
61     */
62    public static void logJVMInfo() {
63      // Print out vm stats before starting up.
64      RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
65      if (runtime != null) {
66        LOG.info("vmName=" + runtime.getVmName() + ", vmVendor=" +
67                 runtime.getVmVendor() + ", vmVersion=" + runtime.getVmVersion());
68        LOG.info("vmInputArguments=" + runtime.getInputArguments());
69      }
70    }
71  
72    /**
73     * Parse and run the given command line. This may exit the JVM if
74     * a nonzero exit code is returned from <code>run()</code>.
75     */
76    public void doMain(String args[]) {
77      try {
78        int ret = ToolRunner.run(HBaseConfiguration.create(), this, args);
79        if (ret != 0) {
80          System.exit(ret);
81        }
82      } catch (Exception e) {
83        LOG.error("Failed to run", e);
84        System.exit(-1);
85      }
86    }
87  }