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.HelpFormatter;
25  import org.apache.commons.cli.Options;
26  import org.apache.commons.cli.PosixParser;
27  import org.apache.commons.cli.ParseException;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.HBaseConfiguration;
33  import org.apache.hadoop.hbase.rest.filter.GzipFilter;
34  
35  import java.util.List;
36  import java.util.ArrayList;
37  
38  import org.mortbay.jetty.Server;
39  import org.mortbay.jetty.servlet.Context;
40  import org.mortbay.jetty.servlet.ServletHolder;
41  
42  import com.sun.jersey.spi.container.servlet.ServletContainer;
43  
44  /**
45   * Main class for launching REST gateway as a servlet hosted by Jetty.
46   * <p>
47   * The following options are supported:
48   * <ul>
49   * <li>-p --port : service port</li>
50   * <li>-ro --readonly : server mode</li>
51   * </ul>
52   */
53  public class Main implements Constants {
54  
55    private static void printUsageAndExit(Options options, int exitCode) {
56      HelpFormatter formatter = new HelpFormatter();
57      formatter.printHelp("bin/hbase rest start", "", options,
58        "\nTo run the REST server as a daemon, execute " +
59        "bin/hbase-daemon.sh start|stop rest [-p <port>] [-ro]\n", true);
60      System.exit(exitCode);
61    }
62  
63    /**
64     * The main method for the HBase rest server.
65     * @param args command-line arguments
66     * @throws Exception exception
67     */
68    public static void main(String[] args) throws Exception {
69      Log LOG = LogFactory.getLog("RESTServer");
70  
71      Configuration conf = HBaseConfiguration.create();
72      RESTServlet servlet = RESTServlet.getInstance(conf);
73  
74      Options options = new Options();
75      options.addOption("p", "port", true, "Port to bind to [default: 8080]");
76      options.addOption("ro", "readonly", false, "Respond only to GET HTTP " +
77        "method requests [default: false]");
78  
79      CommandLine commandLine = null;
80      try {
81        commandLine = new PosixParser().parse(options, args);
82      } catch (ParseException e) {
83        LOG.error("Could not parse: ", e);
84        printUsageAndExit(options, -1);
85      }
86  
87      // check for user-defined port setting, if so override the conf
88      if (commandLine != null && commandLine.hasOption("port")) {
89        String val = commandLine.getOptionValue("port");
90        servlet.getConfiguration()
91            .setInt("hbase.rest.port", Integer.valueOf(val));
92        LOG.debug("port set to " + val);
93      }
94  
95      // check if server should only process GET requests, if so override the conf
96      if (commandLine != null && commandLine.hasOption("readonly")) {
97        servlet.getConfiguration().setBoolean("hbase.rest.readonly", true);
98        LOG.debug("readonly set to true");
99      }
100 
101     @SuppressWarnings("unchecked")
102     List<String> remainingArgs = commandLine != null ?
103         commandLine.getArgList() : new ArrayList<String>();
104     if (remainingArgs.size() != 1) {
105       printUsageAndExit(options, 1);
106     }
107 
108     String command = remainingArgs.get(0);
109     if ("start".equals(command)) {
110       // continue and start container
111     } else if ("stop".equals(command)) {
112       System.exit(1);
113     } else {
114       printUsageAndExit(options, 1);
115     }
116 
117     // set up the Jersey servlet container for Jetty
118     ServletHolder sh = new ServletHolder(ServletContainer.class);
119     sh.setInitParameter(
120       "com.sun.jersey.config.property.resourceConfigClass",
121       ResourceConfig.class.getCanonicalName());
122     sh.setInitParameter("com.sun.jersey.config.property.packages",
123       "jetty");
124 
125     // set up Jetty and run the embedded server
126 
127     int port = servlet.getConfiguration().getInt("hbase.rest.port", 8080);
128 
129     Server server = new Server(port);
130     server.setSendServerVersion(false);
131     server.setSendDateHeader(false);
132     server.setStopAtShutdown(true);
133       // set up context
134     Context context = new Context(server, "/", Context.SESSIONS);
135     context.addServlet(sh, "/*");
136     context.addFilter(GzipFilter.class, "/*", 0);
137 
138     server.start();
139     server.join();
140   }
141 }