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  
21  package org.apache.hadoop.hbase.rest;
22  
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.PosixParser;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import java.util.Arrays;
33  import java.util.List;
34  
35  import org.mortbay.jetty.Server;
36  import org.mortbay.jetty.servlet.Context;
37  import org.mortbay.jetty.servlet.ServletHolder;
38  
39  import com.sun.jersey.spi.container.servlet.ServletContainer;
40  
41  /**
42   * Main class for launching REST gateway as a servlet hosted by Jetty.
43   * <p> 
44   * The following options are supported:
45   * <ul>
46   * <li>-p: service port</li>
47   * </ul>
48   */
49  public class Main implements Constants {
50    private static final String DEFAULT_LISTEN_PORT = "8080";
51  
52    private static void printUsageAndExit(Options options, int exitCode) {
53      HelpFormatter formatter = new HelpFormatter();
54      formatter.printHelp("REST", null, options,
55        "To start the REST server run 'bin/hbase-daemon.sh start rest'\n" +
56        "To shutdown the REST server run 'bin/hbase-daemon.sh stop rest' or" +
57        " send a kill signal to the rest server pid",
58        true);
59      System.exit(exitCode);
60    }
61  
62    public static void main(String[] args) throws Exception {
63      Log LOG = LogFactory.getLog("RESTServer");
64      Options options = new Options();
65      options.addOption("p", "port", true, "Port to bind to [default:" +
66        DEFAULT_LISTEN_PORT + "]");
67      CommandLineParser parser = new PosixParser();
68      CommandLine cmd = parser.parse(options, args);
69      /**
70       * This is so complicated to please both bin/hbase and bin/hbase-daemon.
71       * hbase-daemon provides "start" and "stop" arguments
72       * hbase should print the help if no argument is provided
73       */
74      List<String> commandLine = Arrays.asList(args);
75      boolean stop = commandLine.contains("stop");
76      boolean start = commandLine.contains("start");
77      if (cmd.hasOption("help") || !start || stop) {
78        printUsageAndExit(options, 1);
79      }
80      // Get port to bind to
81      int port = 0;
82      try {
83        port = Integer.parseInt(cmd.getOptionValue("port", DEFAULT_LISTEN_PORT));
84      } catch (NumberFormatException e) {
85        LOG.error("Could not parse the value provided for the port option", e);
86        printUsageAndExit(options, -1);
87      }
88  
89      // set up the Jersey servlet container for Jetty
90  
91      ServletHolder sh = new ServletHolder(ServletContainer.class);
92      sh.setInitParameter(
93        "com.sun.jersey.config.property.resourceConfigClass",
94        ResourceConfig.class.getCanonicalName());
95      sh.setInitParameter("com.sun.jersey.config.property.packages",
96        "jetty");
97  
98      // set up Jetty and run the embedded server
99  
100     RESTServlet servlet = RESTServlet.getInstance();
101     port = servlet.getConfiguration().getInt("hbase.rest.port", port);
102 
103     Server server = new Server(port);
104     server.setSendServerVersion(false);
105     server.setSendDateHeader(false);
106     server.setStopAtShutdown(true);
107       // set up context
108     Context context = new Context(server, "/", Context.SESSIONS);
109     context.addServlet(sh, "/*");
110 
111     server.start();
112     server.join();
113   }
114 }