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