View Javadoc

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