diff --git a/bin/hbase b/bin/hbase index eee4c2b..c46043c 100755 --- a/bin/hbase +++ b/bin/hbase @@ -70,6 +70,7 @@ if [ $# = 0 ]; then echo " master run an HBase HMaster node" echo " regionserver run an HBase HRegionServer node" echo " thrift run an HBase Thrift server" + echo " rest run an HBase REST server" echo " avro run an HBase Avro server" echo " zookeeper run a Zookeeper server" echo " migrate upgrade an hbase.rootdir" @@ -239,6 +240,11 @@ elif [ "$COMMAND" = "thrift" ] ; then if [ "$1" != "stop" ] ; then HBASE_OPTS="$HBASE_OPTS $HBASE_THRIFT_OPTS" fi +elif [ "$COMMAND" = "rest" ] ; then + CLASS='org.apache.hadoop.hbase.rest.Main' + if [ "$1" != "stop" ] ; then + HBASE_OPTS="$HBASE_OPTS $HBASE_REST_OPTS" + fi elif [ "$COMMAND" = "avro" ] ; then CLASS='org.apache.hadoop.hbase.avro.AvroServer' if [ "$1" != "stop" ] ; then diff --git a/src/main/java/org/apache/hadoop/hbase/rest/Main.java b/src/main/java/org/apache/hadoop/hbase/rest/Main.java index ff8c860..368b4b4 100644 --- a/src/main/java/org/apache/hadoop/hbase/rest/Main.java +++ b/src/main/java/org/apache/hadoop/hbase/rest/Main.java @@ -22,9 +22,16 @@ package org.apache.hadoop.hbase.rest; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.PosixParser; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Arrays; +import java.util.List; + import org.mortbay.jetty.Server; import org.mortbay.jetty.servlet.Context; import org.mortbay.jetty.servlet.ServletHolder; @@ -40,17 +47,43 @@ import com.sun.jersey.spi.container.servlet.ServletContainer; * */ public class Main implements Constants { + private static final String DEFAULT_LISTEN_PORT = "8080"; - public static void main(String[] args) throws Exception { - // process command line + private static void printUsageAndExit(Options options, int exitCode) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("REST", null, options, + "To start the REST server run 'bin/hbase-daemon.sh start rest'\n" + + "To shutdown the REST server run 'bin/hbase-daemon.sh stop rest' or" + + " send a kill signal to the rest server pid", + true); + System.exit(exitCode); + } + public static void main(String[] args) throws Exception { + Log LOG = LogFactory.getLog("RESTServer"); Options options = new Options(); - options.addOption("p", "port", true, "service port"); + options.addOption("p", "port", true, "Port to bind to [default:" + + DEFAULT_LISTEN_PORT + "]"); CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); - int port = 8080; - if (cmd.hasOption("p")) { - port = Integer.valueOf(cmd.getOptionValue("p")); + /** + * This is so complicated to please both bin/hbase and bin/hbase-daemon. + * hbase-daemon provides "start" and "stop" arguments + * hbase should print the help if no argument is provided + */ + List commandLine = Arrays.asList(args); + boolean stop = commandLine.contains("stop"); + boolean start = commandLine.contains("start"); + if (cmd.hasOption("help") || !start || stop) { + printUsageAndExit(options, 1); + } + // Get port to bind to + int port = 0; + try { + port = Integer.parseInt(cmd.getOptionValue("port", DEFAULT_LISTEN_PORT)); + } catch (NumberFormatException e) { + LOG.error("Could not parse the value provided for the port option", e); + printUsageAndExit(options, -1); } // set up the Jersey servlet container for Jetty