1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
43
44
45
46
47
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
71
72
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
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
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
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
108 Context context = new Context(server, "/", Context.SESSIONS);
109 context.addServlet(sh, "/*");
110
111 server.start();
112 server.join();
113 }
114 }